Java Reflection Knowledge Point finishing

Source: Internet
Author: User
Tags reflection
1.1 Class Classes
Three ways to get the class object:
Way one: Through the GetObject () method in the object class
Person p = new person ();
Class C = P.getclass ();
Mode two: Get to bytecode file object by class name. Class (Any data type has a class static property that looks simpler than the first method)
Class C2 = Person.class;
Mode three: Pass the class's masterpiece as a string to the static method in the class class by means of the method in the Class class (Forname).
Class C3 = Class.forName ("person");


1.2 Get the construction method through reflection and use the
You can get the construction method from the method provided in the class class (Byte code object):
Mode one: Returns a construction method
No.1 (general access) public constructor<t> getconstructor (class<?> ... parametertypes) gets the public decoration, specifying the construction method corresponding to the parameter type
No.2 (violence acquired) public constructor<t> getdeclaredconstructor (class<?> ... parametertypes) Gets the constructor method for the specified parameter type ( Contains private)


Mode two: Return multiple construction methods
No.1 (General access) public constructor<?>[] GetConstructors () gets all the public-modified construction methods
No.2 (Brute gain) public constructor<?>[] Getdeclaredconstructors () gets all the construction methods (including private)


1.2.1 Through reflection, gets the construction method, creates the object
1. Get to Class object
2. Get the specified construction method
3. Create an object by constructing methods in the method class constructor
Public T newinstance (Object ... initargs)


1.2.2 by reflection, get private construction methods, create objects
Get the private construction method as follows:
1. Get to Class object
2. Get the specified construction method
3. Violence access, setting access rights through the Setaccessible (Boolean flag) method of class constructor
4. Create an object by constructing methods in the method class constructor
Public T newinstance (Object ... initargs)
1.3 Gets the member variable by reflection and uses the
Mode one: Returns a member variable
No.1 (General get) public Field GetField (String name) gets the specified public-decorated variable
No.2 (Brute gain) public Field Getdeclaredfield (String name) gets the specified arbitrary variable
Mode two: Return multiple construction methods
No.1 (General get) public field[] GetFields () gets all public-decorated variables
No.2 (Brute gain) public field[] Getdeclaredfields () gets all the variables (including private)


1.3.1 through reflection, create objects, get specified member variables, assign values and get value operations
To get the member variable, follow these steps:
1. Get Class object
2. Get the Construction method
3. Create object obj by constructing a method
4. Get the specified member variable (private member variable, brute access via Setaccessible (Boolean flag) method)
5. Assign a value to a specified member variable of a specified object or get a value by using the Class field method
 Assignment: public void set (Object obj, Object value)
Sets the member variable represented by this Field object to the specified new value in the specified object obj
Value: Public object get (object obj)
Returns the value of the member variable represented by this Field object in the specified object obj

1.4 Get member methods by reflection and use
Parameter 1:name the method name to find; 2:parametertypes parameter type of the method
Mode one: Returns a method
Gets the method of the public adornment:
public method GetMethod (String name, Class<?> ... parametertypes)
Get any method, including private: 
Public method Getdeclaredmethod (String name, Class<?> ... parametertypes)
Mode two: Return multiple construction methods
public method[] GetMethods () gets all the public-decorated methods in this class and the parent class
Public method[] Getdeclaredmethods () gets all the methods in this class (contains private)
1.4.1 through reflection, creates an object, invokes the specified method
To get a member method, follow these steps:
1. Get Class object
2. Get the Construction method
3. Create an object by constructing a method
4. Get the specified method
5. Implementation of the Found method:
public object Invoke (Object obj, object ... args)
Executes the method represented by the current method object in obj, the specified object, by passing the parameter specified by args.


1.4.2 through reflection, creates an object, invokes the specified private method
To get the private member method, follow these steps:
1. Get Class object
2. Get the Construction method
3. Create an object by constructing a method
4. Get the specified method
5. Opening of violent visits
6. Implementation of the Found method
public object Invoke (Object obj, object ... args)
Executes the method represented by the current method object in obj, the specified object, by passing the parameter specified by args.































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.