📜  Java8 参数反映

📅  最后修改于: 2020-10-13 03:36:25             🧑  作者: Mango

方法参数反映

Java提供了一项新功能,您可以在其中获取任何方法或构造函数的形式参数的名称。 java.lang.reflect包包含所有必需的类,例如Method和Parameter,可用于参数反射。

方法类别

它提供有关类或接口上的单个方法的信息。反映的方法可以是类方法或实例方法。

方法类方法

Method Description
public boolean equals(Object obj) It compares this Method against the specified object.
It returns true if the objects are the same. Two Methods are the same if they were declared by the same class and have the same name and formal parameter types and return type.
public AnnotatedType getAnnotatedReturnType() It returns an AnnotatedType object that represents the use of a type to specify the return type of the method/constructor.
public T getAnnotation(Class annotationClass) It returns this element’s annotation for the specified type if such an annotation is present otherwise returns null.
NullPointerException – if the given annotation class is null
public Annotation[] getDeclaredAnnotations() It returns annotations that are directly present on this element. This method ignores inherited annotations. If there are no annotations directly present on this element, the return value is an array of length 0. The caller of this method is free to modify the returned array. it will have no effect on the arrays returned to other callers.
public Class getDeclaringClass() It returns the Class object representing the class or interface that declares the executable represented by this object.
public Object getDefaultValue() It returns the default value for the annotation member represented by this Method instance.
public Class[] getExceptionTypes() It returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.
public Type[] getGenericExceptionTypes() It returns an array of Type objects that represent the exceptions declared to be thrown by this executable object. It returns an array of length 0 if the underlying executable declares no exceptions in its throws clause.
It throws following exceptions:
GenericSignatureFormatError – if the generic method signature does not conform to the format specified in The Java Virtual Machine Specification.
TypeNotPresentException – if the underlying executable’s throws clause refers to a non-existent type declaration.
MalformedParameterizedTypeException – if the underlying executable’s throws clause refers to a parameterized type that cannot be instantiated for any reason.
public Type[] getGenericParameterTypes() It returns an array of Type objects that represent the formal parameter types.
It throws following exceptions:
GenericSignatureFormatError – if the generic method signature does not conform to the format specified in The Java Virtual Machine Specification.
TypeNotPresentException – if any of the parameter types of the underlying executable refers to a non-existent type declaration.
MalformedParameterizedTypeException – if any of the underlying executable’s parameter types refer to a parameterized type that cannot be instantiated for any reason.
public int getModifiers() It returns the Java language modifiers for the executable represented by this object.
public String getName() It returns the name of the method represented by this Method object as a String.
public Annotation[][] getParameterAnnotations() It returns an array of arrays that represent the annotations on the formal and implicit parameters, in declaration order, of the executable represented by this object.
public int getParameterCount() It returns the number of formal parameters for the executable represented by this object.
public Class[] getParameterTypes() It returns an array of Class objects that represent the formal parameter types. in declaration order, of the executable represented by this object. It returns an array of length 0 if the underlying executable takes no parameters.
public Class getReturnType() It returns a Class object that represents the formal return type of the method represented by this Method object.
public TypeVariable[] getTypeParameters() It returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order.
It throws GenericSignatureFormatError, if the generic signature of this generic declaration does not conform to the format specified in The Java Virtual Machine Specification
public int hashCode() It returns a hashcode for this Method. The hashcode is computed as the exclusive-or of the hashcodes for the underlying method’s declaring class name and the method’s name.
public Object invoke(Object obj,
Object… args)
throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException
It invokes the underlying method represented by this Method object, on the specified object with the specified parameters.
If the underlying method is static, the specified obj argument is ignored. It may be null.
If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null.
If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification.
If the underlying method is static, the class that declared the method is initialized if it has not already been initialized.
If the method completes normally, the value it returns is returned to the caller of invoke.
public boolean isBridge() It returns true if this method is a bridge method. otherwise returns false.
public boolean isDefault() It returns true if this method is a default method otherwise returns false. A default method is a public non-abstract instance method, that is, a non-static method with a body, declared in an interface type.
public boolean isSynthetic() It returns true if this executable is a synthetic construct; returns false otherwise.
public boolean isVarArgs() It returns true if this executable was declared to take a variable number of arguments; returns false otherwise.
public String toGenericString() It returns a string describing this Method, including type parameters.
public String toString() It returns a string.

参数类别

参数类提供有关方法参数的信息,包括其名称和修饰符。它还提供了获取参数属性的替代方法。

参数方法

Methods Description
public boolean equals(Object obj) It compares based on the executable and the index.
public AnnotatedType getAnnotatedType() It returns an AnnotatedType object that represents the use of a type to specify the type of the formal parameter represented by this Parameter.
public T getAnnotation(Class annotationClass) It returns this element’s annotation for the specified type if such an annotation is present, else null.
It throws NullPointerException, if the given annotation class is null.
public Annotation[] getAnnotations() It returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.
public T[] getAnnotationsByType(Class annotationClass) It returns annotations that are associated with this element. If there are no annotations associated with this element, the return value is an array of length 0. The difference between this method and AnnotatedElement.getAnnotation(Class) is that this method detects if its argument is a repeatable annotation type (JLS 9.6), and if so, attempts to find one or more annotations of that type by “looking through” a container annotation.
It throws NullPointerException, if the given annotation class is null.
public T getDeclaredAnnotation(Class annotationClass) It returns this element’s annotation for the specified type if such an annotation is directly present, else null. This method ignores inherited annotations. It throws NullPointerException, if the given annotation class is null.
public Annotation[] getDeclaredAnnotations() It returns annotations that are directly present on this element. This method ignores inherited annotations. If there are no annotations directly present on this element, the return value is an array of length 0.
public T[] getDeclaredAnnotationsByType(Class annotationClass) It returns this element’s annotations for the specified type if such annotations are either directly present or indirectly present. This method ignores inherited annotations. If there are no specified annotations directly or indirectly present on this element, the return value is an array of length 0. The difference between this method and AnnotatedElement.getDeclaredAnnotation(Class) is that this method detects if its argument is a repeatable annotation type (JLS 9.6), and if so, attempts to find one or more annotations of that type by “looking through” a container annotation if one is present. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
It throws NullPointerException, if the given annotation class is null
public Executable getDeclaringExecutable() It returns the Executable which declares this parameter.
public int getModifiers() It returns the modifier flags for the parameter represented by this Parameter object.
public String getName() It returns the name of the parameter. If the parameter’s name is present, this method returns the name provided by the class file. Otherwise, this method synthesizes a name of the form argN, where N is the index of the parameter in the descriptor of the method which declares the parameter.
public Type getParameterizedType() It returns a Type object that identifies the parameterized type for the parameter represented by this Parameter object.
public Class getType() It returns a Class object that identifies the declared type for the parameter represented by this Parameter object.
public int hashCode()mul int arg0 int arg1
add int arg0 int arg1
It returns a hash code based on the executable’s hash code and the index.
public boolean isImplicit() It returns true if this parameter is implicitly declared in source code. Otherwise, returns false.
public boolean isNamePresent() It returns true if the parameter has a name according to the class file, otherwise, returns false.
public boolean isSynthetic() It returns true if this parameter is neither implicitly nor explicitly declared in source code. Otherwise returns false.
public boolean isVarArgs() It returns true if this parameter represents a variable argument list; returns false otherwise.
public String toString() It returns a string describing this parameter. The format is the modifiers for the parameter, if any, in canonical order as recommended by The Java? Language Specification.

Java方法参数反射示例

文件:Calculate.java

public class Calculate {
int add(int a, int b){
return (a+b);
}
int mul(int a, int b){
return (b*a);
}
}

注意-在编译和执行以下代码之前,请先通过以下命令编译Calculate类:

javac -parameters Calculate.java

上面命令中的-parameter标志用于将参数存储在Calculate类文件中。默认情况下,.class不存储参数,并返回argsN作为参数名称,其中N是方法中的多个参数。

文件:ParameterReflection.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

如果您不使用-parameters标志来编译Calculate.java文件,则上面的代码将产生以下输出。

输出:

mul int arg0 int arg1
add int arg0 int arg1