Reflection:
What is reflection?
Anatomy of each component in a Java class is mapped into a Java object
Why use reflection?
More for frames and components, write-up common programs with high reusability
1.Class class.
The class represents our bytecode file.
Three different ways.
1. Class name.
2. Object. GetClass ();
3.class.forname ();
Create in development using a third class.forname (); why?
This method has low coupling and no dependence.
------------------------------------------
2.Constructor (Constructor object)
The role of the constructor is to create objects.
Constructor represents the constructor object, which we can instantiate after we get it.
1. How to get constructor object.
GetConstructor ()-----Get the specified constructor
GetConstructors ();---get all the constructors
2. Notes on constructor
1. If the construction method is not public. Then to get its constructor object use the Getdeclaredconstuctor () method.
2. For privatization permissions, a permission check must be set before use.
public void Setaccessible (Boolean flag)
A value of TRUE indicates that the reflected object should cancel the Java language access check when it is used
3. In development, when we get the class, we typically instantiate the object of the class directly.
2.Field
3.Method
1.
2. How is the static method called?
Static method calls do not need to use objects, write null directly
Staticmethod.invoke (null, parameter);
How do I invoke the parameters of the method if it is an array?
When invoking invoke, the second argument is cast to object, or the outer layer is wrapped in an array.
1
PackageCn.itcast.reflect;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;Importjava.lang.reflect.InvocationTargetException;Importorg.junit.Test; Public classDemo1 {@Test Public voidDemo1 ()throwsclassnotfoundexception, Instantiationexception, illegalaccessexception {Class clazz= Class.forName ("Cn.itcast.reflect.Teacher"); Clazz.newinstance (); Teacher T=NewTeacher (); } //Get Class object@Test Public voidDemo2 ()throwsClassNotFoundException {//1. Through the. Class//Class Clazz=teacher.class; //2. Getting through Objects//Teacher t = new Teacher (); //Class clazz = T.getclass ();//The GetClass method is defined in the object class.//3.class.forname ()Class Clazz= Class.forName ("Cn.itcast.reflect.Teacher"); } //gets the constructor.@Test Public voidDemo3 ()throwsSecurityException, Nosuchmethodexception, IllegalArgumentException, Instantiationexception, Ill Egalaccessexception, InvocationTargetException {//1. Get the class object for teacher.Class clazz = Teacher.class; //2. Get the constructor for the teacher class.Constructor C1 = Clazz.getconstructor ();//No parameter ConstructionConstructor C2 = Clazz.getconstructor (String.class);//String type argument constructionConstructor C3 = Clazz.getconstructor (String.class,int.class);//string,int type parameter construction//3. Create an instance of the teacher class from the constructor object. //Teacher obj1 = (Teacher) c1.newinstance ();//equivalent to New Teacher (); // //obj1.show (); //Teacher obj2 = (Teacher) c2.newinstance ("Zhang San");//New Teacher ("Zhang San"); // //obj2.show ();Teacher obj3= (Teacher) c3.newinstance ("John Doe", 20);//New//Teacher ("John Doe");obj3.show (); } //notes on the constructor@Test Public voidDemo4 ()throwsSecurityException, Nosuchmethodexception, IllegalArgumentException, Instantiationexception, Ill Egalaccessexception, InvocationTargetException {//1. Get the class object for teacher.Class clazz = Teacher.class; //2. Gets the non-public constructor object.Constructor C = Clazz.getdeclaredconstructor (String.class,int.class); //Cancel Security CheckC.setaccessible (true); //3. Create a Teacher objectTeacher obj = (Teacher) c.newinstance ("John Doe", 20); Obj.show (); } @Test Public voidDemo5 ()throwsinstantiationexception, illegalaccessexception {Class clazz= Teacher.class; Teacher T= (Teacher) clazz.newinstance ();//requires the existence of an argument-free construction } //about the field class.---It represents a property.@Test Public voidDemo6 ()throwsSecurityException, Nosuchfieldexception, IllegalArgumentException, Illegalaccessexception, instantiationexception {//The Field object is also obtained through the classClass clazz = Teacher.class; Field fname= Clazz.getdeclaredfield ("name");//equivalent to getting the String Name property.fname.setaccessible (true); //gets the property value.Teacher t=(Teacher) clazz.newinstance (); //Object msg=fname.get (t);//equivalent to T.name; //assigns a value to the property value operation.Fname.set (T,"ABCdef");//t.name= "abcdef";System.out.println (T.getname ()); }}
2
PackageCn.itcast.reflect;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;Importorg.junit.Test;//method Operation Public classDemo2 {//gets the Metho object of the Show method in the teacher class.@Test Public voidFUN1 ()throwsSecurityException, Nosuchmethodexception, IllegalArgumentException, Illegalaccessexception, in Vocationtargetexception, instantiationexception {//1. Get classClass C = Teacher.class; //2. Get Method//Method m1 = C.getmethod ("show");//get Show ();Method m2= C.getmethod ("Show", String.class); //3. Let the method execute.Teacher t = (Teacher) c.newinstance ();//Teacher t=new Teacher (); //M1.invoke (t);//equivalent to T.show ();M2.invoke (T,"Good"); } @Test Public voidFun2 ()throwsSecurityException, Nosuchmethodexception, IllegalArgumentException, Illegalaccessexception, InvocationTargetException, instantiationexception {//1. Get classClass C = Teacher.class; //2. Get MethodMethod M=c.getmethod ("Show", String.class,int.class); //3. Let the method execute.Object Returnvalue=m.invoke (C.newinstance (), "Tom", 20);//String msg=t.show ("Tom");System.out.println (returnvalue); }}
3
PackageCn.itcast.reflect;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;Importorg.junit.Test; Public classDemo3 { Public Static voidMain (string[] args) {//to invoke the main method through reflection. and pass parameters.System.out.println (args[0]); System.out.println (args[1]); } @Test Public voidFun ()throwsSecurityException, Nosuchmethodexception, IllegalArgumentException, Illegalaccessexception, in vocationtargetexception {//1. Get the class of the current classesClass Clazz= Demo3.class; //2. Get the method of the main methods in this classMethod Main= Clazz.getmethod ("main", string[].class); //3. Call the Main method. Invoke. //Main.invoke (NULL, new string[]{"Hello", "World"});//wrong number of argumentsMain.invoke (NULL,Newobject[]{Newstring[]{"Hello", "World"}}); Main.invoke (NULL, (Object) (Newstring[]{"Hello", "World"})); }}
4
PackageCn.itcast.reflect; Public classTeacher { Public intAge = 10; PrivateString name = "Tom"; PublicTeacher () {System.out.println ("Teacher Non-parametric construction method"); } PublicTeacher (String name) { This. Name =name; System.out.println ("Teacher Constructor method----String Name"); } PrivateTeacher (String name,intAge ) { This. Name =name; This. Age =Age ; System.out.println ("Teacher Construction Method----String Name,int Age"); } PublicString GetName () {returnname; } Public voidShow () {System.out.println ("Name:" + name + "Age:" +Age ); } Public voidShow (String msg) {System.out.println (msg); } PublicString Show (String msg,inti) { returnmsg+ "" +i; }}
JDK5.0 New Features-reflection