Question

I have a list of Annotations and want to check if my custom annotation "MyAnnotation" is contained in the this list:

List<java.lang.annotation.Annotation>

My annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) 
public @interface MyAnnotation {

}

Whats the best way to check that. Do I have to interate over the list and compare the names or is there some way I can get the "contains()" - Method of the list to work here?

Was it helpful?

Solution

If I get your question correctly, then according to the documentation on Annotation.equals(java.lang.Object), you should be able to use the contains() method on the list.

Returns true if the specified object represents an annotation that is logically equivalent to this one. In other words, returns true if the specified object is an instance of the same annotation type as this instance, all of whose members are equal to the corresponding member of this annotation, as defined below:

  • Two corresponding primitive typed members whose values are x and y are considered equal if x == y, unless their type is float or double.
  • Two corresponding float members whose values are x and y are considered equal if Float.valueOf(x).equals(Float.valueOf(y)). (Unlike the == operator, NaN is considered equal to itself, and 0.0f unequal to -0.0f.)
  • Two corresponding double members whose values are x and y are considered equal if Double.valueOf(x).equals(Double.valueOf(y)). (Unlike the == operator, NaN is considered equal to itself, and 0.0 unequal to -0.0.)
  • Two corresponding String, Class, enum, or annotation typed members whose values are x and y are considered equal if x.equals(y). (Note that this definition is recursive for annotation typed members.)
  • Two corresponding array typed members x and y are considered equal if Arrays.equals(x, y), for the appropriate overloading of Arrays.equals(long[], long[]).

And the following demo snippet and it's output seem to prove it -

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) 
@interface MyAnnotation {
    String value();
}

public class Main {
    @MyAnnotation(value = "onM1") public static void m1() { }
    @MyAnnotation(value = "onM1") public static void m() { }
    @MyAnnotation(value = "onM2") public static void m2() { }
    @MyAnnotation(value = "onM3") public static void m3() { }


    public static void main(String[] args) throws Exception {
        Method m = Main.class.getMethod("m1", new Class<?>[] {});
        Annotation onM1 = m.getAnnotation(MyAnnotation.class);

        m = Main.class.getMethod("m2", new Class<?>[] {});
        Annotation onM2 = m.getAnnotation(MyAnnotation.class);

        m = Main.class.getMethod("m3", new Class<?>[] {});
        Annotation onM3 = m.getAnnotation(MyAnnotation.class);

        m = Main.class.getMethod("m", new Class<?>[] {});
        Annotation onM = m.getAnnotation(MyAnnotation.class);

        List<Annotation> annots = Arrays.asList(onM1, onM2);

        System.out.println(annots);
        System.out.println(annots.contains(onM3));
        System.out.println(annots.contains(onM1));
        System.out.println(annots.contains(onM2));
        System.out.println(annots.contains(onM));
    }
}

Output

false
true
true
true

OTHER TIPS

boolean containsMyAnnotation = false;
// For each annotation in the list...
for(Annotation annotation : myListOfAnnotations) {
    // If they are MyAnnotation
    if(annotation.class == MyAnnotation.class) {
        // Then it exists in the list
        containsMyAnnotation = true; 
        // Break the for loop
        break;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top