1717 * @author Luc Boutier
1818 */
1919public final class AnnotationScanner {
20- private static final ESLogger LOGGER = Loggers .getLogger (MappingBuilder .class );
20+ private static final ESLogger LOGGER = Loggers .getLogger (MappingBuilder .class );
2121
22- /** Utility classes should have private constructor. */
23- private AnnotationScanner () {}
22+ /** Utility classes should have private constructor. */
23+ private AnnotationScanner () {
24+ }
2425
25- /**
26- * Scan a package to find classes that have the given annotation.
27- *
28- * @param packageRoot The package to scan.
29- * @param anno Annotation that should be on the class that we are interested in.
30- * @return A set of classes that have the annotation.
31- */
32- public static Set <Class <?>> scan (String packageRoot , Class <? extends Annotation > anno ) {
33- ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider (false );
26+ /**
27+ * Scan a package to find classes that have the given annotation.
28+ *
29+ * @param packageRoot The package to scan.
30+ * @param anno Annotation that should be on the class that we are interested in.
31+ * @return A set of classes that have the annotation.
32+ */
33+ public static Set <Class <?>> scan (String packageRoot , Class <? extends Annotation > anno ) {
34+ ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider (false );
3435
35- AnnotationTypeFilter filter = new AnnotationTypeFilter (anno );
36- scanner .addIncludeFilter (filter );
37- Set <BeanDefinition > beanSet = scanner .findCandidateComponents (packageRoot );
36+ AnnotationTypeFilter filter = new AnnotationTypeFilter (anno );
37+ scanner .addIncludeFilter (filter );
38+ Set <BeanDefinition > beanSet = scanner .findCandidateComponents (packageRoot );
3839
39- Set <Class <?>> classSet = new HashSet <Class <?>>();
40- for (BeanDefinition beanDef : beanSet ) {
41- LOGGER .debug ("found candidate bean = " + beanDef .getBeanClassName ());
40+ Set <Class <?>> classSet = new HashSet <Class <?>>();
41+ for (BeanDefinition beanDef : beanSet ) {
42+ LOGGER .debug ("found candidate bean = " + beanDef .getBeanClassName ());
4243
43- Class <?> clazz ;
44- try {
45- clazz = Class .forName (beanDef .getBeanClassName (), true , Thread .currentThread ().getContextClassLoader ());
46- if (clazz .isAnnotationPresent (anno )) {
47- LOGGER .debug ("found annotated class, " + clazz .getName ());
48- classSet .add (clazz );
49- }
50- } catch (ClassNotFoundException e ) {
51- LOGGER .error ("exception while scanning classpath for annotated classes" , e );
52- }
53- }
44+ Class <?> clazz ;
45+ try {
46+ clazz = Class .forName (beanDef .getBeanClassName (), true , Thread .currentThread ().getContextClassLoader ());
47+ if (clazz .isAnnotationPresent (anno )) {
48+ LOGGER .debug ("found annotated class, " + clazz .getName ());
49+ classSet .add (clazz );
50+ }
51+ } catch (ClassNotFoundException e ) {
52+ LOGGER .error ("exception while scanning classpath for annotated classes" , e );
53+ }
54+ }
5455
55- return classSet ;
56- }
56+ return classSet ;
57+ }
5758
58- /**
59- * Get an annotation on the class or one of the super classes.
60- *
61- * @param annotationClass The annotation to get.
62- * @param clazz The class on which to search for the annotation.
63- * @return The annotation for this class or null if not found neither on the class or one of it's super class.
64- */
65- public static <T extends Annotation > T getAnnotation (Class <T > annotationClass , Class <?> clazz ) {
66- if (clazz == Object .class ) {
67- return null ;
68- }
69- T annotationInstance = clazz .getAnnotation (annotationClass );
70- if (annotationInstance == null ) {
71- return getAnnotation (annotationClass , clazz .getSuperclass ());
72- }
73- return annotationInstance ;
74- }
59+ /**
60+ * Get an annotation on the class or one of the super classes.
61+ *
62+ * @param annotationClass The annotation to get.
63+ * @param clazz The class on which to search for the annotation.
64+ * @return The annotation for this class or null if not found neither on the class or one of it's super class.
65+ */
66+ public static <T extends Annotation > T getAnnotation (Class <T > annotationClass , Class <?> clazz ) {
67+ if (clazz == Object .class ) {
68+ return null ;
69+ }
70+ T annotationInstance = clazz .getAnnotation (annotationClass );
71+ if (annotationInstance == null ) {
72+ return getAnnotation (annotationClass , clazz .getSuperclass ());
73+ }
74+ return annotationInstance ;
75+ }
7576}
0 commit comments