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\"
Since an annotation is just a class, you can simply use an == compare:
if (annotation.annotationType() == Valid.class) { /* ... */ }
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.
Are you just looking for
if (annotation.annotationType().equals(javax.validation.Valid.class)){}
?
Or even simpler:
if (annotation instanceof Valid) { /* ... */ }
Just for completeness' sake, another possibility is
if (this.getClass().isAnnotationPresent(MyCustomAnnotation.class)) {