java - 如何在 APT 中访问 @XmlElement 值?

标签 java annotations java-6 apt annotation-processing

我在编译时使用 APT 处理注释,并且需要获取某些类中 @XmlElement 注释的值。该类看起来像这样:

public class ComponentConfig {

    @XmlElements({
        @XmlElement(type = Sample1.class, name = "sample-1-config"),
        @XmlElement(type = Sample2.class, name = "sample-2-config"),
        @XmlElement(type = Sample3.class, name = "sample-3-config"),
    })

    //...
}

我想获取 @XmlElementname 值,但以下处理器代码无法为我获取它:

List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror mirror : annotationMirrors) {
    if (mirror.getAnnotationType().toString().equals(XML_ELEMENT)) {
        System.out.println(getAnnotationValueMapValueOfName(mirror));
        nodes.add(getAnnotationValueMapValueOfName(mirror));
    }
}

最佳答案

这不是 APT,但它适用于 AbstractProcessor JDK6+的:

@Override
public boolean process(final Set<? extends TypeElement> annotations, 
        final RoundEnvironment roundEnv) {
    checkEnvironmentChange();
    System.out.println("   > ---- process2 method starts " + hashCode());
    System.out.println("   > annotations: " + annotations);

    for (final TypeElement annotation: annotations) {
        System.out.println("   >  annotation: " + annotation.toString());
        processAnnotation(roundEnv, annotation);
    }
    System.out.println("   > processingOver: " + roundEnv.processingOver());
    System.out.println("   > ---- process2 method ends " + hashCode());
    return false;
}

private void processAnnotation(final RoundEnvironment roundEnv, 
        final TypeElement annotation) {
    final Set<? extends Element> annotateds = 
            roundEnv.getElementsAnnotatedWith(annotation);
    for (final Element element: annotateds) {
        processElement(element);
    }
}

private void processElement(final Element element) {
    System.out.println("      > class: " + element);
    System.out.println("      > class2: " + element.getClass());
    final List<? extends Element> enclosedElements = 
            element.getEnclosedElements();
    for (final Element enclosedElement: enclosedElements) {
        processEnclosedElement(enclosedElement);
    }
}

private void processEnclosedElement(final Element enclosedElement) {
    final XmlElements xmlElements = 
            enclosedElement.getAnnotation(XmlElements.class);
    if (xmlElements == null) {
        return;
    }
    final XmlElement[] xmlElemntValues = xmlElements.value();
    for (final XmlElement xmlElementValue: xmlElemntValues) {
        System.out.println("           > name: " + xmlElementValue.name());
    }
}

输出:

[...]
   > annotations: [hu.palacsint.annotation.MyAnnotation]
   >  annotation: hu.palacsint.annotation.MyAnnotation
      > class: hu.palacsint.annotation.p2.ClassTwo
      > class2: class com.sun.tools.javac.code.Symbol$ClassSymbol
           > name: sample-1-config
           > name: sample-2-config
           > name: sample-3-config
   > processingOver: false
[...]

我之前的问题也可能有帮助:Processing different annotations with the same Processor instance

关于java - 如何在 APT 中访问 @XmlElement 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11772516/

相关文章:

ejb - 不支持的类版本错误

java - 如何退出 NetBeans 平台 GUI 应用程序?

java - Maven导入项目错误

java - 不正确的 ehcache 统计信息 : hits+misses == 0

java - 如何在 1.6 中禁用 JTable 中的所有排序代码

Java 交叉编译 - 最新 JDK 的好处

java - 播放大型 wav 文件的特定部分

Java 如何将menuBar添加到JFrame而不破坏我的布局

java - OneToMany 和 JoinColumn 注释是分开的

java - @inherited 也使 @Overrided 方法值继承了注释?