Java注释元素方法返回
我有一个自定义注释,如下所示Java注释元素方法返回,java,Java,我有一个自定义注释,如下所示 @customelement(folder = "/path/") public testMethod() { } 如何使用下面的AbstractProcessor提取文件夹值,即“/path/” public class CompileTimeAnnotationProcessor extends AbstractProcessor { @Override public boolean process(Set<? extends Typ
@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;
}
}
公共类CompileTimeAnnotationProcessor扩展了AbstractProcessor{
@凌驾
公共布尔过程(Set我猜你想做以下事情:
@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;
}
@覆盖
public boolean process(set)您是在尝试创建自定义注释处理器,还是希望在运行时以反射方式读取值?如果是前者,您是如何编译的?我只在使用编译源代码时见过/使用注释处理器。@Makoto您可以使用正确的参数运行javac编译器(请参阅:-proc,-processorpath,-s,-processor)以在标准javac编译期间启用和配置注释处理。我认为这是使用注释处理器的典型方式。谢谢..这很有帮助。如何在AbstractProcessor中获取源文件信息和项目名?