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

时间:2016-02-15 13:29:44

标签: java casting annotations

我注释了这样的任何字段:

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用field.getAnnotation()

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