में एनोटेशन वैल्यू से एनम कैप्चर कैसे करें मैं एनोटेशन प्रोसेसर और एनोटेशन मिरर का उपयोग करके एनोटेशन में एनम के मूल्य को पढ़ने की कोशिश कर रहा हूं, लेकिन मैं वापस शून्य हो रहा हूं। मुझे लगता है कि इसे एनोटेशन वैल्यू के साथ एक वैरिएबल एलिमेंट के रूप में एक एनम लपेटना होगा। VariableElement # getConstantValue() के लिए दस्तावेज़ कहते हैं, "इस चर के मान को वापस कर देता है यदि यह एक अंतिम फ़ील्ड है जो संकलन-समय निरंतर प्रारंभ होता है।" ठीक है, लेकिन अंतिम एनोटेशन सदस्य के लिए वैध संशोधक नहीं है। यह भी ध्यान में है कि मुझे अन्य एनोटेशन मूल्यों को पढ़ने में कोई परेशानी नहीं है, केवल Enums।एनोटेशन प्रोसेसर
मैं कुछ sleuthing किया है एक ऐसा लगता है AnnotationValue रन-टाइम में एक Symbol.VarSymbol के रूप में instantiated है, लेकिन Symbol.VarSymbol # getConstantValue() लगता है कि यह सिर्फ ऑब्जेक्ट प्रदान।
अंत में यदि मैं एनोटेशन वैल्यू पर टॉस्ट्रिंग() करता हूं तो मुझे उचित मूल्य मिलता है।
एनोटेशन:
package annotation;
public @interface AnAnnotation
{
String value();
Behavior defaultBehavior() default Behavior.NEW;
public static enum Behavior
{
NEW, NULL;
}
}
मेरी प्रोसेसर का एक हिस्सा और छोरों की अधिकता के अंदर नेस्टेड उचित AnnotaionMirror पर प्राप्त करने के लिए:
Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues = elemUtils.getElementValuesWithDefaults(annotationMirror);
for (ExecutableElement method : annotationValues.keySet())
{
...
else if ("defaultBehavior".equals(method.getSimpleName().toString()))
{
defaultBehavior = (Behavior)((VariableElement)annotationValues.get(method).getValue()).getConstantValue();
// This prints "NEW" or "NULL" correctly
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,annotationValues.get(method).toString());
// This prints null incorrectly (expect "NEW" or "NULL")
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, defaultBehavior + "");
}
...
}
संपादित करें: प्रोसेसर का एक और अधिक पूर्ण संस्करण।
package annotation.processor;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.*;
import javax.tools.*;
import annotation.AnAnnotation;
import annotation.AnAnnotation.Behavior;
@SupportedAnnotationTypes("annotation.AnAnnotation")
public class AnAnnotationProcessor extends AbstractProcessor
{
Types typeUtils;
Elements elemUtils;
@Override
public void init(ProcessingEnvironment processingEnv)
{
super.init(processingEnv);
typeUtils = processingEnv.getTypeUtils();
elemUtils = processingEnv.getElementUtils();
}
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv)
{
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
"Entering AnnotationNullableClassProcessor");
/****** Iterate over all annotaions being processed (only AnAnnotation) ******/
for (TypeElement annotation : annotations)
{
/****** Iterate over all elements that are annotated with the annotation ******/
for (Element element : roundEnv.getElementsAnnotatedWith(annotation))
{
/****** Iterate over all the declared annotations of the element ******/
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors())
{
final String annotationTypeName = annotationMirror.getAnnotationType().toString();
// Process annotations of type AnAnnotation
if (annotationTypeName.equals(AnAnnotation.class.getName()))
{
Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues = elemUtils.getElementValuesWithDefaults(annotationMirror);
/****** Iterate over the annotation's values. ******/
for (ExecutableElement method : accessorValues.keySet())
{
if ("defaultBehavior".equals(method.getSimpleName().toString()))
{
Behavior defaultBehavior = (Behavior)((VariableElement)annotationValues.get(method).getValue()).getConstantValue();
// This prints "NEW" or "NULL" correctly
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,annotationValues.get(method).toString());
// This prints null incorrectly (expect "NEW" or "NULL")
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, defaultBehavior + "");
}
}
}
}
}
}
return true;
}
}
मैं उल्लेख करना भूल गया कि यह एनोटेशन प्रोसेसिंग का जावा एसई 6 संस्करण है। –