Reflection Mechanism Personal Note

Source: Internet
Author: User

A class has several components, such as: member variables, methods, construction methods, and so on.
Reflection is the loading of classes, and the dissection of the various components of the class

Reflection: The load class obtains the direct code of the Class (3 methods)

1.Class clazz =class.forname ("Cn.itcast.reflect.person");
2.Class clazz1=new person (). GetClass ();
3.Class Clazz2=person.class;

Reflection constructors, creating objects of classes

Constructors with string arguments
Class Clazz =class.forname ("Cn.itcast.reflect.person");//Load Class
Constructor C=clazz.getconstructor (String.class); To create a constructor function
Person p= (person) c.newinstance ("xxxx"); Create an instance and pass a parameter

constructor function with string int
Class Clazz =class.forname ("Cn.itcast.reflect.person");//Load Class
Constructor C=clazz.getconstructor (String.class,int.class); To create a constructor function
Person p= (person) c.newinstance ("xxxx", 12); Create an instance and pass a parameter

Private constructors
Class Clazz =class.forname ("Cn.itcast.reflect.person");//Load Class
Constructor C=clazz.getdeclareconstructor (List.class); To create a constructor function
C.setaccessible (ture);//violent reflection, which can reflect private constructors, enabling access to private
Person p= (person) c.newinstance (new ArrayList ()); Create an instance and pass a parameter

Another way to create an object
Class Clazz =class.forname ("Cn.itcast.reflect.person");//Load Class
Person p= (person) clazz.newinstance ();
Instead of reflecting the constructor, he is actually reflecting the parameterless constructor of the class. I want this method to work.
Work means that there must be a parameterless constructor in this class
This method is equivalent to
Class Clazz =class.forname ("Cn.itcast.reflect.person");//Load Class
Constructor c=clazz.getconstructor (NULL); To create a constructor function
Person p= (person) c.newinstance (null); Create an instance and pass a parameter


Methods of Reflection classes
Method of Reflection Class: public void Aa1 ()

Person P =new person ();
Class clazz=class.forname ("Cn.itcast.reflect.person");
Method Method=clazz.getmethod ("Aa1", null);
Method.invoke (P,null);

Method of Reflection Class: public void Aa1 (String name,int passworld)

Person P=new person ();
Class clazz=class.forname ("Cn.itcast.reflect.person");
Method Method=clazz.getmethod ("Aa1", String.class,int.class);
Method.invoke (P, "zxx", 38);

Method of Reflection Class: public class[] Aa1 (String name,int[] passworld)

Person P=new person ();
Class clazz=class.forname ("Cn.itcast.reflect.person");
Method Method=clazz.getmethod ("Aa1", String.class,int[].class);
Class cs[]= (class[]) Method.invoke (p, "AAA", New int[]{1,23});

Method of Reflection Class: private void Aa1 (InputStream in)

Person P=new person ();
Class class= "Class". forname ("Cn.itcast.reflect.person");
Method Method=clazz.getdeclaredmethod ("Aa1", Inputstream.class);
Method.setaccessible (TRUE);
Mrthod.invoke (p,new fileinputstream ("C:\\1.txt"));

Method of Reflection Class: public static void Aa1 (int num)

Person P=new person ();
Class class= "Class". forname ("Cn.itcast.reflect.person");
Method Method=class.getmethod ("Aa1", Int.class);
Method.invoke (null,23)

Method of Reflection Class: public static void Main (string[] args)

Class clazz=class.forname ("Cn.itcast.reflect.person");
Method Method=class.getmethod ("main", String[].class);
Method.invoke (null, (Object) New string[] ("AA", "BB"));
Method.invoke (Null,new object[] (new string[] ("AA", "BB"));

When the reflected method parameter is an array, be aware that if the direct write "Method.invoke (null,new string[] (" AA "," BB "))" will report the number of parameters of the error,
in jdk1.4 it resolves to a (String s1,string S2) to take the array apart as a parameter. Later versions of the JDK are compatible with version 1.4.
This problem is caused by the JDK. Therefore, the above 2 methods should be used to change the reflection of the method of the array is the way to rewrite.

Fields of the Reflection class
Reflection field: Public String name= "AAAA";
public void Test1 () throw exception{
Person P=new person ();
Class clazz=class.forname ("Cn.itcast.reflect.person");
Field F=clazz.getfield ("name");
Object Obj=f.get (P);//Gets the value of the field
Class Type=f.gettype ();//Gets the type of the field
F.set (P, "xxxxx"); Set the value of a field
}

Reflection field: Private int name= "AAAA";
public void Test2 () throw exception{
Person P=new person ();
Class clazz=class.forname ("Cn.itcast.reflect.person");
Field F=clazz.getdeclarefield ("Passworld");
F.setaccessible (TRUE);
Object Obj=f.get (P);//Gets the value of the field
Class Type=f.gettype ();//Gets the type of the field
F.set (P, "xxxxx"); Set the value of a field
System.out.println (F.get (p));
}

Reflection field: private static int age=23;
public void Test3 () throw exception{
Person P=new person ();
Class clazz=class.forname ("Cn.itcast.reflect.person");
Field F=clazz.getdeclarefield ("Age");
F.setaccessible (TRUE);
Object Obj=f.get (P);//Gets the value of the field
Class Type=f.gettype ();//Gets the type of the field
F.set (P, "xxxxx"); Set the value of a field
System.out.println (F.get (p));
}

Reflection Mechanism Personal Note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.