Class MethodParameter
- Direct Known Subclasses:
- SynthesizingMethodParameter
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.
- Since:
- 2.0
- Author:
- Juergen Hoeller, Rob Harrop, Andy Clement, Sam Brannen, Sebastien Deleuze, Phillip Webb
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionMethodParameter(Constructor<?> constructor, int parameterIndex) Create a new MethodParameter for the given constructor, with nesting level 1.MethodParameter(Constructor<?> constructor, int parameterIndex, int nestingLevel) Create a new MethodParameter for the given constructor.MethodParameter(Method method, int parameterIndex) Create a newMethodParameterfor the given method, with nesting level 1.MethodParameter(Method method, int parameterIndex, int nestingLevel) Create a newMethodParameterfor 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.
- 
Method SummaryModifier and TypeMethodDescriptionprotected <A extends Annotation>
 AadaptAnnotation(A annotation) A template method to post-process a given annotation instance before returning it to the caller.protected Annotation[]adaptAnnotationArray(Annotation[] annotations) A template method to post-process a given annotation array before returning it to the caller.clone()voidDeprecated.booleanprotected static intfindParameterIndex(Parameter parameter) static MethodParameterforExecutable(Executable executable, int parameterIndex) Create a new MethodParameter for the given method or constructor.static MethodParameterforFieldAwareConstructor(Constructor<?> ctor, int parameterIndex, @Nullable String fieldName) Create a new MethodParameter for the given field-aware constructor, for example, on a data class or record type.static MethodParameterforMethodOrConstructor(Object methodOrConstructor, int parameterIndex) Deprecated.in favor offorExecutable(Executable, int)static MethodParameterforParameter(Parameter parameter) Create a new MethodParameter for the given parameter descriptor.Return the wrapped annotated element.Return the wrapped Constructor, if any.Class<?> Return the containing class for this method parameter.Class<?> Return the class that declares the underlying Method or Constructor.Return the wrapped executable.Return the generic type of the method/constructor parameter.Return the wrapped member.Return the wrapped Method, if any.<A extends Annotation>
 @Nullable AgetMethodAnnotation(Class<A> annotationType) Return the method/constructor annotation of the given type, if available.Return the annotations associated with the target method/constructor itself.Return the nested generic type of the method/constructor parameter.Class<?> Return the nested type of the method/constructor parameter.intReturn the nesting level of the target type (typically 1; for example, in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List).Return theParameterdescriptor for method/constructor parameter.<A extends Annotation>
 @Nullable AgetParameterAnnotation(Class<A> annotationType) Return the parameter annotation of the given type, if available.Return the annotations associated with the specific method/constructor parameter.intReturn the index of the method/constructor parameter.Return the name of the method/constructor parameter.Class<?> Return the type of the method/constructor parameter.Return the type index for the current nesting level.getTypeIndexForLevel(int nestingLevel) Return the type index for the specified nesting level.inthashCode()<A extends Annotation>
 booleanhasMethodAnnotation(Class<A> annotationType) Return whether the method/constructor is annotated with the given type.<A extends Annotation>
 booleanhasParameterAnnotation(Class<A> annotationType) Return whether the parameter is declared with the given annotation type.booleanReturntrueif the parameter has at least one annotation,falseif it has none.voidDeprecated.in favor ofnested(Integer)voidinitParameterNameDiscovery(@Nullable ParameterNameDiscoverer parameterNameDiscoverer) Initialize parameter name discovery for this method parameter.booleanReturn whether this method indicates a parameter which is not required: either in the form of Java 8'sOptional, JSpecify annotations, any variant of a parameter-level@Nullableannotation (such as from Spring, JSR-305 or Jakarta set of annotations), a language-level nullable type declaration orContinuationparameter in Kotlin.nested()Return a variant of thisMethodParameterwhich points to the same parameter but one nesting level deeper.Return a variant of thisMethodParameterwhich points to the same parameter but one nesting level deeper.Return a variant of thisMethodParameterwhich points to the same parameter but one nesting level deeper in case of aOptionaldeclaration.voidsetTypeIndexForCurrentLevel(int typeIndex) Deprecated.in favor ofwithTypeIndex(int)toString()withContainingClass(@Nullable Class<?> containingClass) Return a variant of thisMethodParameterwhich refers to the given containing class.withTypeIndex(int typeIndex) Return a variant of thisMethodParameterwith the type for the current level set to the specified value.
- 
Constructor Details- 
MethodParameterCreate a newMethodParameterfor the given method, with nesting level 1.- Parameters:
- method- the Method to specify a parameter for
- parameterIndex- the index of the parameter: -1 for the method return type; 0 for the first method parameter; 1 for the second method parameter, etc.
 
- 
MethodParameterCreate a newMethodParameterfor the given method.- Parameters:
- method- the Method to specify a parameter for
- parameterIndex- 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; for example, in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
 
- 
MethodParameterCreate a new MethodParameter for the given constructor, with nesting level 1.- Parameters:
- constructor- the Constructor to specify a parameter for
- parameterIndex- the index of the parameter
 
- 
MethodParameterCreate a new MethodParameter for the given constructor.- Parameters:
- constructor- the Constructor to specify a parameter for
- parameterIndex- the index of the parameter
- nestingLevel- the nesting level of the target type (typically 1; for example, in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List)
 
- 
MethodParameterCopy constructor, resulting in an independent MethodParameter object based on the same metadata and cache state that the original object was in.- Parameters:
- original- the original MethodParameter object to copy from
 
 
- 
- 
Method Details- 
getMethod
- 
getConstructorReturn the wrapped Constructor, if any.Note: Either Method or Constructor is available. - Returns:
- the Constructor, or nullif none
 
- 
getDeclaringClassReturn the class that declares the underlying Method or Constructor.
- 
getMember
- 
getAnnotatedElementReturn the wrapped annotated element.Note: This method exposes the annotations declared on the method/constructor itself (i.e. at the method/constructor level, not at the parameter level). To get the AnnotatedElementat the parameter level, usegetParameter().- Returns:
- the Method or Constructor as AnnotatedElement
 
- 
getExecutableReturn the wrapped executable.- Returns:
- the Method or Constructor as Executable
- Since:
- 5.0
 
- 
getParameter
- 
getParameterIndexpublic int getParameterIndex()Return the index of the method/constructor parameter.- Returns:
- the parameter index (-1 in case of the return type)
 
- 
increaseNestingLevel
- 
decreaseNestingLevelDeprecated.in favor of retaining the original MethodParameter and usingnested(Integer)if nesting is requiredDecrease this parameter's nesting level.- See Also:
 
- 
getNestingLevelpublic int getNestingLevel()Return the nesting level of the target type (typically 1; for example, in case of a List of Lists, 1 would indicate the nested List, whereas 2 would indicate the element of the nested List).
- 
withTypeIndexReturn a variant of thisMethodParameterwith the type for the current level set to the specified value.- Parameters:
- typeIndex- the new type index
- Since:
- 5.2
 
- 
setTypeIndexForCurrentLevelDeprecated.in favor ofwithTypeIndex(int)Set the type index for the current nesting level.- Parameters:
- typeIndex- the corresponding type index (or- nullfor the default type index)
- See Also:
 
- 
getTypeIndexForCurrentLevel
- 
getTypeIndexForLevel
- 
nestedReturn a variant of thisMethodParameterwhich points to the same parameter but one nesting level deeper.- Since:
- 4.3
 
- 
nestedReturn a variant of thisMethodParameterwhich points to the same parameter but one nesting level deeper.- Parameters:
- typeIndex- the type index for the new nesting level
- Since:
- 5.2
 
- 
isOptionalpublic boolean isOptional()Return whether this method indicates a parameter which is not required: either in the form of Java 8'sOptional, JSpecify annotations, any variant of a parameter-level@Nullableannotation (such as from Spring, JSR-305 or Jakarta set of annotations), a language-level nullable type declaration orContinuationparameter in Kotlin.- Since:
- 4.3
- See Also:
 
- 
nestedIfOptionalReturn a variant of thisMethodParameterwhich points to the same parameter but one nesting level deeper in case of aOptionaldeclaration.- Since:
- 4.3
- See Also:
 
- 
withContainingClassReturn a variant of thisMethodParameterwhich refers to the given containing class.- Parameters:
- containingClass- a specific containing class (potentially a subclass of the declaring class, for example, substituting a type variable)
- Since:
- 5.2
- See Also:
 
- 
getContainingClassReturn the containing class for this method parameter.- Returns:
- a specific containing class (potentially a subclass of the declaring class), or otherwise simply the declaring class itself
- See Also:
 
- 
getParameterTypeReturn the type of the method/constructor parameter.- Returns:
- the parameter type (never null)
 
- 
getGenericParameterTypeReturn the generic type of the method/constructor parameter.- Returns:
- the parameter type (never null)
- Since:
- 3.0
 
- 
getNestedParameterTypeReturn the nested type of the method/constructor parameter.- Returns:
- the parameter type (never null)
- Since:
- 3.1
- See Also:
 
- 
getNestedGenericParameterTypeReturn the nested generic type of the method/constructor parameter.- Returns:
- the parameter type (never null)
- Since:
- 4.2
- See Also:
 
- 
getMethodAnnotationsReturn the annotations associated with the target method/constructor itself.
- 
getMethodAnnotationReturn the method/constructor annotation of the given type, if available.- Parameters:
- annotationType- the annotation type to look for
- Returns:
- the annotation object, or nullif not found
 
- 
hasMethodAnnotationReturn whether the method/constructor is annotated with the given type.- Parameters:
- annotationType- the annotation type to look for
- Since:
- 4.3
- See Also:
 
- 
getParameterAnnotationsReturn the annotations associated with the specific method/constructor parameter.
- 
hasParameterAnnotationspublic boolean hasParameterAnnotations()Returntrueif the parameter has at least one annotation,falseif it has none.- See Also:
 
- 
getParameterAnnotationReturn the parameter annotation of the given type, if available.- Parameters:
- annotationType- the annotation type to look for
- Returns:
- the annotation object, or nullif not found
 
- 
hasParameterAnnotationReturn whether the parameter is declared with the given annotation type.- Parameters:
- annotationType- the annotation type to look for
- See Also:
 
- 
initParameterNameDiscoveryInitialize parameter name discovery for this method parameter.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).
- 
getParameterNameReturn the name of the method/constructor parameter.- Returns:
- the parameter name (may be nullif no parameter name metadata is contained in the class file or noParameterNameDiscovererhas been set to begin with)
 
- 
adaptAnnotationA template method to post-process a given annotation instance before returning it to the caller.The default implementation simply returns the given annotation as-is. - Parameters:
- annotation- the annotation about to be returned
- Returns:
- the post-processed annotation (or simply the original one)
- Since:
- 4.2
 
- 
adaptAnnotationArrayA template method to post-process a given annotation array before returning it to the caller.The default implementation simply returns the given annotation array as-is. - Parameters:
- annotations- the annotation array about to be returned
- Returns:
- the post-processed annotation array (or simply the original one)
- Since:
- 4.2
 
- 
equals
- 
hashCode
- 
toString
- 
clone
- 
forMethodOrConstructor@Deprecated(since="5.0") public static MethodParameter forMethodOrConstructor(Object methodOrConstructor, int parameterIndex) Deprecated.in favor offorExecutable(Executable, int)Create a new MethodParameter for the given method or constructor.This is a convenience factory method for scenarios where a Method or Constructor reference is treated in a generic fashion. - Parameters:
- methodOrConstructor- the Method or Constructor to specify a parameter for
- parameterIndex- the index of the parameter
- Returns:
- the corresponding MethodParameter instance
 
- 
forExecutableCreate a new MethodParameter for the given method or constructor.This is a convenience factory method for scenarios where a Method or Constructor reference is treated in a generic fashion. - Parameters:
- executable- the Method or Constructor to specify a parameter for
- parameterIndex- the index of the parameter
- Returns:
- the corresponding MethodParameter instance
- Since:
- 5.0
 
- 
forParameterCreate a new MethodParameter for the given parameter descriptor.This is a convenience factory method for scenarios where a Java 8 Parameterdescriptor is already available.- Parameters:
- parameter- the parameter descriptor
- Returns:
- the corresponding MethodParameter instance
- Since:
- 5.0
 
- 
findParameterIndex
- 
forFieldAwareConstructorpublic static MethodParameter forFieldAwareConstructor(Constructor<?> ctor, int parameterIndex, @Nullable String fieldName) Create a new MethodParameter for the given field-aware constructor, for example, on a data class or record type.A field-aware method parameter will detect field annotations as well, as long as the field name matches the parameter name. - Parameters:
- ctor- the Constructor to specify a parameter for
- parameterIndex- the index of the parameter
- fieldName- the name of the underlying field, matching the constructor's parameter name
- Returns:
- the corresponding MethodParameter instance
- Since:
- 6.1
 
 
- 
nested(Integer)if nesting is required