gpt4 book ai didi

java - 如何从注释处理器中的注释值捕获枚举

转载 作者:搜寻专家 更新时间:2023-10-31 20:16:08 25 4
gpt4 key购买 nike

我正在尝试使用注解处理器和注解镜像读取注解中枚举的值,但我返回的是空值。我认为这与将 Enum 包装为 VariableElement 的 AnnotationValue 有关。 VariableElement#getConstantValue() 的文档说“如果这是初始化为编译时常量的最终字段,则返回此变量的值。”好的,但是 final 不是注解成员的有效修饰符。另外值得注意的是,我在读取其他注释值时没有遇到任何问题,只是枚举。

我做了一些调查,看起来 AnnotationValue 在运行时被实例化为 Symbol.VarSymbol,但 Symbol.VarSymbol#getConstantValue() 看起来应该只返回对象。

最后,如果我在 AnnotationValue 上执行 toString(),我会得到正确的值。

注释:

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;
}
}

最佳答案

来自 getConstantValue 的文档:

"In particular, enum constants are not considered to be compile-time constants. To have a constant value, a field's type must be either a primitive type or String."

http://java.sun.com/javase/6/docs/api/javax/lang/model/element/VariableElement.html#getConstantValue()

要获取枚举常量的值,请使用 getAnnotation API 或使用 AnnotationValueVisitor。

关于java - 如何从注释处理器中的注释值捕获枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3087864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com