diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java index fc10db223e..7ed41af65d 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/jandexlib/JandexScannerService.java @@ -27,7 +27,9 @@ public class JandexScannerService implements SampleAnnotationScanner { try { final IndexReader reader = new IndexReader(appFile.getInputStream()); Index jandexFile = reader.read(); - final List appAnnotationList = jandexFile.getAnnotations(DotName.createSimple("com.baeldung.annotation.scanner.SampleAnnotation")); + final List appAnnotationList = jandexFile + .getAnnotations(DotName + .createSimple("com.baeldung.annotation.scanner.SampleAnnotation")); List annotatedMethods = new ArrayList<>(); for (AnnotationInstance annotationInstance : appAnnotationList) { if (annotationInstance.target() @@ -48,7 +50,9 @@ public class JandexScannerService implements SampleAnnotationScanner { try { final IndexReader reader = new IndexReader(appFile.getInputStream()); Index jandexFile = reader.read(); - final List appAnnotationList = jandexFile.getAnnotations(DotName.createSimple("com.baeldung.annotation.scanner.SampleAnnotation")); + final List appAnnotationList = jandexFile + .getAnnotations(DotName + .createSimple("com.baeldung.annotation.scanner.SampleAnnotation")); List annotatedClasses = new ArrayList<>(); for (AnnotationInstance annotationInstance : appAnnotationList) { if (annotationInstance.target() diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java index 2833bb1326..76a2293965 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/javareflectionlib/JavaReflectionsScannerService.java @@ -1,5 +1,6 @@ package com.baeldung.annotation.scanner.javareflectionlib; +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; @@ -37,9 +38,14 @@ public class JavaReflectionsScannerService implements SampleAnnotationScanner { try { Class clazz = ClassLoader.getSystemClassLoader() .loadClass("com.baeldung.annotation.scanner.SampleAnnotatedClass"); - SampleAnnotation classAnnotation = clazz.getAnnotation(SampleAnnotation.class); List annotatedClasses = new ArrayList<>(); - annotatedClasses.add(classAnnotation.name()); + Annotation[] classAnnotations = clazz.getAnnotations(); + for (Annotation annotation : classAnnotations) { + if (annotation.annotationType() + .equals(SampleAnnotation.class)) { + annotatedClasses.add(((SampleAnnotation) annotation).name()); + } + } return Collections.unmodifiableList(annotatedClasses); } catch (ClassNotFoundException e) { throw new UnexpectedScanException(e); diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java index 82a10e21aa..9cee4f19e2 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/reflectionslib/ReflectionsScannerService.java @@ -16,7 +16,8 @@ public class ReflectionsScannerService implements SampleAnnotationScanner { @Override public List scanAnnotatedMethods() { Reflections reflections = new Reflections("com.baeldung.annotation.scanner"); - Set methods = reflections.getMethodsAnnotatedWith(SampleAnnotation.class); + Set methods = reflections + .getMethodsAnnotatedWith(SampleAnnotation.class); return methods.stream() .map(method -> method.getAnnotation(SampleAnnotation.class) .name()) @@ -26,7 +27,8 @@ public class ReflectionsScannerService implements SampleAnnotationScanner { @Override public List scanAnnotatedClasses() { Reflections reflections = new Reflections("com.baeldung.annotation.scanner"); - Set> types = reflections.getTypesAnnotatedWith(SampleAnnotation.class); + Set> types = reflections + .getTypesAnnotatedWith(SampleAnnotation.class); return types.stream() .map(clazz -> clazz.getAnnotation(SampleAnnotation.class) .name()) diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringBeanAnnotationScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringContextAnnotationScannerService.java similarity index 80% rename from spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringBeanAnnotationScannerService.java rename to spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringContextAnnotationScannerService.java index cd31ae686e..8d52067ccc 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringBeanAnnotationScannerService.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcontextlib/SpringContextAnnotationScannerService.java @@ -17,7 +17,7 @@ import com.baeldung.annotation.scanner.SampleAnnotationScanner; import com.baeldung.annotation.scanner.ScanNotSupportedException; @Service -public class SpringBeanAnnotationScannerService implements SampleAnnotationScanner { +public class SpringContextAnnotationScannerService implements SampleAnnotationScanner { @Override public List scanAnnotatedMethods() { throw new ScanNotSupportedException(); @@ -25,13 +25,16 @@ public class SpringBeanAnnotationScannerService implements SampleAnnotationScann @Override public List scanAnnotatedClasses() { - ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); + ClassPathScanningCandidateComponentProvider provider = + new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new AnnotationTypeFilter(SampleAnnotation.class)); - Set beanDefs = provider.findCandidateComponents("com.baeldung.annotation.scanner"); + Set beanDefs = provider + .findCandidateComponents("com.baeldung.annotation.scanner"); List annotatedBeans = new ArrayList<>(); for (BeanDefinition bd : beanDefs) { if (bd instanceof AnnotatedBeanDefinition) { - Map annotAttributeMap = ((AnnotatedBeanDefinition) bd).getMetadata() + Map annotAttributeMap = ((AnnotatedBeanDefinition) bd) + .getMetadata() .getAnnotationAttributes(SampleAnnotation.class.getCanonicalName()); annotatedBeans.add(annotAttributeMap.get("name") .toString()); diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java index f3421fe46d..14542a9104 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/annotation/scanner/springcorelib/SpringCoreAnnotationScannerService.java @@ -8,21 +8,24 @@ import org.springframework.core.annotation.AnnotationUtils; import org.springframework.stereotype.Service; import org.springframework.util.ClassUtils; -import com.baeldung.annotation.scanner.SampleAnnotationScanner; import com.baeldung.annotation.scanner.SampleAnnotatedClass; import com.baeldung.annotation.scanner.SampleAnnotation; +import com.baeldung.annotation.scanner.SampleAnnotationScanner; import com.baeldung.annotation.scanner.ScanNotSupportedException; @Service public class SpringCoreAnnotationScannerService implements SampleAnnotationScanner { @Override public List scanAnnotatedMethods() { - final Class userClass = ClassUtils.getUserClass(SampleAnnotatedClass.class); - return Arrays.stream(userClass.getMethods()) - .filter(method -> AnnotationUtils.getAnnotation(method, SampleAnnotation.class) != null) + Class userClass = ClassUtils.getUserClass(SampleAnnotatedClass.class); + List annotatedMethods = Arrays.stream(userClass.getMethods()) + .filter(method -> AnnotationUtils + .getAnnotation(method, SampleAnnotation.class) != null) .map(method -> method.getAnnotation(SampleAnnotation.class) .name()) .collect(Collectors.toList()); + + return annotatedMethods; } @Override diff --git a/spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java b/spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java index 80eca2b4c5..7c3d01b5e8 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/test/java/com/baeldung/annotation/scanner/SampleAnnotationScannerUnitTest.java @@ -28,7 +28,8 @@ public class SampleAnnotationScannerUnitTest { assertNotNull(annotatedClasses); assertEquals(4, annotatedClasses.size()); - annotatedClasses.forEach(annotValue -> assertEquals("SampleAnnotatedClass", annotValue)); + annotatedClasses.forEach(annotValue -> assertEquals("SampleAnnotatedClass", + annotValue)); } @Test @@ -41,7 +42,7 @@ public class SampleAnnotationScannerUnitTest { assertNotNull(annotatedMethods); assertEquals(3, annotatedMethods.size()); - annotatedMethods.forEach(annotValue -> assertEquals("annotatedMethod", annotValue)); + annotatedMethods.forEach(annotValue -> assertEquals("annotatedMethod", + annotValue)); } - }