-
-
Notifications
You must be signed in to change notification settings - Fork 309
ClassInfo API
See also the ClassGraph API overview.
💡 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 correspondinggetDirect...methods return only what is one step away -- what the class itself names in itsextendsorimplementsclause, or what is written on it directly.
Holds information about a class. Obtained by calling ScanResult#getAllClasses() and related methods.
-
Properties:
-
.getName()/.getSimpleName()returns the name of the class as aString. (.getName()includes the package prefix,.getSimpleName()does not.) Note that.getSimpleName()is not quite the same asClass#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 aString. -
.getPackageInfo()returns thePackageInfoobject for the package containing the class. -
.getModuleInfo()returns theModuleInfoobject for the module containing the class, or null if the class is not part of a named module. -
.getSourceFile()returns the value of theSourceFileattribute of a classfile. -
.isExternalClass()returnstrueif 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 anint, in the same form asClass#getModifiers(). TheACC_SUPERbit (0x0020) of the classfile'saccess_flagsfield is masked out, since it is not a modifier -- it selects the JVM's treatment of theinvokespecialinstruction, javac sets it on almost every class, and the same bit value injava.lang.reflect.ModifierisSYNCHRONIZED, 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 thejava.lang.reflect.Modifierpredicates, rather than comparing the whole value. -
.getModifiersString()returns the class modifiers as aString(e.g."public abstract"). -
.isPublic()returnstrueif the class is public. -
.isProtected()returnstrueif the class is protected. -
.isPrivate()returnstrueif the class is private. -
.isPackageVisible()returnstrueif the class has default (package) visibility, i.e. is neither public, protected nor private. -
.isAbstract()returnstrueif the class is abstract. -
.isSynthetic()returnstrueif the class is synthetic. -
.isStatic()returnstrueif the class is static. -
.isFinal()returnstrueif the class is final. -
.getTypeSignature()returns the type signature of the class (including any generic type parameters) as aClassTypeSignature, 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 aClassTypeSignature. (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 thanClassInfo#toString(), by using only the simple name of the class and any annotation classes.
-
-
Class type:
-
.isStandardClass()returnstrueif the class is not an annotation or interface. -
.isAnnotation()returnstrueif the class is an annotation. -
.isInterface()returnstrueif the class is an interface that is not an annotation (annotations are interfaces, and can be implemented). -
.isInterfaceOrAnnotation()returnstrueif the class is an interface or an annotation (annotations are interfaces, and can be implemented). -
.isImplementedInterface()returnstrueif this class is an "implemented interface" (meaning a standard interface or an annotation that has been implemented by some class). -
.isEnum()returnstrueif the class is an enum. -
.isRecord()returnstrueif the class is arecordtype. -
.isArrayClass()returnstrueif theClassInfoobject is anArrayClassInfo, indicating that the class is an array class (e.g.Point[][].class). -
.isInnerClass()returnstrueif the class is an inner class. -
.isAnonymousInnerClass()returnstrueif the class is an anonymous inner class. -
.isOuterClass()returnstrueif 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 aClassInfoList. If this class isObject, this returns every standard class in the scan result, but no interfaces, since interfaces don't extendObject. -
.getDirectSubclasses()returns only the classes that name this class in theirextendsclause. -
.getAllSuperclasses()returns all superclasses of the class, in ascending order in the class hierarchy, ending withObjectif 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 aClassInfoobject. As withClass#getSuperclass(), the superclass of a class that extends no other class isObject, and null is returned only forObjectitself and for interfaces. -
.extendsSuperclass(String superclassName | Class<?> superclass)returnstrueif 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 aFieldInfoListofFieldInfoobjects (enum constants are stored as fields in Java classes). Requires.enableFieldInfo(), and throwsIllegalStateExceptionif 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 aClassInfoList. 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'implementsclause, or this interface'sextendsclause. -
.implementsInterface(String interfaceName | Class<?> interfaceClass)returnstrueif the class implements the given interface. -
.getAllClassesImplementing()returns all the classes (and their subclasses) that implement this interface, if this is an interface, as aClassInfoList. 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 returnedClassInfoListto separate the two. -
.getDirectClassesImplementing()returns only the classes that name this interface in theirimplementsclause, and the interfaces that name it in theirextendsclause. -
.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 theirextendsclause.
-
-
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
Ahas annotation@F,Bhas annotations@Fand@E, andChas annotation@G. The annotation classesFandEare both meta-annotated with@J, andEis also meta-annotated with@I, etc. This means that the list of all annotations onAis[J, F], and the list of all annotations onBis[D, K, H, L, J, I, E].-
.getClassesWithAnnotation()if this class is an annotation, returns all classes that are annotated with this annotation, as aClassInfoList. -
.getAllAnnotations()returns all annotations and meta-annotations on this class, as aClassInfoList. Also handles the@Inheritedmeta-annotation, which causes an annotation on a class to annotate all of its subclasses. Meta-annotations in thejava.lang.annotationpackage are filtered out. (TheseClassInfoobjects 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)returnstrueif this class has the given annotation, directly or as a meta-annotation. -
.getAllAnnotationInfo()/.getDirectAnnotationInfo()returns the annotations on this class, as anAnnotationInfoListofAnnotationInfoobjects, which carry the parameter values of each annotation. -
.getAllAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass)/.getDirectAnnotationInfo(...)returns theAnnotationInfoobject for the given non-@Repeatableclass annotation, or null if none. -
.getAllAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass)/.getDirectAnnotationInfoRepeatable(...)returns theAnnotationInfoobjects for the given@Repeatableclass annotation, as anAnnotationInfoList, 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 anAnnotationParameterValueList.
-
-
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 aMethodInfoListofMethodInfoobjects. -
.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 aMethodInfoListofMethodInfoobjects. -
.getMethodAndConstructorInfo()/.getDeclaredMethodAndConstructorInfo()returns methods and constructors of the class, as aMethodInfoListofMethodInfoobjects. -
.hasMethod(String methodName)/.hasDeclaredMethod(String methodName)returnstrueif this class has a method with the given name. -
.getMethodAnnotations()returns the union of classes annotating any methods declared by this class, as aClassInfoList. These annotations do not carry parameter values -- call.getAllAnnotationInfo()on aMethodInfoinstance for those. -
.hasMethodAnnotation(String methodAnnotationName | Class<? extends Annotation> methodAnnotationClass)/.hasDeclaredMethodAnnotation(...)returnstrueif 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 aMethodInfoList, or the empty list if none. Requires.enableAnnotationInfo(). To find annotated constructors, filter.getMethodAndConstructorInfo()usingMethodInfoList#filter(). -
.getMethodParameterAnnotations()returns the union of classes annotating any method parameters of methods declared by this class, as aClassInfoList. -
.hasMethodParameterAnnotation(String parameterAnnotationName | Class<? extends Annotation> parameterAnnotationClass)/.hasDeclaredMethodParameterAnnotation(...)returnstrueif 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 aClassInfoList. -
.getClassesWithMethodParameterAnnotation()if this is an annotation class, returns all classes that have this class as a method parameter annotation, as aClassInfoList.
-
-
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 aFieldInfoListofFieldInfoobjects. -
.getFieldInfo(String fieldName)/.getDeclaredFieldInfo(String fieldName)returns the field of the class that has the given name, as aFieldInfoobject, or null if the named field doesn't exist. -
.hasField(String fieldName)/.hasDeclaredField(String fieldName)returnstrueif this class has a field with the given name. -
.getFieldAnnotations()returns the union of classes annotating any fields in this class, as aClassInfoList. These annotations do not carry parameter values -- call.getAllAnnotationInfo()on aFieldInfoinstance for those. -
.hasFieldAnnotation(String fieldAnnotationName | Class<? extends Annotation> annotationClass)/.hasDeclaredFieldAnnotation(...)returnstrueif 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 aFieldInfoList, 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 aClassInfoList.
-
-
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 aString) for the method that defined this class, if this class is an anonymous inner class, otherwise returns null.
-
-
Location:
-
.getClasspathElementURI()returns theURIof the classpath element or module that the classfile was found within (preferred over.getClasspathElementURL(), sinceURLthrows an exception forjrt:URI types). -
.getClasspathElementURL()returns theURLof the classpath element or module that the classfile was found within. -
.getClasspathElementFile()returns theFile(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 thejava.lang.module.ModuleReferencefor the module that the classfile was found within, or null if the classfile was found in a directory or jarfile. -
.getResource()returns theResourcefor 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 callClassGraph#enableExternalClasses()if you want non-accepted classes in the result.💡 See also
ScanResult#getClassDependencyMap(),ScanResult#getReverseClassDependencyMap(), andGraphVizDotFile#writeFromInterClassDependencies()for rendering the dependency graph.-
.getClassDependencies()returns aClassInfoListof 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 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 anint, e.g. returns2for an array type ofint[][].
-
-
Element type:
💡 These methods apply to the innermost element type, e.g. for an array type of
int[][], the innermost element type isint(and notint[], which is the element type of the toplevel array type).-
.getElementTypeSignature()returns theTypeSignatureof the innermost element type. -
.getElementClassInfo()gets aClassInfoobject for the innermost element type, if available. Returnsnullfor an innermost element type whose class was not found during the scan, or when the innermost element type is a base type likeint,byte, etc.
-
-
Array type:
-
.getArrayTypeSignature()returns theArrayTypeSignatureobject that thisArrayClassInfowas obtained from. TheArrayTypeSignaturecan be used to get the name of the element type, usingArrayTypeSignature#getElementTypeSignature(). -
.getTypeSignatureString()returns the raw internal type signature of the array class, e.g. returns"[[I"if the array type isint[][].
-
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 theClassInfoListas aMap<String, ClassInfo>mapping the class name to the correspondingClassInfoobject.
-
-
Working with class names:
-
.getNames()returns a list of the names of the classes in this list, as aList<String>. -
.getAsStrings()returns a list of the result of calling.toString()on eachClassInfoobject in this list, as aList<String>ofStringrepresentations 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)returnstrueif a class of the given name is contained in this list. -
.get(String className)returns theClassInfoobject in this list with the requested name, if present, otherwise returns null.
-
-
Filtering for direct relationships:
-
.directOnly()returns the subset ofClassInfoitems that were obtained by direct relationship. For example,classInfo.getAllSuperinterfaces()returns all interfaces implemented by a class, butclassInfo.getAllSuperinterfaces().directOnly()returns only the interfaces directly implemented by the class. (ThegetDirect...()methods onClassInfoare shorthand for exactly this.)
-
-
Filtering by class type:
-
.getStandardClasses()returns the subset ofClassInfoobjects in the list that are standard classes (i.e. not interfaces or annotations). -
.getInterfaces()returns the subset ofClassInfoobjects in the list that are standard interfaces (i.e. interfaces that are not annotations). -
.getInterfacesAndAnnotations()returns the subset ofClassInfoobjects in the list that are interfaces or annotations (annotations are interfaces, and can be implemented). -
.getImplementedInterfaces()returns the subset ofClassInfoobjects in the list that are "implemented interfaces", i.e. interfaces or annotations that have been implemented by some class. -
.getAnnotations()returns the subset ofClassInfoobjects in the list that are annotations. -
.getEnums()returns the subset ofClassInfoobjects in the list that are enums. -
.getRecords()returns the subset ofClassInfoobjects in the list that arerecordtypes. -
.getAssignableTo(ClassInfo superclassOrInterface | String superclassOrInterfaceName | Class<?> superclassOrInterface)returns the subset ofClassInfoobjects in the list for whichsuperclassOrInterface.isAssignableFrom(classRef)would return true for the corresponding class references. In other words, returns all elements of the list that extend or implementsuperclassOrInterface.
-
-
Filtering by predicate:
-
.filter(Predicate<ClassInfo> filter)returns aClassInfoListthat is a subset of the original list, obtained by applying the given predicate to eachClassInfoin the list. There are a number of predicate methods inClassInfo(with names starting withis,has,extends, andimplements) 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 aClassInfoListthat is the union of this list and the others. -
.intersect(ClassInfoList... others)returns aClassInfoListthat is the intersection of this list and the others. -
.exclude(ClassInfoList other)returns aClassInfoListthat is the set difference of this list and the other (i.e.this \ other).
-
A ClassInfoList can be rendered as a GraphViz .dot file by the separate classgraph-viz library -- see the GraphViz API.