快速可靠的方法来查找源代码文件是否实现了接口 [英] Fast and reliable way to find out if a source code file implements an interface

查看:171
本文介绍了快速可靠的方法来查找源代码文件是否实现了接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个Java源代码文件,找出它是否实现了给定接口的快速可靠方法是什么?

Given a java source code file, what is a fast and reliable way to find out if it implements a given interface?

一种可靠的方法是将文件解析为语法树(例如使用Eclipse的ASTNode类)并寻找合适的节点.

A reliable way would be to parse the file into a syntax tree (using e.g. the ASTNode class of eclipse) and look for the right nodes.

一种快速的方法是查找带有实现"的行,将那些在注释中的行踢掉,然后将接口与适当的import语句相关联.我可能在这里错过了一些奇怪的其他情况.

A fast way would be to look for lines with "implements", kick out those that are in comments, and relate the interface to an appropriate import statement. I am probably missing some strange other cases here.

你会怎么做?

推荐答案

我会说,解析源代码文件既快速又可靠.但是,我想,快速"是一个模糊的概念,它的确取决于实际情况.不过,与扫描源文件中是否出现实现"一词相比,我不希望有太多开销—因此,如果后者对您来说很好,我认为前者也应该可以接受吗?

I would say that parsing a source code file is both, fast and reliable. But "fast" is such a vague notion that it really depends, I guess. I wouldn't expect too much overhead when compared to scanning the source file for occurrences of the words "implements", though -- thus if the latter is fine for you, I'd assume the former should be acceptable too?

javax.tools.* API是入门的好入口;但是,那里也有许多用于Java的(开源)源代码解析器.

The javax.tools.* API would be a good entrance point to get started; however, there are also a number of (open source) source code parsers for Java out there.

此外,此处是Oracle网站上的介绍性博客文章.

Also, here is an introductory blog post on Oracle's websites.

这篇关于快速可靠的方法来查找源代码文件是否实现了接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆