현재 새 소스 코드를 생성하는 어노테이션 프로세서를 작성 중입니다. 이 프로세서는 프로젝트를 구축하는 단계이기 때문에 애플리케이션 자체와 분리되어 있으며 애플리케이션에서 전체 빌드 시스템을 분리했습니다.어노테이션 처리 알 수 없음
응용 프로그램에서 작성된 주석을 처리하기 때문에 문제가 시작됩니다. 그것을 CustomAnnotation이라고 부르 자. 정규화 된 이름 com.company.api.annotation.CustomAnnotation.
프로세서에서 정규화 된 이름으로 주석을 검색 할 수 있습니다. 정말 멋진 기능입니다. 이제 함수를 호출 할 수 있기 때문에 메소드, 필드 등을 주석 처리 할 수있는 것 같습니다. getElementsAnnotatedWithTypeElement 대신 클래스 대신.
이제 CustomAnnotation에 필드와 변수가 포함되어 있으며 보통 다음과 같은 주석 자체를 얻습니다. Class annotation = Element.getAnnotation(Class)
CustomAnnotation을 클래스 객체로 사용할 수 없으므로 사용할 수 없습니다. (물론, 프로세서에 알려지지 않았기 때문에 사용할 수 없습니다.) TypeMirror 및 기타 사용 가능한 것들을 사용해 보았지만 아무것도 작동하지 않는 것 같습니다.
주석을 가져 와서 값을 읽는 방법을 아는 사람이 있습니까?
편집 : 의이 구현 살펴 보자 :
@SupportedAnnotationTypes("com.company.api.annotation.CustomAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class CustomProcessor extends AbstractProcessor
{
public CustomProcessor()
{
super();
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
{
TypeElement test = annotations.iterator().next();
for (Element elem : roundEnv.getElementsAnnotatedWith(test))
{
//Here is where I would get the Annotation element itself to
//read the content of it if I can use the Annotation as Class Object.
SupportedAnnotationTypes generated = elem.getAnnotation(SupportedAnnotationTypes.class);
}
}
그러나 나는 그것이이 환경에 존재하지 않기 때문에
에게 CustomAnnotation.class를 사용할 필요가 없습니다. Class 객체를 소유하지 않고 어떻게이 작업을 수행 할 수 있습니까?
당신이 무엇을 요구 확실하지에 대한 ... 당신은 Class.forName을을 (할 수 없어) 먼저? – GhostCat
['Element.getAnnotationMirrors()'] (https://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/Element.html#getAnnotationMirrors--)의 문제점 ['AnnotationMirror.getElementValues ()'] (https://docs.oracle.com/javase/8/docs/api/javax/lang/model/element/AnnotationMirror.html#getElementValues--)? – Holger
감사합니다 @Holger는 내가 필요로하는 것입니다! 나는이 문제에 대한 질문을 찾지 못했고 getAnnotationMirrors가 내가 필요한 것임을 알지 못했습니다. – Nico