Reflection is one of the features of the Java program development language, which allows the Java program in the run to check itself, or "self-trial", and can directly operate the internal properties of the program. For example, use it to get the name of each member in the Java class and display it.
This capability of Java may not be used in practical applications, but there is no such feature in other programming languages. For example, there is no way to get the function definition related information in the program in PASCAL, C, C .
JavaBean is one of the practical applications of Reflection, which allows some tools to visualize the software components. These tools are dynamically loaded and acquired by the REFLECTION. The properties of the Java component (class) are obtained.
A simple example
Consider the following simple example, let us see how the reflection works.
Import java.lang.reflect. *;
Public class dumpmethods {
Public static void main (string args []) {
Try {
Class C = Class.Forname (Args [0]);
Method m [] = c.getdeclaredMethods ();
For (int i = 0; i System.out.println (M [i] .tostring ()); } Catch (throwable e) { System.err.Println (e); } } } Execute it as follows: Java DumpMethods java.util.stack Its result output is: Public java.lang.Object java.util.stack.push (java.lang.object) Public synchronized java.lang.Object java.util.stack.pop () Public synchronized java.lang.Object java.util.stack.peek () Public boolean java.util.stack.empty () Public synchronized int java.util.stack.Search (java.lang.object) This lists each method name of the Java.util.stack class and their restrictions and return types. This program uses class.Forname to load the specified class, then call getDeclaredMethods to get the list defined in this class. Java.lang.Reflect.Method is a class for describing a single method in a class. Start using the reflection Classs for Reflection, such as Method, can be found in a java.lang.relFect package. When using these classes, you must follow three steps: The first step is to get the java.lang.class object you want to operate. In the Java program in the run, the classes and interfaces are described with the java.lang.class class. Below is one of the ways to get a Class object: Class C = Class.Forname ("java.lang.string"); This statement gets a class object for a String class. There is also another method, as follows: Class C = int.class; or Class C = Integer.Type; They obtain basic types of information. The latter method is accessed in the basic type of package class (such as Integer) pre-defined Type fields. The second step is to call a method such as getDeclaredMethods to obtain a list of all methods defined in this class. Once this information is obtained, you can perform the third step - using the Reflection API to operate this information, as follows: Class C = Class.Forname ("java.lang.string"); Method m [] = c.getdeclaredMethods (); System.out.println (M [0] .tostring ()); It will print out the first method defined in the string in text. In the following example, the three steps will provide an illustration for special applications using the Reflection. Analog InstanceOf operator After getting class information, usually the next step is to solve some basic problems about the Class object. For example, the class.isinstance method can be used to simulate the InstanceOf operator: Class a {} Public class installation1 { Public static void main (string args []) { Try { Class CLS = Class.Forname ("a"); Boolean B1 = CLS.ISINSTANCE (New Integer (37)); System.out.println (b1); Boolean B2 = CLS.Isinstance (New A ()); System.out.println (b2); } Catch (throwable e) { System.err.Println (e); } } } In this example, create a Class A class object and then check if some objects are instances A. Integer (37) is not, but new a () is. Find a way To find out what ways defined in a class, this is a very valuable and very basic reflection usage. The following code achieves this usage: Import java.lang.reflect. *; PUBLIC CLASS METHOD1 { Private Int F1 (Object P, INT X) THROWS NULLPOINTEREXCEPTION { IF (p == NULL) Throw new nullpointerserException (); Return X; } Public static void main (string args []) { Try { Class CLS = Class.Forname ("Method1"); Method metlist [] = CLS.GetDeclaredMethods (); For (int I = 0; i i ) { Method M = Methlist [i]; System.out.println ("Name = " m.getname ()); System.out.println ("DECL Class =" M.GetDeclaringClass ()); Class PVEC [] = M.GetParameterTypes (); For (int J = 0; j System.out.println (" Param # " J " " PVEC [J]); Class EVEC [] = m.GetexceptionTypes (); for (int J = 0; j System.out.println ("EXC #" J "" EVEC [J]); System.out.Println ("Return Type =" M.Getreturntype ()); System.out.println ("-----"); } } Catch (throwable e) { System.err.Println (e); } } } This program first gets the description of the Method1 class, then calls getDeclaredMethods to get a series of Method objects, which describe each method defined in the class, including the public method, protected method, package method, and private methods. If you use GetMethods in the program instead of getDeclaredMethods, you can also get information about each method of inheritance. After obtaining the list of Method objects, it is not difficult to display the parameter types, exception types, and return value types of these methods. These types are basic types or class types, all can be given in order by the objects described. The result of the output is as follows: Name = f1 DECL Class = Class Method1 PARAM # 0 Class Java.lang.Object Param # 1 int EXC # 0 Class Java.lang.NullPointersRexception Return Type = INT ----- Name = main DECL Class = Class Method1 Param # 0 class [ljava.lang.string; Return Type = Void ----- Get constructor information The usage of the acquisition table constructor is similar to the usage of the above acquisition method, such as: 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 Constructor CT = CTORLIST [I]; System.out.println ("Name = " ct.getname ()); System.out.println ("DECL Class =" Ct.getDeclineClass ()); Class PVEC [] = ct.getParameterTypes (); For (int J = 0; j System.out.println ("Param #" J " PVEC [J]); Class EVEC [] = ct.GetexceptionTypes (); for (int J = 0; j System.out.println ( "EXC #" J " EVEC [J]); System.out.println ("-----"); } } Catch (throwable e) { System.err.Println (e); } } } This example has not been able to obtain the relevant information of the return type, that is because the constructor has no return type. The result of this program is: Name = constructor1 DECL Class = Class Constructor1 ----- Name = constructor1 DECL Class = Class Constructor1 Param # 0 int Param # 1 double ----- Get class fields (domain) Find out which data fields defined in a class are also possible. The following code is doing this: Import java.lang.reflect. *; Public class field1 { Private Double D; Public Static Final INT I = 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 Field fld = fieldList [i]; System.out.println ("Name = " fld.getname ()); System.out.println ("DECL Class =" Fld.getDeclaringClass ()); System.out.println ("TYPE = " fld.gettype ()); Int mod = fld.getmodifiers (); System.out.println ("MODIFIERS =" Modifier.toString (MOD)); System.out.println ("-----"); } } Catch (throwable e) { System.err.Println (e); } } } This example is very similar to the previous example. The example uses a new thing Modifier, which is also a reflection class that describes the modifiers of the field member, such as "Private Int". These modifications themselves are described by integer, and using modifier.tostring to return string descriptions arranged in "official" sequential order (such as "Static" before "Final"). The output of this program is: Name = d DECL Class = Class Field1 TYPE = DOUBLE Modifiers = private ----- Name = i DECL Class = Class Field1 TYPE = INT Modifiers = public static final ------ Name = s DECL Class = Class Field1 TYPE = Class Java.lang.String Modifiers = ----- In the case of obtaining a method, you can only get the field information (getDeclaredFields) declared in the current class, or you can get the field defined in the parent class. Execute a method according to the name of the method The text is here, and the example will be related to how to obtain the information of the class. We can also use reflection to do some other things, such as implementing a method specified by a name. The following example demonstrates this: Import java.lang.reflect. *; Public class method2 { Public Int Add (Int A, INT B) { RETURN A B; } Public static void main (string args []) { Try { Class CLS = Class.Forname ("Method2"); Class Partypes [] = new class [2]; Partypes [0] = integer.type; Partypes [1] = integer.Type; Method method = cls.getMethod "add", pieys); Method2 Methobj = New Method2 (); Object arglist [] = new object [2]; 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); } } } If a program knows a certain method when it is executed, the name of this method is specified during the running process of the program (for example, there is such a thing in the JavaBean development environment), then the above program Demonstrate how to do it. In the above example, getMethod is used to find a method with two integer parameters and namedDD. After finding this method, after the corresponding Method object is created, do it in the correct object instance. When the method is executed, a list of parameters is required, which is two Integer objects that are packaged in the integers 37 and 47 in the previous example. The return of the execution method is also an Integer object, which encapsulates the return value 84. Create new objects For the constructor, it is not possible to perform as the method of execution, because the execution of a constructor means creation a new object (accurately, the process of creating an object includes allocating memory and constructive objects). Therefore, the most similar example of the above example is as follows: Import java.lang.reflect. *; Public class constructor2 { Public constructor2 () { } Public Constructor2 (Int A, INT B) { System.out.println ( "a =" a "b =" b); } Public static void main (string args []) { Try { Class CLS = Class.Forname ("constructor2"); Class Partypes [] = new class [2]; Partypes [0] = integer.type; Partypes [1] = integer.Type; Constructor CT = CLS.GetConstructor (Partypes); Object arglist [] = new object [2]; Arglist [0] = new integer (37); Arglist [1] = new integer (47); Object retobj = ct.newinstance (arglist); } Catch (throwable e) { System.err.Println (e); } } } Find the appropriate constructor based on the specified parameter type and execute it to create a new object instance. Using this method can dynamically create objects when running, not when compiling, this is very valuable. Change the value of the field (domain) There is also a use of REFLECTION to change the value of the object data field. Reflection can find the field of the object from the running program and change it, the following example can explain this: Import java.lang.reflect. *; Public class field2 { Public Double D; Public static void main (string args []) { Try { Class CLS = Class.Forname ("Field2"); Field FLD = CLS.GETFIELD ("D"); Field2 f2obj = new field2 (); System.out.println ("D =" f2obj.d); FLD.SETDOUBLE (F2Obj, 12.34); System.out.println ("D =" f2obj.d); } Catch (throwable e) { System.err.Println (e); } } } In this example, the value of field D is changed to 12.34. Use array The last usage of the Reflection described herein is the created an array of operations. Array is a special class type in Java language, and a reference to an array can assign an Object reference. See how the following examples look at the array: Import java.lang.reflect. *; Public class arrAy1 { Public static void main (string args []) { Try { Class CLS = Class.Forname "java.lang.string"); Object arr = array.newinstance (CLS, 10); Array.Set (Arr, 5, "This Is A Test"); String s = (string) array.get (Arr, 5); System.out.println (s); } Catch (throwable e) { System.err.Println (e); } } } In the example, 10 unit length String arrays are created to assign the value of the character string of the 5th position, and finally the string acquire and print this string from the array. The following code provides a more complicated example: import java.lang.reflect. *; Public class array2 { Public static void main (string args []) { INT DIMS [] = new int {5, 10, 15}; Object Arr = Array.NewInstance (Integer.Type, DIMS); Object arrobj = array.get (Arr, 3); Class CLS = Arrobj.getClass (). getcomponenttype (); System.out.println (CLS); Arrobj = array.get (Arrobj, 5); Array.setint (Arrobj, 10, 37); Int Arrcast [] [] = (int [] [] []) Arr; System.out.println (Arrcast [3] [5] [10]); } } A total of 5 x 10 x 15 is created in the example, and the value of [3] [5] [10] is set to 37. Note that the multidimensional array is actually an array of arrays, for example, after the first array.get, Arrobj is an array of 10 x 15. Furthermore, one element is obtained, that is, an array of length 15 and assigns an array.setInt to its 10th element. Note that the type when creating an array is dynamic, and does not know the type when compiling. summary Java Reflection is very useful, which enables class and data structures to dynamically retrieve relevant information by name and allow this information to be operated in the running program. This feature of Java is very powerful and is other frequent languages, such as C, C , Fortran or Pascal.