Package org.jboss.jandex
Class MethodInfo
- java.lang.Object
-
- org.jboss.jandex.MethodInfo
-
- All Implemented Interfaces:
AnnotationTarget
public final class MethodInfo extends Object implements AnnotationTarget
Represents a Java method, constructor, or static initializer.Thread-Safety
This class is immutable and can be shared between threads without safe publication.- Author:
- Jason T. Greene
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.jandex.AnnotationTarget
AnnotationTarget.Kind
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description AnnotationInstance
annotation(DotName name)
Retrieves an annotation instance declared on this method, it parameters, or any type within the signature of the method, by the name of the annotation.List<AnnotationInstance>
annotations()
Returns the annotation instances declared on this method.List<AnnotationInstance>
annotations(DotName name)
Retrieves annotations declared on this method, by the name of the annotation.List<AnnotationInstance>
annotationsWithRepeatable(DotName name, IndexView index)
Retrieves annotations declared on this method, by the name of the annotation.Type[]
args()
Deprecated.ClassInfo
asClass()
Casts and returns this target as aClassInfo
if it is of kindCLASS
FieldInfo
asField()
Casts and returns this target as aFieldInfo
if it is of kindFIELD
MethodInfo
asMethod()
Casts and returns this target as aMethodInfo
if it is of kindMETHOD
MethodParameterInfo
asMethodParameter()
Casts and returns this target as aMethodParameterInfo
if it is of kindMETHOD_PARAMETER
RecordComponentInfo
asRecordComponent()
Casts and returns this target as aRecordComponentInfo
if it is of kindRECORD_COMPONENT
TypeTarget
asType()
Casts and returns this target as aTypeTarget
if it is of kindTYPE
static MethodInfo
create(ClassInfo clazz, String name, String[] parameterNames, Type[] args, Type returnType, short flags, TypeVariable[] typeParameters, Type[] exceptions)
Construct a new mock Method instance.static MethodInfo
create(ClassInfo clazz, String name, Type[] args, Type returnType, short flags)
Construct a new mock Method instance.static MethodInfo
create(ClassInfo clazz, String name, Type[] args, Type returnType, short flags, TypeVariable[] typeParameters, Type[] exceptions)
Construct a new mock Method instance.ClassInfo
declaringClass()
Returns the class that declared this methodAnnotationValue
defaultValue()
Returns the default annotation value if this method represents an annotation member with a default value.boolean
equals(Object o)
List<Type>
exceptions()
Returns the list of throwable classes declared to be thrown by this method.short
flags()
Returns the access fields of this method.boolean
hasAnnotation(DotName name)
Returns whether or not the annotation instance with the given name occurs on this method, its parameters or its signatureint
hashCode()
boolean
isSynthetic()
AnnotationTarget.Kind
kind()
Returns the kind of object this target represents.String
name()
Returns the name of this methodString
parameterName(int i)
Returns the name of the given parameter.List<Type>
parameters()
Returns a list containing the types of all parameters declared on this method, in parameter order.Type
receiverType()
Returns the receiver type of this method (a declaration of the "this" reference), if specified.Type
returnType()
Returns this method's return parameter type.String
toString()
Returns a string representation describing this field.List<TypeVariable>
typeParameters()
Returns the generic type parameters defined by this method.
-
-
-
Method Detail
-
create
public static MethodInfo create(ClassInfo clazz, String name, Type[] args, Type returnType, short flags)
Construct a new mock Method instance.- Parameters:
clazz
- the class declaring the fieldname
- the name of the fieldargs
- a read only array containing the types of each parameter in parameter orderreturnType
- the return value typeflags
- the method attributes- Returns:
- a mock method
-
create
public static MethodInfo create(ClassInfo clazz, String name, Type[] args, Type returnType, short flags, TypeVariable[] typeParameters, Type[] exceptions)
Construct a new mock Method instance.- Parameters:
clazz
- the class declaring the fieldname
- the name of the fieldargs
- a read only array containing the types of each parameter in parameter orderreturnType
- the return value typeflags
- the method attributestypeParameters
- the generic type parameters for this methodexceptions
- the exceptions declared as thrown by this method- Returns:
- a mock method
- Since:
- 2.1
-
create
public static MethodInfo create(ClassInfo clazz, String name, String[] parameterNames, Type[] args, Type returnType, short flags, TypeVariable[] typeParameters, Type[] exceptions)
Construct a new mock Method instance.- Parameters:
clazz
- the class declaring the fieldname
- the name of the fieldparameterNames
- the names of the method parameterargs
- a read only array containing the types of each parameter in parameter orderreturnType
- the return value typeflags
- the method attributestypeParameters
- the generic type parameters for this methodexceptions
- the exceptions declared as thrown by this method- Returns:
- a mock method
- Since:
- 2.2
-
name
public final String name()
Returns the name of this method- Returns:
- the name of the method
-
parameterName
public final String parameterName(int i)
Returns the name of the given parameter.- Parameters:
i
- the parameter index- Returns:
- the name of the given parameter, or null.
-
kind
public final AnnotationTarget.Kind kind()
Description copied from interface:AnnotationTarget
Returns the kind of object this target represents.- Specified by:
kind
in interfaceAnnotationTarget
- Returns:
- the target kind.
-
declaringClass
public final ClassInfo declaringClass()
Returns the class that declared this method- Returns:
- the declaring class
-
args
@Deprecated public final Type[] args()
Deprecated.Returns an array containing parameter types in parameter order. This method performs a defensive array copy per call, and should be avoided. Instead theparameters()
method should be used.- Returns:
- an array copy contain parameter types
-
parameters
public final List<Type> parameters()
Returns a list containing the types of all parameters declared on this method, in parameter order. This method may return an empty list, but never null.- Returns:
- all parameter types on this method
-
returnType
public final Type returnType()
Returns this method's return parameter type. If this method has a void return, a special void type is returned. This method will never return null.- Returns:
- the type of this method's return value
-
receiverType
public final Type receiverType()
Returns the receiver type of this method (a declaration of the "this" reference), if specified. This is used to convey annotations on the "this" instance.- Returns:
- the receiver type of this method
-
exceptions
public final List<Type> exceptions()
Returns the list of throwable classes declared to be thrown by this method. This method may return an empty list, but never null.- Returns:
- the list of throwable classes thrown by this method
-
typeParameters
public final List<TypeVariable> typeParameters()
Returns the generic type parameters defined by this method. This list will contain resolved type variables which may reference other type parameters, including those declared by the enclosing class of this method.- Returns:
- the list of generic type parameters for this method, or an empty list if none
-
annotations
public final List<AnnotationInstance> annotations()
Returns the annotation instances declared on this method. This includes annotations which are defined against method parameters, as well as type annotations declared on any usage within the method signature. Thetarget()
of the returned annotation instances may be used to determine the exact location of the respective annotation instance.The following is a non-exhaustive list of examples of annotations returned by this method:
@MyMethodAnnotation public void foo() {...} public void foo(@MyParamAnnotation int param) {...} public void foo(List<@MyTypeAnnotation> list) {...} public <@AnotherTypeAnnotation T> void foo(T t) {...}
- Returns:
- the annotation instances declared on this method or its parameters, or an empty list if none
-
annotation
public final AnnotationInstance annotation(DotName name)
Retrieves an annotation instance declared on this method, it parameters, or any type within the signature of the method, by the name of the annotation. If an annotation by that name is not present, null will be returned.The following is a non-exhaustive list of examples of annotations returned by this method:
@MyMethodAnnotation public void foo() {...} public void foo(@MyParamAnnotation int param) {...} public void foo(List<@MyTypeAnnotation> list) {...} public <@AnotherTypeAnnotation T> void foo(T t) {...}
- Parameters:
name
- the name of the annotation to locate within the method- Returns:
- the annotation if found, otherwise, null
-
annotationsWithRepeatable
public final List<AnnotationInstance> annotationsWithRepeatable(DotName name, IndexView index)
Retrieves annotations declared on this method, by the name of the annotation. This includes annotations which are defined against method parameters, as well as type annotations declared on any usage within the method signature. Thetarget()
of the returned annotation instances may be used to determine the exact location of the respective annotation instance. If the specified annotation is repeatable (JLS 9.6), the result also contains all values from the container annotation instances. In this case, theAnnotationInstance.target()
returns the target of the container annotation instance.- Parameters:
name
- the name of the annotationindex
- the index used to obtain the annotation class- Returns:
- the annotation instances declared on this method or its parameters, or an empty list if none
- Throws:
IllegalArgumentException
- If the index does not contain the annotation definition or if it does not represent an annotation type
-
annotations
public final List<AnnotationInstance> annotations(DotName name)
Retrieves annotations declared on this method, by the name of the annotation. This includes annotations which are defined against method parameters, as well as type annotations declared on any usage within the method signature. Thetarget()
of the returned annotation instances may be used to determine the exact location of the respective annotation instance.- Parameters:
name
-- Returns:
- the annotation instances declared on this method or its parameters, or an empty list if none
-
hasAnnotation
public final boolean hasAnnotation(DotName name)
Returns whether or not the annotation instance with the given name occurs on this method, its parameters or its signature- Parameters:
name
- the name of the annotation to look for- Returns:
- true if the annotation is present, false otherwise
- See Also:
annotations()
,annotation(DotName)
-
defaultValue
public AnnotationValue defaultValue()
Returns the default annotation value if this method represents an annotation member with a default value. Otherwise null is returned- Returns:
- default annotation value if available, otherwise null
- Since:
- 2.1
-
flags
public final short flags()
Returns the access fields of this method.Modifier
can be used on this value.- Returns:
- the access flags of this method
-
isSynthetic
public final boolean isSynthetic()
- Returns:
true
if this method is a synthetic method
-
toString
public String toString()
Returns a string representation describing this field. It is similar although not necessarily equivalent to a Java source code expression representing this field.
-
asClass
public final ClassInfo asClass()
Description copied from interface:AnnotationTarget
Casts and returns this target as aClassInfo
if it is of kindCLASS
- Specified by:
asClass
in interfaceAnnotationTarget
- Returns:
- this instance cast to a class
-
asField
public final FieldInfo asField()
Description copied from interface:AnnotationTarget
Casts and returns this target as aFieldInfo
if it is of kindFIELD
- Specified by:
asField
in interfaceAnnotationTarget
- Returns:
- this instance cast to a field
-
asMethod
public final MethodInfo asMethod()
Description copied from interface:AnnotationTarget
Casts and returns this target as aMethodInfo
if it is of kindMETHOD
- Specified by:
asMethod
in interfaceAnnotationTarget
- Returns:
- this instance cast to a method
-
asMethodParameter
public final MethodParameterInfo asMethodParameter()
Description copied from interface:AnnotationTarget
Casts and returns this target as aMethodParameterInfo
if it is of kindMETHOD_PARAMETER
- Specified by:
asMethodParameter
in interfaceAnnotationTarget
- Returns:
- this instance cast to a method parameter
-
asType
public final TypeTarget asType()
Description copied from interface:AnnotationTarget
Casts and returns this target as aTypeTarget
if it is of kindTYPE
- Specified by:
asType
in interfaceAnnotationTarget
- Returns:
- this instance cast to a type target
-
asRecordComponent
public RecordComponentInfo asRecordComponent()
Description copied from interface:AnnotationTarget
Casts and returns this target as aRecordComponentInfo
if it is of kindRECORD_COMPONENT
- Specified by:
asRecordComponent
in interfaceAnnotationTarget
- Returns:
- this instance cast to a record component
-
-