我有一个自定义注释,如下所示。
@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;
}
}
答案 0 :(得分:0)
我想你想做的事情如下:
@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;
}
另请记住使用CompileTimeAnnotationProcessor
和@SupportedAnnotationTypes("package.CustomAnnotation")
注释您的@SupportedSourceVersion(SourceVersion.RELEASE_7)
{{1}}(或者您要支持的任何Java语法版本)。
如果出现其他问题,建议您阅读this great tutorial blog post。