Reflection mechanisms and JavaBean in Java

Source: Internet
Author: User
Tags reflection

Reflection
Reflection: This class is dissected by a class bytecode file that is loaded into the stack's memory when it is loaded into the method area.

By reflection you can get a constructor method for a class, a member method, a member variable
Reflection maps the various parts of a class into corresponding classes

Reflection Acquisition Construction Method
Methods in class classes
Constructor<?>[] GetConstructors () returns the construction method of all public adornments for the current bytecode file object
Constructor<t> GetConstructor (class<?>...parametertypes) returns the constructor of the specified parameter (because it is found in a bytecode file, so the parameter is also a bytecode file)
Parameters
The class Constructor used to describe the construction method returned is the Constructor object
The class<?>...parametertypes variable parameter is the constructor method parameter type if the parameter is not written, the null parameter construction method is returned
Constructor
T newinstance (Object...initargs) Run Construction method Create object
The return value T is generic no generic is type Object
parameter is the constructor method to use the actual parameter parameter is not written to run is the Null parameter construction method (when the method runs, the parameter passed in is the normal parameter)

1. Get the bytecode file object
2. Get constructed method objects from a bytecode file object
3. Calling methods by constructing method objects to run the construction method

Reflection gets the shortcut to the object
Methods for class classes
T newinstance () creates a new instance of the class represented by this class object
Prerequisite must have NULL parameter construction NULL parameter construction must be public decorated
1. Get the bytecode file object
2. Create an object by calling Newinstance () via bytecode file byte

Reflection gets member variables (because member variables are private, so it is seldom used)
Methods in class classes
Because property names cannot be duplicated, field can be obtained by simply passing parameter names
Field GetField (String name) returns the member variable for the specified variable name
Field[] GetFields () returns all member variables
Methods in the Field class
void set (Object obj, object value) assigns a value to the member variable of the specified object, obj is the object to be assigned, and value is the
Object get (Object obj) returns the value of this property for this object
1. Get the bytecode file object
2. Obtaining a Field object from a bytecode file object
3. Call the Set method to assign a value to a field note that an object can be created by a shortcut given a

Violent reflexes:
If you need to get a private attribute or method in the bytecode
Field[] Getdeclaredfields ()
Method[] Getdeclaredmethods ()
You can also get a private specified property or method
Method Getdeclaredmethod (String name, Class<?> .... parametertypes

Method[] Getdeclaredmethods () returns all methods in the bytecode file, without constructs, without inheritance

  After you get a private property or method, set it to reflection when you cancel the language access check

void Setaccessible (Boolean flag)

Reflection Get Member Method
Methods in class classes
Method[] Getmehthods Get all public methods (including inherited)
Method Getmehthod (String name, class<?>...parametertypes) String.class Int.class
Gets the specified public method
Name: Method Name
Parameters required by the Class<?>...parametertypes method (String.class)
1. Get the bytecode file object
2. Obtaining a Method object from a bytecode file object
3. Call the Invoke method to run the current method, requiring the object

Generics in Java are pseudo generics, only valid at compile time, and can be skipped directly with reflection over the compilation period to avoid generics

Gets the object of the bytecode file

1: Class name.

2: Create an instance object of the class, and then call GetClass () method that inherits from object

3:class.forname ("Address of this class") Example: Class.forName ("Java.util.List")

JavaBean: Encapsulation of an entity class, with attributes, Get/set methods, NULL parameter constructs, implementation of serializable interfaces, generally set their own serialization ID (prevent serialization after the modification of class deserialization failed)

Reflection mechanisms and JavaBean in Java

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.