Skip to content

fast-classpath-scanner-2.0.18

Compare
Choose a tag to compare
@lukehutch lukehutch released this 24 Mar 05:53
· 4160 commits to latest since this release

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.)