Reflection is an advanced feature of Java.
Reflection is a key feature of Java as a dynamic (or quasi dynamic) language. This mechanism allows the program to obtain internal information, including its modifiers (such as public, static, and so on), superclass (for example, object) of any class that has a known name through the reflection APIs at run time, Implementation interfaces (such as Serializable), also includes all information about fields and methods, and can change fields content or Invoke methods at run time.
Reflection is a path to an application that can check itself and run the environment. Alternative-metadata of their own information.
Entry level first see if you call a method by reflection and set the value of a member variable:
1. Set Variable values by reflection
Import Java.lang.reflect.Field; /** * Reflection Modify field * * @author Yblin/public class Reffield {public String color; Main (String args[]) {try {Class c = reffield.class; Field Colorfield; Colorfield = C.getfield ("color"); Field Numberfield = C.getfield ("number"); Reffield obj = new Reffield (); Colorfield.set (obj, "blue"); System.out.println ("color=" + colorfield.get (obj)); Numberfield.set (obj, 2.1); System.out.println ("number=" + numberfield.get (obj)); catch (SecurityException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (Nosuchfieldexception e) { TODO auto-generated Catch block E.printstacktrace (); catch (IllegalArgumentException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (Illegalaccessexcep tion e) {//TODO auto-generated catch block E.printstacktrace ();}}
2. Invoke Reflection Invocation method
Import java.lang.reflect.InvocationTargetException; Import Java.lang.reflect.Method; /** * Reflection method invocation. * * @author Yblin */public class Refmethod {private String color, public void Printcolor () {System.out.println ("Color I S: "+ color);} Public Refmethod (string color) {This.color = color;} public void SayHello (string name, Integer age) {System.out.println ("Hello World," + name + "!" + "The" "The" the ":" + Age);} public static void Main (String args[]) {Class CLS = Refmethod.class; Refmethod ref = new Refmethod ("Blue"); try {method SayHello = cls. GetMethod ("SayHello", new class[] {string.class, integer.class});//Find S with String and Integer parameters Ayhello method. Method Printcolor = Cls.getmethod ("Printcolor", null); try {Sayhello.invoke (ref, new object[] {"Ant", 3})//Invokes method Printcolor.invoke (ref, NULL) by invoking calls to the Mechod object;} catch (Il Legalargumentexception e) {//invalid parameter//TODO auto-generated catch block E.printstacktrace ();} catch (Illegalaccessexception e) {//unauthorized access//TODO auto-generated catch block E.printstacktrace (); catch (InvocationTargetException e) {//TODO auto-generated catch block E.printstacktrace ();}} catch (SecurityException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (Nosuchmethodexception e) { TODO auto-generated Catch block E.printstacktrace (); } } }