[英]Java Annotation Element Method return
我有一个如下的自定义注释。
@customelement(folder = "/path/")
public testMethod() {
}
如何使用下面的AbstractProcessor提取文件夹值,即“ / path /”?
public class CompileTimeAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);
for(Element te : elements){
for (Element e : te.getEnclosedElements()) {
if (e.getSimpleName().toString().equals("folder")) {
//Here fetch method return value
}
}
}
return true;
}
}
我想您想做某事:
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(CustomAnnotation.class);
for(Element te : elements){
CustomAnnotation annotation = te.getAnnotation(CustomAnnotation.class);
String folder = annotation.folder();
//... do sth with folder in context of te.
//te represent annotated method, e.g. testMethod from your example
}
return true;
}
还请记住使用@SupportedAnnotationTypes("package.CustomAnnotation")
和@SupportedSourceVersion(SourceVersion.RELEASE_7)
注释您的CompileTimeAnnotationProcessor
(或您要支持的任何Java语法版本)。
如果出现其他问题,建议阅读这篇很棒的教程博客文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.