The premise of reflection is to obtain a class object that is to be reflected in the following ways:
1 //using Class.forName2Class C = class.forname ("Com.test.Person");3 //using the class attribute4Class C = person.class;5 //using GetClass6Person p =NewPerson ();7Class C =P.getclass ();8 //the basic type of class can be obtained using the following methods9Class C =int.class;//Output intTenClass C = integer.type;//Output int OneClass C = Integer.class;//Output Java.lang.Integer
Then create the class to reflect the person
1 classPerson {2 PrivateString name;3 Private intAge ;4 //Non-parametric construction method5 PublicPerson () {6 7 }8 //Parametric Constructors9 PublicPerson (String name,intAge ) {Ten This. Name =name; One This. Age =Age ; A } - //Output Information - Private voidGetInfo () { theSYSTEM.OUT.PRINTLN (name + "\ T" +Age ); - } - //Private Methods - Private voidPrivatemethod () { +System.out.println ("Private method called"); - } + //private static Methods A Private Static voidGetstaticprivatemethod () { atSystem.out.println ("Getstaticprivatemethod"); - } - - //Public Method - Public voidGetpublicmethod () { -System.out.println ("Getpublicmethod"); in } - to //Public static Method + Public Static voidPubicstaticmethod () { -SYSTEM.OUT.PRINTLN ("public static method called"); the } * //public static method with parameters $ Public Static voidgetmsg (String msg) {Panax Notoginseng System.out.println (msg); - } the +}
1. Reflection Private Method
1 //reflect private methods in the person class2Class clazz = Class.forName ("Com.test.Person");3 //create an instance of the person class, provided that the person has an argument-free constructor method4Person person =(person) clazz.newinstance ();5 //get method, use Getdeclaredmethod is access to private method, but must set Setaccessible to True6method = Clazz.getdeclaredmethod ("Privatemethod");7 //This is not a security check when accessing private methods8Method.setaccessible (true);9 //calling the Getage method in the person classTenMethod.invoke (person);
2. Reflection private static method
// gets the class pair object for person Class C = person. class ; // gets the method to invoke, where Getdeclaremethod is required to get the private method of the class Method m = C.getdeclaredmethod ("Getstaticprivatemethod")// Set Private method accessibility to True M.setaccessible (true); // Call M.invoke (c);
3. Reflection Public methods
// Get Class object Class C = person. class ; // Building the Person object (provided that the person has a parameterless constructor method) Person person = (person) c.newinstance (); // gets the method to invoke method = C.getmethod ("Getpublicmethod"); // invokes the method Method.invoke (person) in the person object;
4. Reflection public static method
// Get Class object Class cc = person. class ; // gets the method to invoke method = Cc.getmethod ("Pubicstaticmethod"); // call the method because it is a static method, so call Method.invoke (cc) directly from the class.
5. Reflection of public static methods with parameters
// Get class Class C = person. class ; // get method, the class object to pass the parameter array, a parameter of the case can also be directly passed class, such as String.class method = C.getmethod ("getmsg",new class[]{string. Class}); Method.invoke (C,"secret that cannot be said");
6. private methods that reflect the parameters of a constructor
//Get classClass C = person.class;//construct a method of constructing a parameterConstructor Constructor = C.getconstructor (NewClass[]{string.class,int.class});//getting a Person object instance through constructorPerson person = (person) constructor.newinstance (Newobject[]{"Liusilong", 18});//here GetInfo if is the public method, then directly person.getinfo ();//If GetInfo is a private method, you also need to reflect the proprietary method, as follows:method = C.getdeclaredmethod ("GetInfo"); Method.setaccessible (true); Method.invoke (person);
7. Reflection Properties and output
//Get Class objectClass C = person.class;//generate an instance of the person classPerson p =(person) c.newinstance ();//get private properties in the person classField name = C.getdeclaredfield ("name"); Field Age= C.getdeclaredfield ("Age");//set private properties to accessName.setaccessible (true); Age.setaccessible (true);//set the value of a private property in a person objectName.set (P, "Liusilong"); Age.set (P,);//gets the private method in person GetInfomethod = C.getdeclaredmethod ("GetInfo");//set private methods to accessMethod.setaccessible (true);//Calling Private MethodsMethod.invoke (P);
java--Reflex