Skip to content
Luke Hutchison edited this page Aug 11, 2026 · 21 revisions

See also the ClassGraph API overview.

Contents

💡 Methods whose name begins with getAll... return the whole transitive closure of a relationship (all subclasses, all superinterfaces, all annotations including meta-annotations, and so on). The corresponding getDirect... methods return only what is one step away -- what the class itself names in its extends or implements clause, or what is written on it directly.

ClassInfo

Holds information about a class. Obtained by calling ScanResult#getAllClasses() and related methods.

  • Properties:

    • .getName() / .getSimpleName() returns the name of the class as a String. (.getName() includes the package prefix, .getSimpleName() does not.) Note that .getSimpleName() is not quite the same as Class#getSimpleName(), which returns "" for anonymous classes -- ClassGraph returns everything after the last . or $ in the class name.
    • .getPackageName() returns the name of the class' package, as a String.
    • .getPackageInfo() returns the PackageInfo object for the package containing the class.
    • .getModuleInfo() returns the ModuleInfo object for the module containing the class, or null if the class is not part of a named module.
    • .getSourceFile() returns the value of the SourceFile attribute of a classfile.
    • .isExternalClass() returns true if this class was referenced by an accepted class -- as a superclass, interface or annotation -- but is not itself an accepted class. Only the class' name and its place in the class graph are known for an external class; its classfile was never read.
    • .getModifiers() returns the class modifier bits as an int, in the same form as Class#getModifiers(). The ACC_SUPER bit (0x0020) of the classfile's access_flags field is masked out, since it is not a modifier -- it selects the JVM's treatment of the invokespecial instruction, javac sets it on almost every class, and the same bit value in java.lang.reflect.Modifier is SYNCHRONIZED, which is not a legal class modifier. Class#getModifiers() masks it out for the same reason.

      ⚠️ Do not compare the returned value against a hardcoded integer. Use the named accessors below (.isPublic(), .isStatic(), etc.), or test individual bits with the java.lang.reflect.Modifier predicates, rather than comparing the whole value.

    • .getModifiersString() returns the class modifiers as a String (e.g. "public abstract").
    • .isPublic() returns true if the class is public.
    • .isProtected() returns true if the class is protected.
    • .isPrivate() returns true if the class is private.
    • .isPackageVisible() returns true if the class has default (package) visibility, i.e. is neither public, protected nor private.
    • .isAbstract() returns true if the class is abstract.
    • .isSynthetic() returns true if the class is synthetic.
    • .isStatic() returns true if the class is static.
    • .isFinal() returns true if the class is final.
    • .getTypeSignature() returns the type signature of the class (including any generic type parameters) as a ClassTypeSignature, if the class is generic, otherwise returns null.

      🛑 ClassGraph makes no attempt to resolve type variables in the types of superclasses or interfaces, or their fields or methods, by substituting type arguments for type parameters. If you need concrete types for a specific type context, you will need to do the type substitution yourself.

    • .getTypeSignatureString() returns the raw internal Java type signature string for the class (including any generic type parameters), if available, otherwise returns null.
    • .getTypeDescriptor() returns a synthesized "class descriptor" for the class, built from the class name, superclass name and directly implemented interface names, as a ClassTypeSignature. (Java does not store a class descriptor in classfiles.) This is how you read type annotations that were written on the superclass or interfaces of a non-generic class, since such a class has no type signature to hang them from.
    • .getTypeSignatureOrTypeDescriptor() returns .getTypeSignature() if the class is generic, otherwise falls back to .getTypeDescriptor(). This is usually the method you want, since it works for both generic and non-generic classes.
    • .getClassfileMinorVersion() and .getClassfileMajorVersion() get the classfile version for the class, or 0 if the classfile was not scanned, i.e. if this is a placeholder for a referenced class that was not found or not accepted during the scan.
    • .toStringWithSimpleNames() returns a simpler rendering of the class than ClassInfo#toString(), by using only the simple name of the class and any annotation classes.
  • Class type:

    • .isStandardClass() returns true if the class is not an annotation or interface.
    • .isAnnotation() returns true if the class is an annotation.
    • .isInterface() returns true if the class is an interface that is not an annotation (annotations are interfaces, and can be implemented).
    • .isInterfaceOrAnnotation() returns true if the class is an interface or an annotation (annotations are interfaces, and can be implemented).
    • .isImplementedInterface() returns true if this class is an "implemented interface" (meaning a standard interface or an annotation that has been implemented by some class).
    • .isEnum() returns true if the class is an enum.
    • .isRecord() returns true if the class is a record type.
    • .isArrayClass() returns true if the ClassInfo object is an ArrayClassInfo, indicating that the class is an array class (e.g. Point[][].class).
    • .isInnerClass() returns true if the class is an inner class.
    • .isAnonymousInnerClass() returns true if the class is an anonymous inner class.
    • .isOuterClass() returns true if the class contains one or more inner classes.
  • Standard classes:

    • .getAllSubclasses() returns all subclasses of the class -- the classes that extend it, and the classes that extend those, transitively -- as a ClassInfoList. If this class is Object, this returns every standard class in the scan result, but no interfaces, since interfaces don't extend Object.
    • .getDirectSubclasses() returns only the classes that name this class in their extends clause.
    • .getAllSuperclasses() returns all superclasses of the class, in ascending order in the class hierarchy, ending with Object if the whole superclass chain was scanned. Does not include superinterfaces if this is an interface -- use .getAllSuperinterfaces() for those.
    • .getSuperclass() returns the single direct superclass of the class, as a ClassInfo object. As with Class#getSuperclass(), the superclass of a class that extends no other class is Object, and null is returned only for Object itself and for interfaces.
    • .extendsSuperclass(String superclassName | Class<?> superclass) returns true if the class extends the given superclass (i.e. if the class is a subclass of the superclass).
  • Enums:

    • .getEnumConstants() returns the enum constants of an enum class as a FieldInfoList of FieldInfo objects (enum constants are stored as fields in Java classes). Requires .enableFieldInfo(), and throws IllegalStateException if this class is not an enum.
  • Interfaces:

    • .getAllSuperinterfaces() returns all interfaces implemented by this class or by one of its superclasses, if this is a standard class, or all interfaces extended by this interface, directly or indirectly, if this is an interface, as a ClassInfoList. Returns the empty list if none. The list is not sorted by name, since the order in which interfaces are declared is significant for inheritance.
    • .getDirectSuperinterfaces() returns only the interfaces named in this class' implements clause, or this interface's extends clause.
    • .implementsInterface(String interfaceName | Class<?> interfaceClass) returns true if the class implements the given interface.
    • .getAllClassesImplementing() returns all the classes (and their subclasses) that implement this interface, if this is an interface, as a ClassInfoList. Returns the empty list if none. The returned list also includes the transitive subinterfaces of this interface, since an interface that extends this one is a subtype of it -- call .getInterfaces() or .getStandardClasses() on the returned ClassInfoList to separate the two.
    • .getDirectClassesImplementing() returns only the classes that name this interface in their implements clause, and the interfaces that name it in their extends clause.
    • .getAllSubinterfaces() returns all the transitive subinterfaces of this interface, i.e. the interfaces that extend this interface, and the interfaces that extend those. Returns the empty list if none, or if this is not an interface. (This is the interface-hierarchy equivalent of .getAllSubclasses(), which only traverses the superclass hierarchy.)
    • .getDirectSubinterfaces() returns only the interfaces that name this interface in their extends clause.
  • Annotations: (N.B. call .enableAnnotationInfo() before .scan() to enable annotation scanning, and call .ignoreClassVisibility() if you want to scan non-public annotations.)

    💡 ClassGraph handles meta-annotations transitively. For example, in this class graph, the class A has annotation @F, B has annotations @F and @E, and C has annotation @G. The annotation classes F and E are both meta-annotated with @J, and E is also meta-annotated with @I, etc. This means that the list of all annotations on A is [J, F], and the list of all annotations on B is [D, K, H, L, J, I, E].

    • .getClassesWithAnnotation() if this class is an annotation, returns all classes that are annotated with this annotation, as a ClassInfoList.
    • .getAllAnnotations() returns all annotations and meta-annotations on this class, as a ClassInfoList. Also handles the @Inherited meta-annotation, which causes an annotation on a class to annotate all of its subclasses. Meta-annotations in the java.lang.annotation package are filtered out. (These ClassInfo objects do not carry the annotation parameter values -- call .getAllAnnotationInfo() for those.)
    • .getDirectAnnotations() returns only the annotations written on this class itself.
    • .hasAnnotation(String annotationName | Class<? extends Annotation> annotation) returns true if this class has the given annotation, directly or as a meta-annotation.
    • .getAllAnnotationInfo() / .getDirectAnnotationInfo() returns the annotations on this class, as an AnnotationInfoList of AnnotationInfo objects, which carry the parameter values of each annotation.
    • .getAllAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfo(...) returns the AnnotationInfo object for the given non-@Repeatable class annotation, or null if none.
    • .getAllAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfoRepeatable(...) returns the AnnotationInfo objects for the given @Repeatable class annotation, as an AnnotationInfoList, or the empty list if none.
    • .getAnnotationDefaultParameterValues() if this is an annotation class, and it has default parameter values, returns the default parameter values as an AnnotationParameterValueList.
  • Methods: (N.B. call .enableMethodInfo() before .scan() to enable method scanning, and call .ignoreMethodVisibility() to scan non-public methods.)

    💡 The .getDeclared...() and .hasDeclared...() versions of the following methods only apply to methods defined in the base class, i.e. they exclude default methods defined in any interfaces implemented by the class, and methods inherited from the class' superclasses. See also this note on the effect of .ignoreMethodVisibility().

    • .getMethodInfo() / .getDeclaredMethodInfo() returns the methods of the class that are not constructors, as a MethodInfoList of MethodInfo objects.
    • .getMethodInfo(String methodName) / .getDeclaredMethodInfo(String methodName) returns methods of the class with the given name (constructors have the name "<init>"). May return more than one method, due to overloading.
    • .getConstructorInfo() / .getDeclaredConstructorInfo() returns constructors of the class, as a MethodInfoList of MethodInfo objects.
    • .getMethodAndConstructorInfo() / .getDeclaredMethodAndConstructorInfo() returns methods and constructors of the class, as a MethodInfoList of MethodInfo objects.
    • .hasMethod(String methodName) / .hasDeclaredMethod(String methodName) returns true if this class has a method with the given name.
    • .getMethodAnnotations() returns the union of classes annotating any methods declared by this class, as a ClassInfoList. These annotations do not carry parameter values -- call .getAllAnnotationInfo() on a MethodInfo instance for those.
    • .hasMethodAnnotation(String methodAnnotationName | Class<? extends Annotation> methodAnnotationClass) / .hasDeclaredMethodAnnotation(...) returns true if this class has a method that has the given annotation.
    • .getMethodInfoWithAnnotation(String annotationName | Class<? extends Annotation> annotationClass) / .getDeclaredMethodInfoWithAnnotation(...) returns the methods of the class that are not constructors and that have the given annotation or meta-annotation, as a MethodInfoList, or the empty list if none. Requires .enableAnnotationInfo(). To find annotated constructors, filter .getMethodAndConstructorInfo() using MethodInfoList#filter().
    • .getMethodParameterAnnotations() returns the union of classes annotating any method parameters of methods declared by this class, as a ClassInfoList.
    • .hasMethodParameterAnnotation(String parameterAnnotationName | Class<? extends Annotation> parameterAnnotationClass) / .hasDeclaredMethodParameterAnnotation(...) returns true if this class has a method with a parameter that has the given annotation.
    • .getClassesWithMethodAnnotation() if this is an annotation class, returns all classes that have this class as a method annotation, as a ClassInfoList.
    • .getClassesWithMethodParameterAnnotation() if this is an annotation class, returns all classes that have this class as a method parameter annotation, as a ClassInfoList.
  • Fields: (N.B. call .enableFieldInfo() before .scan() to enable field scanning, and call .ignoreFieldVisibility() to scan non-public fields.)

    💡 The .getDeclared...() and .hasDeclared...() versions of the following methods only apply to fields defined in the base class, i.e. they exclude fields inherited from the class' superclasses. See also this note on the effect of .ignoreFieldVisibility().

    • .getFieldInfo() / .getDeclaredFieldInfo() returns fields of the class, as a FieldInfoList of FieldInfo objects.
    • .getFieldInfo(String fieldName) / .getDeclaredFieldInfo(String fieldName) returns the field of the class that has the given name, as a FieldInfo object, or null if the named field doesn't exist.
    • .hasField(String fieldName) / .hasDeclaredField(String fieldName) returns true if this class has a field with the given name.
    • .getFieldAnnotations() returns the union of classes annotating any fields in this class, as a ClassInfoList. These annotations do not carry parameter values -- call .getAllAnnotationInfo() on a FieldInfo instance for those.
    • .hasFieldAnnotation(String fieldAnnotationName | Class<? extends Annotation> annotationClass) / .hasDeclaredFieldAnnotation(...) returns true if this class has a field with the given annotation.
    • .getFieldInfoWithAnnotation(String annotationName | Class<? extends Annotation> annotationClass) / .getDeclaredFieldInfoWithAnnotation(...) returns the fields of the class that have the given annotation or meta-annotation, as a FieldInfoList, or the empty list if none. Requires .enableAnnotationInfo().
    • .getClassesWithFieldAnnotation() if this is an annotation class, returns all classes that have this class as a field annotation, as a ClassInfoList.
  • Inner classes:

    • .getInnerClasses() returns the inner classes within this class, if this class is an outer class.
    • .getOuterClasses() returns the outer classes enclosing this class, if this class is an inner class, from innermost to outermost.
    • .getFullyQualifiedDefiningMethodName() returns the fully-qualified method name (as a String) for the method that defined this class, if this class is an anonymous inner class, otherwise returns null.
  • Location:

    • .getClasspathElementURI() returns the URI of the classpath element or module that the classfile was found within (preferred over .getClasspathElementURL(), since URL throws an exception for jrt: URI types).
    • .getClasspathElementURL() returns the URL of the classpath element or module that the classfile was found within.
    • .getClasspathElementFile() returns the File (directory or jarfile) of the classpath element that the classfile was found within, or null if the classfile was found in a module.
    • .getModuleReference() returns the java.lang.module.ModuleReference for the module that the classfile was found within, or null if the classfile was found in a directory or jarfile.
    • .getResource() returns the Resource for the class' classfile.
    • .getClassLoaderString() returns the string form of the classloader that this class was found under -- the same string the verbose scanning log shows for the classpath element. This is an identifier, not the classloader itself: ClassGraph reads classfiles directly rather than through a classloader, and drops every classloader reference once the classpath has been found, so that a scan cannot keep a classloader alive. Returns null if the classloader is not known, e.g. for an external class, or for a class in a module loaded by the bootstrap classloader.
  • Finding class dependencies:

    💡 Call ClassGraph#enableInterClassDependencies() before #scan() to enable the following method; you can also call ClassGraph#enableExternalClasses() if you want non-accepted classes in the result.

    💡 See also ScanResult#getClassDependencyMap(), ScanResult#getReverseClassDependencyMap(), and GraphVizDotFile#writeFromInterClassDependencies() for rendering the dependency graph.

    • .getClassDependencies() returns a ClassInfoList of all the classes a given class depends upon, found by looking for class references in superclasses, interfaces, methods, fields, annotations, local variables, intermediate values within a method's code, concrete type parameters, etc.

ArrayClassInfo

ArrayClassInfo is a subclass of ClassInfo that is used to hold metadata about an array class, e.g. int[][].class.

An ArrayClassInfo reference is obtained from an ArrayTypeSignature by calling ArrayTypeSignature#getArrayClassInfo().

The property method ClassInfo#isArrayClass() returns true if a ClassInfo object is an ArrayClassInfo.

ArrayClassInfo is assignable to ClassInfo for convenience, but most of the ClassInfo methods return empty or default values, e.g. ArrayClassInfo#getMethodInfo() and ArrayClassInfo#getFieldInfo() both return empty lists.

However, ArrayClassInfo extends ClassInfo with the following additional methods for dealing with arrays:

  • Dimensions:
    • .getNumDimensions() gets the number of dimensions of the array as an int, e.g. returns 2 for an array type of int[][].
  • Element type:

    💡 These methods apply to the innermost element type, e.g. for an array type of int[][], the innermost element type is int (and not int[], which is the element type of the toplevel array type).

    • .getElementTypeSignature() returns the TypeSignature of the innermost element type.
    • .getElementClassInfo() gets a ClassInfo object for the innermost element type, if available. Returns null for an innermost element type whose class was not found during the scan, or when the innermost element type is a base type like int, byte, etc.
  • Array type:
    • .getArrayTypeSignature() returns the ArrayTypeSignature object that this ArrayClassInfo was obtained from. The ArrayTypeSignature can be used to get the name of the element type, using ArrayTypeSignature#getElementTypeSignature().
    • .getTypeSignatureString() returns the raw internal type signature of the array class, e.g. returns "[[I" if the array type is int[][].

ClassInfoList

A list of ClassInfo objects. The list is deduplicated (a ClassInfoList is produced from Set<ClassInfo> internally), and the ClassInfo objects in the list are sorted in order of class name, with three exceptions: ClassInfo#getAllSuperclasses() returns classes sorted in ascending order of inheritance hierarchy, ClassInfo#getOuterClasses() returns containing classes from innermost to outermost, and ClassInfo#getAllSuperinterfaces() preserves declaration order, since the order in which interfaces are declared is significant for inheritance.

ClassInfoList extends ArrayList<ClassInfo> with the following convenience methods:

  • Converting to Map:
    • .asMap() returns the ClassInfoList as a Map<String, ClassInfo> mapping the class name to the corresponding ClassInfo object.
  • Working with class names:
    • .getNames() returns a list of the names of the classes in this list, as a List<String>.
    • .getAsStrings() returns a list of the result of calling .toString() on each ClassInfo object in this list, as a List<String> of String representations of each class, including annotations, modifiers, generic type params, class name, etc.
      • .getAsStringsWithSimpleNames() works like .getAsStrings(), but uses only the simple name of any referenced classes, by calling .toStringWithSimpleNames() on each list element rather than .toString().
    • .containsName(String className) returns true if a class of the given name is contained in this list.
    • .get(String className) returns the ClassInfo object in this list with the requested name, if present, otherwise returns null.
  • Filtering for direct relationships:
    • .directOnly() returns the subset of ClassInfo items that were obtained by direct relationship. For example, classInfo.getAllSuperinterfaces() returns all interfaces implemented by a class, but classInfo.getAllSuperinterfaces().directOnly() returns only the interfaces directly implemented by the class. (The getDirect...() methods on ClassInfo are shorthand for exactly this.)
  • Filtering by class type:
    • .getStandardClasses() returns the subset of ClassInfo objects in the list that are standard classes (i.e. not interfaces or annotations).
    • .getInterfaces() returns the subset of ClassInfo objects in the list that are standard interfaces (i.e. interfaces that are not annotations).
    • .getInterfacesAndAnnotations() returns the subset of ClassInfo objects in the list that are interfaces or annotations (annotations are interfaces, and can be implemented).
    • .getImplementedInterfaces() returns the subset of ClassInfo objects in the list that are "implemented interfaces", i.e. interfaces or annotations that have been implemented by some class.
    • .getAnnotations() returns the subset of ClassInfo objects in the list that are annotations.
    • .getEnums() returns the subset of ClassInfo objects in the list that are enums.
    • .getRecords() returns the subset of ClassInfo objects in the list that are record types.
    • .getAssignableTo(ClassInfo superclassOrInterface | String superclassOrInterfaceName | Class<?> superclassOrInterface) returns the subset of ClassInfo objects in the list for which superclassOrInterface.isAssignableFrom(classRef) would return true for the corresponding class references. In other words, returns all elements of the list that extend or implement superclassOrInterface.
  • Filtering by predicate:
    • .filter(Predicate<ClassInfo> filter) returns a ClassInfoList that is a subset of the original list, obtained by applying the given predicate to each ClassInfo in the list. There are a number of predicate methods in ClassInfo (with names starting with is, has, extends, and implements) that you can use directly as the predicate, e.g. .filter(ClassInfo::isInterface), or as part of a lambda, e.g. .filter(classInfo -> classInfo.hasAnnotation("com.xyz.Checked")).
  • Set operations:
    • .union(ClassInfoList... others) returns a ClassInfoList that is the union of this list and the others.
    • .intersect(ClassInfoList... others) returns a ClassInfoList that is the intersection of this list and the others.
    • .exclude(ClassInfoList other) returns a ClassInfoList that is the set difference of this list and the other (i.e. this \ other).

Visualizing the class graph

A ClassInfoList can be rendered as a GraphViz .dot file by the separate classgraph-viz library -- see the GraphViz API.

Clone this wiki locally