Java IClassFile注释

Java IClassFile注释,java,eclipse,eclipse-jdt,Java,Eclipse,Eclipse Jdt,在Eclipse中使用IClassFile时,如何检查注释 这似乎不起作用classFile.getClass().isAnnotationPresent?感谢您的帮助 使用的问题 for (final IClassFile classFile : classFiles) { IAnnotation[] annotations = classFile.getType().getAnnotations(); 我必须得到所有的包,然后得到包中的类文件,然后得到注释。它将需要3个循环。有没有

在Eclipse中使用IClassFile时,如何检查注释

这似乎不起作用classFile.getClass().isAnnotationPresent?感谢您的帮助

使用的问题

for (final IClassFile classFile : classFiles) {
    IAnnotation[] annotations = classFile.getType().getAnnotations();

我必须得到所有的包,然后得到包中的类文件,然后得到注释。它将需要3个循环。有没有办法将此最小化?

我想说,查找注释的最简单方法是通过三重循环,但使用“搜索引擎
可能会稍微快一点(假设您正在查找特定注释)。请看一下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));
IJavaElement[]allPackagesToSearch=。。。
搜索请求者请求者=
IJavaSearchScope=SearchEngine.createJavaSearchScope(二进制软件包,IJavaSearchScope.APPLICATION_库);
int matchRule=SearchPattern.R_精确匹配| SearchPattern.R_区分大小写;
SearchPattern runWithPattern=SearchPattern.createPattern(“com.foo.MyAnnotation”,IJavaSearchConstants.ANNOTATION\u类型,IJavaSearchConstants.ANNOTATION\u类型\u引用,匹配规则);
SearchParticipant[]searchParticipants=新的SearchParticipant[]{SearchEngine.getDefaultSearchParticipant()};
新的搜索引擎().search(注释SPATERN、搜索参与者、范围、请求者、新的SubgressMonitor(pm,2));
这有点多,为了弄清楚它是如何工作的,我建议阅读JavaDoc for SearchEngine、SearchPattern和SearchRequestor


如果要查找所有批注,请更改匹配规则,并使用“*”
classFile.getChildren
,而不是“com.foo.MyAnnotation”,搜索元素s.t.
element.getType()==IJavaElement.ANNOTATION
,这不起作用。classFile.getType().getAnnotations()就可以了。我应该单独提出一个问题。谢谢。我去看看这个。