gpt4 book ai didi

java - IClassFile 注释

转载 作者:行者123 更新时间:2023-12-01 15:09:37 26 4
gpt4 key购买 nike

在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com