Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Porting FastClasspathScanner code to ClassGraph

Luke Hutchison edited this page Jun 12, 2020 · 12 revisions

FastClasspathScanner was renamed to ClassGraph for the version 4 release. There are several reasons behind the name change:

  1. Most importantly, the Java runtime environment is more or less deprecating the legacy classpath in favor of a fully modular future. FastClasspathScanner already has complete support for module scanning -- but "FastClasspathAndModulePathScanner" would not have been a good name.
  2. FastClasspathScanner has outgrown what it was created to do, which was to scan the classpath for annotations. It is now able to reconstruct the entire class graph, along with information about methods and fields, and complex type signature information. The name "ClassGraph" better reflects this. The new v4 API is pretty uniform and complete at this stage: https://github.com/classgraph/classgraph/wiki/ClassGraph-API
  3. "FastClasspathScanner" was never a good name to begin with :-) It was too long to comfortably type or say.

The version 4 release is the result of a huge amount of refactoring, code cleanup and API simplification. The resulting API is now much more flexible, powerful and uniform than the Version 3 API. See the docs for the Version 4 API for an overview. Scanning speed has improved through the use of memory-mapped file buffers where possible, and through some optimization work.

If you want to stay on the old FastClasspathScanner v3 API, you should depend specifically on fast-classpath-scanner version 3.1.13.

Important: All significant development will be continuing in ClassGraph moving forward, and numerous bugs have been fixed between FastClasspathScanner version 3.1.13 and ClassGraph version 4. Therefore, it is highly recommended that you port your code from the FastClasspathScanner version 3 API to the ClassGraph version 4 API.


Porting from the version 3 to the version 4 API:

  • The Maven group id has changed from io.github.lukehutch.fast-classpath-scanner to io.github.classgraph, and the artifact id has changed from fast-classpath-scanner to classgraph. The Maven dependency rule is now:
    <dependency>
        <groupId>io.github.classgraph</groupId>
        <artifactId>classgraph</artifactId>
        <version>LATEST</version>
    </dependency>
  • The module name has changed to io.github.classgraph -- you will need to update your requires statement in the module-info.java of a modular project to:
    requires io.github.classgraph;
    
  • Package names have changed. "Organize Imports" in your IDE can fix some of these issues.
  • The name of the constructor of the main entry class must be manually changed though -- the constructor invocation has changed from new FastClasspathScanner() to new ClassGraph().
  • The constructor no longer takes a "scan spec" with accepted and rejected packages (in fact, the ClassGraph constructor doesn't accept any parameters at all now).
  • Accepted and rejected packages are now given using .acceptPackages() / .rejectPackages(), as opposed to in arguments to the constructor.
  • Classfile scanning is now disabled by default. It is enabled by calling .acceptPackages(), or .enableClassInfo(), or .enableAllInfo() (see the wiki link above). If you don't do this, you can't query the ScanResult for ClassInfo objects.
  • Only public classes are now scanned by default. Call .ignoreClassVisibility() or .enableAllInfo() to scan non-public classes.
  • Annotations are no longer scanned by default. Call .enableAnnotationInfo() or .enableAllInfo() to scan annotations.
  • You can now call .disableNestedJarScanning() if you don't care about scanning lib jars within jars.
  • MatchProcessors are no longer supported, and neither is looking up classes based on Class<?> references. They caused too many problems, due to the interactions between classloading and classpath scanning. You will need to convert your MatchProcessors to use the ClassInfo API (see the wiki).
  • ScanResult should be assigned in a try-with-resources block so that resources are freed after you have finished with the results of the scan (see code examples in the documentation).
  • ScanResult#getClassNameToClassInfo() is now replaced with a richer API for fetching ClassInfo objects from the ScanResult.
    • e.g. scanResult.getClassNameToClassInfo().get(className) becomes scanResult.getClassInfo(className)
  • All getNamesOf...() classes have been removed in favor of methods that return ClassInfoList lists of ClassInfo objects. However, you can call ClassInfoList#getNames() to get a list of the class names. e.g.:
    • scanResult.getNamesOfAllClasses() becomes scanResult.getAllClasses().getNames()
    • scanResult.getNamesOfSubclassesOf(className) becomes scanResult.getSubclasses(className).getNames()
    • scanResult.getClassNameToClassInfo().keySet() becomes scanResult.getAllClasses().getNames()
  • You can now accept individual directories or packages to be scanned non-recursively (i.e. the directory or package is scanned, but not its sub-directories or sub-packages), e.g. new ClassGraph(pkg).disableRecursiveScanning() becomes new ClassGraph().acceptPackagesNonRecursive(pkg)
  • Classpath-fetching methods have been renamed: .getUniqueClasspathElementURLs() becomes .getClasspathURLs(), etc.
  • Annotation APIs now handle meta-annotations (so methods with "Meta" in them have been removed). For example, scanResult.getNamesOfAnnotationsWithMetaAnnotation(metaAnnotationName) becomes scanResult.getAllAnnotations().filter(ci -> ci.hasAnnotation(metaAnnotationName)).getNames() . To List the names of all meta-annotations, usescanResult.getAllAnnotations().filter(ci -> !ci.getAnnotations().isEmpty()).getNames(). If you want all direct annotations on a class (without including meta-annotations), you can call scanResult.getAllAnnotations().getDirect().getNames().
  • To enable the scanning of system jars, packages and modules, new FastClasspathScanner("!!") becomes new FastClasspathScanner().enableSystemPackages().
  • Some API methods have been simplified and unified, for example ClassInfo#getClassName() becomes ClassInfo#getName(); MethodInfo#getMethodName() becomes MethodInfo#getName(); FieldInfo#getFieldName() becomes FieldInfo#getFieldName(); etc.
  • Classloading methods have been renamed to indicate they trigger classloading, e.g. ClassInfo#getClassRef() becomes ClassInfo#loadClass().
  • collections of ClassInfo results are now returned in a ClassInfoList, MethodInfo in a MethodInfoList, FieldInfo in a FieldInfoList, etc.
  • You can now generate a GraphViz graph from any ClassInfoList, to visualize the result of any filter criteria.
    • e.g. scanResult.generateClassGraphDotFile() becomes scanResult.getAllClasses().generateClassGraphDotFile(File file)
  • FileMatchProcessors have been replaced with a new Resource and ResourceInfo system (see the API docs).

To view the documentation for the older Version 3 API:

git clone https://github.com/classgraph/classgraph.wiki.git
cd classgraph.wiki
git checkout v3
Clone this wiki locally