Skip to content

Commit

Permalink
migrate to java 8.
Browse files Browse the repository at this point in the history
removed guava and migrated to streams api.
simplified store.
cleanups.
  • Loading branch information
ronma committed Jan 7, 2020
1 parent 83433db commit 7befa02
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 158 deletions.
45 changes: 16 additions & 29 deletions src/main/java/org/reflections/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.reflections.util.Utils.filter;

Expand Down Expand Up @@ -194,9 +196,8 @@ public static <T extends AnnotatedElement> Predicate<T> withAnnotations(final An
if (input != null) {
Annotation[] inputAnnotations = input.getAnnotations();
if (inputAnnotations.length == annotations.length) {
for (int i = 0; i < inputAnnotations.length; i++) {
if (!areAnnotationMembersMatching(inputAnnotations[i], annotations[i])) return false;
}
return IntStream.range(0, inputAnnotations.length)
.allMatch(i -> areAnnotationMembersMatching(inputAnnotations[i], annotations[i]));
}
}
return true;
Expand Down Expand Up @@ -325,14 +326,10 @@ public static Class<?> forName(String typeName, ClassLoader... classLoaders) {

/** try to resolve all given string representation of types to a list of java types */
public static <T> Set<Class<? extends T>> forNames(final Collection<String> classes, ClassLoader... classLoaders) {
Set<Class<? extends T>> result = new LinkedHashSet<>();
for (String className : classes) {
Class<?> type = forName(className, classLoaders);
if (type != null) {
result.add((Class<? extends T>) type);
}
}
return result;
return classes.stream()
.map(className -> (Class<? extends T>) forName(className, classLoaders))
.filter(Objects::nonNull)
.collect(Collectors.toCollection(LinkedHashSet::new));
}

private static Class[] parameterTypes(Member member) {
Expand All @@ -342,24 +339,18 @@ private static Class[] parameterTypes(Member member) {
}

private static Set<Annotation> parameterAnnotations(Member member) {
Set<Annotation> result = new HashSet<>();
Annotation[][] annotations =
member instanceof Method ? ((Method) member).getParameterAnnotations() :
member instanceof Constructor ? ((Constructor) member).getParameterAnnotations() : null;
for (Annotation[] annotation : annotations) Collections.addAll(result, annotation);
return result;
return Arrays.stream(annotations).flatMap(Arrays::stream).collect(Collectors.toSet());
}

private static Set<Class<? extends Annotation>> annotationTypes(Iterable<Annotation> annotations) {
Set<Class<? extends Annotation>> result = new HashSet<>();
for (Annotation annotation : annotations) result.add(annotation.annotationType());
return result;
private static Set<Class<? extends Annotation>> annotationTypes(Collection<Annotation> annotations) {
return annotations.stream().map(Annotation::annotationType).collect(Collectors.toSet());
}

private static Class<? extends Annotation>[] annotationTypes(Annotation[] annotations) {
Class<? extends Annotation>[] result = new Class[annotations.length];
for (int i = 0; i < annotations.length; i++) result[i] = annotations[i].annotationType();
return result;
return Arrays.stream(annotations).map(Annotation::annotationType).toArray(Class[]::new);
}

//
Expand Down Expand Up @@ -402,12 +393,8 @@ private static boolean isAssignable(Class[] childClasses, Class[] parentClasses)
if (childClasses.length != parentClasses.length) {
return false;
}
for (int i = 0; i < childClasses.length; i++) {
if (!parentClasses[i].isAssignableFrom(childClasses[i]) ||
(parentClasses[i] == Object.class && childClasses[i] != Object.class)) {
return false;
}
}
return true;
return IntStream.range(0, childClasses.length)
.noneMatch(i -> !parentClasses[i].isAssignableFrom(childClasses[i]) ||
parentClasses[i] == Object.class && childClasses[i] != Object.class);
}
}
52 changes: 21 additions & 31 deletions src/main/java/org/reflections/Reflections.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,7 @@ private void expandSupertypes(Store store, String key, Class<?> type) {
* <p/>depends on SubTypesScanner configured
*/
public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) {
return ReflectionUtils.forNames(
store.getAll(SubTypesScanner.class, Collections.singletonList(type.getName())), loaders());
return forNames(store.getAll(SubTypesScanner.class, type.getName()), loaders());
}

/**
Expand All @@ -428,8 +427,8 @@ public Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> ann
*/
public Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> annotation, boolean honorInherited) {
Set<String> annotated = store.get(TypeAnnotationsScanner.class, annotation.getName());
Set<String> classes = getAllAnnotated(annotated, annotation.isAnnotationPresent(Inherited.class), honorInherited);
return new HashSet<>(concat(forNames(annotated, loaders()), forNames(classes, loaders())));
annotated.addAll(getAllAnnotated(annotated, annotation, honorInherited));
return forNames(annotated, loaders());
}

/**
Expand All @@ -448,25 +447,26 @@ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation) {
*/
public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) {
Set<String> annotated = store.get(TypeAnnotationsScanner.class, annotation.annotationType().getName());
Set<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation));
Set<String> classes = getAllAnnotated(new HashSet<>(names(filter)), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited);
return concat(filter, forNames(filter(classes, s -> !annotated.contains(s)), loaders()));
Set<Class<?>> allAnnotated = filter(forNames(annotated, loaders()), withAnnotation(annotation));
Set<Class<?>> classes = forNames(filter(getAllAnnotated(names(allAnnotated), annotation.annotationType(), honorInherited), s -> !annotated.contains(s)), loaders());
allAnnotated.addAll(classes);
return allAnnotated;
}

protected Set<String> getAllAnnotated(Set<String> annotated, boolean inherited, boolean honorInherited) {
protected Collection<String> getAllAnnotated(Collection<String> annotated, Class<? extends Annotation> annotation, boolean honorInherited) {
if (honorInherited) {
if (inherited) {
Set<String> subTypes = store.get(SubTypesScanner.class, filter(annotated, (Predicate<String>) input -> {
if (annotation.isAnnotationPresent(Inherited.class)) {
Set<String> subTypes = store.get(SubTypesScanner.class, filter(annotated, input -> {
final Class<?> type = forName(input, loaders());
return type != null && !type.isInterface();
}));
return concat(subTypes, store.getAll(SubTypesScanner.class, subTypes));
return store.getAllIncluding(SubTypesScanner.class, subTypes);
} else {
return annotated;
}
} else {
Set<String> subTypes = concat(annotated, store.getAll(TypeAnnotationsScanner.class, annotated));
return concat(subTypes, store.getAll(SubTypesScanner.class, subTypes));
Collection<String> subTypes = store.getAllIncluding(TypeAnnotationsScanner.class, annotated);
return store.getAllIncluding(SubTypesScanner.class, subTypes);
}
}

Expand All @@ -475,8 +475,7 @@ protected Set<String> getAllAnnotated(Set<String> annotated, boolean inherited,
* <p/>depends on MethodAnnotationsScanner configured
*/
public Set<Method> getMethodsAnnotatedWith(final Class<? extends Annotation> annotation) {
Set<String> methods = store.get(MethodAnnotationsScanner.class, annotation.getName());
return getMethodsFromDescriptors(methods, loaders());
return getMethodsFromDescriptors(store.get(MethodAnnotationsScanner.class, annotation.getName()), loaders());
}

/**
Expand Down Expand Up @@ -505,19 +504,15 @@ public Set<Method> getMethodsWithAnyParamAnnotated(Class<? extends Annotation> a

/** get methods with any parameter annotated with given annotation, including annotation member values matching */
public Set<Method> getMethodsWithAnyParamAnnotated(Annotation annotation) {
return getMethodsWithAnyParamAnnotated(annotation.annotationType())
.stream()
.filter(withAnyParameterAnnotation(annotation))
.collect(Collectors.toSet());
return filter(getMethodsWithAnyParamAnnotated(annotation.annotationType()), withAnyParameterAnnotation(annotation));
}

/**
* get all constructors annotated with a given annotation
* <p/>depends on MethodAnnotationsScanner configured
*/
public Set<Constructor> getConstructorsAnnotatedWith(final Class<? extends Annotation> annotation) {
Set<String> methods = store.get(MethodAnnotationsScanner.class, annotation.getName());
return getConstructorsFromDescriptors(methods, loaders());
return getConstructorsFromDescriptors(store.get(MethodAnnotationsScanner.class, annotation.getName()), loaders());
}

/**
Expand All @@ -540,22 +535,17 @@ public Set<Constructor> getConstructorsWithAnyParamAnnotated(Class<? extends Ann

/** get constructors with any parameter annotated with given annotation, including annotation member values matching */
public Set<Constructor> getConstructorsWithAnyParamAnnotated(Annotation annotation) {
return getConstructorsWithAnyParamAnnotated(annotation.annotationType())
.stream()
.filter(withAnyParameterAnnotation(annotation))
.collect(Collectors.toSet());
return filter(getConstructorsWithAnyParamAnnotated(annotation.annotationType()), withAnyParameterAnnotation(annotation));
}

/**
* get all fields annotated with a given annotation
* <p/>depends on FieldAnnotationsScanner configured
*/
public Set<Field> getFieldsAnnotatedWith(final Class<? extends Annotation> annotation) {
final Set<Field> result = new HashSet<>();
for (String annotated : store.get(FieldAnnotationsScanner.class, annotation.getName())) {
result.add(getFieldFromString(annotated, loaders()));
}
return result;
return store.get(FieldAnnotationsScanner.class, annotation.getName()).stream()
.map(annotated -> getFieldFromString(annotated, loaders()))
.collect(Collectors.toSet());
}

/**
Expand Down Expand Up @@ -661,7 +651,7 @@ public File save(final String filename) {
*/
public File save(final String filename, final Serializer serializer) {
File file = serializer.save(this, filename);
if (log != null) //noinspection ConstantConditions
if (log != null)
log.info("Reflections successfully saved in " + file.getAbsolutePath() + " using " + serializer.getClass().getSimpleName());
return file;
}
Expand Down
31 changes: 11 additions & 20 deletions src/main/java/org/reflections/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ private Map<String, Collection<String>> get(String index) {
}

/** get the values stored for the given {@code index} and {@code keys} */
public Set<String> get(Class<?> scannerClass, String keys) {
return get(index(scannerClass), keys);
public Set<String> get(Class<?> scannerClass, String key) {
return get(index(scannerClass), Collections.singletonList(key));
}

/** get the values stored for the given {@code index} and {@code keys} */
public Set<String> get(String index, String keys) {
return get(index, Collections.singletonList(keys));
public Set<String> get(String index, String key) {
return get(index, Collections.singletonList(key));
}

/** get the values stored for the given {@code index} and {@code keys} */
public Set<String> get(Class<?> scannerClass, Iterable<String> keys) {
public Set<String> get(Class<?> scannerClass, Collection<String> keys) {
return get(index(scannerClass), keys);
}

/** get the values stored for the given {@code index} and {@code keys} */
public Set<String> get(String index, Iterable<String> keys) {
private Set<String> get(String index, Collection<String> keys) {
Map<String, Collection<String>> mmap = get(index);
Set<String> result = new LinkedHashSet<>();
for (String key : keys) {
Expand All @@ -70,7 +70,8 @@ public Set<String> get(String index, Iterable<String> keys) {
}

/** recursively get the values stored for the given {@code index} and {@code keys}, including keys */
private Set<String> getAllIncluding(String index, Set<String> keys) {
public Set<String> getAllIncluding(Class<?> scannerClass, Collection<String> keys) {
String index = index(scannerClass);
Map<String, Collection<String>> mmap = get(index);
List<String> workKeys = new ArrayList<>(keys);

Expand All @@ -89,22 +90,12 @@ private Set<String> getAllIncluding(String index, Set<String> keys) {

/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
public Set<String> getAll(Class<?> scannerClass, String key) {
return getAll(index(scannerClass), key);
}

/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
public Set<String> getAll(String index, String key) {
return getAllIncluding(index, get(index, key));
}

/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
public Set<String> getAll(Class<?> scannerClass, Iterable<String> keys) {
return getAll(index(scannerClass), keys);
return getAllIncluding(scannerClass, get(scannerClass, key));
}

/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
public Set<String> getAll(String index, Iterable<String> keys) {
return getAllIncluding(index, get(index, keys));
public Set<String> getAll(Class<?> scannerClass, Collection<String> keys) {
return getAllIncluding(scannerClass, get(scannerClass, keys));
}

public Set<String> keys(String index) {
Expand Down
23 changes: 5 additions & 18 deletions src/main/java/org/reflections/adapters/JavaReflectionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.reflections.ReflectionUtils.forName;
import static org.reflections.util.Utils.join;
Expand All @@ -36,19 +38,10 @@ public String getMethodName(Member method) {
}

public List<String> getParameterNames(final Member member) {
List<String> result = new ArrayList<>();

Class<?>[] parameterTypes = member instanceof Method ? ((Method) member).getParameterTypes() :
member instanceof Constructor ? ((Constructor) member).getParameterTypes() : null;

if (parameterTypes != null) {
for (Class<?> paramType : parameterTypes) {
String name = getName(paramType);
result.add(name);
}
}

return result;
return parameterTypes != null ? Arrays.stream(parameterTypes).map(JavaReflectionAdapter::getName).collect(Collectors.toList()) : Collections.emptyList();
}

public List<String> getClassAnnotationNames(Class aClass) {
Expand Down Expand Up @@ -122,9 +115,7 @@ public String getSuperclassName(Class cls) {

public List<String> getInterfacesNames(Class cls) {
Class[] classes = cls.getInterfaces();
List<String> names = new ArrayList<>(classes != null ? classes.length : 0);
if (classes != null) for (Class cls1 : classes) names.add(cls1.getName());
return names;
return classes != null ? Arrays.stream(classes).map(Class::getName).collect(Collectors.toList()) : Collections.emptyList();
}

public boolean acceptsInput(String file) {
Expand All @@ -133,11 +124,7 @@ public boolean acceptsInput(String file) {

//
private List<String> getAnnotationNames(Annotation[] annotations) {
List<String> names = new ArrayList<>(annotations.length);
for (Annotation annotation : annotations) {
names.add(annotation.annotationType().getName());
}
return names;
return Arrays.stream(annotations).map(annotation -> annotation.annotationType().getName()).collect(Collectors.toList());
}

public static String getName(Class type) {
Expand Down
Loading

0 comments on commit 7befa02

Please sign in to comment.