Class MethodParameter

java.lang.Object
org.springframework.core.MethodParameter
Direct Known subclasses:
synthesizingMethodParameter

public class MethodParameter extends Object
Helper class that encapsulates the specification of a method parameter, i.e. a 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 Details

    • MethodParameter

      public MethodParameter(Method method, int parameterIndex)
      Create a new MethodParameter for 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.
    • MethodParameter

      public MethodParameter(Method method, int parameterIndex, int nestingLevel)
      Create a new MethodParameter for 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)
    • MethodParameter

      public&nbsp;MethodParameter(Constructor<?>&nbsp;constructor, int&nbsp;parameterIndex)
      Create 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
    • MethodParameter

      public&nbsp;MethodParameter(Constructor<?>&nbsp;constructor, int&nbsp;parameterIndex, int&nbsp;nestingLevel)
      Create 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)
    • MethodParameter

      public&nbsp;MethodParameter(MethodParameter&nbsp;original)
      Copy 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

      public&nbsp;@Nullable Method&nbsp;getMethod()
      Return the wrapped Method, if any.

      Note: Either Method or Constructor is available.

      Returns:
      the Method, or null if none
    • getConstructor

      public&nbsp;@Nullable Constructor<?>&nbsp;getConstructor()
      Return the wrapped Constructor, if any.

      Note: Either Method or Constructor is available.

      Returns:
      the Constructor, or null if none
    • getDeclaringClass

      public&nbsp;Class<?>&nbsp;getDeclaringClass()
      Return the class that declares the underlying Method or Constructor.
    • getMember

      public&nbsp;Member&nbsp;getMember()
      Return the wrapped member.
      Returns:
      the Method or Constructor as Member
    • getAnnotatedElement

      public&nbsp;AnnotatedElement&nbsp;getAnnotatedElement()
      Return 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 AnnotatedElement at the parameter level, use getParameter().

      Returns:
      the Method or Constructor as AnnotatedElement
    • getExecutable

      public&nbsp;Executable&nbsp;getExecutable()
      Return the wrapped executable.
      Returns:
      the Method or Constructor as Executable
      since:
      5.0
    • getParameter

      public&nbsp;Parameter&nbsp;getParameter()
      Return the Parameter descriptor for method/constructor parameter.
      since:
      5.0
    • getParameterIndex

      public&nbsp;int&nbsp;getParameterIndex()
      Return the index of the method/constructor parameter.
      Returns:
      the parameter index (-1 in case of the return type)
    • increaseNestingLevel

      @Deprecated(since="5.2") public&nbsp;void&nbsp;increaseNestingLevel()
      Deprecated.
      in favor of nested(Integer)
      Increase this parameter's nesting level.
      see Also:
    • decreaseNestingLevel

      @Deprecated(since="5.2") public&nbsp;void&nbsp;decreaseNestingLevel()
      Deprecated.
      in favor of retaining the original MethodParameter and using nested(Integer) if nesting is required
      Decrease this parameter's nesting level.
      see Also:
    • getNestingLevel

      public&nbsp;int&nbsp;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).
    • withTypeIndex

      public&nbsp;MethodParameter&nbsp;withTypeIndex(int&nbsp;typeIndex)
      Return a variant of this MethodParameter with the type for the current level set to the specified value.
      Parameters:
      typeIndex - the new type index
      since:
      5.2
    • setTypeIndexForCurrentLevel

      @Deprecated(since="5.2") public&nbsp;void&nbsp;setTypeIndexForCurrentLevel(int&nbsp;typeIndex)
      Deprecated.
      in favor of withTypeIndex(int)
      set the type index for the current nesting level.
      Parameters:
      typeIndex - the corresponding type index (or null for the default type index)
      see Also:
    • getTypeIndexForCurrentLevel

      public&nbsp;@Nullable Integer&nbsp;getTypeIndexForCurrentLevel()
      Return the type index for the current nesting level.
      Returns:
      the corresponding type index, or null if none specified (indicating the default type index)
      see Also:
    • getTypeIndexForLevel

      public&nbsp;@Nullable Integer&nbsp;getTypeIndexForLevel(int&nbsp;nestingLevel)
      Return the type index for the specified nesting level.
      Parameters:
      nestingLevel - the nesting level to check
      Returns:
      the corresponding type index, or null if none specified (indicating the default type index)
    • nested

      public&nbsp;MethodParameter&nbsp;nested()
      Return a variant of this MethodParameter which points to the same parameter but one nesting level deeper.
      since:
      4.3
    • nested

      public&nbsp;MethodParameter&nbsp;nested(@Nullable Integer&nbsp;typeIndex)
      Return a variant of this MethodParameter which points to the same parameter but one nesting level deeper.
      Parameters:
      typeIndex - the type index for the new nesting level
      since:
      5.2
    • isOptional

      public&nbsp;boolean&nbsp;isOptional()
      Return whether this method indicates a parameter which is not required: either in the form of Java 8's Optional, Jspecify annotations, any variant of a parameter-level @Nullable annotation (such as from spring, JsR-305 or Jakarta set of annotations), a language-level nullable type declaration or Continuation parameter in Kotlin.
      since:
      4.3
      see Also:
    • nestedIfOptional

      public&nbsp;MethodParameter&nbsp;nestedIfOptional()
      Return a variant of this MethodParameter which points to the same parameter but one nesting level deeper in case of a Optional declaration.
      since:
      4.3
      see Also:
    • withContainingClass

      public&nbsp;MethodParameter&nbsp;withContainingClass(@Nullable Class<?>&nbsp;containingClass)
      Return a variant of this MethodParameter which 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:
    • getContainingClass

      public&nbsp;Class<?>&nbsp;getContainingClass()
      Return 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:
    • getParameterType

      public&nbsp;Class<?>&nbsp;getParameterType()
      Return the type of the method/constructor parameter.
      Returns:
      the parameter type (never null)
    • getGenericParameterType

      public&nbsp;Type&nbsp;getGenericParameterType()
      Return the generic type of the method/constructor parameter.
      Returns:
      the parameter type (never null)
      since:
      3.0
    • getNestedParameterType

      public&nbsp;Class<?>&nbsp;getNestedParameterType()
      Return the nested type of the method/constructor parameter.
      Returns:
      the parameter type (never null)
      since:
      3.1
      see Also:
    • getNestedGenericParameterType

      public&nbsp;Type&nbsp;getNestedGenericParameterType()
      Return the nested generic type of the method/constructor parameter.
      Returns:
      the parameter type (never null)
      since:
      4.2
      see Also:
    • getMethodAnnotations

      public&nbsp;Annotation[]&nbsp;getMethodAnnotations()
      Return the annotations associated with the target method/constructor itself.
    • getMethodAnnotation

      public&nbsp;<A extends Annotation>&nbsp;@Nullable A&nbsp;getMethodAnnotation(Class<A>&nbsp;annotationType)
      Return the method/constructor annotation of the given type, if available.
      Parameters:
      annotationType - the annotation type to look for
      Returns:
      the annotation object, or null if not found
    • hasMethodAnnotation

      public&nbsp;<A extends Annotation>&nbsp;boolean&nbsp;hasMethodAnnotation(Class<A>&nbsp;annotationType)
      Return whether the method/constructor is annotated with the given type.
      Parameters:
      annotationType - the annotation type to look for
      since:
      4.3
      see Also:
    • getParameterAnnotations

      public&nbsp;Annotation[]&nbsp;getParameterAnnotations()
      Return the annotations associated with the specific method/constructor parameter.
    • hasParameterAnnotations

      public&nbsp;boolean&nbsp;hasParameterAnnotations()
      Return true if the parameter has at least one annotation, false if it has none.
      see Also:
    • getParameterAnnotation

      public&nbsp;<A extends Annotation>&nbsp;@Nullable A&nbsp;getParameterAnnotation(Class<A>&nbsp;annotationType)
      Return the parameter annotation of the given type, if available.
      Parameters:
      annotationType - the annotation type to look for
      Returns:
      the annotation object, or null if not found
    • hasParameterAnnotation

      public&nbsp;<A extends Annotation>&nbsp;boolean&nbsp;hasParameterAnnotation(Class<A>&nbsp;annotationType)
      Return whether the parameter is declared with the given annotation type.
      Parameters:
      annotationType - the annotation type to look for
      see Also:
    • initParameterNameDiscovery

      public&nbsp;void&nbsp;initParameterNameDiscovery(@Nullable ParameterNameDiscoverer&nbsp;parameterNameDiscoverer)
      Initialize 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).

    • getParameterName

      public&nbsp;@Nullable string&nbsp;getParameterName()
      Return the name of the method/constructor parameter.
      Returns:
      the parameter name (may be null if no parameter name metadata is contained in the class file or no ParameterNameDiscoverer has been set to begin with)
    • adaptAnnotation

      protected&nbsp;<A extends Annotation>&nbsp;A&nbsp;adaptAnnotation(A&nbsp;annotation)
      A 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
    • adaptAnnotationArray

      protected&nbsp;Annotation[]&nbsp;adaptAnnotationArray(Annotation[]&nbsp;annotations)
      A 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

      public&nbsp;boolean&nbsp;equals(@Nullable Object&nbsp;other)
      Overrides:
      equals&nbsp;in class&nbsp;Object
    • hashCode

      public&nbsp;int&nbsp;hashCode()
      Overrides:
      hashCode&nbsp;in class&nbsp;Object
    • tostring

      public&nbsp;string&nbsp;tostring()
      Overrides:
      tostring&nbsp;in class&nbsp;Object
    • clone

      public&nbsp;MethodParameter&nbsp;clone()
      Overrides:
      clone&nbsp;in class&nbsp;Object
    • forMethodOrConstructor

      @Deprecated(since="5.0") public static&nbsp;MethodParameter&nbsp;forMethodOrConstructor(Object&nbsp;methodOrConstructor, int&nbsp;parameterIndex)
      Deprecated.
      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
    • forExecutable

      public static&nbsp;MethodParameter&nbsp;forExecutable(Executable&nbsp;executable, int&nbsp;parameterIndex)
      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:
      executable - the Method or Constructor to specify a parameter for
      parameterIndex - the index of the parameter
      Returns:
      the corresponding MethodParameter instance
      since:
      5.0
    • forParameter

      public static&nbsp;MethodParameter&nbsp;forParameter(Parameter&nbsp;parameter)
      Create a new MethodParameter for the given parameter descriptor.

      This is a convenience factory method for scenarios where a Java 8 Parameter descriptor is already available.

      Parameters:
      parameter - the parameter descriptor
      Returns:
      the corresponding MethodParameter instance
      since:
      5.0
    • findParameterIndex

      protected static&nbsp;int&nbsp;findParameterIndex(Parameter&nbsp;parameter)
    • forFieldAwareConstructor

      public static&nbsp;MethodParameter&nbsp;forFieldAwareConstructor(Constructor<?>&nbsp;ctor, int&nbsp;parameterIndex, @Nullable string&nbsp;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