A lot of days did not write blog project just go online to write a write Deom last night reviewed the Java reflection mechanism wrote a small Deom review under the reflection mechanism code this thing or write to be more cool
No more nonsense.
Reflection: The Java reflection mechanism allows programs to load, detect, and use classes that are completely unknown during compilation.
In other words, Java can load a runtime to know the name of the class, to obtain its full structure.
The whole structure is that we can get anything inside by reflection, whether it's private, breaking the limits of private,public,protected, and dynamically getting the methods in the class.
Steps and methods to take advantage of reflection:
The first step: Get the class file first, can be obtained in three ways, the first kind of class.forname ("Class"), the second is through the first new out of an object, through the object to call their own Class method, The third type is directly through the class. Class method, you can get
Step two: You can get the method of property, method, constructor through the class file already obtained.
The third step: can choose the brute force crack setaccessible (), otherwise some private cannot get, then can carry on the action which the inverse wants to do
Next do not say what, on the code:
The first is the test class student
Public classStudent {Private intAgePrivateString name; Public Student() { } Public Student(intAge,string name) {//TODO auto-generated constructor stub This. age=age; This. name=name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public void Testprint() {System. out. println ("=============== Instantiate ===================== by reflection"); }}
Test Deom
ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Modifier; Public class Reflect { Public Static void Main(string[] args)throwsException {//TODO auto-generated method stub There are three ways to get the reflection class in the first step //First through Class.forName (); the second is called directly through the class through the GetClass. Class MethodClass C1 = Class.forName ("Student"); Class C2 = Student.class; Student S1 =NewStudent (); Class C3 = S1.getclass ();/** * When you get the class file, which is the bytecode file, you can get all the content inside, * There are two attractions, one is dynamic loading, one can get all the content inside, rather than through the subclass or new out of an object * It is direct to get anything inside, whether public or private or hide hidden method */ //Second step the object of this class can be instantiated after the bytecode file is obtainedStudent Student = (Student) c1.newinstance (); Student.testprint ();/** * The result of this output is that =============== is instantiated by reflection ===================== also means that if it is instantiated * It will directly execute the parameterless construction method */ ///The third step avoids constructing the object to get the attribute in class field constructor constructor method /** * Field fields can get properties of all classes through Getdeclaredfields or GetFields methods * The former can obtain the properties of this class, including private the latter obtains the class and the parent class does not Contains the Priate property * /field[] fs = C1.getdeclaredfields ();//define variable-length strings to store propertiesStringBuffer SB =NewStringBuffer ();//Stitch each property into this string by appending the method //Outermost public definitionSb.append (Modifier.tostring (C1.getmodifiers ()) +"Class"+ c1.getsimplename () +"{\ n");//each property inside for(Field Field:fs) {Sb.append ("\ T");//SpacesSb.append (Modifier.tostring (Field.getmodifiers ()) +" ");//Get modifiers for attributes, such as public,static, etc.Sb.append (Field.gettype (). Getsimplename () +" ");the name of the type of the//attributeSb.append (Field.getname () +"; \ n");//attribute name + Enter} sb.append ("}"); System.out.println (SB);/** * Each field can be taken out and then the private method is cracked by setaccessible (true) to set it, bypassing the object to get the */Field age = C1.getdeclaredfield ("Age"); Age.setaccessible (true);//Can I call it brute force?Age.set (student,Ten); System.out.println (Student.getage ());//The output at this time is 10 is not feeling very magical haha haha}/** * Other constructor methods are similar steps note: It is best to use Setaccessible () method for brute force or if the private will report abnormal * Get method, need to involve */
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Preliminary study on reflection mechanism of java--