Reflection: When a bytecode file is loaded into memory, the JVM will dissect the bytecode and then create an object's class object, storing all the information of the bytecode file in the class object, and we just get to the class object, We can use the bytecode object to set the properties of the object or invoke the object's methods, and so on.
The simple use of reflection is recorded here for easy reference
1 /*******************2 Get class3 *******************/4 //Class clazz = person.class;//mode one, obtained by the class name5 //Class clazz = new Person (). GetClass ();//mode two, through object acquisition6Class clazz = Class.forName ("reflect. Person ");//method Three, obtained by completing the qualified name. The parameter is (package name + class name). Recommended, is also the most commonly used7 8 /*******************9 get the construction method for a classTen ******************/ One //constructor[] Constructor = Clazz.getconstructors ();//get all common construction methods A //constructor[] Constructor = Clazz.getdeclaredconstructors ();//get all construction methods - //Constructor Constructor = Clazz.getconstructor (string.class,string.class);//gets a constructor method that does not include a private constructor method, and a null argument to get the parameterless constructor method -Constructor Constructor = Clazz.getdeclaredconstructor (String.class, String.class);//gets a constructor method (including a private constructor method) with a null argument to get the parameterless constructor method the /******************* - To perform a construction method - ******************/ -Constructor.setaccessible (true);//if the construction method is private, you need to change the access adornment permission +Person P = (person) constructor.newinstance ("n", "jiuxiangfeng1");//parameter null means execution of an parameterless construction method - + /******************* A Get Method at ******************/ - //method[] Method = Clazz.getmethods ();//get all common methods - //method[] Method = Clazz.getdeclaredmethods ();//get all methods - //method = Clazz.getmethod ("SetName", string.class);//gets a method (excluding private methods), the second is a mutable argument, and if the second argument is null, the method has no arguments -method = Clazz.getmethod ("SetName", String.class);//gets a method (including a private method), the second is a mutable argument, and if the second argument is null, the method has no arguments - /******************* in Execution Method - ******************/ to //method.setaccessible (true);//If the method is private, you need to change the access adornment permission before executing, otherwise an exception will be thrown +Method.invoke (P, "jiuxiangfeng2");//The first parameter is the object that the method executes, and if it is a static method, the first argument should be null, the second argument is a mutable argument, and if the second argument is null, the method has no arguments - the /******************* * Get member Variable $ ******************/Panax Notoginseng //field[] Field = Clazz.getfields ();//get all public member variables - //field[] Field = Clazz.getdeclaredfields ();//get all member variables the //Field field = Clazz.getfield ("name");//gets a single public member variable. parameter is a member variable name +Field field = Clazz.getdeclaredfield ("name");//gets a single member variable. parameter is a member variable name A /******************* the set the value of a member variable + ******************/ -Field.setaccessible (true);//if it is a private member variable, then it should change its access adornment permission to be accessible, or throw an exception $Field.set (P, "jiuxiangfeng3");//sets the value of an object member variable. Which object is parameter 1, and parameter 2 is the value of its member variable $SYSTEM.OUT.PRINTLN (P);
The above code execution assumes that there is a person.class
Packagereflect; Public classPerson { PublicString ID; PublicString name; PublicPerson (string ID, string name) {Super(); This. ID =ID; This. Name =name; } PublicPerson () {} PublicString getId () {returnID; } Public voidsetId (String id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return This. id+ "=" + This. Name; }}
(Java) Zero-based-reflection reflect