Checking if an annotation is of a specific type

22,349

Solution 1

Are you just looking for

if (annotation.annotationType().equals(javax.validation.Valid.class)){}

?

Solution 2

Or even simpler:

if (annotation instanceof Valid) { /* ... */ }

Solution 3

Just for completeness' sake, another possibility is

if (this.getClass().isAnnotationPresent(MyCustomAnnotation.class)) {

Solution 4

Ok, I guess I should have done a little more research before posting the question. I discovered that I could use Class.isAssignableFrom(Class<?> cls):

import javax.validation.Valid;

if(Valid.class.isAssignableFrom(annotation.annotationType())) {
   ...
}

This seems to do the job. I'm not sure if there are any caveats to using this approach, though.

Solution 5

Since an annotation is just a class, you can simply use an == compare:

if (annotation.annotationType() == Valid.class) { /* ... */ }
Share:
22,349
Vivin Paliath
Author by

Vivin Paliath

Check out regula. It is very neat and will make all your dreams come true. Your monkey poured coffee in my boots! I assume that everyone knows more than I do. map{@n=split//;$j.=$n[0]x$n[1]}split/:/,"01:11:02". ":11:01:11:02:13:01:11:01:11:01:13:02:12:01:13:01". ":11:04:11:06:12:04:11:01:12:01:13:02:12:01:14:01". ":13:01:11:03:12:01:11:04:12:02:11:01:11:01:13:02". ":11:03:11:06:11:01:11:05:12:02:11:01:11:01:13:02". ":11:02:12:01:12:04:11:06:12:01:11:04:12:04:11:01". ":12:03:12:01:12:01:11:01:12:01:12:02:11:01:11:01". ":13:02:11:01:02:11:01:12:02";map{print chr unpack" i",pack"B32",$_}$j=~m/.{8}/g

Updated on May 11, 2020

Comments

  • Vivin Paliath
    Vivin Paliath almost 4 years

    I am using reflection to see if an annotation that is attached to a property of a class, is of a specific type. Current I am doing:

    if("javax.validation.Valid".equals(annotation.annotationType().getName())) {
       ...
    }
    

    Which strikes me as a little kludgey because it relies on a string that is a fully-qualified class-name. If the namespace changes in the future, this could cause subtle errors.

    I would like to do:

    if(Class.forName(annotation.annotationType().getName()).isInstance(
         new javax.validation.Valid()
    )) {
       ...
    }
    

    But javax.validation.Valid is an abstract class and cannot be instantiated. Is there a way to simulate instanceof (or basically use isInstance) against an interface or an abstract class?

  • Affe
    Affe almost 14 years
    It's kind of overkill since it's illegal to extend an annotation anyway. Don't know if there's any meaningful performance penalty compared to plain equals.
  • Vivin Paliath
    Vivin Paliath almost 14 years
    @Affe Saw your solution - I wasn't aware that you could simply do a an .equals or an == on .class! Thanks for that. I was under the impression that using .class simply compares the Class objects and not the actual types.
  • Affe
    Affe almost 14 years
    The class is effectively a singleton. It is guaranteed that there is only one instance of the Class object for a given Type per classloader, so you can compare them that way in java.
  • adevine
    adevine over 10 years
    I think this answer should be higher. If you just want to see if an Annotation instance is of a particular type, just use instanceOf, as it's more straightforward than doing annotation.annotationType().equals(javax.validation.Valid.cl‌​ass)
  • Felype
    Felype over 8 years
    Just an observation, this will be always false if the programmer forgets to annotate the @interface with @Retention(value=RUNTIME)