Skip to content
Luke Hutchison edited this page Jun 6, 2020 · 1 revision

See also the ClassGraph API overview.

Two reflection-based wrapper classes are provided for accessing modules, ModuleRef which proxies calls to ModuleReference, and ModuleReaderProxy which proxies calls to ModuleReader, for backwards compatibility when compiling code for JDK 7 and JDK 8. These proxy classes provide some additional convenience methods over the JDK versions they wrap.

Contents

Obtaining module references

You can get a list of visible modules in several ways:

(1) Get a list of all visible modules

List<ModuleRef> modules =
    new ClassGraph()
        .enableSystemPackages() // Optional, to return system modules
        .getModules();

(2) Get a list of all modules that were scanned

List<ModuleRef> modules = scanResult.getModules()

(3) Get a ModuleRef from a ModuleInfo object

ModuleInfo myModuleInfo = scanResult.getModuleInfo("my.module");
if (myModuleInfo != null) {
    ModuleRef moduleRef = myModuleInfo.getModuleRef();
    // ...
}

ModuleRef

Holds a reference to a module. This is a JDK 7/8 compatible, introspection-based wrapper for JPMS' ModuleReference class.

  • .getName() returns the name of the module as a String.
  • .getRawVersion() returns the raw version of the module as a String, or null if there was no version information available.
  • .getLocation() returns the location of the module as a URI.
  • .getLocationStr() returns the location of the module as a URI string.
  • .getPackages() returns the packages in the module as a List<String>. (Does not include non-package directories.)
  • .isSystemModule() returns true if this is a system module (i.e. java.*, jdk.*, javax.*, oracle.*, etc.)
  • .open() returns a ModuleReaderProxy that can be used to read from the module. Make sure you call .close() on the ModuleReaderProxy when you have finished.
  • .getReference() gets the wrapped JPMS ModuleReference.
  • .getLayer() gets the ModuleLayer of the module.
  • .getDescriptor() gets the ModuleDescriptor for the module.

ModuleReaderProxy

Holds a reference to a module reader. This is an introspection-based wrapper for JPMS' ModuleReader class. This wrapper gives backwards compatibility with JDK 7 and JDK 8.

  • .list() returns the resource paths in the module as a List<String>.
  • .open(String path) opens the resource with the given path as an InputStream. Make sure you call close() on this InputStream when you have finished.
  • .read(String path) opens the resource with the given path as a ByteBuffer. Make sure you call .release(byteBuffer)when you have finished.
  • .release(ByteBuffer byteBuffer) releases a ByteBuffer after it was allocated by calling .read(String path).
  • .close() closes the ModuleReader. Make sure you call this method when you have finished with the ModuleReaderProxy.