Today I studied the reflection mechanism of Java, here to make a note.
Basic step: Get the Class object first, and then use the object to get the method. Just look at the code. The reflection mechanism will be better understood in the light of the final results of this article.
The class that will be reflected:
Package com.gzy.reflect; public class reflect {private string name; private string PSW. public void SetName (String name) {this.name=name; String GetName () {return name.} public void Setpsw (string name) {THIS.PSW=PSW} is public string getpsw () {return PSW;}}
Classes that take advantage of Java mechanisms
Package com.gzy.reflect; Import java.lang.reflect.InvocationTargetException; Import Java.lang.reflect.Method; public class Reflectuse {public boolean usereflect () {Boolean flag=false; try {//>>>>>>>>>&G There are three ways to t;>>>>>>>>>>>>//Get class objects for a class (in fact, to get the classes reflect) as follows:// Method One: Using Class.forName (String param); Class cls=class.forname ("Com.gzy.reflect.Reflect"); Method Two: Use class name. class//class Cls=reflect.class; Method III: The use of GetClass ()//reflect r=new reflect (); Class Cls=r.getclass (); <<<<<<<<<<<<<<<<<<<<<<<< System.out.println ("-------------cls is" +cls+ "-------------------------"); Creates a new instance (equivalent to the reflect new object) using the class object of the acquired classes (the same as the Obj=cls.newinstance) object (); System.out.println ("-------------obj is" +obj+ "-------------------------"); Class[] Param=new class[1]; Param[0]=string.class; System.out.println ("-------------param[0] is" +param[0]+ "-------------------------"); Get RefleCt method, the first parameter is the method name, the second parameter is the type of the parameter, note is the type of the parameter. Method Med=cls.getmethod ("SetName", param); Null indicates that the GetName method has no parameters to Med1=cls.getmethod ("GetName", null); System.out.println ("-------------med is" +med+ "-------------------------"); Object O=med.invoke (obj, new object[]{"This is a reflect test."}); Starts the call method, the first parameter is the object that invokes the method, and the second parameter is the value, the Value object O=med.invoke (obj, "This is a reflect test.") to be passed in the SetName method. System.out.println ("-------------O is" +o+ "-------------------------"); method uses NULL to represent the Object o1=med1.invoke (obj, null) without arguments; System.out.println ("-------------O1 is" +o1+ "-------------------------"); Flag=true; catch (Instantiationexception e) {e.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();} CATC H (SecurityException e) {e.printstacktrace ();} catch (Nosuchmethodexception e) {e.printstacktrace ();} catch (Illegalar Gumentexception e) {e.printstacktrace ();} catch (InvocationTargetException e) {e.printstacktrace ();} catch (CLASSNOTFO Undexception e) {E.printstaCktrace (); return flag; } }
Test class:
Package com.gzy.test; Import Com.gzy.reflect.ReflectUse; public class Testreflect {public static void main (string[] args) {Boolean flag=false; Reflectuse rlu=new reflectuse (); Flag=rlu. Usereflect (); if (flag) {System.out.println ("successful!");} else{System.out.println ("fail~~~~");} }
Test results:
-------------The CLS is class Com.gzy.reflect.Reflect-------------------------
-------------obj is com.gzy.reflect.reflect@d9f9c3-------------------------
-------------Param[0] is class java.lang.String-------------------------
-------------med is public void Com.gzy.reflect.Reflect.setName (java.lang.String)-------------------------
-------------O is null-------------------------
-------------O1 is it is a reflect test.-------------------------
successful!