4.1 KiB
Scanpath v1
Bytecode-based discovery of Java types on the classpath.
Scanpath scans directories, JARs, and ZIP archives and builds an index of type metadata without loading every discovered class into the JVM. Use it to find implementations, annotated types, and types whose fields or methods carry a particular annotation.
Scanpath v1 is the Javassist-backed implementation. For a newer implementation with no external runtime dependency, see Scanpath v2.
Highlights
- Scans the system classpath by default
- Supports directories, JAR files, ZIP files, and folders of libraries
- Filters scans by package, fully qualified class name, or Java regular expression
- Finds direct and indirect implementations or subclasses
- Finds annotated classes, annotated fields, and annotated methods
- Defers class loading until results are returned
- Supports a custom
ClassLoaderfor resolving result types
Requirements
- Java 17 or newer
- Maven 3.8 or newer when building from source
- Javassist 3.30.2-GA (declared by Maven)
Install
Build and install the snapshot into your local Maven repository:
mvn install
The build also produces source and Javadoc artifacts. The project currently uses the coordinates
dux.scanpath:dux.scanpath:v1-SNAPSHOT.
Quick start
Configure a scope, load it once, and query the resulting index:
import dux.scanpath.v1.ClasspathScanner;
import dux.scanpath.v1.ScanScope;
import java.util.List;
ScanScope scope = new ScanScope()
.includePackage("com.example.plugins")
.excludePackage("com.example.plugins.internal")
.addClassPath("build/classes/java/main")
.addLibPath("lib");
ClasspathScanner scanner = new ClasspathScanner();
scanner.load(scope);
List<Class<?>> implementations =
scanner.listInheritingClasses(MyPlugin.class);
List<Class<?>> annotatedTypes =
scanner.listAnnotatedClasses(Discoverable.class);
List<Class<?>> annotatedFields =
scanner.listClassesWithAnnotatedFields(Inject.class);
List<Class<?>> annotatedMethods =
scanner.listClassesWithAnnotatedMethods(Handler.class);
addClassPath accepts a class directory or an individual .jar or .zip file. addLibPath
accepts a directory; every JAR and ZIP directly inside that directory is scanned.
Configuring a scan
ScanScope uses a fluent API:
| Method | Purpose |
|---|---|
addClassPath(path) |
Add a class directory, JAR, or ZIP archive. |
addLibPath(path) |
Scan JAR and ZIP files in a library directory. |
excludeSystemClassPath() |
Do not include entries from java.class.path. |
includePackage(name) |
Include a package and all of its subpackages. |
excludePackage(name) |
Exclude a package and all of its subpackages. |
includeClass(name) |
Include one fully qualified type name. |
excludeClass(name) |
Exclude one fully qualified type name. |
includeClassesByPattern(regex) |
Include names matching a Java regular expression. |
excludeClassesByPattern(regex) |
Exclude names matching a Java regular expression. |
The system classpath is included automatically. Add explicit classpaths or library paths when scanning plugin directories, external extensions, or other entries that are not part of the running application's regular classpath.
Querying results
After load(scope), ClasspathScanner provides these queries:
listInheritingClasses(type)- types that directly or indirectly extend or implementtypelistAnnotatedClasses(annotation)- types annotated withannotation, including inheritorslistClassesWithAnnotatedFields(annotation)- types with an annotated field, including inheritorslistClassesWithAnnotatedMethods(annotation)- types with an annotated method, including inheritors
To resolve returned classes with a custom loader, call setClassLoader(classLoader) before loading
the scope.
License
Scanpath is distributed under the DuxTechnica Limited Use Source Code License Agreement. This is not an open source license. Review the license before redistributing the library or incorporating it into another project.