-
-
Notifications
You must be signed in to change notification settings - Fork 309
MethodInfo API
See also the ClassGraph API overview.
-
MethodInfoMethodParameterInfo- The effect of
.ignoreMethodVisibility()
MethodInfoList
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 aString. 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 theClassInfoobject for the declaring class (i.e. the class that declares the method). -
.getModifiers()returns the method modifier bits as anint. -
.getModifiersString()returns the method modifiers as aString(e.g."public static"). -
.isConstructor()returnstrueif the method is a constructor. -
.isPublic()returnstrueif the method is public. -
.isProtected()returnstrueif the method is protected. -
.isPrivate()returnstrueif the method is private. -
.isAbstract()returnstrueif the method is abstract. -
.isStatic()returnstrueif the method is static. -
.isFinal()returnstrueif the method is final. -
.isSynchronized()returnstrueif the method is synchronized. -
.isSynthetic()returnstrueif the method is synthetic. -
.isBridge()returnstrueif the method is a bridge method. -
.isStrict()returnstrueif the method is strict. -
.isVarArgs()returnstrueif the method is a varargs method. -
.isNative()returnstrueif the method is a native method. -
.isDefault()returnstrueif the method is a default method on an interface. -
.hasBody()returnstrueif 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 thanMethodInfo#toString(), by using only the simple name of any classes or annotations in the method's type.
-
-
Parameters:
-
.getParameterInfo()returns aListofMethodParameterInfoobjects, one per method parameter, or the empty list if the method has no parameters.
-
-
Thrown exceptions:
-
.getThrownExceptions()returns the exceptions declared in the method'sthrowsclause, as aClassInfoList, or the empty list if none. -
.getThrownExceptionNames()returns the names of those exceptions, as aList<String>.
-
-
Type:
-
.getTypeSignature()returns the type signature of the method (including any generic type parameters) as aMethodTypeSignature, 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 aMethodTypeSignature. -
.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 aTypeSignature. -
.getTypeSignatureOrTypeDescriptor().getTypeParameters()returns the type parameters of a generic method, as aListofTypeParameterobjects, or the empty list if the method is not generic. For example, for the method<T> void doSomething(), the type parameter isT. -
.getParameterInfo().get(parameterIndex).getTypeSignatureOrTypeDescriptor()returns the type of the parameter at indexparameterIndex.
-
-
Annotations:
💡 Every annotation getter comes in an
Allform and aDirectform. TheAllform also includes meta-annotations -- the annotations on the method's own annotations, transitively. TheDirectform includes only the annotations written on the method itself.-
.getAllAnnotationInfo()/.getDirectAnnotationInfo()returns the annotations on this method as anAnnotationInfoList. -
.hasAnnotation(String annotationName | Class<? extends Annotation> annotationClass)returnstrueif the method has the given annotation. -
.getAllAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass)/.getDirectAnnotationInfo(...)returns theAnnotationInfoobject for the given non-@Repeatablemethod annotation, or null if none. -
.getAllAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass)/.getDirectAnnotationInfoRepeatable(...)returns theAnnotationInfoobjects for the given@Repeatablemethod annotation, as anAnnotationInfoList, or the empty list if none. -
.hasParameterAnnotation(String annotationName | Class<? extends Annotation> annotationClass)returnstrueif the method has a parameter with the given annotation.
-
-
Dependencies:
-
.getClassDependencies()returns aClassInfoListof 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 itsthrowsclause. UnlikeClassInfo#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.
-
Holds information about one parameter of a method. Obtained by calling MethodInfo#getParameterInfo().
-
Properties:
-
.getName()returns the method parameter name as aString.💡 Method parameter names are only available if you invoked
javacwith the-parametersswitch. In Eclipse this setting is Project Properties > Java Compiler > Store information about method parameters (usable via reflection). -
.getMethodInfo()returns theMethodInfoobject 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 anint. -
.getModifiersString()returns the method parameter modifiers as aString, e.g."final". -
.isFinal()returnstrueif the method parameter is final. -
.isSynthetic()returnstrueif the method parameter is synthetic. -
.isMandated()returnstrueif the method parameter is mandated. -
.isVarArgs()returnstrueif this is the variadic parameter of a variadic method, i.e. the parameter declared asT.... The type of a variadic parameter is the array typeT[], 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 aTypeSignature, 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 aTypeSignature. -
.getTypeSignatureOrTypeDescriptor()returns the type signature of the method parameter, if available, otherwise returns the type descriptor.
-
-
Annotations:
-
.getAllAnnotationInfo()/.getDirectAnnotationInfo()returns anAnnotationInfoListofAnnotationInfoobjects for annotations on the method parameter, or the empty list if none. -
.hasAnnotation(String annotationName | Class<? extends Annotation> annotationClass)returnstrueif the method parameter has the named annotation. -
.getAllAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass)/.getDirectAnnotationInfo(...)returns theAnnotationInfoobject for the given non-@Repeatablemethod parameter annotation, or null if none. -
.getAllAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass)/.getDirectAnnotationInfoRepeatable(...)returns theAnnotationInfoobjects for the given@Repeatablemethod parameter annotation, as anAnnotationInfoList, or the empty list if none.
-
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 unlessClassGraph#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 unlessClassGraph#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
|
Extends ArrayList<MethodInfo> -- unmodifiable when it came from a scan -- with the following convenience methods:
-
.asMap()returns theMethodInfoListas aMap<String, MethodInfoList>mapping the method name to aMethodInfoListof 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 aList<String>. -
.getAsStrings()returns a list of the result of calling.toString()on eachMethodInfoobject in this list, producing aList<String>ofStringrepresentations 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)returnstrueif one or more methods of the given name is contained in this list. -
.get(String methodName)returns a newMethodInfoListconsisting of theMethodInfoobjects 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 singleMethodInfoobject 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. ThrowsIllegalArgumentExceptionif there are two or more methods with the requested name. -
.filter(Predicate<MethodInfo> filter)returns aMethodInfoListthat is a subset of the original list, obtained by applying the given predicate to eachMethodInfoobject in the list, e.g..filter(MethodInfo::isConstructor).