如何在APT中访问@XmlElement值?

时间:2012-08-02 07:00:29

标签: java annotations jdk1.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"),
    })

    //...
}

我想获得name的{​​{1}}值,但以下处理器代码无法为我提取:

@XmlElement

2 个答案:

答案 0 :(得分:1)

XML_ELEMENT在哪里定义?它至少应该是一个完全限定的名称。

使用getAnnotationType().toString()不是进行比较的良好基础。

如果您打印出找到的所有getAnnotationType().toString()的值,您可能会发现问题。

同样应该使用javax.annotation.processing包,因为APT现已弃用(因为它最近已从即将推出的JDK8-IIRC的构建中删除)。它使用起来更好一点,并得到所有JDK6 +实现的支持。

答案 1 :(得分:1)

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

@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