获取定义TypeElement的元素?

时间:2015-04-27 11:07:34

标签: java annotations annotation-processing

我有以下课程:

public class Start extends PlacePresenter {

    @NameToken("startPage")
    public interface MyProxy extends ProxyPlace {
    }
}

在注释处理器中,我使用@NameToken注释了元素,即MyProxy。

@Override
public boolean process(Set<? extends TypeElement> annotations,
        RoundEnvironment roundEnv) {

  for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(NameToken.class)) {

    TypeElement typeElement = (TypeElement) annotatedElement;
    // typeElement is MyProxy
  }
}

如何定义MyProxy类的元素,即Start

1 个答案:

答案 0 :(得分:0)

包含的类将由调用Element返回的annotatedElement.getEnclosingElement()表示:

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(NameToken.class)) {
        System.out.println(annotatedElement.getEnclosingElement()); // prints "Start"
    }
    return true;
}