簡體   English   中英

Java注釋元素方法返回

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM