-
-
Notifications
You must be signed in to change notification settings - Fork 292
Porting FastClasspathScanner code to ClassGraph
FastClasspathScanner was renamed to ClassGraph for the version 4 release. There are several reasons behind the name change:
- 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.
- 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
- "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
toio.github.classgraph
, and the artifact id has changed fromfast-classpath-scanner
toclassgraph
. 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 yourrequires
statement in themodule-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()
tonew 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 theScanResult
forClassInfo
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 theClassInfo
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 fetchingClassInfo
objects from theScanResult
.- e.g.
scanResult.getClassNameToClassInfo().get(className)
becomesscanResult.getClassInfo(className)
- e.g.
- All
getNamesOf...()
classes have been removed in favor of methods that returnClassInfoList
lists ofClassInfo
objects. However, you can callClassInfoList#getNames()
to get a list of the class names. e.g.:-
scanResult.getNamesOfAllClasses()
becomesscanResult.getAllClasses().getNames()
-
scanResult.getNamesOfSubclassesOf(className)
becomesscanResult.getSubclasses(className).getNames()
-
scanResult.getClassNameToClassInfo().keySet()
becomesscanResult.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()
becomesnew 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)
becomesscanResult.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 callscanResult.getAllAnnotations().getDirect().getNames()
. - To enable the scanning of system jars, packages and modules,
new FastClasspathScanner("!!")
becomesnew FastClasspathScanner().enableSystemPackages()
. - Some API methods have been simplified and unified, for example
ClassInfo#getClassName()
becomesClassInfo#getName()
;MethodInfo#getMethodName()
becomesMethodInfo#getName()
;FieldInfo#getFieldName()
becomesFieldInfo#getFieldName()
; etc. - Classloading methods have been renamed to indicate they trigger classloading, e.g.
ClassInfo#getClassRef()
becomesClassInfo#loadClass()
. - collections of
ClassInfo
results are now returned in aClassInfoList
,MethodInfo
in aMethodInfoList
,FieldInfo
in aFieldInfoList
, etc. - You can now generate a GraphViz graph from any
ClassInfoList
, to visualize the result of any filter criteria.- e.g.
scanResult.generateClassGraphDotFile()
becomesscanResult.getAllClasses().generateClassGraphDotFile(File file)
- e.g.
- FileMatchProcessors have been replaced with a new
Resource
andResourceInfo
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