From 133bd4c78bd0ce289f8ba65b5805b50cef9ad1b1 Mon Sep 17 00:00:00 2001 From: Martin Hickson Date: Sun, 17 Mar 2024 07:47:26 +1300 Subject: [PATCH] Reduce scanning verbosity #1 --- pom.xml | 11 +-- .../org/reflections8/ReflectionUtils.java | 96 ++++++++++++------- 2 files changed, 65 insertions(+), 42 deletions(-) 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; }