Skip to content
Luke Hutchison edited this page Aug 16, 2026 · 17 revisions

See also the ClassGraph API overview.

Contents

MethodInfo

Holds information about one method of a class. Obtained by calling ClassInfo#getMethodInfo() and similar methods.

  • Properties: (N.B. call .enableMethodInfo() before .scan() to enable method scanning, and call .ignoreMethodVisibility() if you want to scan non-public methods.)
    • .getName() returns the name of the method as a String. Constructors are named "<init>", and class static initializer blocks are named "<clinit>".
    • .getClassName() returns the name of the declaring class (i.e. the class that declares the method).
    • .getClassInfo() returns the ClassInfo object for the declaring class (i.e. the class that declares the method).
    • .getModifiers() returns the method modifier bits as an int.
    • .getModifiersString() returns the method modifiers as a String (e.g. "public static").
    • .isConstructor() returns true if the method is a constructor.
    • .isPublic() returns true if the method is public.
    • .isProtected() returns true if the method is protected.
    • .isPrivate() returns true if the method is private.
    • .isAbstract() returns true if the method is abstract.
    • .isStatic() returns true if the method is static.
    • .isFinal() returns true if the method is final.
    • .isSynchronized() returns true if the method is synchronized.
    • .isSynthetic() returns true if the method is synthetic.
    • .isBridge() returns true if the method is a bridge method.
    • .isStrict() returns true if the method is strict.
    • .isVarArgs() returns true if the method is a varargs method.
    • .isNative() returns true if the method is a native method.
    • .isDefault() returns true if the method is a default method on an interface.
    • .hasBody() returns true if the method has a body (or implementation).
    • .getMinLineNum() and .getMaxLineNum() return the line number of the first and last non-blank line in a method body (if available).
    • .toStringWithSimpleNames() returns a simpler rendering of the method than MethodInfo#toString(), by using only the simple name of any classes or annotations in the method's type.
  • Parameters:
    • .getParameterInfo() returns a List of MethodParameterInfo objects, one per method parameter, or the empty list if the method has no parameters.
  • Thrown exceptions:
    • .getThrownExceptions() returns the exceptions declared in the method's throws clause, as a ClassInfoList, or the empty list if none.
    • .getThrownExceptionNames() returns the names of those exceptions, as a List<String>.
  • Type:
    • .getTypeSignature() returns the type signature of the method (including any generic type parameters) as a MethodTypeSignature, if available, otherwise returns null.

      🛑 ClassGraph makes no attempt to resolve type variables in the result types or parameter types of methods. If you need concrete types for a specific type context, you will need to do the type substitution yourself.

    • .getTypeSignatureString() returns the type signature of the method (including any generic type parameters) as a raw internal Java type signature string, or null if there is no generic type signature.
    • .getTypeDescriptor() returns the type descriptor of the method (without generic type parameters) as a MethodTypeSignature.
    • .getTypeDescriptorString() returns the type descriptor of the method (without any generic type parameters) as a raw internal Java type descriptor string.
    • .getTypeSignatureOrTypeDescriptor() returns the type signature of the method, if available, otherwise returns the type descriptor. This is usually the method you want.
    • .getTypeSignatureOrTypeDescriptorString() returns the raw internal type signature string of the method, if available, otherwise returns the raw internal type descriptor string of the method.
    • .getTypeSignatureOrTypeDescriptor().getResultType() returns the result type for the method, as a TypeSignature.
    • .getTypeSignatureOrTypeDescriptor().getTypeParameters() returns the type parameters of a generic method, as a List of TypeParameter objects, or the empty list if the method is not generic. For example, for the method <T> void doSomething(), the type parameter is T.
    • .getParameterInfo().get(parameterIndex).getTypeSignatureOrTypeDescriptor() returns the type of the parameter at index parameterIndex.
  • Annotations:

    💡 Every annotation getter comes in an All form and a Direct form. The All form also includes meta-annotations -- the annotations on the method's own annotations, transitively. The Direct form includes only the annotations written on the method itself.

    • .getAllAnnotationInfo() / .getDirectAnnotationInfo() returns the annotations on this method as an AnnotationInfoList.
    • .hasAnnotation(String annotationName | Class<? extends Annotation> annotationClass) returns true if the method has the given annotation.
    • .getAllAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfo(...) returns the AnnotationInfo object for the given non-@Repeatable method annotation, or null if none.
    • .getAllAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfoRepeatable(...) returns the AnnotationInfo objects for the given @Repeatable method annotation, as an AnnotationInfoList, or the empty list if none.
    • .hasParameterAnnotation(String annotationName | Class<? extends Annotation> annotationClass) returns true if the method has a parameter with the given annotation.
  • Dependencies:
    • .getClassDependencies() returns a ClassInfoList of the classes referenced by this method: the classes named in its type descriptor and type signature, the classes of the annotations on it and on its parameters, and the exception types in its throws clause. Unlike ClassInfo#getClassDependencies(), this does not require .enableInterClassDependencies(), since the information comes from the method's own signature and annotations. Only classes that were themselves encountered during the scan are returned -- call .enableExternalClasses() before scanning to also get classes outside the accepted packages.

MethodParameterInfo

Holds information about one parameter of a method. Obtained by calling MethodInfo#getParameterInfo().

  • Properties:
    • .getName() returns the method parameter name as a String.

      💡 Method parameter names are only available if you invoked javac with the -parameters switch. In Eclipse this setting is Project Properties > Java Compiler > Store information about method parameters (usable via reflection).

    • .getMethodInfo() returns the MethodInfo object for the method that declares this parameter.
    • .getIndex() returns the zero-based index of this parameter in the method's parameter list.
    • .getModifiers() returns the method parameter modifier bits as an int.
    • .getModifiersString() returns the method parameter modifiers as a String, e.g. "final".
    • .isFinal() returns true if the method parameter is final.
    • .isSynthetic() returns true if the method parameter is synthetic.
    • .isMandated() returns true if the method parameter is mandated.
    • .isVarArgs() returns true if this is the variadic parameter of a variadic method, i.e. the parameter declared as T.... The type of a variadic parameter is the array type T[], so this is the only way to tell a variadic parameter apart from a parameter that was declared as an array.
  • Type:
    • .getTypeSignature() returns the type signature of the method parameter as a TypeSignature, if available, otherwise returns null.

      🛑 ClassGraph makes no attempt to resolve type variables in the parameter types of methods. If you need concrete types for a specific type context, you will need to do the type substitution yourself.

    • .getTypeDescriptor() returns the type descriptor of the method parameter as a TypeSignature.
    • .getTypeSignatureOrTypeDescriptor() returns the type signature of the method parameter, if available, otherwise returns the type descriptor.
  • Annotations:
    • .getAllAnnotationInfo() / .getDirectAnnotationInfo() returns an AnnotationInfoList of AnnotationInfo objects for annotations on the method parameter, or the empty list if none.
    • .hasAnnotation(String annotationName | Class<? extends Annotation> annotationClass) returns true if the method parameter has the named annotation.
    • .getAllAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfo(...) returns the AnnotationInfo object for the given non-@Repeatable method parameter annotation, or null if none.
    • .getAllAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass) / .getDirectAnnotationInfoRepeatable(...) returns the AnnotationInfo objects for the given @Repeatable method parameter annotation, as an AnnotationInfoList, or the empty list if none.

The effect of .ignoreMethodVisibility()

If you call ClassGraph#ignoreMethodVisibility() before calling scan(), then non-public methods will be scanned, and MethodInfo objects will be added to the ScanResult for any non-public methods encountered while scanning classes.

Note that ClassGraph differs in its behavior from the Java reflection API. You will notice the following differences in behavior:

  • Java reflection:

    • Class#getMethods() returns public methods but not non-public methods, for a class and all its superclasses.
    • Class#getDeclaredMethods() returns public and non-public methods for a class, but not its superclasses.
  • ClassGraph:

    • ClassInfo#getMethodInfo() returns methods for a class and all its superclasses. Only public methods are returned unless ClassGraph#ignoreMethodVisibility() was called, then both public and non-public methods are returned.
    • ClassInfo#getDeclaredMethodInfo() returns methods for a class but not its superclasses. Only public methods are returned unless ClassGraph#ignoreMethodVisibility() was called, then both public and non-public methods are returned.

This difference in behavior is intended to separate the concerns of whether or not to obtain method info from superclasses from whether or not to return non-public methods, making it simpler with the ClassGraph API to find non-public methods in superclasses, and/or to filter out non-public methods from a given base class, if they are not needed. With the Java reflection API, you cannot get non-public methods from superclasses using Sub.class.getMethods() -- if you do want the non-public methods, you have to iterate up through the superclass hierarchy and call .getDeclaredMethods() on each superclass. Conversely, with the Java reflection API you cannot request only public methods that are declared in a base class but not its superclasses (when calling .getDeclaredMethods()).

With ClassGraph, you can separately decide (1) whether or not to include methods from superclasses (by calling .getDeclaredMethodInfo() if you want only the methods of a class but not its superclasses, or .getMethodInfo() if you want the methods of a class and all of its superclasses), and (2) whether or not to include non-public methods in results (by calling .ignoreMethodVisibility() to enable non-public methods).

To illustrate, given the following classes:

public class Super {
    public int publicSuper() { }
    private int privateSuper() { }
}

public class Sub extends Super {
    public int publicSub() { }
    private int privateSub() { }
}

The following results are obtained:

Non-declared methods of Super:

Mechanism Method call Result
Java reflection Super.class.getMethods() publicSuper
ClassGraph superClassInfo.getMethodInfo() publicSuper
ClassGraph after .ignoreMethodVisibility() superClassInfo.getMethodInfo() publicSuper, privateSuper

Non-declared methods of Sub:

Mechanism Method call Result
Java reflection Sub.class.getMethods() publicSuper, publicSub
ClassGraph subClassInfo.getMethodInfo() publicSuper, publicSub
ClassGraph after .ignoreMethodVisibility() subClassInfo.getMethodInfo() publicSuper, publicSub, privateSuper, privateSub

Declared methods of Super:

Mechanism Method call Result
Java reflection Super.class.getDeclaredMethods() publicSuper, privateSuper
ClassGraph superClassInfo.getDeclaredMethodInfo() publicSuper
ClassGraph after .ignoreMethodVisibility() superClassInfo.getDeclaredMethodInfo() publicSuper, privateSuper

Declared methods of Sub:

Mechanism Method call Result
Java reflection Sub.class.getDeclaredMethods() publicSub, privateSub
ClassGraph subClassInfo.getDeclaredMethodInfo() publicSub
ClassGraph after .ignoreMethodVisibility() subClassInfo.getDeclaredMethodInfo() publicSub, privateSub

MethodInfoList

Extends ArrayList<MethodInfo> -- unmodifiable when it came from a scan -- with the following convenience methods:

  • .asMap() returns the MethodInfoList as a Map<String, MethodInfoList> mapping the method name to a MethodInfoList of methods with that name. (There may be multiple methods in the list with a given name, due to overloading.)
  • .getNames() returns a list of the names of the methods in this list, as a List<String>.
  • .getAsStrings() returns a list of the result of calling .toString() on each MethodInfo object in this list, producing a List<String> of String representations of each method, including annotations, modifiers, generic type params, method name, method parameters, 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 methodName) returns true if one or more methods of the given name is contained in this list.
  • .get(String methodName) returns a new MethodInfoList consisting of the MethodInfo objects in this list with the requested name, or the empty list if none. (There may be multiple methods in the list with a given name, due to overloading.)
  • .getSingleMethod(String methodName) returns the single MethodInfo object in this list with the requested name, if there is exactly one method with the requested name. Returns null if there are no methods with the requested name. Throws IllegalArgumentException if there are two or more methods with the requested name.
  • .filter(Predicate<MethodInfo> filter) returns a MethodInfoList that is a subset of the original list, obtained by applying the given predicate to each MethodInfo object in the list, e.g. .filter(MethodInfo::isConstructor).

Clone this wiki locally