github classgraph/classgraph fast-classpath-scanner-2.0.18

latest releases: classgraph-4.8.172, classgraph-4.8.171, classgraph-4.8.170...
7 years ago

Feature release. Adds new capabilities for matching and querying methods and fields. It is now possible to introspect the methods and fields of classes on the classpath without ever loading the classes.

Obtaining information about the fields of a class:

ScanResult scanResult = new FastClasspathScanner(packageName)
    // If needed, to get non-public fields
    .ignoreFieldVisibility()
    // Must call this before .scan()
    .enableFieldInfo()
    .scan();
ClassInfo widgetClassInfo = scanResult.getClassNameToClassInfo().get("pkg.Widget");
List<FieldInfo> allWidgetFieldInfo = widgetClassInfo.getFieldInfo();
FieldInfo widgetSizeFieldInfo = widgetClassInfo.getFieldInfo("size");
// Then the individual FieldInfo methods can then be queried
// for annotations, visibility, type, etc.
String fieldTypeStr = widgetSizeFieldInfo.getTypeStr();
// Returns a string representation of all info about the field
String fieldStr = widgetSizeFieldInfo.toString();

Obtaining information about the methods of a class:

ScanResult scanResult = new FastClasspathScanner(packageName)
    // If needed, to get non-public methods
    .ignoreMethodVisibility()
    // Must call this before .scan()
    .enableMethodInfo()
    .scan();
ClassInfo widgetClassInfo = scanResult.getClassNameToClassInfo().get("pkg.Widget");
List<MethodInfo> allWidgetMethodInfo = widgetClassInfo.getMethodInfo();
MethodInfo widgetDrawMethodInfo = widgetClassInfo.getMethodInfo("draw");
// Then the individual MethodInfo methods can then be queried
// for annotations, visibility, return type, parameter types, etc.
String drawReturnTypeStr = widgetDrawMethodInfo.getReturnTypeStr();
List<String> drawParamTypeStrs = widgetDrawMethodInfo.getParameterTypeStrs();
// Returns a string representation of all info about the method
String methodStr = widgetDrawMethodInfo.toString();

New MatchProcessor type for matching fields with a given annotation:

@FunctionalInterface
interface FieldAnnotationMatchProcessor {
    public void processMatch(Class<?> matchingClass, Field matchingField);
}

FastClasspathScanner FastClasspathScanner#matchClassesWithFieldAnnotation(
    Class<? extends Annotation> annotation,
    FieldAnnotationMatchProcessor fieldAnnotationMatchProcessor)

This is in addition to the support that already existed in previous versions for matching methods with a given annotation.

Added methods

  • FastClasspathScanner#getUniqueClasspathElementsAsPathStr() to get the classpath as a delimited string, regardless of what ClassLoader the classpath elements originated from.
  • FastClasspathScanner#findBestClassLoader() to find the ClassLoader that is most likely used by the caller. (Implements these rules -- tl;dr: it's complicated.)

Don't miss a new classgraph release

NewReleases is sending notifications on new releases.