Reflection mechanism in java and java reflection mechanism

Source: Internet
Author: User

Reflection mechanism in java and java reflection mechanism

1. What is reflection?

The reflection mechanism is in the running state. For any class, all attributes and methods of the class can be known. For any object, any method and attribute of the class can be called, this function of dynamically obtaining information and calling methods of objects is called the reflection mechanism of java.

2. Significance of reflection

① A non-existent class or a class that will not exist in the future can be used through reflection;

② Through reflection, you can write the top-level general code;

③ Through reflection, you can access private members of a class (violent reflection );

3. A Class-loaded task

When we use a class for the first time, the class will be loaded, and the system will do two things:

<1> the bytecode files of this class are loaded into the memory to form a class loading zone (method zone)

<2> A Class object is created for this bytecode file.

4. When a class is loaded

① Create a class instance

② Class static variables or static variables are assigned values

③ Class static method called

④ Use reflection to obtain the Class Object of A Class

⑤ Initialize a subclass of A Class

Worker directly uses the java.exe command to run a main class

5. Steps for Using Reflection

Get the Class Object of A Class ------> get the java file -----> get the Internal Attributes and Methods

Figure:

 

5.1 three methods for getting Class objects

① Get Student. class by class Name

② Get Student. getClass () through the object ()

③ Obtain Class. forName ("pack01.Student") by fully qualified Class names ")

 

5.2 obtain the constructor of a class:

Get all the structures in a class: getConstructors () can only get public-modified structures

GetConstructor (Class <?>... ParameterTypes) can only obtain a single public-modified constructor.

Note:

You can create an object using a Class object without reflecting its constructor Clazz. newInstance ()

5.3 obtain the member methods of a class

Get all member methods of A Class: getMethods () can only get public modifier Methods

Note:

This method can be used to obtain both its own and its parent class.

GetMethod (String name, Class <?>... ParameterTypes) can only obtain a single public modifier method.

5.4 obtain the member variables of A Class

The get and set methods are reflected internally.

Configure brute-force access:

Field. setAccessible (true)

5.5Static Method reflection

Note:

① A static method can be called without creating an object. The parameter is null.

② The reflection of the variable parameter method is the same as that of the array method.

6. Case studies

Generic erasure in a set. An Integer Set stores other non-Integer data.

The above is a summary of my studies. If you have any mistakes, please correct me!

 

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.