120 lines
4.3 KiB
Markdown
120 lines
4.3 KiB
Markdown
# Scanpath v2
|
|
|
|
> Class-file-based discovery of Java types on the classpath.
|
|
|
|
Scanpath scans directories and JAR or ZIP archives, indexes type metadata, and lets applications
|
|
find implementations, annotated types, and types whose fields or methods carry a particular
|
|
annotation. It inspects class files without loading every discovered type into the JVM.
|
|
|
|
Scanpath v2 uses the Java Class-File API and has no external runtime dependencies. It is the
|
|
successor to the Javassist-backed [Scanpath v1](https://rprime.indroneel.co/libzero/scanpath-v1).
|
|
|
|
## Highlights
|
|
|
|
- Scans the application classpath and additional classpath entries
|
|
- Supports directories, JAR files, and ZIP files
|
|
- Filters types by package, fully qualified name, or Java regular expression
|
|
- Finds direct and indirect implementations or subclasses
|
|
- Finds annotated classes, annotated fields, and annotated methods
|
|
- Defers class loading until query results are returned
|
|
- Supports a custom `ClassLoader` for resolving result types
|
|
|
|
## Requirements
|
|
|
|
- Java 25 or newer
|
|
- Maven 3.8 or newer when building from source
|
|
- No external runtime dependencies
|
|
|
|
## Install
|
|
|
|
Build and install the snapshot into your local Maven repository:
|
|
|
|
```bash
|
|
mvn install
|
|
```
|
|
|
|
The build also produces source and Javadoc artifacts. The project coordinates are:
|
|
|
|
```xml
|
|
<dependency>
|
|
<groupId>dux.scanpath</groupId>
|
|
<artifactId>dux.scanpath</artifactId>
|
|
<version>v2-SNAPSHOT</version>
|
|
</dependency>
|
|
```
|
|
|
|
## Quick start
|
|
|
|
Configure a scope, load it once, and query the resulting index:
|
|
|
|
```java
|
|
import dxt.scanpath.v2.ClasspathScanner;
|
|
import dxt.scanpath.v2.ScanScope;
|
|
|
|
import java.util.List;
|
|
|
|
ScanScope scope = new ScanScope()
|
|
.includePackage("com.example.plugins")
|
|
.excludePackage("com.example.plugins.internal")
|
|
.addClassPath("build/classes/java/main")
|
|
.includeClassesByPattern("com\\.example\\.plugins\\..*Plugin");
|
|
|
|
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);
|
|
```
|
|
|
|
`ScanScope` includes the application classpath by default. Add explicit classpath entries when
|
|
scanning plugin directories, external extensions, or other locations outside the running
|
|
application's regular classpath.
|
|
|
|
## Configuring a scan
|
|
|
|
`ScanScope` uses a fluent API. A scope must define at least one inclusion rule; exclusions refine
|
|
the included set.
|
|
|
|
| Method | Purpose |
|
|
| --- | --- |
|
|
| `addClassPath(path)` | Add a class directory, JAR, or ZIP archive. |
|
|
| `excludeApplicationClassPath()` | 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. |
|
|
| `duplicate()` | Create an independent copy of the scope. |
|
|
|
|
Exclusion rules take precedence over inclusion rules. Regular expressions are matched against
|
|
fully qualified type names.
|
|
|
|
## Querying results
|
|
|
|
After `load(scope)`, `ClasspathScanner` provides these queries:
|
|
|
|
- `listInheritingClasses(type)` - types that directly or indirectly extend or implement `type`
|
|
- `listAnnotatedClasses(annotation)` - types annotated with `annotation`, including inheritors
|
|
- `listClassesWithAnnotatedFields(annotation)` - types with an annotated field, including inheritors
|
|
- `listClassesWithAnnotatedMethods(annotation)` - types with an annotated method, including
|
|
inheritors
|
|
|
|
Call `setClassLoader(classLoader)` after loading and before querying when result types must be
|
|
resolved with a custom loader. `dumpState(indent)` is available for inspecting the indexed
|
|
inheritance and annotation metadata.
|
|
|
|
## License
|
|
|
|
Scanpath is distributed under the
|
|
[DuxTechnica Limited Use Source Code License Agreement](LICENSE). This is not an open source
|
|
license. Review the license before redistributing the library or incorporating it into another
|
|
project. |