BAEL-5645 - Scanning Java Annotations at Runtime (#12511)

* 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

Co-authored-by: elcimduran <elcim.duran@kboxglobal.com>
This commit is contained in:
Elçim Duran 2022-07-21 22:17:26 +03:00 committed by GitHub
parent b23fcdc4c4
commit 50955510ca
12 changed files with 403 additions and 0 deletions

View File

@ -62,6 +62,16 @@
<artifactId>spqr</artifactId> <artifactId>spqr</artifactId>
<version>0.11.2</version> <version>0.11.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>2.4.3.Final</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -92,6 +102,27 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<phase>compile</phase>
<id>make-index</id>
<goals>
<goal>jandex</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>

View File

@ -0,0 +1,14 @@
package com.baeldung.annotation.scanner;
@SampleAnnotation(name = "SampleAnnotatedClass")
public class SampleAnnotatedClass {
@SampleAnnotation(name = "annotatedMethod")
public void annotatedMethod() {
//Do something
}
public void notAnnotatedMethod() {
//Do something
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.annotation.scanner;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target({ METHOD, TYPE })
@Retention(RUNTIME)
public @interface SampleAnnotation {
String name();
}

View File

@ -0,0 +1,13 @@
package com.baeldung.annotation.scanner;
import java.util.List;
public interface SampleAnnotationScanner {
List<String> scanAnnotatedMethods();
List<String> scanAnnotatedClasses();
boolean supportsMethodScan();
boolean supportsClassScan();
}

View File

@ -0,0 +1,4 @@
package com.baeldung.annotation.scanner;
public class ScanNotSupportedException extends RuntimeException{
}

View File

@ -0,0 +1,7 @@
package com.baeldung.annotation.scanner;
public class UnexpectedScanException extends RuntimeException {
public UnexpectedScanException(Throwable ex) {
super(ex);
}
}

View File

@ -0,0 +1,76 @@
package com.baeldung.annotation.scanner.jandexlib;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import com.baeldung.annotation.scanner.SampleAnnotationScanner;
import com.baeldung.annotation.scanner.UnexpectedScanException;
@Service
public class JandexScannerService implements SampleAnnotationScanner {
@Value("classpath:META-INF/jandex.idx")
private Resource appFile;
@Override
public List<String> scanAnnotatedMethods() {
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"));
List<String> annotatedMethods = new ArrayList<>();
for (AnnotationInstance annotationInstance : appAnnotationList) {
if (annotationInstance.target()
.kind() == AnnotationTarget.Kind.METHOD) {
annotatedMethods.add(annotationInstance.value("name")
.value()
.toString());
}
}
return Collections.unmodifiableList(annotatedMethods);
} catch (IOException e) {
throw new UnexpectedScanException(e);
}
}
@Override
public List<String> scanAnnotatedClasses() {
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"));
List<String> annotatedClasses = new ArrayList<>();
for (AnnotationInstance annotationInstance : appAnnotationList) {
if (annotationInstance.target()
.kind() == AnnotationTarget.Kind.CLASS) {
annotatedClasses.add(annotationInstance.value("name")
.value()
.toString());
}
}
return Collections.unmodifiableList(annotatedClasses);
} catch (IOException e) {
throw new UnexpectedScanException(e);
}
}
@Override
public boolean supportsMethodScan() {
return true;
}
@Override
public boolean supportsClassScan() {
return true;
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.annotation.scanner.javareflectionlib;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Service;
import com.baeldung.annotation.scanner.SampleAnnotation;
import com.baeldung.annotation.scanner.SampleAnnotationScanner;
import com.baeldung.annotation.scanner.UnexpectedScanException;
@Service
public class JavaReflectionsScannerService implements SampleAnnotationScanner {
@Override
public List<String> scanAnnotatedMethods() {
try {
Class<?> clazz = ClassLoader.getSystemClassLoader()
.loadClass("com.baeldung.annotation.scanner.SampleAnnotatedClass");
Method[] methods = clazz.getMethods();
List<String> annotatedMethods = new ArrayList<>();
for (Method method : methods) {
SampleAnnotation annotation = method.getAnnotation(SampleAnnotation.class);
if (annotation != null) {
annotatedMethods.add(annotation.name());
}
}
return Collections.unmodifiableList(annotatedMethods);
} catch (ClassNotFoundException e) {
throw new UnexpectedScanException(e);
}
}
@Override
public List<String> scanAnnotatedClasses() {
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());
return Collections.unmodifiableList(annotatedClasses);
} catch (ClassNotFoundException e) {
throw new UnexpectedScanException(e);
}
}
@Override
public boolean supportsMethodScan() {
return true;
}
@Override
public boolean supportsClassScan() {
return true;
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.annotation.scanner.reflectionslib;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.reflections.Reflections;
import org.springframework.stereotype.Service;
import com.baeldung.annotation.scanner.SampleAnnotation;
import com.baeldung.annotation.scanner.SampleAnnotationScanner;
@Service
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);
return methods.stream()
.map(method -> method.getAnnotation(SampleAnnotation.class)
.name())
.collect(Collectors.toList());
}
@Override
public List<String> scanAnnotatedClasses() {
Reflections reflections = new Reflections("com.baeldung.annotation.scanner");
Set<Class<?>> types = reflections.getTypesAnnotatedWith(SampleAnnotation.class);
return types.stream()
.map(clazz -> clazz.getAnnotation(SampleAnnotation.class)
.name())
.collect(Collectors.toList());
}
@Override
public boolean supportsMethodScan() {
return true;
}
@Override
public boolean supportsClassScan() {
return true;
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.annotation.scanner.springcontextlib;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Service;
import com.baeldung.annotation.scanner.SampleAnnotation;
import com.baeldung.annotation.scanner.SampleAnnotationScanner;
import com.baeldung.annotation.scanner.ScanNotSupportedException;
@Service
public class SpringBeanAnnotationScannerService implements SampleAnnotationScanner {
@Override
public List<String> scanAnnotatedMethods() {
throw new ScanNotSupportedException();
}
@Override
public List<String> scanAnnotatedClasses() {
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(SampleAnnotation.class));
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()
.getAnnotationAttributes(SampleAnnotation.class.getCanonicalName());
annotatedBeans.add(annotAttributeMap.get("name")
.toString());
}
}
return Collections.unmodifiableList(annotatedBeans);
}
@Override
public boolean supportsMethodScan() {
return false;
}
@Override
public boolean supportsClassScan() {
return true;
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.annotation.scanner.springcorelib;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
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.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)
.map(method -> method.getAnnotation(SampleAnnotation.class)
.name())
.collect(Collectors.toList());
}
@Override
public List<String> scanAnnotatedClasses() {
throw new ScanNotSupportedException();
}
@Override
public boolean supportsMethodScan() {
return true;
}
@Override
public boolean supportsClassScan() {
return false;
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.annotation.scanner;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleAnnotationScannerUnitTest {
@Autowired
private List<SampleAnnotationScanner> scannerList;
@Test
public void givenPackage_whenScanAnnotatedClasses_thenAnnotationValues() {
final List<String> annotatedClasses = scannerList.stream()
.filter(SampleAnnotationScanner::supportsClassScan)
.map(SampleAnnotationScanner::scanAnnotatedClasses)
.flatMap(Collection::stream)
.collect(Collectors.toList());
assertNotNull(annotatedClasses);
assertEquals(4, annotatedClasses.size());
annotatedClasses.forEach(annotValue -> assertEquals("SampleAnnotatedClass", annotValue));
}
@Test
public void givenPackage_whenScanAnnotatedMethods_thenAnnotationValues() {
final List<String> annotatedMethods = scannerList.stream()
.filter(SampleAnnotationScanner::supportsMethodScan)
.map(SampleAnnotationScanner::scanAnnotatedMethods)
.flatMap(Collection::stream)
.collect(Collectors.toList());
assertNotNull(annotatedMethods);
assertEquals(3, annotatedMethods.size());
annotatedMethods.forEach(annotValue -> assertEquals("annotatedMethod", annotValue));
}
}