← 输入框|github.com|刷新
Skip to content
Luke Hutchison edited this page Oct 11, 2026 · 12 revisions

See also the ClassGraph API overview.

Contents

ModuleInfo

Holds information about a named module encountered during scanning. A directory or jar on the traditional classpath is in the unnamed module at runtime, but it is still listed here, under the name it declares through module-info.class or the Automatic-Module-Name manifest attribute. A jar that declares neither is listed under the automatic module name that the module system would derive from its filename, if that is a legal module name; a directory that declares neither is not listed. Obtain these objects by calling ScanResult#getModuleInfo(moduleName) or ScanResult#getModuleInfo().

There is one ModuleInfo for each classpath element that holds a module, so two classpath elements with the same module name (for example foo-1.0.jar and foo-2.0.jar, which both derive the name foo) each have their own ModuleInfo, with their own location. ScanResult#getModuleInfo() then lists the name twice, and ScanResult#getModuleInfo(moduleName) returns the first one in scan order, as the module system would resolve it. Modules on the module path come before the traditional classpath in the scan order.

🛑 A module resolved in a module layer is only scanned if a module source was enabled before the scan: ClassGraph#enableNonSystemModules() or #enableSystemModules(). A directory or jar on the traditional classpath can also produce a ModuleInfo when classpath scanning is enabled. Nothing is scanned by default. See Saying what to scan.

  • Properties: (Needs .enableClassInfo() before .scan(). Call .ignoreClassVisibility() as well to get the modules of non-public classes.)
    • .getName() returns the non-empty name of the module as a String. ClassGraph does not create a ModuleInfo for the unnamed module.
    • .getLocationURI() returns the location of the module as a URI, or null for modules whose location is unknown.
    • .getModuleReference() returns the java.lang.module.ModuleReference for the module, or null if the name came from a directory or jar on the traditional classpath -- whether it declared the name or the name was derived from the jar's filename -- rather than from a module resolved in a ModuleLayer.
  • Classes:
    • .getClassInfo() returns a ClassInfoList of ClassInfo objects for all classes found in this module. Rejected classes are not listed, and neither are external classes unless ClassGraph#enableExternalClasses() was called before the scan.
    • .getClassInfo(String className) returns the ClassInfo object for the named class in this module, or null if the class was not found in this module.
  • Packages:
    • .getPackageInfo() returns a PackageInfoList of PackageInfo objects for all packages that are members of this module.
    • .getPackageInfo(String packageName) returns the PackageInfo object for the package of the given name in this module, or null if the package was not found in this module. The PackageInfo object is shared by the whole scan: it is the object that ScanResult#getPackageInfo(packageName) returns. So if the package is split across modules (which is allowed on the traditional classpath), its .getClassInfo() lists the package's classes in every module. To list only this module's classes in the package, call moduleInfo.getClassInfo().filter(ci -> ci.getPackageName().equals(packageName)).
  • Annotations: (module annotations are the annotations on the module-info.java module descriptor)
    • .getAllAnnotationInfo() / .getDirectAnnotationInfo() returns the annotations on this module as an AnnotationInfoList. The All form also includes meta-annotations -- the annotations on the module's own annotations, transitively.
    • .hasAnnotation(String annotationName | Class<? extends Annotation> annotationClass) returns true if the module has the given annotation.
    • .getAllAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfo(...) returns the AnnotationInfo object for the given non-@Repeatable module annotation, or null if none.
    • .getAllAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfoRepeatable(...) returns the AnnotationInfo objects for the given @Repeatable module annotation, as an AnnotationInfoList, or the empty list if none.

ModuleInfoList

An unmodifiable List<ModuleInfo>, whether returned by ClassGraph or constructed from a completed collection, with the following convenience methods:

  • .asMap() returns the ModuleInfoList as a Map<String, ModuleInfo> mapping the module name to the corresponding ModuleInfo object, sorted by module name. If the list holds more than one module with the same name, the name maps to the one that .get(String) returns.
  • .getNames() returns a list of the names of the modules in this list as a List<String>.
  • .getAsStrings() returns a list of the result of calling .toString() on each ModuleInfo object in this list, producing a List<String> of String representations of each module, of the form module <name>.
  • .containsName(String moduleName) returns true if a module of the given name is contained in this list.
  • .get(String moduleName) returns the first ModuleInfo object in this list with the requested name, if present, otherwise returns null. For the list returned by ScanResult#getModuleInfo(), this is the same object that ScanResult#getModuleInfo(moduleName) returns.
  • .filter(Predicate<ModuleInfo> filter) returns a ModuleInfoList that is a subset of the original list, obtained by applying the given predicate to each ModuleInfo object in the list.

See also

The module path itself -- the --module-path, --add-modules, --patch-module, --add-exports, --add-opens and --add-reads arguments the JVM was launched with -- is described by ModulePathInfo, which lives in the classpath-finding library. See ModulePathInfo.

Clone this wiki locally