This statement states that the original content of these materials comes from www.java.sun.com, which is just translated and organized on the basis of their content. One. Overview The REFLECTION API can make Java code dynamic queries and operations running Java classes or interfaces. The Reflection contains many classes, such as the Method class, which can be found in the java.lang.reflect package. Three steps are required to use the classes in the Reflection: 1. Gets an object to be operated, which is a java.lang.Object package that represents a class or interface that is running. The following three methods are commonly used acquisition objects: (1) Class C = Class.Forname ("java.lang.string"); use .forname method to load a class, here is a string class, thus get one Class objects corresponding to this class. (2) Class C = int.class; (3) Class C = Integer.Type; 2. The simpler and common methods of obtaining the method of obtaining a class object to be manipulated by getting a method of obtaining a class object is a getDeclareMethods () method. This method returns a method array of all methods declared in the class object (Method []). There are other methods that will be introduced later. 3. Use the REFLECTION API operation class. two. The java.lang.reflect package introduces two interfaces, eight classes in the java.lang.Reflect package. InvocationHandler interface: MEMBER interface: This interface can obtain information about the latter constructor of the class member (domain or method). AccessibleObject class: This class is the base class of a domain (Field) object, method (Method) object, constructor. Array Class: This class provides a way to dynamically generate and access Java arrays. CONSTRUCTOR Class: Provides information of a class constructor and an interface to access the class constructor. Field class: Provide information of a class domain and an interface to access the domain of the class. Method class: Provide information about a class method and an interface to access the class. Modifier Class: Proxy class: Provides a dynamically generating a static method of a proxy class and class instance.
ReflectionperMission class: three. Example and Description 3.1 All methods declared in the Finding class Import java.lang.reflect. *; Public class method1 {private int F1 (Object P, int x) throws nullpointerexception {if (p == null) throw new nullpointersRext () Return X;} public static void main (string args []) {try {class cls = class.forname ("method1"); method method [] = cls.getdeclaredMethods (); for (int i = 0; i < Methlist.Length; i ) {method m = methodlist [i]; system.out.println ("name =" m.getname ()); system.out.println ("decl class =" m.GetDeclaringClass () ); Class PVEC [] = m.GetParameterTypes (); for (int J = 0; j M.GetDeclaringClass (); Returns an instance of a class that declares the method. The return value is a class. M.GetName (): Returns the name of the method, the return value is a string type. Class PVEC [] = m.getParameterTypes (): An array of types of the parameters of the method. Note that the return order of parameters is the same as the order in which the method declares. Class EVEC [] = M.GETEXCEPTIONTYPES (): Gets a type of array of exceptions thrown by this method. M.Getreturntype (): Returns the type of return value for this method. The return value is a Class. In addition to the method of the above Method class, there are other methods. Among them, it is important: Object Invoke (Object Obj, Object [] ARGS) method: The method is actually called and executed. The meaning of the two parameters is a class instance object that calls the method, and the array of parameter objects that call the method. For details, please refer to Section 3.4. 3.2 Get constructor information import java.lang.reflect *;. Public class constructor1 {public constructor1 () {} protected constructor1 (int i, double d) {} public static void main (String args []) {try {Class cls = Class.Forname ("constructor1"); constructor ctorlist [] = cls.getdeclaredconstructors (); for (int i = 0; i In addition to the above method, there is also a very important way for the CONSTRUCTOR class: Object NewInstance (Object InIargs []): The constructor actually calls the constructor and generates an instance object. Specific Application See Section 3.5. 3.3 Get information of information in class import java.lang.reflect. *; Public class field1 {private double d; public static final int = 37; string s = "testing"; public static void main (string args []) { Try {class cls = class.Forname ("Field1"); Field FieldList [] = cls.getdeclaredfields (); for (int i = 0; i Arglist [0] = new integer (37); arglist [1] = new integer (47); object retobj = meth.invoke (methobj, arglist); integer RetVal = (Integer) Retobj; System.Out.println (RetVal. INTVALUE ());} catch (throwable e) {system.err.println (e);}}} We will carefully introduce the implementation of method calls. First, declare a class Method2, which has a method public int Add (int A, int b). Note that the method named 'add', two form parameters of the data type INT, and return value type INT. Because these information is very important to dynamically call a class. The functions implemented in the main adjustment function are as follows: 1. Class CLS = Class.Forname ("Method2"): Get class instance object CLS. 2. Class Partypes [] = new class [2]; partypes [0] = integer.type; party ppess [1] = integer.type; declare a class array to save the data type of two parameters. 3. Method Meth = CLS.GETMETHOD ("Add", Partypes); Note GetMethod method, the method will return a matching method. There are two parts to be limited. One is the method name, one is the parameter type array of methods. (Because the overload of the method in Java, the order of the data type of the parameter must be explained) The order of the various parameter types in the parameter type array must be the same as the order of the method declaration. 4. Method2 Methobj = new method2 (): Declare an instance variable of a class Method2. 5. Object arglist [] = new object [2]; arglist [0] = new integer (37); arglist [1] = new integer (47); declare an object array to store two parameter instances. 6. Object retobj = method.invoke (METHOBJ, Arglist): actually calls the add function. Note the two parameters of the information invoke (), Methobj is an instance of the class that calls the method (or declared), and ArgList is indeed called the method (here the add method), the parameter instance array. The return value is still an instance of an object Retobj. 7. Integer RetVal = (Integer) Retobj; System.Out.println (RetVal.intValue ()); converts the object instance to the object, and outputs.