New characteristics of 23--JDK 7-reflection 2-filed, construction Class (construction method)

Source: Internet
Author: User
Tags reflection

1. The constructor class provides the following methods for creating objects for a class:

Public Object newinstance (Object...initargs)

Initargs used to specify parameters received by the constructor

Learn more:Sun Company creates an object for simplifying developers, and it also provides a newinstance method in the class object to create objects for the class. This allows developers to avoid having to reflect the constructor class each time to create objects.

However , it is important to note that the Class.newinstance method internally is an object created by a reflection class parameterless constructor, so when creating a class object in this way, the class must have an parameterless constructor.

Specific case Analysis

packageCom.java.reflect;

importjava.lang.reflect.Constructor;

importjava.lang.reflect.InvocationTargetException;

importorg.junit.Test;

Publicclass Demo2 {

@Test

Reflects the parameterless build function and creates the person object

publicvoid test1 ()throws classnotfoundexception,nosuchmethodexception, SecurityException, Instantiationexception,illegalaccessexception, IllegalArgumentException, invocationtargetexception {

Load class

Class C = class.forname ("Com.java.reflect.Person");

The method of reflection construction

Constructor cs = C.getconstructor (null);

Person P = (person) cs.newinstance (null);

System.out.println (P.getname ());

The output result is: parameterless constructor Zhangsan

}

@Test

Reflection out of a build function with one argument public person (String name)

publicvoid test2 ()throws ClassNotFoundException, Nosuchmethodexception,securityexception, Instantiationexception, Illegalaccessexception,illegalargumentexception, invocationtargetexception {

Load class

Class C = class.forname ("Com.java.reflect.Person");

The method of reflection construction

Constructor cs = C.getconstructor (String. Class);

Person P = (person) cs.newinstance ("Dick");

System.out.println (P.getname ());

The output is: Dick Zhangsan analysis: The first output Lee four is the calling of the constructor method, the first Zhangsan is the property value in the person class, because there is no this.name = name in the constructor for this parameter

}

@Test

Reflection out of a constructor with two arguments public person (String Name,int age)

publicvoid test3 ()throws classnotfoundexception,nosuchmethodexception, SecurityException, Instantiationexception,illegalaccessexception, IllegalArgumentException, invocationtargetexception {

Load class

Class C = class.forname ("Com.java.reflect.Person");

The method of reflection construction

Constructor cs = C.getconstructor (String. Class,int. class);

Person P = (person) cs.newinstance ("Dick", 20);

System.out.println (P.getname ());

The output is: Dick ... Zhangsan

}

@Test

Reflects private constructors (requires a getdeclaredconstructor () method)

publicvoid test4 ()throws classnotfoundexception,nosuchmethodexception, SecurityException, Instantiationexception,illegalaccessexception, IllegalArgumentException, invocationtargetexception {

Load class

Class C = class.forname ("Com.java.reflect.Person");

The method of reflection construction

Constructor cs = c.getdeclaredconstructor (int. Class);

Cs.setaccessible (true); If it is private, you must set access permissions

Person P = (person) cs.newinstance (60);

System.out.println (P.getname ());

The output is: 60zhangsan

}

@Test

Using the Newinstance () method of class classes to reflect the method of constructing without parameters

publicvoid test5 ()throws Classnotfoundexception,instantiationexception, illegalaccessexception {

Load class

Class C = class.forname ("Com.java.reflect.Person");

Person P = (person) c.newinstance ();

System.out.println (P.getname ());

The output result is: The parameterless construction method Zhangsan

}

}

2. The method object provides the following methods for executing the method it represents:

public Object Invoke(object Obj,object ... args)

This is essentially the same as when the construction class calls the constructor method:

Specific cases:

packageCom.java.reflect;

importjava.lang.reflect.InvocationTargetException;

importJava.lang.reflect.Method;

importorg.junit.Test;

Publicclass Demo3 {

/**

* @param args

* @throws classnotfoundexception

* @throws SecurityException

* @throws nosuchmethodexception

* @throws invocationtargetexception

* @throws illegalargumentexception

* @throws illegalaccessexception

*/

@Test

Call parameterless method public void run ()

publicvoid test1 ()throws Classnotfoundexception,nosuchmethodexception,

Securityexception,illegalaccessexception,

illegalargumentexception,invocationtargetexception {

Person p = new person ();

Class C = class.forname ("Com.java.reflect.Person");

Method m = C.getmethod ("Run",null);

M.invoke (p, null);

Output result: The parameterless constructor (because the parameterless constructor of the person class was called) run ....

}

@Test

Call method with one parameter public void run (String name)

publicvoid test2 ()throws ClassNotFoundException, Nosuchmethodexception,

Securityexception,illegalaccessexception,

illegalargumentexception,invocationtargetexception {

Person p = new person ();

Class C = class.forname ("Com.java.reflect.Person");

Method m = C.getmethod ("Run", String. ) Class);

M.invoke (P, "Yang");

Output Result: parameterless constructor (because the parameterless constructor of the person class was called) run ... Yang

}

@Test

Call method with two arguments public void run (int i,int j)

publicvoid test3 ()throws Classnotfoundexception,nosuchmethodexception,

Securityexception,illegalaccessexception,

illegalargumentexception,invocationtargetexception {

Person p = new person ();

Class C = class.forname ("Com.java.reflect.Person");

Method m = C.getmethod ("sum",int. Class,int. class);

System.out.println (M.invoke (p, 10, 20));

/*

* The following output results are as int value = (Integer) m.invoke (p,10,20);

*system.out.println (value);

*/

Output Result: parameterless constructor (because the parameterless constructor of the person class was called) 30

}

@Test

Call private method prvate void sum ()

publicvoid test4 ()throws Classnotfoundexception,nosuchmethodexception,

Securityexception,illegalaccessexception,

illegalargumentexception,invocationtargetexception {

Person p = new person ();

Class C = class.forname ("Com.java.reflect.Person");

Method m = C.getdeclaredmethod ("sum",null);

M.setaccessible (true);

M.invoke (p, null);

Output Result: parameterless constructor (because the parameterless constructor of the person class is called) private ....

}

@Test

Call static method public static void Run1 ()

publicvoid test5 ()throws Classnotfoundexception,nosuchmethodexception,

SecurityException, Illegalaccessexception,

illegalargumentexception,invocationtargetexception {

Person P = Newperson (); Calling a static method can not pass in an object

Class C = class.forname ("Com.java.reflect.Person"

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.