jeudi 21 mars 2019

unchecked call to getAnnotationsByType(Class) as a member of the raw type Class

I am playing around Annotation in java. I am using reflection to get the values in the annotated values at runtime. Below is how I am using reflection to get all the annotated variables and their values.

Reflections reflections = new Reflections(".*");
Set<Class<?>> flagAnnotatedClasses =
    new HashSet<Class<?>>() {
      {
        addAll(reflections.getTypesAnnotatedWith(Flag.class));
      }
    };
for (Class cl : flagAnnotatedClasses) {
  // Get all the flags for a particular class annotated with @Flag.
  Annotation[] flags = cl.getAnnotationsByType(Flag.class);
  for (Annotation flag : flags) {
    String name = ((Flag) flag).name();
    String value = ((Flag) flag).value();
    System.out.println("name = " + name + ", value = " + value);
  }
}

It is working fine, except that during compiling, it gives me the warning:

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

So, I ran it with -Xlint:unchecked and I got below warning message:

warning: [unchecked] unchecked call to <A>getAnnotationsByType(Class<A>) as a member of the raw type Class
      Annotation[] flags = cl.getAnnotationsByType(Flag.class);
                                                  ^
  where A is a type-variable:
    A extends Annotation declared in method <A>getAnnotationsByType(Class<A>)

How could I get rid of this warning?





Aucun commentaire:

Enregistrer un commentaire