Java的.lang.reflect.Parameter类在Java中
Java.lang.reflect.Parameter 类提供有关方法参数的信息,包括它们的名称和修饰符。它还提供了一种获取参数属性的替代方法。
Parameter 类的一些有用方法是:
- public int getModifiers():它返回此 Parameter 对象表示的参数的修饰符标志。
- public String getName():它返回方法参数的名称。
- public Type getParameterizedType():它返回参数的类型。
Java.lang.reflect.Parameter 类的所有方法列举如下:Method Description equals(Object obj) Compares based on the executable and the index. getAnnotatedType() Returns an AnnotatedType object that represents the use of a type to specify the type of the formal parameter represented by this Parameter. getAnnotation(Class Returns this element’s annotation for the specified type if such an annotation is present, else null. getAnnotations() Returns annotations that are present on this element. getAnnotationsByType(Class Returns annotations that are associated with this element. getDeclaredAnnotations() Returns annotations that are directly present on this element. getDeclaredAnnotation(Class Returns this element’s annotation for the specified type if such an annotation is directly present, else null. getDeclaringExecutable() Return the Executable which declares this parameter. getDeclaredAnnotationsByType(Class Returns this element’s annotation(s) for the specified type if such annotations are either directly present or indirectly present. getModifiers() Get the modifier flags for this parameter represented by this Parameter object. getName() Returns the name of the parameter. getParameterizedType() Returns a Type object that identifies the parameterized type for the parameter represented by this Parameter object. getType() Returns a Class object that identifies the declared type for the parameter represented by this Parameter object. hashCode() Returns a hash code based on the executable’s hash code and the index. isImplicit() Returns true if the parameter is implicitly declared in the source code else returning false isNamePresent() Returning true if the parameter has the name in accordance with the class file isVarArgs() Returning true if this parameter represents a variable argument list. isSynthetic() Returning true if a parameter is not implicitly nor explicitly declared else false toString() Returns a string describing this parameter
例子:
Java
// Java program to illustrate Parameter class of
// java.lang.reflect package
// Importing java.lang.reflect package to
// obtain reflective information about classes and objects
import java.lang.reflect.*;
// Class 1
// Helper class
class Calculate {
// Function 1
// To add to numbers
int add(int a, int b)
{
// Returning the sum
return (a + b);
}
// Function 2
// To multiply two numbers
int mul(int a, int b)
{
// Returning the number obtained
// after multiplying numbers
return (b * a);
}
// Function 3
// Substracting two numbers
long subtract(long a, long b)
{
// Return the numbers after substracting
// second number from the first number
return (a - b);
}
}
// Class 2
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of helper class
// in the main method
Class cls = Calculate.class;
// Getting all the methods of
// a particular class
Method[] methods = cls.getDeclaredMethods();
// Iterating over each method
// using the for each loop
for (Method method : methods) {
// Print and display the method name
// using getName() method
System.out.print(
"Method Name: " + method.getName() + " ");
// Getting all the parameters of
// a particular method
Parameter parameters[] = method.getParameters();
// Print and display
System.out.println("\nparameters of "
+ method.getName()
+ "() methods: ");
// Iterating over parameters
// using for each loop
for (Parameter parameter : parameters) {
// Display the type of parameters
System.out.print(
parameter.getParameterizedType() + " ");
// Print the parameters of method
System.out.print(parameter.getName() + " ");
}
// New line
System.out.print("\n\n");
}
}
}
Method Name: subtract
parameters of subtract() methods:
long arg0 long arg1
Method Name: add
parameters of add() methods:
int arg0 int arg1
Method Name: mul
parameters of mul() methods:
int arg0 int arg1