CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 294
MethodInfo API
See also the ClassGraph API overview.
-
MethodInfo
MethodParameterInfo
- 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 theClassInfo
object for the declaring class (i.e. the class that declares the method). -
.getModifiers()
returns the method modifier bits as anint
. -
.getModifiersStr()
returns the method modifiers as aString
(e.g."public static"
). -
.isConstructor()
returnstrue
if the method is a constructor. -
.isPublic()
returnstrue
if the method is public. -
.isProtected()
returnstrue
if the method is protected. -
.isPrivate()
returnstrue
if the method is private. -
.isAbstract()
returnstrue
if the method is abstract. -
.isStatic()
returnstrue
if the method is static. -
.isFinal()
returnstrue
if the method is final. -
.isSynchronized()
returnstrue
if the method is synchronized. -
.isSynthetic()
returnstrue
if the method is synthetic. -
.isBridge()
returnstrue
if the method is a bridge method. -
.isStrict()
returnstrue
if the method is strict. -
.isVarArgs()
returnstrue
if the method is a varargs method. -
.isNative()
returnstrue
if the method is a native method. -
.hasBody()
returnstrue
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()
returnstrue
if the method is a default method on an interface. -
.toStringWithSimpleNames()
returns a simpler rendering of the method than thanMethodInfo#toString()
, by using only the simple name of any classes or annotations in the method's type.
-
-
Parameters:
-
.getParameterInfo()
returns an array ofMethodParameterInfo
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 aMethodTypeSignature
, 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 aMethodTypeSignature
. -
.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 aTypeSignature
. -
.getParameterInfo()[parameterIndex].getTypeSignatureOrTypeDescriptor()
returns the type of the parameter at indexparameterIndex
, 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, ornull
if none. For example, for the methodvoid <T> doSomething()
, the generic type parameter isT
. The type parameters are returned as aList
ofTypeParameter
objects.
-
-
Annotations:
-
.getAnnotationInfo()
returns the annotations on this method as anAnnotationInfoList
. -
.hasAnnotation(String annotationName | Class<? extends Annotation> annotationClass)
returnstrue
if the method has the given annotation. -
.getAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass)
returns theAnnotationInfo
object for the given non-@Repeatable
method annotation, or null if none. -
.getAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass)
returns theAnnotationInfo
object for the given@Repeatable
method annotation, as anAnnotationInfoList
, or the empty list if none. -
.hasParameterAnnotation(String annotationName | Class<? extends Annotation> annotationClass)
returnstrue
if the method has a parameter with the given annotation.
-
-
Classloading / reflection:
-
.loadClassAndGetMethod()
loads the defining class, then gets thejava.lang.reflection.Method
object for this non-constructor method. Only call this ifMethodInfo#isConstructor()
is false, otherwiseIllegalArgumentException
will be thrown. -
.loadClassAndGetConstructor()
loads the defining class, then gets thejava.lang.reflection.Constructor<?>
object for this constructor method. Only call this ifMethodInfo#isConstructor()
is true, otherwiseIllegalArgumentException
will be thrown.
-
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
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 anint
. -
.getModifiersStr()
returns the method parameter modifiers as aString
, e.g."final"
. -
.isFinal()
returnstrue
if the method parameter is final. -
.isSynthetic()
returnstrue
if the method parameter is synthetic. -
.isMandated()
returnstrue
if the method parameter is mandated.
-
-
Type:
-
.getTypeSignature()
returns the type signature of the method parameter as aTypeSignature
, 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 aTypeSignature
. -
.getTypeSignatureOrTypeDescriptor()
returns the type signature of the method parameter, if available, otherwise returns the type descriptor.
-
-
Annotations:
-
.getAnnotationInfo()
returns anAnnotationInfoList
ofAnnotationInfo
objects for annotations on the method parameter, or the empty list if none. -
.hasAnnotation(String annotationName | Class<? extends Annotation> annotationClass)
returnstrue
if the method parameter has the named annotation. -
.getAnnotationInfo(String annotationName | Class<? extends Annotation> annotationClass)
returns theAnnotationInfo
object for the given non-@Repeatable
method parameter annotation, or null if none. -
.getAnnotationInfoRepeatable(String annotationName | Class<? extends Annotation> annotationClass)
returns theAnnotationInfo
object for the given@Repeatable
method 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 .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
|
Extends ArrayList<MethodInfo>
with the following convenience methods:
-
.asMap()
returns theMethodInfoList
as aMap<String, MethodInfoList>
mapping the method name to aMethodInfoList
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 aList<String>
. -
.getAsStrings()
returns a list of the result of calling.toString()
on eachMethodInfo
object in this list, producing aList<string>
ofString
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)
returnstrue
if one or more methods of the given name is contained in this list. -
.get(String methodName)
returns a newMethodInfoList
consisting of theMethodInfo
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 singleMethodInfo
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. ThrowsIllegalArgumentException
if there are two or more methods with the requested name. -
.filter(MethodInfoFilter filter)
returns aMethodInfoList
that is a subset of the original list, obtained by applying the given filter predicate to eachMethodInfo
object in the list.-
MethodInfoFilter
is aFunctionalInterface
with the single abstract methodboolean accept(MethodInfo methodInfo)
.
-