Skip to content

Commit

Permalink
Update reflections library (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kapferer authored Mar 11, 2022
1 parent 5c82823 commit 064a892
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ ossReleaseStagingRepository=https://oss.sonatype.org/service/local/staging/deplo

# dependency versions
jUnitVersion=5.5.2
cmlVersion=6.3.0
reflectionsVersion=0.9.12
springBootVersion=2.2.0.RELEASE
springWebVersion=5.2.0.RELEASE
cmlVersion=6.6.1
reflectionsVersion=0.10.2
springBootVersion=2.6.4
springWebVersion=5.3.16
commonsLangVersion=3.9
commonsIOVersion=2.6
snakeYMLVersion=1.25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,18 @@
*/
package org.contextmapper.discovery.strategies.helper;

import org.reflections.Configuration;
import org.reflections.Reflections;
import org.reflections.ReflectionsException;
import org.reflections.Store;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.scanners.Scanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import org.slf4j.Logger;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import static org.reflections.util.Utils.findLogger;
import static org.reflections.scanners.Scanners.*;

/**
* Provides methods for annotation scanning.
Expand All @@ -43,8 +35,6 @@
*/
public class AnnotationScanner {

public static Logger log = findLogger(AnnotationScanner.class);

/**
* Finds all types within a package annotated with a given annotation.
*
Expand All @@ -53,7 +43,7 @@ public class AnnotationScanner {
* @return the set of types within the given package which are annotated with the given annotation
*/
public Set<Class<?>> scanForAnnotatedType(String packageName, Class<? extends Annotation> annotation) {
Reflections reflections = new Reflections(packageName, new SubTypesScanner(false), new TypeAnnotationsScanner());
Reflections reflections = new Reflections(packageName);
return reflections.getTypesAnnotatedWith(annotation);
}

Expand All @@ -66,20 +56,13 @@ public Set<Class<?>> scanForAnnotatedType(String packageName, Class<? extends An
*/
public Set<Method> scanForAnnotatedMethods(Class<?> type, Class<? extends Annotation>... annotations) {
Set<Method> methods = new HashSet<>();
try {
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forClass(type))
.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner(), new MethodAnnotationsScanner())
.filterInputsBy(new FilterBuilder().includePackage(type))
);
for (Class<? extends Annotation> annotation : annotations) {
methods.addAll(reflections.getMethodsAnnotatedWith(annotation));
}
} catch (ReflectionsException e) {
// unfortunately the reflections library in version 0.9.12 (needed for Java 11) throws an exception if nothing is found
// just log errors here for now; should no longer be needed to catch this when the following issue is fixed:
// https://github.com/ronmamo/reflections/issues/273
log.error(e.getMessage());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(ClasspathHelper.forClass(type))
.setScanners(TypesAnnotated, MethodsAnnotated, MethodsReturn)
.filterInputsBy(new FilterBuilder().includePackage(type.getPackageName()))
);
for (Class<? extends Annotation> annotation : annotations) {
methods.addAll(reflections.getMethodsAnnotatedWith(annotation));
}
return methods.stream().filter(m -> m.getDeclaringClass().equals(type)).collect(Collectors.toSet());
}
Expand Down

0 comments on commit 064a892

Please sign in to comment.