-
-
Notifications
You must be signed in to change notification settings - Fork 298
ClassInfo API
See also the ClassGraph API overview.
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.)🛑 IMPORTANT: Do not try to do your own classloading using the class name, e.g. using
Class.forName(classInfo.getName())-- always useClassInfo#loadClass(), or you may end up loading the class with the wrong classloader, which can cause difficult-to-debug problems. -
.
getPackageName()returns the name of the class' package. -
.getSourceFile()returns the value of theSourceFileattribute of a classfile. -
.getModifiers()returns the class modifier bits as anint. -
.getModifiersStr()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. -
.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 available, otherwise returns null. See also.getTypeSignatureOrTypeDescriptor().🛑 Currently 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.
-
.getTypeSignatureStr()returns the raw internal Java type signature string for the class (including any generic type parameters), if available, otherwise returns null. -
.getTypeSignatureOrTypeDescriptor()returns theClassTypeSignaturefor the class, if the class is generic, otherwise it synthesizes a "class descriptor" for the class, using the class name, superclass names, and interface names, returning the class descriptor as aClassTypeSignature(Java does not include a class descriptor in classfiles). This is useful for reading type annotations that have been added to superclasses or interfaces, since without this method there would be no way to get those type annotations for non-generic classes. -
.getClassfileMinorVersion()and.getClassfileMajorVersion()get the classfile version for the class (or 0 if the classfile was not scanned, ie. if this was not a accepted class). -
.toStringWithSimpleNames()returns a simpler rendering of the class than 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 (JDK 14+). -
.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:
-
.getSubclasses()returns all subclasses of the class, as aClassInfoListofClassInfoobjects representing the subclasses. -
.getSuperclasses()returns all superclasses of the class, as aClassInfoListofClassInfoobjects representing the superclasses. -
.getSuperclass()returns the single direct superclasses of the class, as aClassInfoobject, or null if none. -
.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, without loading the enum class. -
.getEnumConstantObjects()returns the enum constants of an enum class as aList<Object>, where the objects have the same concrete type as the enum. Causes the enum class to be loaded and enum constants to be initialized.
-
-
Interfaces:
-
.getInterfaces()returns the list of interfaces implemented by this class or by one of its superclasses, if this is a standard class, or the superinterfaces extended by this interface, if this is an interface, as aClassInfoListofClassInfoobjects for the interfaces. Returns the empty list if none. -
.implementsInterface(String interfaceName | Class<?> interfaceClass)returnstrueif the class implements the given interface. -
.getClassesImplementing()returns the list of the classes (and their subclasses) that implement this interface, if this is an interface, as aClassInfoListofClassInfoobjects for the implementing classes. Returns the empty list if none.
-
-
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-annotation 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 aClassInfoListofClassInfoobjects. -
.getAnnotations()returns all annotations on this class, as aClassInfoListofClassInfoobjects (these do not include specific annotation parameters of the annotations -- if you need annotation parameter values, call.getAnnotationInfo()instead). -
.hasAnnotation(String annotationName | Class<? extends Annotation> annotation)returnstrueif this class has the given annotation. -
.getAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass)returns theAnnotationInfoobject for the given non-@Repeatableclass annotation, or null if none. -
.getAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass)returns theAnnotationInfoobject for the given@Repeatableclass annotation, as anAnnotationInfoList, or the empty list if none. -
.getAnnotationInfo()returns all annotations on this class, as anAnnotationInfoListofAnnotationInfoobjects, which contain the specific annotation parameters of each annotation. -
.getAnnotationDefaultParameterValues()if this is an annotation class, and it has default parameter values, returns the default parameter values as aListofAnnotationParameterValueobjects.
-
-
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 fieldName)/.hasDeclaredMethod(String fieldName)returnstrueif this class has a method with the given name. -
.getMethodAnnotations()returns the union of classes annotating any methods declared by this class, as aClassInfoListofClassInfoobjects. These annotations do not contain specific annotation parameters -- call.getAnnotationInfo()on aMethodInfoinstance to get details on specific method annotation instances. -
.hasMethodAnnotation(String methodAnnotationName | Class<? extends Annotation> methodAnnotationClass)/.hasDeclaredMethodAnnotation(String methodAnnotationName | Class<? extends Annotation> methodAnnotationClass)returnstrueif this class has a method that has the given annotation. -
.getMethodParameterAnnotations()returns the union of classes annotating any method parameters of methods declared by this class, as aClassInfoListofClassInfoobjects. These annotations do not contain specific annotation parameters -- call.getAnnotationInfo()on aMethodInfoinstance to get details on specific method annotation instances. -
.hasMethodParameterAnnotation(String parameterAnnotationName | Class<? extends Annotation> parameterAnnotationClass)/.hasDeclaredMethodParameterAnnotation(String parameterAnnotatinoName | Class<? extends Annotation> parameterAnnotationClass)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 aClassInfoListofClassInfoobjects. -
.getClassesWithMethodParameterAnnotation()if this is an annotation class, returns all classes that have this class as a method parameter annotation, as aClassInfoListofClassInfoobjects.
-
-
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()/.getDeclaredFieldAnnotations()returns the union of classes annotating any fields in this class, as aClassInfoListofClassInfoobjects. These annotations do not contain specific annotation parameters -- call.getAnnotationInfo()on aFieldInfoinstance to get details on specific field annotation instances. -
.hasFieldAnnotation(String fieldAnnotationName | Class<? extends Annotation> annotationClass)/.hasDeclaredFieldAnnotation(String fieldAnnotationName | Class<? extends Annotation> annotationClass)returnstrueif this class has a field with the given annotation. -
.getClassesWithFieldAnnotation()if this is an annotation class, returns all classes that have this class as a field annotation, as aClassInfoListofClassInfoobjects.
-
-
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. -
.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.
-
-
Classloading:
💡 In JDK 9+, to load non-public classes from a package (or classes extending non-public superclasses, or implementing non-public interfaces), the packages containing the class' hierarchy must be
opento the world or to ClassGraph.-
.loadClass()loads the class (if it is not already loaded), using the classloader associated with the classpath element where this classfile was found, and returns aClass<?>reference.🛑 Make sure that you are not trying to load classes after the
ScanResultgoes out of scope orScanResult#close()is called. TheScanResultmust still exist for classloading to succeed.-
.loadClass(boolean ignoreExceptions)loads the class, returning aClass<?>reference, unless there was an exception while trying to load the class, andignoreExceptionsis true, in which case returnsnull. -
.loadClass(Class<T> superclassOrInterfaceType)loads the class, casts the class reference to the requested superclass or interface type, and returns aClass<T>reference.🛑 Only use the
Class<T> superclassOrInterfaceTypeparameter if the superclass or interface type is loaded by the same classloader as the loaded class, otherwise you will get aClassCastException.-
.loadClass(Class<T> superclassOrInterfaceType, boolean ignoreExceptions)loads the class, casts the class reference to the requested superclass or interface type, and returns aClass<T>reference. IfignoreExceptionsis true, and an error occurs during either classloading or casting, returnsnull.
-
-
-
-
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. -
.getModuleRef()returns theModuleReffor 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.
-
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()will return theTypeSignatureof the innermost element type. -
.getElementClassInfo()gets aClassInfoobject for the innermost element type, if available. Will returnnullfor 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. -
.loadElementClass()loads the class of the innermost element type and returns aClass<?>reference for the class, e.g. this method will returnint.classif the array type isint[][].
-
-
Array type:
-
.getArrayTypeSignature()will return theArrayTypeSignatureobject that thisArrayClassInfowas obtained from. TheArrayTypeSignaturecan be used to get the name of the element type without loading it, usingArrayTypeSignature#getElementTypeSignature(). -
.getTypeSignatureStr()will return the raw internal type signature of the array class, e.g. will return"[[I"for an if the array type isint[][]. -
.loadClass()creates an array class, given the element type and the number of dimensions, and returns aClass<?>reference, e.g. will returnint[][].classfor an array type ofint[][], orpkg.X[].classfor an array type ofpkg.X[].
-
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 two exceptions: ClassInfo#getSuperclasses() returns classes sorted in ascending order of inheritance hierarchy, and ClassInfo#getOuterClasses() returns containing classes from innermost to outermost.
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.getInterfaces()returns all interfaces implemented by a class, butclassInfo.getInterfaces().directOnly()returns only the interfaces directly implemented by the class.
-
-
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 (JDK 14+). -
.getAssignableTo(ClassInfo superClassOrInterface)returns the subset ofClassInfoobjects in the list for whichsuperClassOrInterfaceRef.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(ClassInfoFilter filter)returns aClassInfoListthat is a subset of the original list, obtained by applying the given filter 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 in place of a customClassInfoFilter, e.g..filter(ClassInfo::isInterface), or as part of aClassInfoFilter, e.g..filter(classInfo -> classInfo.hasAnnotation("com.xyz.Checked")).-
ClassInfoFilteris aFunctionalInterfacewith the single abstract methodboolean accept(ClassInfo classInfo).
-
-
-
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).
-
-
Classloading:
💡 In JDK 9+, to load non-public classes from a package (or classes extending non-public superclasses, or implementing non-public interfaces), the packages containing the class' hierarchy must be
opento the world or to ClassGraph.-
.loadClasses()loads each class in the list, if not already loaded, using the classloader associated with the classpath element where this classfile was found, and returns aListofClass<?>references.🛑 Make sure that you are not trying to load classes after the
ScanResultgoes out of scope orScanResult#close()is called. TheScanResultmust still exist for classloading to succeed.-
.loadClasses(boolean ignoreExceptions)loads each class in the class, returning aListofClass<?>references. IfignoreExceptionsis true, silently skip adding entries to the list for any classes that cannot be loaded. -
.loadClasses(Class<T> superclassOrInterfaceType)loads each class in the list, if not already loaded, casts each class reference to the requested superclass or interface type, and returns a list ofClass<T>references.🛑 Only use the
Class<T> superclassOrInterfaceTypeparameter if the superclass or interface type is loaded by the same classloader as each of the loaded classes, otherwise you will get aClassCastException.-
.loadClass(Class<T> superclassOrInterfaceType, boolean ignoreExceptions)loads each class in the list, casting the class reference to the requested superclass or interface type, and returning aListofClass<T>references. IfignoreExceptionsis true, silently skip adding entries to the list for any classes that cannot be loaded or cast to the superclass or interface type.
-
-
-
-
Finding class dependencies:
💡 Call
ClassGraph 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()andClassInfoList#generateGraphVizDotFileFromInterClassDependencies().-
ClassInfo#getClassDependencies()returns aClassInfoListfor all the classes a given class depends upon, by looking for class references in superclasses, interfaces, methods, fields, annotations, local variables, intermediate values within a method's code, concrete type parameters, etc.
-
Generating a GraphViz .dot file for class graph visualization
ClassInfoList#generateGraphVizDotFile() can be called on any ClassInfoList to generate a .dot file that can be fed into GraphViz (e.g. using dot -Tsvg < classgraph.dot > classgraph.svg) to visualize the relationships between the classes in the list. (There are several variants of this method that allow you to add more or less information to the graph.) For example, call this method on the result of ScanResult#getAllClasses() to plot the relationships between all classes, at least for basic relationships such as implemented interfaces, superclasses, and method/field types (relationships between classes that are based on code in method bodies cannot be visualized this way -- see next section).
💡 Note that you need to call
.enableClassInfo(),.enableFieldInfo(),.enableMethodInfo(),.ignoreFieldVisibility(), and/or.ignoreMethodVisibility(), for relevant information to be shown in the output graph.
💡 Method parameter names will only be visible in the GraphViz output if you invoked
javacwith the-parametersswitch (only available in JDK 8 and above). In Eclipse this setting is Project Properties > Java Compiler > Store information about method parameters (usable via reflection).
Note that ClassInfoList#generateGraphVizDotFile() does not show dependencies between classes that are a result of class references in local variables or intermediate values. For that, you need full inter-class dependency analysis.
You can enable full inter-class dependency analysis by calling ClassGraph ClassGraph#enableInterClassDependencies() before #scan() (you can also call ClassGraph#enableExternalClasses() if you want to show non-accepted classes). You can then call ClassInfoList#generateGraphVizDotFileFromInterClassDependencies() to get the complete inter-class dependency graph.
There is only one arrow type in an inter-class dependency graph, indicating that a dependent class depends upon a dependency class in some way. What the exact relationship is cannot be determined without parsing the full bytecode of every method in a class, which ClassGraph does not attempt to do (ClassGraph finds these dependencies using the types of fields and methods, but also the names of classes referenced in the constant pool of the class, which is used to encode class names that are used in bytecode, e.g. for static method calls).