Skip to content
Luke Hutchison edited this page Jul 6, 2022 · 15 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.
    • .getModifiersStr() 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.
    • .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).
    • .isDefault() returns true if the method is a default method on an interface.
    • .toStringWithSimpleNames() returns a simpler rendering of the method than than MethodInfo#toString(), by using only the simple name of any classes or annotations in the method's type.
  • Parameters:
    • .getParameterInfo() returns an array of MethodParameterInfo objects, one per method parameter, or an empty array, if the method has no parameters.
  • Type:
    • .getTypeSignature() returns the type signature of the method (including any generic type parameters) as a MethodTypeSignature, if available, otherwise returns null.

      🛑 Currently 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.

    • .getTypeSignatureStr() 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.
    • .getTypeDescriptorStr() 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.
    • .getTypeSignatureOrTypeDescriptor().getResultType() returns the result type for the method, as a TypeSignature.
    • .getParameterInfo()[parameterIndex].getTypeSignatureOrTypeDescriptor() returns the type of the parameter at index parameterIndex, assuming there is at least one parameter.
    • .getTypeSignatureOrTypeDescriptorStr() returns the raw internal type signature string of the method, if available, otherwise returns the raw internal type descriptor string of the method.
    • .getTypeParameters() returns the type parameters of generic methods, or null if none. For example, for the method void <T> doSomething(), the generic type parameter is T. The type parameters are returned as a List of TypeParameter objects.
  • Annotations:
    • .getAnnotationInfo() 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.
    • .getAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass) returns the AnnotationInfo object for the given non-@Repeatable method annotation, or null if none.
    • .getAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass) returns the AnnotationInfo object 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.
  • Classloading / reflection:
    • .loadClassAndGetMethod() loads the defining class, then gets the java.lang.reflection.Method object for this non-constructor method. Only call this if MethodInfo#isConstructor() is false, otherwise IllegalArgumentException will be thrown.
    • .loadClassAndGetConstructor() loads the defining class, then gets the java.lang.reflection.Constructor<?> object for this constructor method. Only call this if MethodInfo#isConstructor() is true, otherwise IllegalArgumentException will be thrown.

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 (only available in JDK 8 and above). In Eclipse this setting is Project Properties > Java Compiler > Store information about method parameters (usable via reflection).

    • .getModifiers() returns the method parameter modifier bits as an int.
    • .getModifiersStr() 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.
  • Type:
    • .getTypeSignature() returns the type signature of the method parameter as a TypeSignature, if available, otherwise returns null.

      🛑 Currently 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:
    • .getAnnotationInfo() 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.
    • .getAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass) returns the AnnotationInfo object for the given non-@Repeatable method parameter annotation, or null if none.
    • .getAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass) returns the AnnotationInfo object 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 .getDeclaredMethods() if you want only the methods of a class but not its superclasses, or .getMethods() 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> 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(MethodInfoFilter filter) returns a MethodInfoList that is a subset of the original list, obtained by applying the given filter predicate to each MethodInfo object in the list.
    • MethodInfoFilter is a FunctionalInterface with the single abstract method boolean accept(MethodInfo methodInfo).
Clone this wiki locally