简体   繁体   English

处理注释的最佳方法

[英]Best way to process the annotations

I have 3 different kinds of custom annotations. 我有3种不同的自定义注释。 Assume those are Annotation1,Annotation2,Annotation3. 假设这些是Annotation1,Annotation2,Annotation3。

I was applied these 3 annotations to some of the fields in my class. 我将这3个注释应用于班级中的某些字段。 Now I am extracting/getting all the fields which are assigned these 3 annotations. 现在,我提取/获取分配了这3个注释的所有字段。 So for that I wrote a method like 所以我写了一个像

public List<Field> getAnnotation1Fields(Annotation1 argAnnotation1){
     // Code to extract those fields...
}

So for my 3 annotations I need to write 3 different methods like 因此,对于我的3个注释,我需要编写3种不同的方法,例如

public List<Field> getAnnotation2Fields(Annotation2 argAnnotation2){
     // Code to extract those fields...
}

public List<Field> getAnnotation3Fields(Annotation3 argAnnotation3){
     // Code to extract those fields...
}

In the above methods the Extraction logic is same but the parameter type is different (Argument). 在上述方法中,提取逻辑相同,但参数类型不同(自变量)。 Here my question is how can I call these three methods on single annotation...? 在这里,我的问题是如何在单个注解上调用这三种方法...? So that one common method can be called for any type of annotation (Including our custom annotations). 这样就可以为任何类型的注释(包括我们的自定义注释)调用一个通用方法。

Use method generics - You can define a variable type parameter for methods, as well as classes, like so: 使用方法泛型-您可以为方法以及类定义变量类型参数,如下所示:

public <T> List<Field> getAnnotationFields(T argAnnotation) {
  // Get fields with annotation type T
}

And then you can call it as easily as: 然后,您可以像以下这样轻松地调用它:

Annotation3 thingy = ...;
getAnnotationFields(thingy); // Extracts fields for Annotation3

public List<Field> getAnnotatedFields(java.lang.annotation.Annotation annotation)

or 要么

public List<Field> getAnnotatedFields(Class<? extends Annotation> annotationType)

depending on what you have instance (first) or type (second). 取决于您的实例(第一个)或类型(第二个)。

You could do it with this simple method: 您可以使用以下简单方法进行操作:

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public static List<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotationType) {
    Field[] declaredFields = clazz.getDeclaredFields();
    List<Field> annotatedFields = new LinkedList<>();
    for(Field field : declaredFields) {
        if(field.getAnnotation(annotationType) != null)
            annotatedFields.add(field);
    }
    return annotatedFields;
}

Usage example: 用法示例:

getAnnotatedFields(TestClass.class, Deprecated.class);

I hope that's closer to your needs: 希望更贴近您的需求:

static <A extends Annotation> Map<String,A> getField2Annotation(
  Class<?> declaringClass, Class<A> annotationType) {

  Field[] fields=declaringClass.getDeclaredFields();
  Map<String, A> map=Collections.emptyMap();
  for(Field f:fields) {
    A anno=f.getAnnotation(annotationType);
    if(anno!=null) {
      if(map.isEmpty()) map=new HashMap<String, A>();
      map.put(f.getName(), anno);
    }
  }
  return map;
}

Then you can do something like this: 然后,您可以执行以下操作:

public class Example {
  @Retention(RetentionPolicy.RUNTIME)
  @interface Foo { String value() default "bar"; }

  @Foo static String X;
  @Foo("baz") String Y;

  public static void main(String[] args) {
    Map<String, Foo> map = getField2Annotation(Example.class, Foo.class);
    for(Map.Entry<String,Foo> e:map.entrySet()) {
      System.out.println(e.getKey()+": "+e.getValue().value());
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM