Black Horse programmer _ reflection

Source: Internet
Author: User

 

Reflection

1. Origin:

Later extension of the program without source code modification

2. java. Lang. Class description. Class class

Java. Lang. Reflect. constructor describes the constructor class

Java. Lang. Reflect. Field class that describes member variables

Java. Lang. Reflect. method class that describes member Methods

3. Reflection:

A. Get the Class Object

First: Get Through objects

Person P = new person ();

Getclass () in object. The returned value is of the class type.

Second:

Obtained through the static attribute class of the class

Method 3: Get it through the static forname method of the class

Class. forname ("package name. person"); // write the full class name

B. Obtain the constructor

Constructor [] getconstructors () in the class to obtain the constructor's public permissions,

Constructor getconstructor (class... C) in the class obtains the constructor of the specified parameter list with public permissions. If you get a list of parameters without variable parameters, you can leave it empty.

The constructor [] getdeclaredconstructors () in the class obtains all constructors.

In the class, constructor getdeclaredconstructor (class... C) can independently obtain the private constructor.

    

Run the constructor:

Object newinstance (object... O) in the constructor class)

If setaccessible (Boolean 0) in the accessible class of the parent class of the constructor class is true, the JVM access check is canceled during the runtime to run the private constructor.

In the class, object newinstance () can directly obtain and run the constructor of the public-modified null parameter. Make sure that this method exists.

C. obtain common methods:

The method [] getmathods () in the class returns methods in the bytecode file. These methods are public-modified, including inherited and implemented methods.

In the class, method getmethod (string method name, class... C) returns a method, passing the method name and parameter list

Getdeclaremethod (string method name, class... C) in the class can obtain the private constructor.

Run the common method:

Object invoke in method class (Object O object, parameter in object... ARGs method)

 

4. Use reflection to implement temporary erasure of generics

1 import Java. util. *; 2 Import Java. lang. reflect. *; 3 Public class listdemo {4 public static void main (string [] ARGs) throws exception {5 6 arraylist <string> array = new arraylist <string> (); 7 array. add ("ASDF"); 8 array. add ("dgfer"); 9 // get the bytecode file object of the arraylist class 10 class clazz = array. getclass (); 11 // obtain the add12 method = clazz. getmethod ("add", object. class); 13 method. invoke (array, 123); 14 method. invoke (array, 12.34); 15 method. invoke (array, true); 16 system. out. println (array); 17 iterator it = array. iterator (); 18 while (it. hasnext () {19 system. out. println (it. next (); 20} 21} 22}

 

Black Horse programmer _ reflection

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.