如何从数组注释文件中获取值



我像这样注释任何字段:

@MyAnnotation("annotation")
public String[] values;

我这样做:

for(Field field : AnnotatedClass.getClass.getFields())
   if (field.isAnnotationPresent(MyAnnotation.class)){
       // but I don't know how to get values String[] array
       // I try to cast but inconvertible types
   }

感谢您的任何想法和帮助!!

您可以使用field.getAnnotation()来实现此目的:

for (Field f : AnnotatedClass.getClass.getFields())
    if (field.isAnnotationPresent(MyAnnotation.class)) {
        MyAnnotation anno = field.getAnnotation(MyAnnotation.class);
        String values = anno.value();
    }
 }

最新更新