方法 | 说明 |
public boolean equals(Object obj) | 它将此Method与指定对象进行比较。如果对象相同,则返回true。如果两个方法由相同的类声明且具有相同的名称,形式参数类型和返回类型,则它们是相同的。 |
public AnnotatedType getAnnotatedReturnType() | 它返回AnnotatedType对象,该对象表示使用类型指定方法/构造方法的返回类型。 |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) | 如果存在这样的注释,它将返回指定类型的该元素的注释,否则返回null。 NullPointerException-如果给定的注释类为null |
public Annotation[] getDeclaredAnnotations() | 它返回直接存在于此元素上的注释。此方法将忽略继承的注释。如果此元素上没有直接存在的注释,则返回值为长度为0的数组。此方法的调用者可以随意修改返回的数组。它对返回给其他调用方的数组没有影响。 |
public Class <?> getDeclaringClass() | 它返回Class对象,该对象表示声明此对象表示的可执行文件的类或接口。 |
public Object getDefaultValue() | 它返回此Method实例表示的注释成员的默认值。 |
public Class <?>[] getExceptionTypes() | 它返回一个Class对象数组,这些对象表示声明为由此对象表示的基础可执行文件引发的异常的类型。 |
public Type[] getGenericExceptionTypes() | 它返回一个Type对象数组,这些对象表示声明为此可执行对象引发的异常。如果基础可执行文件在throws子句中未声明任何异常,则它将返回长度为0的数组。它引发以下异常: GenericSignatureFormatError -如果通用方法签名不符合Java虚拟机规范中指定的格式。 TypeNotPresentException -如果基础可执行文件的throws子句引用了不存在的类型声明。 MalformedParameterizedTypeException -如果基础可执行文件的throws子句引用了由于某种原因而无法实例化的参数化类型。 |
public Type [] getGenericParameterTypes() | 它返回代表形式参数类型的Type对象数组。它引发以下异常: GenericSignatureFormatError -如果通用方法签名不符合Java虚拟机规范中指定的格式。 TypeNotPresentException -如果基础可执行文件的任何参数类型引用了不存在的类型声明。 MalformedParameterizedTypeException -如果任何基础可执行文件的参数类型引用由于某种原因而无法实例化的参数化类型。 |
public int getModifiers() | 它返回此对象表示的可执行文件的Java语言修饰符。 |
public String getName() | 它以String形式返回此Method对象表示的方法的名称。 |
public Annotation[][] getParameterAnnotations() | 它返回一个数组数组,这些数组按声明顺序表示此对象表示的可执行文件的形式参数和隐式参数上的注释。 |
public int getParameterCount() | 它返回此对象表示的可执行文件的形式参数的数量。 |
public Class <?>[] getParameterTypes() | 它返回代表形式参数类型的Class对象数组。按照此对象表示的可执行文件的声明顺序。如果基础可执行文件不带任何参数,它将返回长度为0的数组。 |
public Class <?> getReturnType() | 它返回一个Class对象,该对象表示此Method对象表示的方法的形式返回类型。 |
public TypeVariable<Method>[] getTypeParameters() | 它以声明顺序返回一个TypeVariable对象数组,这些对象表示此GenericDeclaration对象表示的通用声明所声明的类型变量。如果此通用声明的通用签名不符合Java虚拟机规范中指定的格式,则会引发GenericSignatureFormatError。 |
public int hashCode() | 它为此方法返回一个哈希码。哈希码被计算为基础方法的声明类名和方法名的哈希码的异或。 |
public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException | 它在具有指定参数的指定对象上调用此Method对象表示的基础方法。如果基础方法是静态的,则忽略指定的obj参数。它可以为空。如果基础方法所需的形式参数数量为0,则提供的args数组的长度可以为0或为null。如果基础方法是实例方法,则使用《 Java语言规范》中所述的动态方法查找来调用它。如果基础方法是静态的,则声明该方法的类将被初始化(如果尚未初始化)。如果该方法正常完成,则它将返回的值返回给invoke的调用者。 |
public boolean isBridge() | 如果此方法是桥接方法,则返回true。否则返回false。 |
public boolean isDefault() | 如果此方法是默认方法,则返回true,否则返回false。默认方法是公共非抽象实例方法,即以接口类型声明的带有主体的非静态方法。 |
public boolean isSynthetic() | 如果此可执行文件是合成构造,则返回true;否则,返回true。否则返回false。 |
public boolean isVarArgs() | 如果声明此可执行文件采用可变数量的参数,则返回true;否则,返回true。否则返回false。 |
public String toGenericString() | 它返回描述此Method的字符串,包括类型参数。 |
public String toString() | 它返回一个字符串。 |
方法 | 说明 |
public boolean equals(Object obj) | 它根据可执行文件和索引进行比较。 |
public AnnotatedType getAnnotatedType() | 它返回一个AnnotatedType对象,该对象表示使用类型指定此Parameter表示的形式参数的类型。 |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) | 如果存在这样的注释,它将返回指定类型的该元素的注释,否则为null。如果给定的注释类为null,则抛出NullPointerException。 |
public Annotation[] getAnnotations() | 它返回此元素上存在的注释。如果此元素上没有注释,则返回值为长度为0的数组。 |
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) | 它返回与此元素关联的注释。如果没有与此元素相关联的注释,则返回值为长度为0的数组。此方法与AnnotatedElement.getAnnotation(Class)之间的区别在于,此方法检测其参数是否为可重复的注释类型(JLS 9.6),如果是这样,则尝试通过"浏览"容器注释来查找该类型的一个或多个注释。如果给定的注释类为null,则抛出NullPointerException。 |
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) | 如果指定的类型直接存在,则返回该元素的注释,否则为null。此方法将忽略继承的注释。如果抛出异常,则抛出NullPointerException。给定的注释类为空。 |
public Annotation[] getDeclaredAnnotations() | 它返回直接存在于此元素上的注释。此方法将忽略继承的注释。如果此元素上没有直接存在的注释,则返回值为长度为0的数组。 |
public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) | 如果指定的类型是直接存在或间接存在,它将返回该元素的注释。此方法将忽略继承的注释。如果此元素上没有直接或间接存在的指定注释,则返回值为长度为0的数组。此方法与AnnotatedElement.getDeclaredAnnotation(Class)之间的区别是该方法检测其参数是否为可重复的注释类型( JLS 9.6),如果是这样,则尝试通过"浏览"容器注释(如果存在)来查找该类型的一个或多个注释。该方法的调用者可以自由修改返回的数组;它对返回给其他调用方的数组没有影响。如果给定的注释类为null,则抛出NullPointerException |
public Executable getDeclaringExecutable() | 它返回声明此参数的可执行文件。 |
public int getModifiers() | 它返回此Parameter对象表示的参数的修饰符标志。 |
public String getName() | 它返回参数的名称。如果存在参数的名称,则此方法返回由类文件提供的名称。否则,此方法将合成形式为argN的名称,其中N是声明该参数的方法的描述符中该参数的索引。 |
public Type getParameterizedType() | 它返回一个Type对象,该对象标识此Parameter对象表示的参数的参数化类型。 |
public Class <?> getType() | 它返回一个Class对象,该对象标识此Parameter对象表示的参数的声明类型。 |
public int hashCode(int mul int arg0 int arg1 add int arg0 int arg1) | 它基于可执行文件的哈希码和索引返回哈希码。 |
public boolean isImplicit() | 如果此参数在源代码中隐式声明,则返回true。否则,返回false。 |
public boolean isNamePresent() | 如果参数具有根据类文件的名称,则返回true,否则返回false。 |
public boolean isSynthetic() | 如果源代码中没有隐式或显式声明此参数,则返回true。否则返回false。 |
public boolean isVarArgs() | 如果此参数表示变量参数列表,则返回true;否则返回true。否则返回false。 |
public String toString() | 它返回描述此参数的字符串。格式是参数的修饰符(如果有的话),按照Java语言规范。 |
public class Calculate { int add(int a, int b){ return (a+b); } int mul(int a, int b){ return (b*a); } }
javac -parameters Calculate.java
import java.lang.reflect.Method; import java.lang.reflect.Parameter; public class ParameterReflection { public static void main(String[] args) { // Creating object of a class Calculate calculate = new Calculate(); // instantiating class class Classcls = calculate.getClass(); // Getting declared methods inside the Calculate class Method[] method = cls.getDeclaredMethods(); // It returns array of methods // Iterating method array for (Method method2 : method) { System.out.print(method2.getName()); // getting name of method // Getting parameters of each method Parameter parameter[] = method2.getParameters(); // It returns array of parameters // Iterating parameter array for (Parameter parameter2 : parameter) { System.out.print(""+parameter2.getParameterizedType()); // returns type of parameter System.out.print(""+parameter2.getName()); // returns parameter name } System.out.println(); } } }
mul int a int b add int a int b
mul int arg0 int arg1 add int arg0 int arg1