Bael-5645 - Scanning Java Annotations at Runtime (#12527)

* BAEL-5645 - Annotation scanners using Spring context, Spring core, reflections, java reflection, and Jandex libraries are implemented

* BAEL-5645 - Library versions are incremented in pom.xml

* BAEL-5645 - Some refactoring and indentation adjustments

* BAEL-5645 - Revert of annotation value in unit test

* BAEL-5645 - Merge with the latest remote version (after resolution of the conflicts)

* BAEL-5645 - Some refactoring due to review comments

Co-authored-by: elcimduran <elcim.duran@kboxglobal.com>
This commit is contained in:
Elçim Duran 2022-07-29 16:13:54 +03:00 committed by GitHub
parent d41f65b479
commit 67c9cfec23
6 changed files with 36 additions and 17 deletions

View File

@ -27,7 +27,9 @@ public class JandexScannerService implements SampleAnnotationScanner {
try {
final IndexReader reader = new IndexReader(appFile.getInputStream());
Index jandexFile = reader.read();
final List<AnnotationInstance> appAnnotationList = jandexFile.getAnnotations(DotName.createSimple("com.baeldung.annotation.scanner.SampleAnnotation"));
final List<AnnotationInstance> appAnnotationList = jandexFile
.getAnnotations(DotName
.createSimple("com.baeldung.annotation.scanner.SampleAnnotation"));
List<String> 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<AnnotationInstance> appAnnotationList = jandexFile.getAnnotations(DotName.createSimple("com.baeldung.annotation.scanner.SampleAnnotation"));
final List<AnnotationInstance> appAnnotationList = jandexFile
.getAnnotations(DotName
.createSimple("com.baeldung.annotation.scanner.SampleAnnotation"));
List<String> annotatedClasses = new ArrayList<>();
for (AnnotationInstance annotationInstance : appAnnotationList) {
if (annotationInstance.target()

View File

@ -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<String> 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);

View File

@ -16,7 +16,8 @@ public class ReflectionsScannerService implements SampleAnnotationScanner {
@Override
public List<String> scanAnnotatedMethods() {
Reflections reflections = new Reflections("com.baeldung.annotation.scanner");
Set<Method> methods = reflections.getMethodsAnnotatedWith(SampleAnnotation.class);
Set<Method> 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<String> scanAnnotatedClasses() {
Reflections reflections = new Reflections("com.baeldung.annotation.scanner");
Set<Class<?>> types = reflections.getTypesAnnotatedWith(SampleAnnotation.class);
Set<Class<?>> types = reflections
.getTypesAnnotatedWith(SampleAnnotation.class);
return types.stream()
.map(clazz -> clazz.getAnnotation(SampleAnnotation.class)
.name())

View File

@ -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<String> scanAnnotatedMethods() {
throw new ScanNotSupportedException();
@ -25,13 +25,16 @@ public class SpringBeanAnnotationScannerService implements SampleAnnotationScann
@Override
public List<String> scanAnnotatedClasses() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(SampleAnnotation.class));
Set<BeanDefinition> beanDefs = provider.findCandidateComponents("com.baeldung.annotation.scanner");
Set<BeanDefinition> beanDefs = provider
.findCandidateComponents("com.baeldung.annotation.scanner");
List<String> annotatedBeans = new ArrayList<>();
for (BeanDefinition bd : beanDefs) {
if (bd instanceof AnnotatedBeanDefinition) {
Map<String, Object> annotAttributeMap = ((AnnotatedBeanDefinition) bd).getMetadata()
Map<String, Object> annotAttributeMap = ((AnnotatedBeanDefinition) bd)
.getMetadata()
.getAnnotationAttributes(SampleAnnotation.class.getCanonicalName());
annotatedBeans.add(annotAttributeMap.get("name")
.toString());

View File

@ -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<String> 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<String> 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

View File

@ -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));
}
}