Reputation: 65
My current situation: I want to write a test that checks, if all my files with "Service" in the name have the "@Valid" Annotation. I tried researching it but could not find anything.
Upvotes: 0
Views: 539
Reputation: 11
The Java Reflection API can be used to check the annotations of a class.
E.g. the following code prints the name and value of each class annotation of type MyAnnotation
.
Class aClass = TheClass.class;
Annotation[] annotations = aClass.getAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println("name: " + myAnnotation.name());
System.out.println("value: " + myAnnotation.value());
}
}
The comment by Lino shows how to find all classes in the current classpath.
Upvotes: 1