Reflection (Runtime of the class information), in Java has been a lot of use, especially in some frameworks (annotations and so on), more or less will be used to reflect, understand the Java reflection, we will learn the framework and writing framework is very important!
The class class, together with the Java.lang.reflect class library, supports the concept of reflection, which includes the Fieid, method, and constructor classes. These types of objects are created by the JVM at run time to represent the corresponding members in the unknown class. This allows you to create new objects using constructor, read and modify the fields associated with the Fieid object with the Get () and set () methods, invoke the method associated with the methods object with the Invoke () method, and call Getfieids (), GetMethods () and GetConstructors () are handy ways to return an array of objects that represent fields, methods, and constructors.
First, we create a class with the following code
Public classReflexobject {Private intA; protected intb; intC; Public intBD; PublicReflexobject () {Super(); } PublicReflexobject (intAintBintCintd) {Super(); This. A =A; This. B =b; This. C =C; This, db=D; } Private voidSetA (inta) { This. a=A; System.out.println ("I am the public method"); } protected voidSETB (intb) { This. b=b; System.out.println ("I'm a private method."); } voidSETC (intc) { This. c=B; System.out.println ("I am the Package method"); } Public voidSETD (intd) { This. d=D; System.out.println ("I'm a protected method."); } @Override PublicString toString () {return"Reflexobject [a=" + A + ", b=" + B + ", c=" + C + ", d=" + D + "]"; } }
1?? Get the information inside the class through the GetConstructors (), GetMethods (), and GetFields () methods, as follows
Public Static voidMain (string[] args) {Constructor<?>[] Constructors = Reflexobject.class. GetConstructors ();//gets the constructor method inside the classMethod[] methods = Reflexobject.class. GetMethods ();//get the method inside the classfield[] fields = Reflexobject.class. GetFields ();//Get Properties inside a class for(constructor<?>c:constructors) {System.out.println (c.tostring ()); } System.out.println ("-------------------------------"); for(Method m:methods) {System.out.println (m.tostring ()); } System.out.println ("-------------------------------"); for(Field f:fields) {System.out.println (f.tostring ()); } System.out.println ("-------------------------------");}
Output results
You can see that the information inside the class is only public by using the GetConstructors (), GetMethods (), and GetFields () methods, and the public method of the parent class object can be obtained.
2?? Use the Invoke () method to invoke the methods associated with the method object, as shown in the code
Public Static voidMain (string[] args) {reflexobject robj=NewReflexobject (); System.out.println (RobJ); Try{Method MethodA= Reflexobject.class. Getdeclaredmethod ("SetA",int.class); Method MethodB= Reflexobject.class. Getdeclaredmethod ("Setb",int.class); Method METHODC= Reflexobject.class. Getdeclaredmethod ("SetC",int.class); Method methodd= Reflexobject.class. Getdeclaredmethod ("setd",int.class); Methoda.setaccessible (true); Methoda.invoke (RobJ,3); Methodb.setaccessible (true); Methodb.invoke (RobJ,3); Methodc.setaccessible (true); Methodc.invoke (RobJ,3); Methodd.setaccessible (true); Methodd.invoke (RobJ,3); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (RobJ);}
Because Invoke (Obj,args ...) Is the method, and does not know that the object obj is to execute the modification method, you must specify the object that needs to be modified obj, passed the parameter args
The output is as follows
As you can see, it is true that the attribute 0 has been changed to 3, the method is executed, even the private method. Note that other than the public method, the setaccessible is set to True, so you can try it out.
3?? modifying property values directly through the Fieid class
Public Static voidMain (string[] args) {reflexobject robj=NewReflexobject (); System.out.println (RobJ); Try{Field Fielda= Reflexobject.class. Getdeclaredfield ("a"); Field Fieldb= Reflexobject.class. Getdeclaredfield ("B"); Field FIELDC= Reflexobject.class. Getdeclaredfield ("C"); Field fieldd= Reflexobject.class. Getdeclaredfield ("D"); Fielda.setaccessible (true); Fielda.set (RobJ,3); Fieldb.setaccessible (true); Fieldb.set (RobJ,3); Fieldc.setaccessible (true); Fieldc.set (RobJ,3); Fieldd.setaccessible (true); Fieldd.set (RobJ,3); } Catch(nosuchfieldexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(SecurityException e) {//TODO auto-generated Catch blockE.printstacktrace (); }Catch(IllegalArgumentException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(illegalaccessexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (RobJ);}
Output results
Conclusions and points of note similar to 2??
4?? To reflect an object through constructor, the code is as follows
Public Static voidMain (string[] args) {Try{Constructor<ReflexObject> Constructor1 = Reflexobject.class. Getdeclaredconstructor ();//no parameter constructorConstructor<reflexobject> Constructor2 = Reflexobject.class. Getdeclaredconstructor (int.class,int.class,int.class,int.class);//Constructors with parametersReflexobject Object1 =constructor1.newinstance (); Reflexobject Object2= Constructor2.newinstance (3,3,3,3); System.out.println (Object1); System.out.println (OBJECT2); } Catch(Exception e) {e.printstacktrace (); }}
The output is as follows
I'm not setting setaccessible to true here, because my constructor is public and you can try other access permissions.
OK, reflection I wrote here, if there is any problem in writing, we can leave a message below, thank you!
Usage of Java Reflection