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:
parent
d41f65b479
commit
67c9cfec23
|
@ -27,7 +27,9 @@ public class JandexScannerService implements SampleAnnotationScanner {
|
||||||
try {
|
try {
|
||||||
final IndexReader reader = new IndexReader(appFile.getInputStream());
|
final IndexReader reader = new IndexReader(appFile.getInputStream());
|
||||||
Index jandexFile = reader.read();
|
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<>();
|
List<String> annotatedMethods = new ArrayList<>();
|
||||||
for (AnnotationInstance annotationInstance : appAnnotationList) {
|
for (AnnotationInstance annotationInstance : appAnnotationList) {
|
||||||
if (annotationInstance.target()
|
if (annotationInstance.target()
|
||||||
|
@ -48,7 +50,9 @@ public class JandexScannerService implements SampleAnnotationScanner {
|
||||||
try {
|
try {
|
||||||
final IndexReader reader = new IndexReader(appFile.getInputStream());
|
final IndexReader reader = new IndexReader(appFile.getInputStream());
|
||||||
Index jandexFile = reader.read();
|
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<>();
|
List<String> annotatedClasses = new ArrayList<>();
|
||||||
for (AnnotationInstance annotationInstance : appAnnotationList) {
|
for (AnnotationInstance annotationInstance : appAnnotationList) {
|
||||||
if (annotationInstance.target()
|
if (annotationInstance.target()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.annotation.scanner.javareflectionlib;
|
package com.baeldung.annotation.scanner.javareflectionlib;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -37,9 +38,14 @@ public class JavaReflectionsScannerService implements SampleAnnotationScanner {
|
||||||
try {
|
try {
|
||||||
Class<?> clazz = ClassLoader.getSystemClassLoader()
|
Class<?> clazz = ClassLoader.getSystemClassLoader()
|
||||||
.loadClass("com.baeldung.annotation.scanner.SampleAnnotatedClass");
|
.loadClass("com.baeldung.annotation.scanner.SampleAnnotatedClass");
|
||||||
SampleAnnotation classAnnotation = clazz.getAnnotation(SampleAnnotation.class);
|
|
||||||
List<String> annotatedClasses = new ArrayList<>();
|
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);
|
return Collections.unmodifiableList(annotatedClasses);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new UnexpectedScanException(e);
|
throw new UnexpectedScanException(e);
|
||||||
|
|
|
@ -16,7 +16,8 @@ public class ReflectionsScannerService implements SampleAnnotationScanner {
|
||||||
@Override
|
@Override
|
||||||
public List<String> scanAnnotatedMethods() {
|
public List<String> scanAnnotatedMethods() {
|
||||||
Reflections reflections = new Reflections("com.baeldung.annotation.scanner");
|
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()
|
return methods.stream()
|
||||||
.map(method -> method.getAnnotation(SampleAnnotation.class)
|
.map(method -> method.getAnnotation(SampleAnnotation.class)
|
||||||
.name())
|
.name())
|
||||||
|
@ -26,7 +27,8 @@ public class ReflectionsScannerService implements SampleAnnotationScanner {
|
||||||
@Override
|
@Override
|
||||||
public List<String> scanAnnotatedClasses() {
|
public List<String> scanAnnotatedClasses() {
|
||||||
Reflections reflections = new Reflections("com.baeldung.annotation.scanner");
|
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()
|
return types.stream()
|
||||||
.map(clazz -> clazz.getAnnotation(SampleAnnotation.class)
|
.map(clazz -> clazz.getAnnotation(SampleAnnotation.class)
|
||||||
.name())
|
.name())
|
||||||
|
|
|
@ -17,7 +17,7 @@ import com.baeldung.annotation.scanner.SampleAnnotationScanner;
|
||||||
import com.baeldung.annotation.scanner.ScanNotSupportedException;
|
import com.baeldung.annotation.scanner.ScanNotSupportedException;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class SpringBeanAnnotationScannerService implements SampleAnnotationScanner {
|
public class SpringContextAnnotationScannerService implements SampleAnnotationScanner {
|
||||||
@Override
|
@Override
|
||||||
public List<String> scanAnnotatedMethods() {
|
public List<String> scanAnnotatedMethods() {
|
||||||
throw new ScanNotSupportedException();
|
throw new ScanNotSupportedException();
|
||||||
|
@ -25,13 +25,16 @@ public class SpringBeanAnnotationScannerService implements SampleAnnotationScann
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> scanAnnotatedClasses() {
|
public List<String> scanAnnotatedClasses() {
|
||||||
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
|
ClassPathScanningCandidateComponentProvider provider =
|
||||||
|
new ClassPathScanningCandidateComponentProvider(false);
|
||||||
provider.addIncludeFilter(new AnnotationTypeFilter(SampleAnnotation.class));
|
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<>();
|
List<String> annotatedBeans = new ArrayList<>();
|
||||||
for (BeanDefinition bd : beanDefs) {
|
for (BeanDefinition bd : beanDefs) {
|
||||||
if (bd instanceof AnnotatedBeanDefinition) {
|
if (bd instanceof AnnotatedBeanDefinition) {
|
||||||
Map<String, Object> annotAttributeMap = ((AnnotatedBeanDefinition) bd).getMetadata()
|
Map<String, Object> annotAttributeMap = ((AnnotatedBeanDefinition) bd)
|
||||||
|
.getMetadata()
|
||||||
.getAnnotationAttributes(SampleAnnotation.class.getCanonicalName());
|
.getAnnotationAttributes(SampleAnnotation.class.getCanonicalName());
|
||||||
annotatedBeans.add(annotAttributeMap.get("name")
|
annotatedBeans.add(annotAttributeMap.get("name")
|
||||||
.toString());
|
.toString());
|
|
@ -8,21 +8,24 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
import com.baeldung.annotation.scanner.SampleAnnotationScanner;
|
|
||||||
import com.baeldung.annotation.scanner.SampleAnnotatedClass;
|
import com.baeldung.annotation.scanner.SampleAnnotatedClass;
|
||||||
import com.baeldung.annotation.scanner.SampleAnnotation;
|
import com.baeldung.annotation.scanner.SampleAnnotation;
|
||||||
|
import com.baeldung.annotation.scanner.SampleAnnotationScanner;
|
||||||
import com.baeldung.annotation.scanner.ScanNotSupportedException;
|
import com.baeldung.annotation.scanner.ScanNotSupportedException;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class SpringCoreAnnotationScannerService implements SampleAnnotationScanner {
|
public class SpringCoreAnnotationScannerService implements SampleAnnotationScanner {
|
||||||
@Override
|
@Override
|
||||||
public List<String> scanAnnotatedMethods() {
|
public List<String> scanAnnotatedMethods() {
|
||||||
final Class<?> userClass = ClassUtils.getUserClass(SampleAnnotatedClass.class);
|
Class<?> userClass = ClassUtils.getUserClass(SampleAnnotatedClass.class);
|
||||||
return Arrays.stream(userClass.getMethods())
|
List<String> annotatedMethods = Arrays.stream(userClass.getMethods())
|
||||||
.filter(method -> AnnotationUtils.getAnnotation(method, SampleAnnotation.class) != null)
|
.filter(method -> AnnotationUtils
|
||||||
|
.getAnnotation(method, SampleAnnotation.class) != null)
|
||||||
.map(method -> method.getAnnotation(SampleAnnotation.class)
|
.map(method -> method.getAnnotation(SampleAnnotation.class)
|
||||||
.name())
|
.name())
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
return annotatedMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -28,7 +28,8 @@ public class SampleAnnotationScannerUnitTest {
|
||||||
|
|
||||||
assertNotNull(annotatedClasses);
|
assertNotNull(annotatedClasses);
|
||||||
assertEquals(4, annotatedClasses.size());
|
assertEquals(4, annotatedClasses.size());
|
||||||
annotatedClasses.forEach(annotValue -> assertEquals("SampleAnnotatedClass", annotValue));
|
annotatedClasses.forEach(annotValue -> assertEquals("SampleAnnotatedClass",
|
||||||
|
annotValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -41,7 +42,7 @@ public class SampleAnnotationScannerUnitTest {
|
||||||
|
|
||||||
assertNotNull(annotatedMethods);
|
assertNotNull(annotatedMethods);
|
||||||
assertEquals(3, annotatedMethods.size());
|
assertEquals(3, annotatedMethods.size());
|
||||||
annotatedMethods.forEach(annotValue -> assertEquals("annotatedMethod", annotValue));
|
annotatedMethods.forEach(annotValue -> assertEquals("annotatedMethod",
|
||||||
|
annotValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue