Java Reflection Classic Summary

Source: Internet
Author: User

1: Parse the constructor of this class by reflection

public class Person {public String name= ' Jack ';p rivate int password=123;private static int age;public person () {System.out . println ("person");} Public person (String name) {System.out.println ("person Name:" +name);} Public person (String Name,int age) {System.out.println (' Person name: ' +name+ ' person ' Age: ' +age);} Private person (List list) {System.out.println (' person list ');} public void aa () {System.out.println ("AA");} public void Aa1 (String name,int age) {System.out.println ("Name:" +name+ "Age:" +age);} private void Aa2 (InputStream inputstream) {System.out.println ("in");} public static void Aa3 (int num) {System.out.println ("Aa3" +num);}}

2: Reflection of the constructor of this class

//the constructor of the reflection class that creates the object of the class/** The purpose of the structure method of reflection: To create an image of a human being, to instantiate an image as a 1: to load a type Class.forName * 2: To obtain the structure of a function GetConstructor * 3: The method of construction (in practice) newinstance ()*/ Public classDemo1 {//reflection Constructor Public person ()@Test Public voidTest1 ()throwsclassnotfoundexception, SecurityException, Nosuchmethodexception, IllegalArgumentException, Ins Tantiationexception, Illegalaccessexception, invocationtargetexception {//The class is loaded first, and the constructor for the class is obtained (the constructor is intended to create the object), and the object is createdClass C = class.forname ("Com.reflction.person"); Constructor Constructor= C.getconstructor (NULL);//accept mutable arguments, NULL for no argumentsPerson person = (Com.reflction.person) constructor.newinstance (NULL);    System.out.println (Person.name); }    //reflection Constructor Public person (String name)@Test Public voidtest2 () {Try{Class C= Class.forName ("Com.reflction.person"); Constructor Constructor1= C.getconstructor (String.class); Person Person1= (person) constructor1.newinstance ("* * *");        System.out.println (Person1.name); } Catch(Exception e) {}}//Reflection Structure Public number (String Name,int age)@Test Public voidtest3 () {Try{Class C= Class.forName ("Com.reflction.person"); Constructor Constructor3= C.getconstructor (String.class,int.class); Person Person3= (person) constructor3.newinstance ("Jack", 23);        System.out.println (Person3.name); } Catch(Exception e) {}}//Reflection structure function private person (list list)@Test Public voidtest4 () {Try{Class C= Class.forName ("Com.reflction.person"); Constructor Constructor4=c.getdeclaredconstructor (java.util.List.class); //private things are not accessible to outsiders, but reflection can, compellingly openConstructor4.setaccessible (true);//violent reflexesArraylist<string> list=NewArraylist<string>(); List.add ("Name"); List.add ("Age"); Person Person4=(person) constructor4.newinstance (list);        System.out.println (Person4.name); } Catch(Exception e) {} }}

3: Method of Reflection Class

Method of Reflection Type */* The purpose of the method of reflection: To run the method * 1: Load the object Class.forName () * 2: Get the Method Getmethod,getdeclaredmethod () * 3: Run the method Invoke ();            */public class demo2{//reflection type public void AA () @Testpublic void Test1 () {try {human person=new person (); Load Class c = class.forname ("Com.reflction.person");//Obtain Method Method=c.getmethod ("AA", NULL);//The first method, the second one /Let the method Method.invoke (person, null);//Let the elephant run//For example: WC method, who must go to WC, to spread the person to the like} catch (Exception e) {}}//reflect this method public void Aa1 (String name,int age) @Testpublic void Test2 () {try {person person=new person (); Class C = class.forname ("Com.reflction.person"); Method Method=c.getmethod ("Aa1", String.class,int.class); Method.invoke (person, "Jack", 23);} catch (Exception e) {}}//reflect this method private void Aa2 (InputStream inputstream) @Testpublic void Test3 () {try {person person=new P Erson (); Class C = class.forname ("Com.reflction.person"); Method Method=c.getdeclaredmethod ("Aa2", Inputstream.class);//private method.setaccessible (TRUE);//Violence opens, Without this remark, private cannot visit Method.invoke (person, New FileInputStream ("F:\\1.txt"));} catch (Exception e) {}}//reflection public static void Aa3 (int num) @Testpublic void Test4 () throws Exception{person Person=new Perso N (); Class C = class.forname ("Com.reflction.person"); Method Method=c.getmethod ("Aa3", Int.class), Method.invoke (null,23),//static method does not have to be transferred to the image, and of course can also be transferred}}

3: Field of Reflection class

 

Reflection Field/** Reflection field purpose: To get the value of a field, or to block a field's value by * 1: Load class * 2: Fetch field * 3: Gets the value of that field *///public String Npublic class Demo3 {@Testpublic void tes T1 () throws Exception{person person=new person ();//load Class c = class.forname ("Com.reflction.person");//Fetch field fields Field=c.getfield ("name");//Obtain the field of the image, take the value of the field System.out.println (Field.get (person));//You can also set the value of the field field.set (person, "Xiaoli"); System.out.println (person.name);} The Reflection field private int password=123; @Testpublic void Test2 () throws Exception{person person=new person (); Class C = class.forname ("Com.reflction.person"); Field Field=c.getdeclaredfield ("password"); Field.setaccessible (true);//Violence opens PrivateSystem.out.println (Field.get ( person)); Field.set (person, 456); System.out.println (Field.get (person));}}

  

Java Reflection Classic Summary

Related Article

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.