Skip to content

Commit 7befa02

Browse files
author
ronma
committed
migrate to java 8.
removed guava and migrated to streams api. simplified store. cleanups.
1 parent 83433db commit 7befa02

8 files changed

Lines changed: 81 additions & 158 deletions

File tree

src/main/java/org/reflections/ReflectionUtils.java

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
import java.util.ArrayList;
1212
import java.util.Arrays;
1313
import java.util.Collection;
14-
import java.util.Collections;
1514
import java.util.HashSet;
1615
import java.util.LinkedHashSet;
1716
import java.util.List;
17+
import java.util.Objects;
1818
import java.util.Set;
1919
import java.util.function.Predicate;
2020
import java.util.regex.Pattern;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.IntStream;
2123

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

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

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

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

344341
private static Set<Annotation> parameterAnnotations(Member member) {
345-
Set<Annotation> result = new HashSet<>();
346342
Annotation[][] annotations =
347343
member instanceof Method ? ((Method) member).getParameterAnnotations() :
348344
member instanceof Constructor ? ((Constructor) member).getParameterAnnotations() : null;
349-
for (Annotation[] annotation : annotations) Collections.addAll(result, annotation);
350-
return result;
345+
return Arrays.stream(annotations).flatMap(Arrays::stream).collect(Collectors.toSet());
351346
}
352347

353-
private static Set<Class<? extends Annotation>> annotationTypes(Iterable<Annotation> annotations) {
354-
Set<Class<? extends Annotation>> result = new HashSet<>();
355-
for (Annotation annotation : annotations) result.add(annotation.annotationType());
356-
return result;
348+
private static Set<Class<? extends Annotation>> annotationTypes(Collection<Annotation> annotations) {
349+
return annotations.stream().map(Annotation::annotationType).collect(Collectors.toSet());
357350
}
358351

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

365356
//
@@ -402,12 +393,8 @@ private static boolean isAssignable(Class[] childClasses, Class[] parentClasses)
402393
if (childClasses.length != parentClasses.length) {
403394
return false;
404395
}
405-
for (int i = 0; i < childClasses.length; i++) {
406-
if (!parentClasses[i].isAssignableFrom(childClasses[i]) ||
407-
(parentClasses[i] == Object.class && childClasses[i] != Object.class)) {
408-
return false;
409-
}
410-
}
411-
return true;
396+
return IntStream.range(0, childClasses.length)
397+
.noneMatch(i -> !parentClasses[i].isAssignableFrom(childClasses[i]) ||
398+
parentClasses[i] == Object.class && childClasses[i] != Object.class);
412399
}
413400
}

src/main/java/org/reflections/Reflections.java

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ private void expandSupertypes(Store store, String key, Class<?> type) {
401401
* <p/>depends on SubTypesScanner configured
402402
*/
403403
public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type) {
404-
return ReflectionUtils.forNames(
405-
store.getAll(SubTypesScanner.class, Collections.singletonList(type.getName())), loaders());
404+
return forNames(store.getAll(SubTypesScanner.class, type.getName()), loaders());
406405
}
407406

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

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

456-
protected Set<String> getAllAnnotated(Set<String> annotated, boolean inherited, boolean honorInherited) {
456+
protected Collection<String> getAllAnnotated(Collection<String> annotated, Class<? extends Annotation> annotation, boolean honorInherited) {
457457
if (honorInherited) {
458-
if (inherited) {
459-
Set<String> subTypes = store.get(SubTypesScanner.class, filter(annotated, (Predicate<String>) input -> {
458+
if (annotation.isAnnotationPresent(Inherited.class)) {
459+
Set<String> subTypes = store.get(SubTypesScanner.class, filter(annotated, input -> {
460460
final Class<?> type = forName(input, loaders());
461461
return type != null && !type.isInterface();
462462
}));
463-
return concat(subTypes, store.getAll(SubTypesScanner.class, subTypes));
463+
return store.getAllIncluding(SubTypesScanner.class, subTypes);
464464
} else {
465465
return annotated;
466466
}
467467
} else {
468-
Set<String> subTypes = concat(annotated, store.getAll(TypeAnnotationsScanner.class, annotated));
469-
return concat(subTypes, store.getAll(SubTypesScanner.class, subTypes));
468+
Collection<String> subTypes = store.getAllIncluding(TypeAnnotationsScanner.class, annotated);
469+
return store.getAllIncluding(SubTypesScanner.class, subTypes);
470470
}
471471
}
472472

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

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

506505
/** get methods with any parameter annotated with given annotation, including annotation member values matching */
507506
public Set<Method> getMethodsWithAnyParamAnnotated(Annotation annotation) {
508-
return getMethodsWithAnyParamAnnotated(annotation.annotationType())
509-
.stream()
510-
.filter(withAnyParameterAnnotation(annotation))
511-
.collect(Collectors.toSet());
507+
return filter(getMethodsWithAnyParamAnnotated(annotation.annotationType()), withAnyParameterAnnotation(annotation));
512508
}
513509

514510
/**
515511
* get all constructors annotated with a given annotation
516512
* <p/>depends on MethodAnnotationsScanner configured
517513
*/
518514
public Set<Constructor> getConstructorsAnnotatedWith(final Class<? extends Annotation> annotation) {
519-
Set<String> methods = store.get(MethodAnnotationsScanner.class, annotation.getName());
520-
return getConstructorsFromDescriptors(methods, loaders());
515+
return getConstructorsFromDescriptors(store.get(MethodAnnotationsScanner.class, annotation.getName()), loaders());
521516
}
522517

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

541536
/** get constructors with any parameter annotated with given annotation, including annotation member values matching */
542537
public Set<Constructor> getConstructorsWithAnyParamAnnotated(Annotation annotation) {
543-
return getConstructorsWithAnyParamAnnotated(annotation.annotationType())
544-
.stream()
545-
.filter(withAnyParameterAnnotation(annotation))
546-
.collect(Collectors.toSet());
538+
return filter(getConstructorsWithAnyParamAnnotated(annotation.annotationType()), withAnyParameterAnnotation(annotation));
547539
}
548540

549541
/**
550542
* get all fields annotated with a given annotation
551543
* <p/>depends on FieldAnnotationsScanner configured
552544
*/
553545
public Set<Field> getFieldsAnnotatedWith(final Class<? extends Annotation> annotation) {
554-
final Set<Field> result = new HashSet<>();
555-
for (String annotated : store.get(FieldAnnotationsScanner.class, annotation.getName())) {
556-
result.add(getFieldFromString(annotated, loaders()));
557-
}
558-
return result;
546+
return store.get(FieldAnnotationsScanner.class, annotation.getName()).stream()
547+
.map(annotated -> getFieldFromString(annotated, loaders()))
548+
.collect(Collectors.toSet());
559549
}
560550

561551
/**
@@ -661,7 +651,7 @@ public File save(final String filename) {
661651
*/
662652
public File save(final String filename, final Serializer serializer) {
663653
File file = serializer.save(this, filename);
664-
if (log != null) //noinspection ConstantConditions
654+
if (log != null)
665655
log.info("Reflections successfully saved in " + file.getAbsolutePath() + " using " + serializer.getClass().getSimpleName());
666656
return file;
667657
}

src/main/java/org/reflections/Store.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ private Map<String, Collection<String>> get(String index) {
4242
}
4343

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

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

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

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

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

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

9091
/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
9192
public Set<String> getAll(Class<?> scannerClass, String key) {
92-
return getAll(index(scannerClass), key);
93-
}
94-
95-
/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
96-
public Set<String> getAll(String index, String key) {
97-
return getAllIncluding(index, get(index, key));
98-
}
99-
100-
/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
101-
public Set<String> getAll(Class<?> scannerClass, Iterable<String> keys) {
102-
return getAll(index(scannerClass), keys);
93+
return getAllIncluding(scannerClass, get(scannerClass, key));
10394
}
10495

10596
/** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */
106-
public Set<String> getAll(String index, Iterable<String> keys) {
107-
return getAllIncluding(index, get(index, keys));
97+
public Set<String> getAll(Class<?> scannerClass, Collection<String> keys) {
98+
return getAllIncluding(scannerClass, get(scannerClass, keys));
10899
}
109100

110101
public Set<String> keys(String index) {

src/main/java/org/reflections/adapters/JavaReflectionAdapter.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import java.lang.reflect.Modifier;
1212
import java.util.ArrayList;
1313
import java.util.Arrays;
14+
import java.util.Collections;
1415
import java.util.List;
16+
import java.util.stream.Collectors;
1517

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

3840
public List<String> getParameterNames(final Member member) {
39-
List<String> result = new ArrayList<>();
40-
4141
Class<?>[] parameterTypes = member instanceof Method ? ((Method) member).getParameterTypes() :
4242
member instanceof Constructor ? ((Constructor) member).getParameterTypes() : null;
4343

44-
if (parameterTypes != null) {
45-
for (Class<?> paramType : parameterTypes) {
46-
String name = getName(paramType);
47-
result.add(name);
48-
}
49-
}
50-
51-
return result;
44+
return parameterTypes != null ? Arrays.stream(parameterTypes).map(JavaReflectionAdapter::getName).collect(Collectors.toList()) : Collections.emptyList();
5245
}
5346

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

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

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

134125
//
135126
private List<String> getAnnotationNames(Annotation[] annotations) {
136-
List<String> names = new ArrayList<>(annotations.length);
137-
for (Annotation annotation : annotations) {
138-
names.add(annotation.annotationType().getName());
139-
}
140-
return names;
127+
return Arrays.stream(annotations).map(annotation -> annotation.annotationType().getName()).collect(Collectors.toList());
141128
}
142129

143130
public static String getName(Class type) {

0 commit comments

Comments
 (0)