diff --git a/src/main/java/org/reflections/ReflectionUtils.java b/src/main/java/org/reflections/ReflectionUtils.java index aa178136..bd82e2ca 100644 --- a/src/main/java/org/reflections/ReflectionUtils.java +++ b/src/main/java/org/reflections/ReflectionUtils.java @@ -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; @@ -194,9 +196,8 @@ public static Predicate 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; @@ -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 Set> forNames(final Collection classes, ClassLoader... classLoaders) { - Set> result = new LinkedHashSet<>(); - for (String className : classes) { - Class type = forName(className, classLoaders); - if (type != null) { - result.add((Class) type); - } - } - return result; + return classes.stream() + .map(className -> (Class) forName(className, classLoaders)) + .filter(Objects::nonNull) + .collect(Collectors.toCollection(LinkedHashSet::new)); } private static Class[] parameterTypes(Member member) { @@ -342,24 +339,18 @@ private static Class[] parameterTypes(Member member) { } private static Set parameterAnnotations(Member member) { - Set 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> annotationTypes(Iterable annotations) { - Set> result = new HashSet<>(); - for (Annotation annotation : annotations) result.add(annotation.annotationType()); - return result; + private static Set> annotationTypes(Collection annotations) { + return annotations.stream().map(Annotation::annotationType).collect(Collectors.toSet()); } private static Class[] annotationTypes(Annotation[] annotations) { - Class[] 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); } // @@ -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); } } diff --git a/src/main/java/org/reflections/Reflections.java b/src/main/java/org/reflections/Reflections.java index 627f6a6f..724b67e6 100644 --- a/src/main/java/org/reflections/Reflections.java +++ b/src/main/java/org/reflections/Reflections.java @@ -401,8 +401,7 @@ private void expandSupertypes(Store store, String key, Class type) { *

depends on SubTypesScanner configured */ public Set> getSubTypesOf(final Class type) { - return ReflectionUtils.forNames( - store.getAll(SubTypesScanner.class, Collections.singletonList(type.getName())), loaders()); + return forNames(store.getAll(SubTypesScanner.class, type.getName()), loaders()); } /** @@ -428,8 +427,8 @@ public Set> getTypesAnnotatedWith(final Class ann */ public Set> getTypesAnnotatedWith(final Class annotation, boolean honorInherited) { Set annotated = store.get(TypeAnnotationsScanner.class, annotation.getName()); - Set 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()); } /** @@ -448,25 +447,26 @@ public Set> getTypesAnnotatedWith(final Annotation annotation) { */ public Set> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Set annotated = store.get(TypeAnnotationsScanner.class, annotation.annotationType().getName()); - Set> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); - Set classes = getAllAnnotated(new HashSet<>(names(filter)), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); - return concat(filter, forNames(filter(classes, s -> !annotated.contains(s)), loaders())); + Set> allAnnotated = filter(forNames(annotated, loaders()), withAnnotation(annotation)); + Set> classes = forNames(filter(getAllAnnotated(names(allAnnotated), annotation.annotationType(), honorInherited), s -> !annotated.contains(s)), loaders()); + allAnnotated.addAll(classes); + return allAnnotated; } - protected Set getAllAnnotated(Set annotated, boolean inherited, boolean honorInherited) { + protected Collection getAllAnnotated(Collection annotated, Class annotation, boolean honorInherited) { if (honorInherited) { - if (inherited) { - Set subTypes = store.get(SubTypesScanner.class, filter(annotated, (Predicate) input -> { + if (annotation.isAnnotationPresent(Inherited.class)) { + Set 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 subTypes = concat(annotated, store.getAll(TypeAnnotationsScanner.class, annotated)); - return concat(subTypes, store.getAll(SubTypesScanner.class, subTypes)); + Collection subTypes = store.getAllIncluding(TypeAnnotationsScanner.class, annotated); + return store.getAllIncluding(SubTypesScanner.class, subTypes); } } @@ -475,8 +475,7 @@ protected Set getAllAnnotated(Set annotated, boolean inherited, *

depends on MethodAnnotationsScanner configured */ public Set getMethodsAnnotatedWith(final Class annotation) { - Set methods = store.get(MethodAnnotationsScanner.class, annotation.getName()); - return getMethodsFromDescriptors(methods, loaders()); + return getMethodsFromDescriptors(store.get(MethodAnnotationsScanner.class, annotation.getName()), loaders()); } /** @@ -505,10 +504,7 @@ public Set getMethodsWithAnyParamAnnotated(Class a /** get methods with any parameter annotated with given annotation, including annotation member values matching */ public Set getMethodsWithAnyParamAnnotated(Annotation annotation) { - return getMethodsWithAnyParamAnnotated(annotation.annotationType()) - .stream() - .filter(withAnyParameterAnnotation(annotation)) - .collect(Collectors.toSet()); + return filter(getMethodsWithAnyParamAnnotated(annotation.annotationType()), withAnyParameterAnnotation(annotation)); } /** @@ -516,8 +512,7 @@ public Set getMethodsWithAnyParamAnnotated(Annotation annotation) { *

depends on MethodAnnotationsScanner configured */ public Set getConstructorsAnnotatedWith(final Class annotation) { - Set methods = store.get(MethodAnnotationsScanner.class, annotation.getName()); - return getConstructorsFromDescriptors(methods, loaders()); + return getConstructorsFromDescriptors(store.get(MethodAnnotationsScanner.class, annotation.getName()), loaders()); } /** @@ -540,10 +535,7 @@ public Set getConstructorsWithAnyParamAnnotated(Class getConstructorsWithAnyParamAnnotated(Annotation annotation) { - return getConstructorsWithAnyParamAnnotated(annotation.annotationType()) - .stream() - .filter(withAnyParameterAnnotation(annotation)) - .collect(Collectors.toSet()); + return filter(getConstructorsWithAnyParamAnnotated(annotation.annotationType()), withAnyParameterAnnotation(annotation)); } /** @@ -551,11 +543,9 @@ public Set getConstructorsWithAnyParamAnnotated(Annotation annotati *

depends on FieldAnnotationsScanner configured */ public Set getFieldsAnnotatedWith(final Class annotation) { - final Set 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()); } /** @@ -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; } diff --git a/src/main/java/org/reflections/Store.java b/src/main/java/org/reflections/Store.java index 008baf0b..4e4ecf6c 100644 --- a/src/main/java/org/reflections/Store.java +++ b/src/main/java/org/reflections/Store.java @@ -42,22 +42,22 @@ private Map> get(String index) { } /** get the values stored for the given {@code index} and {@code keys} */ - public Set get(Class scannerClass, String keys) { - return get(index(scannerClass), keys); + public Set 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 get(String index, String keys) { - return get(index, Collections.singletonList(keys)); + public Set 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 get(Class scannerClass, Iterable keys) { + public Set get(Class scannerClass, Collection keys) { return get(index(scannerClass), keys); } /** get the values stored for the given {@code index} and {@code keys} */ - public Set get(String index, Iterable keys) { + private Set get(String index, Collection keys) { Map> mmap = get(index); Set result = new LinkedHashSet<>(); for (String key : keys) { @@ -70,7 +70,8 @@ public Set get(String index, Iterable keys) { } /** recursively get the values stored for the given {@code index} and {@code keys}, including keys */ - private Set getAllIncluding(String index, Set keys) { + public Set getAllIncluding(Class scannerClass, Collection keys) { + String index = index(scannerClass); Map> mmap = get(index); List workKeys = new ArrayList<>(keys); @@ -89,22 +90,12 @@ private Set getAllIncluding(String index, Set keys) { /** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */ public Set 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 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 getAll(Class scannerClass, Iterable 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 getAll(String index, Iterable keys) { - return getAllIncluding(index, get(index, keys)); + public Set getAll(Class scannerClass, Collection keys) { + return getAllIncluding(scannerClass, get(scannerClass, keys)); } public Set keys(String index) { diff --git a/src/main/java/org/reflections/adapters/JavaReflectionAdapter.java b/src/main/java/org/reflections/adapters/JavaReflectionAdapter.java index 9c854c24..aab18adf 100644 --- a/src/main/java/org/reflections/adapters/JavaReflectionAdapter.java +++ b/src/main/java/org/reflections/adapters/JavaReflectionAdapter.java @@ -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; @@ -36,19 +38,10 @@ public String getMethodName(Member method) { } public List getParameterNames(final Member member) { - List 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 getClassAnnotationNames(Class aClass) { @@ -122,9 +115,7 @@ public String getSuperclassName(Class cls) { public List getInterfacesNames(Class cls) { Class[] classes = cls.getInterfaces(); - List 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) { @@ -133,11 +124,7 @@ public boolean acceptsInput(String file) { // private List getAnnotationNames(Annotation[] annotations) { - List 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) { diff --git a/src/main/java/org/reflections/adapters/JavassistAdapter.java b/src/main/java/org/reflections/adapters/JavassistAdapter.java index 7a3c286b..dc5cd93c 100644 --- a/src/main/java/org/reflections/adapters/JavassistAdapter.java +++ b/src/main/java/org/reflections/adapters/JavassistAdapter.java @@ -18,7 +18,11 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static javassist.bytecode.AccessFlag.isPrivate; import static javassist.bytecode.AccessFlag.isProtected; @@ -33,12 +37,10 @@ public class JavassistAdapter implements MetadataAdapter getFields(final ClassFile cls) { - //noinspection unchecked return cls.getFields(); } public List getMethods(final ClassFile cls) { - //noinspection unchecked return cls.getMethods(); } @@ -153,29 +155,19 @@ public boolean acceptsInput(String file) { // private List getAnnotationNames(final AnnotationsAttribute... annotationsAttributes) { - List result = new ArrayList<>(); - if (annotationsAttributes != null) { - for (AnnotationsAttribute annotationsAttribute : annotationsAttributes) { - if (annotationsAttribute != null) { - for (Annotation annotation : annotationsAttribute.getAnnotations()) { - result.add(annotation.getTypeName()); - } - } - } + return Arrays.stream(annotationsAttributes) + .filter(Objects::nonNull) + .flatMap(annotationsAttribute -> Arrays.stream(annotationsAttribute.getAnnotations())) + .map(Annotation::getTypeName) + .collect(Collectors.toList()); + } else { + return Collections.emptyList(); } - - return result; } private List getAnnotationNames(final Annotation[] annotations) { - List result = new ArrayList<>(); - - for (Annotation annotation : annotations) { - result.add(annotation.getTypeName()); - } - - return result; + return Arrays.stream(annotations).map(Annotation::getTypeName).collect(Collectors.toList()); } private List splitDescriptorToTypeNames(final String descriptors) { @@ -190,10 +182,9 @@ private List splitDescriptorToTypeNames(final String descriptors) { } indices.add(descriptors.length()); - for (int i = 0; i < indices.size() - 1; i++) { - String s1 = Descriptor.toString(descriptors.substring(indices.get(i), indices.get(i + 1))); - result.add(s1); - } + result = IntStream.range(0, indices.size() - 1) + .mapToObj(i -> Descriptor.toString(descriptors.substring(indices.get(i), indices.get(i + 1)))) + .collect(Collectors.toList()); } diff --git a/src/main/java/org/reflections/util/Utils.java b/src/main/java/org/reflections/util/Utils.java index 3dbab487..b9402635 100644 --- a/src/main/java/org/reflections/util/Utils.java +++ b/src/main/java/org/reflections/util/Utils.java @@ -12,7 +12,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Member; import java.lang.reflect.Method; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -20,8 +19,7 @@ import java.util.Set; import java.util.function.Predicate; import java.util.stream.Collectors; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; +import java.util.stream.IntStream; import static org.reflections.ReflectionUtils.forName; @@ -31,13 +29,7 @@ public abstract class Utils { public static String repeat(String string, int times) { - StringBuilder sb = new StringBuilder(); - - for (int i = 0; i < times; i++) { - sb.append(string); - } - - return sb.toString(); + return IntStream.range(0, times).mapToObj(i -> string).collect(Collectors.joining()); } /** @@ -47,10 +39,6 @@ public static boolean isEmpty(String s) { return s == null || s.length() == 0; } - public static boolean isEmpty(Object[] objects) { - return objects == null || objects.length == 0; - } - public static File prepareFile(String filename) { File file = new File(filename); File parent = file.getAbsoluteFile().getParentFile(); @@ -73,11 +61,7 @@ public static Member getMemberFromDescriptor(String descriptor, ClassLoader... c Class[] parameterTypes = null; if (!isEmpty(methodParameters)) { String[] parameterNames = methodParameters.split(","); - List> result = new ArrayList<>(parameterNames.length); - for (String name : parameterNames) { - result.add(forName(name.trim(), classLoaders)); - } - parameterTypes = result.toArray(new Class[result.size()]); + parameterTypes = Arrays.stream(parameterNames).map(name -> forName(name.trim(), classLoaders)).toArray(Class[]::new); } Class aClass = forName(className, classLoaders); @@ -182,10 +166,8 @@ public static String name(Class type) { } - public static List names(Iterable> types) { - List result = new ArrayList<>(); - for (Class type : types) result.add(name(type)); - return result; + public static List names(Collection> types) { + return types.stream().map(Utils::name).collect(Collectors.toList()); } public static List names(Class... types) { @@ -210,23 +192,18 @@ public static Predicate and(Predicate... predicates) { return Arrays.stream(predicates).reduce(t -> true, Predicate::and); } - public static Stream stream(Iterable elements) { - return StreamSupport.stream(elements.spliterator(), false); - } - public static String join(Collection elements, String delimiter) { return elements.stream().map(Object::toString).collect(Collectors.joining(delimiter)); } - public static Set concat(Set forNames, Set forNames1) { - forNames.addAll(forNames1); - return forNames; - } - public static Set filter(Collection result, Predicate... predicates) { return result.stream().filter(and(predicates)).collect(Collectors.toSet()); } + public static Set filter(Collection result, Predicate predicate) { + return result.stream().filter(predicate).collect(Collectors.toSet()); + } + public static Set filter(T[] result, Predicate... predicates) { return Arrays.stream(result).filter(and(predicates)).collect(Collectors.toSet()); } diff --git a/src/main/java/org/reflections/vfs/JarInputDir.java b/src/main/java/org/reflections/vfs/JarInputDir.java index aebf9184..01917287 100644 --- a/src/main/java/org/reflections/vfs/JarInputDir.java +++ b/src/main/java/org/reflections/vfs/JarInputDir.java @@ -27,7 +27,7 @@ public String getPath() { } public Iterable getFiles() { - return (Iterable) () -> new Iterator() { + return () -> new Iterator() { { try { jarInputStream = new JarInputStream(url.openConnection().getInputStream()); } diff --git a/src/main/java/org/reflections/vfs/Vfs.java b/src/main/java/org/reflections/vfs/Vfs.java index 4c49edf6..ad4d93b3 100644 --- a/src/main/java/org/reflections/vfs/Vfs.java +++ b/src/main/java/org/reflections/vfs/Vfs.java @@ -161,7 +161,7 @@ public static java.io.File getFile(URL url) { try { path = url.toURI().getSchemeSpecificPart(); if ((file = new java.io.File(path)).exists()) return file; - } catch (URISyntaxException e) { + } catch (URISyntaxException ignored) { } try { @@ -169,7 +169,7 @@ public static java.io.File getFile(URL url) { if (path.contains(".jar!")) path = path.substring(0, path.lastIndexOf(".jar!") + ".jar".length()); if ((file = new java.io.File(path)).exists()) return file; - } catch (UnsupportedEncodingException e) { + } catch (UnsupportedEncodingException ignored) { } try { @@ -184,7 +184,7 @@ public static java.io.File getFile(URL url) { path = path.replace("%20", " "); if ((file = new java.io.File(path)).exists()) return file; - } catch (Exception e) { + } catch (Exception ignored) { } return null;