简体   繁体   English

Java注释元素方法返回

[英]Java Annotation Element Method return

I have a custom annotation like below. 我有一个如下的自定义注释。

@customelement(folder = "/path/")
public testMethod() {

}

How I can extract folder value ie"/path/", using AbstractProcessor, below? 如何使用下面的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;
    }

}

I guess that you want to do sth like: 我想您想做某事:

    @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;
    }

Also remember to annotate your CompileTimeAnnotationProcessor with @SupportedAnnotationTypes("package.CustomAnnotation") and @SupportedSourceVersion(SourceVersion.RELEASE_7) (or whatever version of Java syntax are you going to support). 还请记住使用@SupportedAnnotationTypes("package.CustomAnnotation")@SupportedSourceVersion(SourceVersion.RELEASE_7)注释您的CompileTimeAnnotationProcessor (或您要支持的任何Java语法版本)。

In case of further problems, I recommend to read this great tutorial blog post . 如果出现其他问题,建议阅读这篇很棒的教程博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM