1. The concept of Java reflection
Reflection meaning: You can get a running Java object.
2, the function of Java reflection
1) You can determine the class to which the run-time object belongs
2) You can determine the member variables and methods that run-time objects have
3) A method that can even be called to private by reflection
4) Generate dynamic agent
3. Classes that implement Java reflection
1) Class: It represents classes and interfaces in a running Java application
2) Field: Provides information about the properties of a class or interface, as well as its dynamic access rights
3) Constructor: Provides information about a single construction method of a class and access to it
4) Method: Provides information about one of the methods in a class or interface
Note: The class class is the most important feature class in Java reflection, and all information that gets the object (including: Method/property/constructor/access) needs it to implement
4. Steps to write the Java Reflector program:
1) You must first obtain the class object
For example:
Class C1 = Test.class; Class C2 = class.forname ("Com.reflection.Test"); Class C3 = New Test (). GetClass ();
2) Then call the methods in the class object to get the structure of the properties/methods/constructors of a class
Note: If you want to be able to get a normal method/property/constructor in the class, you should focus on the following reflection classes
Field
Constructor
Method
Example: This program example shows you how to manipulate Class/field/constructor/method and other classes related to Java reflection
Package com.reflection; Import Java.lang.reflect.Constructor; Import Java.lang.reflect.Field; Import java.lang.reflect.InvocationTargetException; Import Java.lang.reflect.Method; Import Java.lang.reflect.Modifier; public class Testreflection {private String username; private String password; Private int[] age; public void Setusername (String username) {this.username = username; } private void SetPassword (String password) {this.password = password; } public static void Test01 () throws ClassNotFoundException {Class C1 = Testreflection.class; Class C2 = class.forname ("com.reflection.TestReflection"); Gets the specified package name, String package01 = C1.getpackage (). GetName (); String package02 = C2.getpackage (). GetName (); System.out.println ("package01 =" + package01); System.out.println ("PACKAGE02 =" + package02); Gets the modifier of the class int mod = C1.getmodifiers (); String modifier = modifier.tostring (mod); System.out.println ("modifier =" + modifier); Gets the fully qualified name of the specified class String className = C1.geTname (); System.out.println ("className =" + ClassName); Gets the parent class of the specified class Superclazz = C1.getsuperclass (); String superclazzname = Superclazz.getname (); System.out.println ("superclazzname =" + Superclazzname); Gets the implemented interface class[] interfaces = C1.getinterfaces (); for (Class t:interfaces) {System.out.println ("Interfacesname =" + T.getname ()); }//Gets the member variable of the specified class field[] fields = C1.getdeclaredfields (); for (field field:fields) {modifier = modifier.tostring (Field.getmodifiers ());//get access modifier for each field Class type = Field.get Type (); Gets the class object that corresponds to the data type of the field String name = Field.getname (); Gets the field name if (Type.isarray ()) {///If the array type requires special handling of String Arrtype = Type.getcomponenttype (). GetName () + "[]"; System.out.println ("" + modifier + "" + Arrtype + "" + name + ";");} else {System.out.println ("" + modifier + "" + Type + "" + name + ";");}} Gets the constructor method of the class constructor[] constructors = c1.getdeclaredconstructors (); for (Constructor constructor:constructors) {String name = Constructor.getname (); Constructor method Name modifier = modifier.tostring (Constructor.getmodifiers ()); Gets the access modifier System.out.println ("" + modifier + "" + Name + "("); class[] Paramtypes = Constructor.getparametertypes (); Gets the parameters in the constructor method for (int i = 0; i < paramtypes.length; i++) {if (i > 0) {System.out.print (","); } if (Paramtypes[i].isarray ()) {System.out.println (paramtypes [I].getcomponenttype (). GetName () + "[]"); } else {System.out.print (Paramtypes[i].getname ()); }} System.out.println (");"); }//Get member Method method[] methods = C1.getdeclaredmethods (); for (Method method:methods) {modifier = modifier.tostring (Method.getmodifiers ()); Class returntype = Method.getreturntype (); Gets the return type of the method if (Returntype.isarray ()) {String Arrtype = Returntype.getcomponenttype (). GetName () + "[]"; System.out.print ("+modifier+" "+ Arrtype +" "+ method.getname () +" ("); } else {System.out.print ("" + modifier + "" + returntype.getname () + "" + method.getname () + "("); } class[]Paramtypes = Method.getparametertypes (); for (int i = 0; i < paramtypes.length; i++) {if (i > 0) {System.out.print (","); } if (Paramtypes[i].isarray ()) {System.out.println (paramtypes [I].getcomponenttype (). GetName () + "[]"); } else {System.out.print (Paramtypes[i].getname ()); }} System.out.println (");"); }} public static void test02 () throws Instantiationexception, Illegalaccessexception, SecurityException, Nosuchmethode Xception, IllegalArgumentException, invocationtargetexception {///Reflection call method, the Invoke method of method class can be used to implement the call of dynamic methods//public Object Invoke (Object obj, object ... args)///The first parameter represents the object//The second parameter represents the parameter on the execution method//If reflection is to invoke a private method of the class, you can call Seta on the Mehtod object corresponding to the private method. Ccessible (TRUE) Class C1 = Testreflection.class; testreflection T1 = (testreflection) c1.newinstance (); Use reflection to create objects of class System.out.println ("username = =" + T1.username); SYSTEM.OUT.PRINTLN ("Password = =" + T1.password); method = C1.getdeclaredmethod ("Setusername", String.class); Method.invoke (T1, "JaVA reflex learning "); System.out.println ("username = =" + T1.username); method = C1.getdeclaredmethod ("SetPassword", String.class); Method.setaccessible (TRUE); Method.invoke (T1, "reflection performs a method of a private modification"); SYSTEM.OUT.PRINTLN ("Password = =" + T1.password); } public static void Main (string[] args) throws ClassNotFoundException, SecurityException, IllegalArgumentException, Ins Tantiationexception, Illegalaccessexception, nosuchmethodexception, invocationtargetexception {//test01 (); Test02 (); } }
Examples of Java reflection