public class MethodParameter
extends java.lang.Object
Method
or Constructor
plus a parameter index and a nested type index for a declared generic
type. Useful as a specification object to pass along.
As of 4.2, there is a SynthesizingMethodParameter
subclass available which synthesizes annotations with attribute aliases. That subclass is used
for web and message endpoint processing, in particular.
SynthesizingMethodParameter
Modifier and Type | Class and Description |
---|---|
private static class |
MethodParameter.KotlinDelegate
Inner class to avoid a hard dependency on Kotlin at runtime.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.reflect.Constructor<?> |
constructor |
private java.lang.Class<?> |
containingClass |
private java.lang.reflect.Type |
genericParameterType |
private static boolean |
kotlinPresent |
private java.lang.reflect.Method |
method |
private MethodParameter |
nestedMethodParameter |
private int |
nestingLevel |
private java.lang.reflect.Parameter |
parameter |
private java.lang.annotation.Annotation[] |
parameterAnnotations |
private int |
parameterIndex |
private java.lang.String |
parameterName |
private ParameterNameDiscoverer |
parameterNameDiscoverer |
private java.lang.Class<?> |
parameterType |
(package private) java.util.Map<java.lang.Integer,java.lang.Integer> |
typeIndexesPerLevel
Map from Integer level to Integer type index
|
Constructor and Description |
---|
MethodParameter(java.lang.reflect.Constructor<?> constructor,
int parameterIndex)
Create a new MethodParameter for the given constructor, with nesting level 1.
|
MethodParameter(java.lang.reflect.Constructor<?> constructor,
int parameterIndex,
int nestingLevel)
Create a new MethodParameter for the given constructor.
|
MethodParameter(java.lang.reflect.Method method,
int parameterIndex)
Create a new
MethodParameter for the given method, with nesting level 1. |
MethodParameter(java.lang.reflect.Method method,
int parameterIndex,
int nestingLevel)
Create a new
MethodParameter for the given method. |
MethodParameter(MethodParameter original)
Copy constructor, resulting in an independent MethodParameter object
based on the same metadata and cache state that the original object was in.
|
Modifier and Type | Method and Description |
---|---|
protected <A extends java.lang.annotation.Annotation> |
adaptAnnotation(A annotation)
A template method to post-process a given annotation instance before
returning it to the caller.
|
protected java.lang.annotation.Annotation[] |
adaptAnnotationArray(java.lang.annotation.Annotation[] annotations)
A template method to post-process a given annotation array before
returning it to the caller.
|
MethodParameter |
clone() |
void |
decreaseNestingLevel()
Decrease this parameter's nesting level.
|
boolean |
equals(java.lang.Object other) |
protected static int |
findParameterIndex(java.lang.reflect.Parameter parameter) |
static MethodParameter |
forExecutable(java.lang.reflect.Executable executable,
int parameterIndex)
Create a new MethodParameter for the given method or constructor.
|
static MethodParameter |
forMethodOrConstructor(java.lang.Object methodOrConstructor,
int parameterIndex)
Deprecated.
as of 5.0, in favor of
forExecutable(java.lang.reflect.Executable, int) |
static MethodParameter |
forParameter(java.lang.reflect.Parameter parameter)
Create a new MethodParameter for the given parameter descriptor.
|
java.lang.reflect.AnnotatedElement |
getAnnotatedElement()
Return the wrapped annotated element.
|
java.lang.reflect.Constructor<?> |
getConstructor()
Return the wrapped Constructor, if any.
|
java.lang.Class<?> |
getContainingClass() |
java.lang.Class<?> |
getDeclaringClass()
Return the class that declares the underlying Method or Constructor.
|
java.lang.reflect.Executable |
getExecutable()
Return the wrapped executable.
|
java.lang.reflect.Type |
getGenericParameterType()
Return the generic type of the method/constructor parameter.
|
java.lang.reflect.Member |
getMember()
Return the wrapped member.
|
java.lang.reflect.Method |
getMethod()
Return the wrapped Method, if any.
|
<A extends java.lang.annotation.Annotation> |
getMethodAnnotation(java.lang.Class<A> annotationType)
Return the method/constructor annotation of the given type, if available.
|
java.lang.annotation.Annotation[] |
getMethodAnnotations()
Return the annotations associated with the target method/constructor itself.
|
java.lang.reflect.Type |
getNestedGenericParameterType()
Return the nested generic type of the method/constructor parameter.
|
java.lang.Class<?> |
getNestedParameterType()
Return the nested type of the method/constructor parameter.
|
int |
getNestingLevel()
Return the nesting level of the target type
(typically 1; e.g.
|
java.lang.reflect.Parameter |
getParameter()
Return the
Parameter descriptor for method/constructor parameter. |
<A extends java.lang.annotation.Annotation> |
getParameterAnnotation(java.lang.Class<A> annotationType)
Return the parameter annotation of the given type, if available.
|
java.lang.annotation.Annotation[] |
getParameterAnnotations()
Return the annotations associated with the specific method/constructor parameter.
|
int |
getParameterIndex()
Return the index of the method/constructor parameter.
|
java.lang.String |
getParameterName()
Return the name of the method/constructor parameter.
|
java.lang.Class<?> |
getParameterType()
Return the type of the method/constructor parameter.
|
private java.util.Map<java.lang.Integer,java.lang.Integer> |
getTypeIndexesPerLevel()
Obtain the (lazily constructed) type-indexes-per-level Map.
|
java.lang.Integer |
getTypeIndexForCurrentLevel()
Return the type index for the current nesting level.
|
java.lang.Integer |
getTypeIndexForLevel(int nestingLevel)
Return the type index for the specified nesting level.
|
int |
hashCode() |
<A extends java.lang.annotation.Annotation> |
hasMethodAnnotation(java.lang.Class<A> annotationType)
Return whether the method/constructor is annotated with the given type.
|
private boolean |
hasNullableAnnotation()
Check whether this method parameter is annotated with any variant of a
Nullable annotation, e.g. |
<A extends java.lang.annotation.Annotation> |
hasParameterAnnotation(java.lang.Class<A> annotationType)
Return whether the parameter is declared with the given annotation type.
|
boolean |
hasParameterAnnotations()
Return
true if the parameter has at least one annotation,
false if it has none. |
void |
increaseNestingLevel()
Increase this parameter's nesting level.
|
void |
initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDiscoverer)
Initialize parameter name discovery for this method parameter.
|
boolean |
isOptional()
Return whether this method indicates a parameter which is not required:
either in the form of Java 8's
Optional , any variant
of a parameter-level Nullable annotation (such as from JSR-305
or the FindBugs set of annotations), or a language-level nullable type
declaration in Kotlin. |
MethodParameter |
nested()
Return a variant of this
MethodParameter which points to the
same parameter but one nesting level deeper. |
MethodParameter |
nestedIfOptional()
Return a variant of this
MethodParameter which points to
the same parameter but one nesting level deeper in case of a
Optional declaration. |
(package private) void |
setContainingClass(java.lang.Class<?> containingClass)
Set a containing class to resolve the parameter type against.
|
(package private) void |
setParameterType(java.lang.Class<?> parameterType)
Set a resolved (generic) parameter type.
|
void |
setTypeIndexForCurrentLevel(int typeIndex)
Set the type index for the current nesting level.
|
java.lang.String |
toString() |
private static int |
validateIndex(java.lang.reflect.Executable executable,
int parameterIndex) |
private static final boolean kotlinPresent
private final java.lang.reflect.Method method
private final java.lang.reflect.Constructor<?> constructor
private final int parameterIndex
private volatile java.lang.reflect.Parameter parameter
private int nestingLevel
java.util.Map<java.lang.Integer,java.lang.Integer> typeIndexesPerLevel
private volatile java.lang.Class<?> containingClass
private volatile java.lang.Class<?> parameterType
private volatile java.lang.reflect.Type genericParameterType
private volatile java.lang.annotation.Annotation[] parameterAnnotations
private volatile ParameterNameDiscoverer parameterNameDiscoverer
private volatile java.lang.String parameterName
private volatile MethodParameter nestedMethodParameter
public MethodParameter(java.lang.reflect.Method method, int parameterIndex)
MethodParameter
for the given method, with nesting level 1.method
- the Method to specify a parameter forparameterIndex
- the index of the parameter: -1 for the method
return type; 0 for the first method parameter; 1 for the second method
parameter, etc.public MethodParameter(java.lang.reflect.Method method, int parameterIndex, int nestingLevel)
MethodParameter
for the given method.method
- the Method to specify a parameter forparameterIndex
- the index of the parameter: -1 for the method
return type; 0 for the first method parameter; 1 for the second method
parameter, etc.nestingLevel
- the nesting level of the target type
(typically 1; e.g. in case of a List of Lists, 1 would indicate the
nested List, whereas 2 would indicate the element of the nested List)public MethodParameter(java.lang.reflect.Constructor<?> constructor, int parameterIndex)
constructor
- the Constructor to specify a parameter forparameterIndex
- the index of the parameterpublic MethodParameter(java.lang.reflect.Constructor<?> constructor, int parameterIndex, int nestingLevel)
constructor
- the Constructor to specify a parameter forparameterIndex
- the index of the parameternestingLevel
- the nesting level of the target type
(typically 1; e.g. in case of a List of Lists, 1 would indicate the
nested List, whereas 2 would indicate the element of the nested List)public MethodParameter(MethodParameter original)
original
- the original MethodParameter object to copy frompublic java.lang.reflect.Method getMethod()
Note: Either Method or Constructor is available.
null
if nonepublic java.lang.reflect.Constructor<?> getConstructor()
Note: Either Method or Constructor is available.
null
if nonepublic java.lang.Class<?> getDeclaringClass()
public java.lang.reflect.Member getMember()
public java.lang.reflect.AnnotatedElement getAnnotatedElement()
Note: This method exposes the annotations declared on the method/constructor itself (i.e. at the method/constructor level, not at the parameter level).
public java.lang.reflect.Executable getExecutable()
public java.lang.reflect.Parameter getParameter()
Parameter
descriptor for method/constructor parameter.public int getParameterIndex()
public void increaseNestingLevel()
getNestingLevel()
public void decreaseNestingLevel()
getNestingLevel()
public int getNestingLevel()
public void setTypeIndexForCurrentLevel(int typeIndex)
typeIndex
- the corresponding type index
(or null
for the default type index)getNestingLevel()
public java.lang.Integer getTypeIndexForCurrentLevel()
null
if none specified (indicating the default type index)getNestingLevel()
public java.lang.Integer getTypeIndexForLevel(int nestingLevel)
nestingLevel
- the nesting level to checknull
if none specified (indicating the default type index)private java.util.Map<java.lang.Integer,java.lang.Integer> getTypeIndexesPerLevel()
public MethodParameter nested()
MethodParameter
which points to the
same parameter but one nesting level deeper. This is effectively the
same as increaseNestingLevel()
, just with an independent
MethodParameter
object (e.g. in case of the original being cached).public boolean isOptional()
Optional
, any variant
of a parameter-level Nullable
annotation (such as from JSR-305
or the FindBugs set of annotations), or a language-level nullable type
declaration in Kotlin.private boolean hasNullableAnnotation()
Nullable
annotation, e.g. javax.annotation.Nullable
or
edu.umd.cs.findbugs.annotations.Nullable
.public MethodParameter nestedIfOptional()
MethodParameter
which points to
the same parameter but one nesting level deeper in case of a
Optional
declaration.isOptional()
,
nested()
void setContainingClass(java.lang.Class<?> containingClass)
public java.lang.Class<?> getContainingClass()
void setParameterType(java.lang.Class<?> parameterType)
public java.lang.Class<?> getParameterType()
null
)public java.lang.reflect.Type getGenericParameterType()
null
)public java.lang.Class<?> getNestedParameterType()
null
)getNestingLevel()
public java.lang.reflect.Type getNestedGenericParameterType()
null
)getNestingLevel()
public java.lang.annotation.Annotation[] getMethodAnnotations()
public <A extends java.lang.annotation.Annotation> A getMethodAnnotation(java.lang.Class<A> annotationType)
annotationType
- the annotation type to look fornull
if not foundpublic <A extends java.lang.annotation.Annotation> boolean hasMethodAnnotation(java.lang.Class<A> annotationType)
annotationType
- the annotation type to look forgetMethodAnnotation(Class)
public java.lang.annotation.Annotation[] getParameterAnnotations()
public boolean hasParameterAnnotations()
true
if the parameter has at least one annotation,
false
if it has none.getParameterAnnotations()
public <A extends java.lang.annotation.Annotation> A getParameterAnnotation(java.lang.Class<A> annotationType)
annotationType
- the annotation type to look fornull
if not foundpublic <A extends java.lang.annotation.Annotation> boolean hasParameterAnnotation(java.lang.Class<A> annotationType)
annotationType
- the annotation type to look forgetParameterAnnotation(Class)
public void initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDiscoverer)
This method does not actually try to retrieve the parameter name at
this point; it just allows discovery to happen when the application calls
getParameterName()
(if ever).
public java.lang.String getParameterName()
null
if no
parameter name metadata is contained in the class file or no
ParameterNameDiscoverer
has been set to begin with)protected <A extends java.lang.annotation.Annotation> A adaptAnnotation(A annotation)
The default implementation simply returns the given annotation as-is.
annotation
- the annotation about to be returnedprotected java.lang.annotation.Annotation[] adaptAnnotationArray(java.lang.annotation.Annotation[] annotations)
The default implementation simply returns the given annotation array as-is.
annotations
- the annotation array about to be returnedpublic boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public MethodParameter clone()
clone
in class java.lang.Object
@Deprecated public static MethodParameter forMethodOrConstructor(java.lang.Object methodOrConstructor, int parameterIndex)
forExecutable(java.lang.reflect.Executable, int)
This is a convenience factory method for scenarios where a Method or Constructor reference is treated in a generic fashion.
methodOrConstructor
- the Method or Constructor to specify a parameter forparameterIndex
- the index of the parameterpublic static MethodParameter forExecutable(java.lang.reflect.Executable executable, int parameterIndex)
This is a convenience factory method for scenarios where a Method or Constructor reference is treated in a generic fashion.
executable
- the Method or Constructor to specify a parameter forparameterIndex
- the index of the parameterpublic static MethodParameter forParameter(java.lang.reflect.Parameter parameter)
This is a convenience factory method for scenarios where a
Java 8 Parameter
descriptor is already available.
parameter
- the parameter descriptorprotected static int findParameterIndex(java.lang.reflect.Parameter parameter)
private static int validateIndex(java.lang.reflect.Executable executable, int parameterIndex)