2014-09-14 2 views
0

다음과 같은 사용자 정의 주석이 있습니다.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; 
    } 

} 
+0

사용자 정의 주석 처리기를 만들거나 런타임에 값을 반영하고 싶습니까? 그것이 전자의 경우, 어떻게 컴파일하고 있습니까? 필자는 ['JavaCompiler'] (http://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html)를 사용하여 소스를 컴파일 할 때 주석 프로세서를 보거나 사용했습니다. – Makoto

+0

@Makoto 표준 javac 컴파일 중에 주석 처리를 활성화하고 구성하려면 적절한 인수 (-proc, -processorpath, -s, -processor 참조)로 javac 컴파일러를 실행할 수 있습니다. 나는 이것이 주석 프로세서를 사용하는 전형적인 방법이라고 생각한다. –

답변

0

난 당신이 STH 같은 작업을 수행 할 추측 :

@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 구문의 어떤 버전이 지원 예정) 주석을 기억하십시오.

추가 문제가있는 경우 this great tutorial blog post을 읽는 것이 좋습니다.

+0

고마워요.이게 도움이됩니다. AbstractProcessor 내에서 소스 파일 정보 및 프로젝트 이름을 어떻게 얻을 수 있습니까? – Sandip

+0

이것이 완료되었습니다. 감사 – Sandip