Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Luke Hutchison edited this page Jun 12, 2020 · 171 revisions

See also the code examples page.

Contents

General usage pattern

  1. Create a new ClassGraph() instance, and configure it for scanning:
    • Optionally call .verbose() to enable verbose logging to stderr.
    • Enable classfile scanning, if you want to scan classes -- in the simplest case, call .enableAllInfo() to enable the scanning of classes, methods, fields, and annotations.
    • Call .acceptPackages(String... packages) to accept specific packages to scan, or .acceptPaths(String... paths) if you only want to scan resources and not classes. If you don't call either of these methods, then all packages or paths will be scanned.
  2. Start the scan, by calling .scan(), to produce a ScanResult object.

    💡 The ScanResult should be assigned in a try-with-resources block or equivalent. See ScanResult lifecycle.

  3. Query the ScanResult object: (the ScanResult object can be queried repeatedly without re-running the scan)
    • For class scan results: Call methods such as .getAllClasses(), .getAllInterfaces() etc. to get ClassInfoList lists of ClassInfo objects for classes of interest
      • Query the ClassInfo objects for class properties or class relationships of interest.
      • ClassInfoList lists can be filtered using .filter(predicateFunction) or combined using .union(ClassInfoList... others), .intersect(ClassInfoList... others), or .exclude(ClassInfoList other), to return a new ClassInfoList representing the predicate-filtered subset, union, intersection or set difference respectively.
    • And/or for resource scan results: Call methods such as .getAllResources(), .getResourcesWithPath(path), .getResourcesWithExtension(ext) etc. to get ResourceList lists of Resource (file) objects matching a given path or filename pattern.
      • Call methods ResourceList#forEachByteArray(ByteArrayConsumer) and similar functions to open the content of each Resource object as a ByteBuffer or InputStream, or read the complete contents into a byte[] array, then pass the buffer, stream or array to a consumer method, closing the buffer or stream when the consumer exits.

You can scan either at runtime (the normal usecase), or at build time (for faster startup speed, or to support Android, since it does not use the standard Java bytecode format).

See the code examples page for specific examples of how to use the ClassGraph API.

API Index

Documentation for legacy API (FastClasspathScanner)

ClassGraph version 4 is a major upgrade over the previous version, FastClasspathScanner version 3, and there were a number of major API changes between FastClasspathScanner and ClassGraph. See here for information on porting FastClasspathScanner v3 code to the ClassGraph v4 API, and for info on how to obtain the older FastClasspathScanner version 3 documentation.

Clone this wiki locally
Morty Proxy This is a proxified and sanitized view of the page, visit original site.