diff --git a/README.md b/README.md index 12d56423..56470aa0 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,3 @@ -This fork is changed to use java8 instead of guava. - -```xml - - net.oneandone.reflections8 - reflections8 - 0.11.5 - -``` - -To avoid mixup of classes between "pre java8"-reflections and reflections8 all is moved from org.reflections to org.reflections8 -[![Build Status](https://travis-ci.org/aschoerk/reflections8.svg?branch=master)](https://travis-ci.org/aschoerk/reflections8) - - - ----- - ## Java runtime metadata analysis, in the spirit of [Scannotations](http://bill.burkecentral.com/2008/01/14/scanning-java-annotations-at-runtime/) Reflections scans your classpath, indexes the metadata, allows you to query it on runtime and may save and collect that information for many modules within your project. @@ -28,14 +11,6 @@ Using Reflections you can query your metadata such as: [![Build Status](https://travis-ci.org/ronmamo/reflections.svg?branch=master)](https://travis-ci.org/ronmamo/reflections) ### Intro -Add Reflections to your project. for maven projects just add this dependency: -```xml - - net.oneandone.reflections8 - reflections8 - 0.11.5 - -``` A typical use of Reflections would be: ```java diff --git a/pom.xml b/pom.xml index cc957b90..ee92bf38 100644 --- a/pom.xml +++ b/pom.xml @@ -1,13 +1,12 @@ 4.0.0 - net.oneandone.reflections8 - reflections8 - 0.11.6 + org.reflections + reflections17 + 0.11.6.jdk17.1 - Reflections8 - Reflections8 - a Java runtime metadata analysis without guava on Java 8 - http://github.com/aschoerk/reflections8 + Reflections17 + Reflections17 - a Java runtime metadata analysis component diff --git a/src/main/java/org/reflections8/ReflectionUtils.java b/src/main/java/org/reflections8/ReflectionUtils.java index ffcce3e7..54f80421 100644 --- a/src/main/java/org/reflections8/ReflectionUtils.java +++ b/src/main/java/org/reflections8/ReflectionUtils.java @@ -24,6 +24,7 @@ import org.reflections8.util.AlwaysTruePredicate; import org.reflections8.util.ClasspathHelper; +import org.slf4j.Logger; /** convenient java reflection helper methods *

@@ -38,7 +39,7 @@ *

  • {@link #getAllMethods(Class, Predicate...)} *
  • {@link #getAllConstructors(Class,Predicate...)} * - *

    and predicates included here all starts with "with", such as + *

    and predicates included here all starts with "with", such as *

      *
    • {@link #withAnnotation(java.lang.annotation.Annotation)} *
    • {@link #withModifier(int)} @@ -51,28 +52,47 @@ *
    • {@link #withReturnType(Class)} *
    • {@link #withType(Class)} *
    • {@link #withTypeAssignableTo} - *
    + * * *


    * for example, getting all getters would be: *

      *      Set<Method> getters = getAllMethods(someClasses,
      *              Predicates.and(
    - *                      withModifier(Modifier.PUBLIC), 
    - *                      withPrefix("get"), 
    + *                      withModifier(Modifier.PUBLIC),
    + *                      withPrefix("get"),
      *                      withParametersCount(0)));
      *     
    * */ @SuppressWarnings("unchecked") public abstract class ReflectionUtils { + private static final String VERBOSE_PROPERTY = "reflections17.verbose"; + + private static final boolean REFLECTIONS_VERBOSE = Boolean.getBoolean(VERBOSE_PROPERTY) + || "true".equals(System.getenv(VERBOSE_PROPERTY)); + + private static final String SEMICOLON = ";"; + + private static final String L = "L"; + + private static final String EMPTY_STRING = ""; + + private static final String CLOSE_SQUARE_BRACKET = "]"; + + private static final String OPEN_SQUARE_BRACKET = "["; + + private static final String TYPE_UNAVAILABLE_SHORT = "Type: %s unavailable"; + + private static final String TYPE_UNAVAILABLE = "Type: %s unavailable from any class loader"; + /** would include {@code Object.class} when {@link #getAllSuperTypes(Class, Predicate[])}. default is false. */ public static final boolean includeObject = false; /** get all super types of given {@code type}, including, optionally filtered by {@code predicates} *

    include {@code Object.class} if {@link #includeObject} is true */ public static Set> getAllSuperTypes(final Class type, Predicate>... predicates) { - Set> result = new LinkedHashSet(); + Set> result = new LinkedHashSet>(); if (type != null && (includeObject || !type.equals(Object.class))) { result.add(type); for (Class supertype : getSuperTypes(type)) { @@ -94,7 +114,7 @@ public static Set> getSuperTypes(Class type) { /** get all methods of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */ public static Set getAllMethods(final Class type, Predicate... predicates) { - Set result = new HashSet(); + Set result = new HashSet(); for (Class t : getAllSuperTypes(type)) { result.addAll(getMethods(t, predicates)); } @@ -108,7 +128,7 @@ public static Set getMethods(Class t, Predicate... pr /** get all constructors of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */ public static Set getAllConstructors(final Class type, Predicate... predicates) { - Set result = new HashSet(); + Set result = new HashSet(); for (Class t : getAllSuperTypes(type)) { result.addAll(getConstructors(t, predicates)); } @@ -122,7 +142,7 @@ public static Set getConstructors(Class t, Predicate getAllFields(final Class type, Predicate... predicates) { - Set result = new HashSet(); + Set result = new HashSet(); for (Class t : getAllSuperTypes(type)) result.addAll(getFields(t, predicates)); return result; } @@ -134,7 +154,7 @@ public static Set getFields(Class type, Predicate... pr /** get all annotations of given {@code type}, up the super class hierarchy, optionally filtered by {@code predicates} */ public static Set getAllAnnotations(T type, Predicate... predicates) { - Set result = new HashSet(); + Set result = new HashSet(); if (type instanceof Class) { for (Class t : getAllSuperTypes((Class) type)) { result.addAll(getAnnotations(t, predicates)); @@ -370,56 +390,60 @@ public static Class forName(String typeName, ClassLoader ... classLoaders) { } else { return forName(typeName, Optional.of(classLoaders)); } - } - - // - /** tries to resolve a java type name to a Class - *

    if optional {@link ClassLoader}s are not specified, then both {@link org.reflections8.util.ClasspathHelper#contextClassLoader()} and {@link org.reflections8.util.ClasspathHelper#staticClassLoader()} are used - * */ + /** + * Tries to resolve a java type name to a Class + *

    if optional {@link ClassLoader}s are not specified, then both {@link org.reflections8.util.ClasspathHelper#contextClassLoader()} + * and {@link org.reflections8.util.ClasspathHelper#staticClassLoader()} are used + */ public static Class forName(String typeName, Optional classLoaders) { if (getPrimitiveNames().contains(typeName)) { return getPrimitiveTypes().get(getPrimitiveNames().indexOf(typeName)); } else { String type; - if (typeName.contains("[")) { - int i = typeName.indexOf("["); + if (typeName.contains(OPEN_SQUARE_BRACKET)) { + int i = typeName.indexOf(OPEN_SQUARE_BRACKET); type = typeName.substring(0, i); - String array = typeName.substring(i).replace("]", ""); - + String array = typeName.substring(i).replace(CLOSE_SQUARE_BRACKET, EMPTY_STRING); if (getPrimitiveNames().contains(type)) { type = getPrimitiveDescriptors().get(getPrimitiveNames().indexOf(type)); } else { - type = "L" + type + ";"; + type = L + type + SEMICOLON; } - type = array + type; } else { type = typeName; } - - List reflectionsExceptions = new ArrayList(); + List reflectionsExceptions = new ArrayList(); for (ClassLoader classLoader : ClasspathHelper.classLoaders(classLoaders).get()) { - if (type.contains("[")) { - try { return Class.forName(type, false, classLoader); } - catch (Throwable e) { - reflectionsExceptions.add(new ReflectionsException("could not get type for name " + typeName, e)); + if (type.contains(OPEN_SQUARE_BRACKET)) { + try { + return Class.forName(type, false, classLoader); + } catch (Throwable e) { + reflectionsExceptions.add(new ReflectionsException(String.format(TYPE_UNAVAILABLE_SHORT, typeName), e)); } } - try { return classLoader.loadClass(type); } - catch (Throwable e) { - reflectionsExceptions.add(new ReflectionsException("could not get type for name " + typeName, e)); + try { + return classLoader.loadClass(type); + } catch (Throwable e) { + reflectionsExceptions.add(new ReflectionsException(String.format(TYPE_UNAVAILABLE_SHORT, typeName), e)); } } - if (Reflections.log.isPresent()) { + Logger logger = Reflections.log.get(); for (ReflectionsException reflectionsException : reflectionsExceptions) { - Reflections.log.get().warn("could not get type for name " + typeName + " from any class loader", - reflectionsException); + boolean traceEnabled = logger.isTraceEnabled(); + if (REFLECTIONS_VERBOSE || traceEnabled) { + String message = String.format(TYPE_UNAVAILABLE, typeName); + if (traceEnabled) { + logger.trace(message, reflectionsException); + } else { + logger.warn(message, reflectionsException); + } + } } } - return null; } } @@ -451,7 +475,7 @@ private static Class[] parameterTypes(Member member) { } private static Set parameterAnnotations(Member member) { - Set result = new HashSet(); + Set result = new HashSet(); Annotation[][] annotations = member instanceof Method ? ((Method) member).getParameterAnnotations() : member instanceof Constructor ? ((Constructor) member).getParameterAnnotations() : null; @@ -462,7 +486,7 @@ private static Set parameterAnnotations(Member member) { } private static Set> annotationTypes(Iterable annotations) { - Set> result = new HashSet(); + Set> result = new HashSet>(); for (Annotation annotation : annotations) result.add(annotation.annotationType()); return result; } diff --git a/src/main/java/org/reflections8/Reflections.java b/src/main/java/org/reflections8/Reflections.java index d341f030..15a555d6 100644 --- a/src/main/java/org/reflections8/Reflections.java +++ b/src/main/java/org/reflections8/Reflections.java @@ -64,6 +64,7 @@ import org.reflections8.util.Utils; import org.reflections8.vfs.Vfs; import org.slf4j.Logger; +import org.slf4j.event.Level; /** * Reflections one-stop-shop object @@ -132,9 +133,18 @@ *

    For Javadoc, source code, and more information about Reflections Library, see http://github.com/ronmamo/reflections/ */ public class Reflections { + + private static final String VERBOSE_PROPERTY = "reflections17.verbose"; + + private static final boolean REFLECTIONS_VERBOSE = Boolean.getBoolean(VERBOSE_PROPERTY) + || "true".equals(System.getenv(VERBOSE_PROPERTY)); + + private static final String TIMING_INFO = "Reflections initialized in {} ms"; + public static final Optional log = findLogger(Reflections.class); protected final transient Configuration configuration; + protected Store store; /** @@ -142,22 +152,28 @@ public class Reflections { *

    it is preferred to use {@link org.reflections8.util.ConfigurationBuilder} */ public Reflections(final Configuration configuration) { + long start = System.currentTimeMillis(); this.configuration = configuration; store = new Store(configuration); - if (configuration.getScanners() != null && !configuration.getScanners().isEmpty()) { - //inject to scanners for (Scanner scanner : configuration.getScanners()) { scanner.setConfiguration(configuration); scanner.setStore(store.getOrCreate(index(scanner.getClass()))); } - scan(); - if (configuration.shouldExpandSuperTypes()) { expandSuperTypes(); } } + Logger logger = log.get(); + if (REFLECTIONS_VERBOSE || logger.isTraceEnabled()) { + long end = System.currentTimeMillis(); + if (REFLECTIONS_VERBOSE) { + logger.warn(TIMING_INFO, end - start); + } else { + logger.trace(TIMING_INFO, end - start); + } + } } /**