Java 如何在TypeElement中找到带注释的方法?
假设我有这个类:Java 如何在TypeElement中找到带注释的方法?,java,annotation-processing,Java,Annotation Processing,假设我有这个类: public class MyClass extends Ancestor{ @MyAnnotation void doSomething(){ } @MyAnnotation void doAnotherthing(String[] args){ } } public class Ancestor{ @MyAnnotation void doFirst(){ } } 在注释处理器中,我有一个T
public class MyClass extends Ancestor{
@MyAnnotation
void doSomething(){
}
@MyAnnotation
void doAnotherthing(String[] args){
}
}
public class Ancestor{
@MyAnnotation
void doFirst(){
}
}
在注释处理器中,我有一个TypeElement
实例MyClass
如何在MyClass
及其祖先中找到带有@MyAnnotation
的带注释方法
(我知道可以使用
RoundEnvironment
,但我不想使用它)如果您编写注释处理器,则必须使用RoundEnvironment。这就是注释处理器的工作方式。请记住,javac可能会根据其内部需求运行多次如果您编写注释处理器,则必须使用RoundEnvironment。这就是注释处理器的工作方式。请记住,javac可能会根据其内部需要运行多次(
static Set<Element> getAnnotatedElements(
Elements elements,
TypeElement type,
Class<? extends Annotation> annotation)
{
Set<Element> found = new HashSet<Element>();
for (Element e : elements.getAllMembers(type)) {
if (e.getAnnotation(annotation) != null)
found.add(e);
}
return found;
}
元素,
类型元素类型,
类静态集getAnnotatedElements(
元素,
类型元素类型,
等级