类 MethodParameter


  • public class MethodParameter
    extends Object
    封装方法参数规范的帮助程序类

    参考 spring 实现

    • 构造器详细资料

      • MethodParameter

        public MethodParameter​(@NotNull
                               @NotNull Method method,
                               int parameterIndex)
        为给定方法创建一个新的 方法参数,嵌套级别为 1
        参数:
        method - 方法对象
        parameterIndex - 参数的索引:-1 表示方法返回类型;0 表示第一个方法参数;1 表示第二个方法参数,依此类推。
      • MethodParameter

        public MethodParameter​(@NotNull
                               @NotNull Method method,
                               int parameterIndex,
                               int nestingLevel)
        为给定方法创建一个新的 方法参数,嵌套级别为 1
        参数:
        method - 方法对象
        parameterIndex - 参数的索引:-1 表示方法返回类型;0 表示第一个方法参数;1 表示第二个方法参数,依此类推。
        nestingLevel - 目标类型的嵌套级别(通常为 1;例如,在列表列表的情况下,1 表示嵌套列表,而 2 表示嵌套列表的元素)
      • MethodParameter

        public MethodParameter​(Constructor<?> constructor,
                               int parameterIndex)
        为给定构造函数创建一个嵌套级别为 1 的新方法参数。
        参数:
        constructor - 构造函数对象
        parameterIndex - 参数的索引:-1 表示方法返回类型;0 表示第一个方法参数;1 表示第二个方法参数,依此类推。
      • MethodParameter

        public MethodParameter​(@NotNull
                               @NotNull Constructor<?> constructor,
                               int parameterIndex,
                               int nestingLevel)
        Create a new MethodParameter for the given constructor.
        参数:
        constructor - 构造函数对象
        parameterIndex - 参数的索引:-1 表示方法返回类型;0 表示第一个方法参数;1 表示第二个方法参数,依此类推。
        nestingLevel - 目标类型的嵌套级别(通常为 1;例如,在列表列表的情况下,1 表示嵌套列表,而 2 表示嵌套列表的元素)
      • MethodParameter

        public MethodParameter​(@NotNull
                               @NotNull MethodParameter original)
        复制构造函数,从而基于原始对象所在的相同元数据和缓存状态生成独立的 MethodParameter 对象
    • 方法详细资料

      • getMethod

        @Nullable
        public @Nullable Method getMethod()
        返回包装的方法
      • getConstructor

        @Nullable
        public @Nullable Constructor<?> getConstructor()
        返回构造函数
      • getDeclaringClass

        public Class<?> getDeclaringClass()
        返回声明基础方法或构造函数的类
      • getExecutable

        public Executable getExecutable()
        返回包装的可执行对象
      • getParameter

        public Parameter getParameter()
        返回方法构造函数参数的 Parameter 描述符
      • getParameterIndex

        public int getParameterIndex()
        返回方法构造函数参数的索引
        返回:
        参数索引(返回类型为 -1)
      • getNestingLevel

        public int getNestingLevel()
        返回目标类型的嵌套级别(通常为 1;例如,在列表列表的情况下,1 表示嵌套列表,而 2 表示嵌套列表的元素)
      • withTypeIndex

        public MethodParameter withTypeIndex​(int typeIndex)
        返回此 方法参数 的变体,其中当前级别的类型设置为指定值。
        参数:
        typeIndex - the new type index
      • getTypeIndexForCurrentLevel

        @Nullable
        public @Nullable Integer getTypeIndexForCurrentLevel()
        返回当前嵌套层的类型索引
        返回:
        相应的类型索引,或者null 如果没有指定(表示默认的类型索引)
        另请参阅:
        getNestingLevel()
      • getTypeIndexForLevel

        @Nullable
        public @Nullable Integer getTypeIndexForLevel​(int nestingLevel)
        返回指定嵌套级别的类型索引
        参数:
        nestingLevel - 要检查的嵌套级别
        返回:
        相应的类型索引,或者null 如果没有指定(表示默认的类型索引)。
      • nested

        public MethodParameter nested()
        返回这个MethodParameter的变体,该变体指向相同的参数,但要深入一个嵌套级别。
      • nested

        public MethodParameter nested​(@Nullable
                                      @Nullable Integer typeIndex)
        返回这个MethodParameter的变体,该变体指向相同的参数,但要深入一个嵌套级别。
        参数:
        typeIndex - 新嵌套层的类型索引
      • isOptional

        public boolean isOptional()
        返回该方法是否指示了一个不需要的参数 无论是Java 8的Optional形式,还是参数级 Nullable 注解的任何变体(例如来自JSR-305或 FindBugs 注解集), 或者语言级的 nullable 类型声明或 Kotlin 的Continuation 参数。
      • nestedIfOptional

        public MethodParameter nestedIfOptional()
        返回这个 MethodParameter 的变体,它指向相同的参数,但在 Optional声明的情况下,是一个更深的嵌套级别。
        另请参阅:
        isOptional(), nested()
      • withContainingClass

        public MethodParameter withContainingClass​(@Nullable
                                                   @Nullable Class<?> containingClass)
        返回这个 MethodParameter 的一个变体,它指的是给定的包含类。
        参数:
        containingClass - 一个特定的包含类(可能是声明类的一个子类,例如,替换一个类型变量)
        另请参阅:
        getParameterType()
      • getContainingClass

        public Class<?> getContainingClass()
        返回该方法参数的包含类
        返回:
        一个特定的包含类(可能是声明类的一个子类),或者仅仅是声明类本身
        另请参阅:
        getDeclaringClass()
      • getParameterType

        public Class<?> getParameterType()
        返回方法/构造器参数的类型
        返回:
        参数的类型(绝不是null
      • getGenericParameterType

        public Type getGenericParameterType()
        返回方法/构造函数参数的通用类型
        返回:
        参数的类型(绝不是null
      • getNestedParameterType

        public Class<?> getNestedParameterType()
        返回方法/构造器参数的嵌套类型
        返回:
        参数的类型(绝不是null
        另请参阅:
        getNestingLevel()
      • getNestedGenericParameterType

        public Type getNestedGenericParameterType()
        返回方法/构造函数参数的嵌套通用类型
        返回:
        参数的类型(绝不是null
        另请参阅:
        getNestingLevel()
      • getMethodAnnotations

        public Annotation[] getMethodAnnotations()
        返回与目标方法/构造器本身相关的注释
      • getMethodAnnotation

        @Nullable
        public <A extends Annotation> A getMethodAnnotation​(Class<A> annotationType)
        如果有的话,返回给定类型的方法/构造器注释
        参数:
        annotationType - 要寻找的注释类型
        返回:
        注释对象,如果没有找到,则null
      • hasMethodAnnotation

        public <A extends Annotation> boolean hasMethodAnnotation​(Class<A> annotationType)
        返回该方法/构造函数是否有给定类型的注释
        参数:
        annotationType - 要寻找的注释类型
        另请参阅:
        getMethodAnnotation(Class)
      • getParameterAnnotations

        public Annotation[] getParameterAnnotations()
        返回与特定方法/构造函数参数相关的注释
      • hasParameterAnnotations

        public boolean hasParameterAnnotations()
        如果参数至少有一个注释,返回true;如果没有注释,返回false
        另请参阅:
        getParameterAnnotations()
      • getParameterAnnotation

        @Nullable
        public <A extends Annotation> A getParameterAnnotation​(Class<A> annotationType)
        返回给定类型的参数注解,如果有的话
        参数:
        annotationType - 要寻找的注释类型
        返回:
        注释对象,如果没有找到,则null
      • hasParameterAnnotation

        public <A extends Annotation> boolean hasParameterAnnotation​(Class<A> annotationType)
        返回参数是否以给定的注解类型声明
        参数:
        annotationType - 要寻找的注释类型
        另请参阅:
        getParameterAnnotation(Class)
      • initParameterNameDiscovery

        public void initParameterNameDiscovery​(@Nullable
                                               @Nullable ParameterNameDiscoverer parameterNameDiscoverer)
        为这个方法参数初始化参数名称发现

        这个方法实际上并不试图在此时检索参数名称,它只是允许在应用程序调用 getParameterName()时发生发现(如果有的话)

      • getParameterName

        @Nullable
        public @Nullable String getParameterName()
        返回方法/构造器参数的名称
        返回:
        参数名称(如果在类文件中没有包含参数名称元数据,或者没有 ParameterNameDiscoverer 被设置为开始,则可能是null)。
      • adaptAnnotation

        protected <A extends Annotation> A adaptAnnotation​(A annotation)
        一个模板方法,在将一个给定的注释实例返回给调用者之前,对其进行后处理

        默认实现只是按原样返回给定的注解

        参数:
        annotation - 将要返回的注释
        返回:
        经过处理的注释(或仅仅是原始注释)
      • adaptAnnotationArray

        protected Annotation[] adaptAnnotationArray​(Annotation[] annotations)
        一个模板方法,在将给定的注释数组返回给调用者之前对其进行后处理

        默认实现只是按原样返回给定的注释数组

        参数:
        annotations - 将要返回的注释阵列
        返回:
        后处理的注释阵列(或简单的原始注释)
      • equals

        public boolean equals​(@Nullable
                              @Nullable Object other)
        覆盖:
        equals 在类中 Object
      • hashCode

        public int hashCode()
        覆盖:
        hashCode 在类中 Object
      • forExecutable

        @Contract("null, _ -> fail")
        @NotNull
        public static @NotNull MethodParameter forExecutable​(Executable executable,
                                                             int parameterIndex)
        为给定的方法或构造函数创建一个新的MethodParameter。
        参数:
        executable - 方法或构造函数
        parameterIndex - 参数的索引
        返回:
        相应的 MethodParameter 实例
      • forParameter

        @NotNull
        public static @NotNull MethodParameter forParameter​(@NotNull
                                                            @NotNull Parameter parameter)
        为给定的参数描述符创建一个新的MethodParameter

        这是一个方便的工厂方法,适用于已经有一个Java 8 Parameter描述符的情况。

        参数:
        parameter - 参数描述符
        返回:
        相应的 MethodParameter 实例
      • findParameterIndex

        protected static int findParameterIndex​(@NotNull
                                                @NotNull Parameter parameter)