作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Eclipse 中使用 IClassFile 时如何检查注释?
这似乎不起作用 classFile.getClass().isAnnotationPresent?如有任何帮助,我们将不胜感激。
使用问题
for (final IClassFile classFile : classFiles) {
IAnnotation[] annotations = classFile.getType().getAnnotations();
我必须获取所有包,然后获取该包中的类文件,然后获取注释。需要 3 个循环。有没有办法尽量减少这种情况?
最佳答案
我想说,查找注释的最简单方法是通过三重循环,但使用“SearchEngine instead. Take a look at the source code for the
”可能会稍微快一些(假设您正在查找特定注释)。 org.eclipse.jdt.internal.junit.launcher.JUnit4TestFinder` 类。它查找用 @Test 或 @RunWith 注释的(源)类,这与您想要做的类似,但针对的是二进制类。
你会做这样的事情:
IJavaElement[] allPackagesToSearch = ...
SearchRequestor requestor = <implement the SearchRequestor abstract class and store all matches>
IJavaSearchScope scope= SearchEngine.createJavaSearchScope(binaryPackages, IJavaSearchScope.APPLICATION_LIBRARIES);
int matchRule= SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
SearchPattern runWithPattern= SearchPattern.createPattern("com.foo.MyAnnotation", IJavaSearchConstants.ANNOTATION_TYPE, IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE, matchRule);
SearchParticipant[] searchParticipants= new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
new SearchEngine().search(annotationsPattern, searchParticipants, scope, requestor, new SubProgressMonitor(pm, 2));
这有点拗口,为了弄清楚它是如何工作的,我建议阅读 SearchEngine、SearchPattern 和 SearchRequestor 的 JavaDoc。
如果您想查找所有个注释,请更改匹配规则,使用“*”代替“com.foo.MyAnnotation”。
关于java - IClassFile 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12505194/
在 Eclipse 中使用 IClassFile 时如何检查注释? 这似乎不起作用 classFile.getClass().isAnnotationPresent?如有任何帮助,我们将不胜感激。 使
我是一名优秀的程序员,十分优秀!