Overview of the reflection of Java

Source: Internet
Author: User

Class loader and reflection

Class Loader:

Loading process for Class 1:
When a program wants to use a class, if the class has not yet been loaded into memory, the system initializes the class by loading, connecting, and initializing the three steps.
Load: Refers to reading the class file into memory and creating a class object for it. The system creates a class object when any class is used
Connection: Verify that there is a correct internal structure and that it is consistent with other classes.
Prepare to allocate memory for static members of the class and set default initialization values
parsing, replacing a symbolic reference in a class's binary data with a direct reference
Initialize: Stack, heap memory space, create object, default initialize, display initialize, construct initialize


Loading time for Class 2
1) Create an instance of the class
2) Accessing static variables of a class, or assigning values to static variables of a class
3) Calling the static method of the class
4) Use the Reflection method to force the creation of a Java.lang.Class object for a class or interface object
5) Initializes a subclass of a class: When the subclass is initialized, its parent class is loaded first
6) run a main class directly using the Java.exe command

The main role of Class 3 loaders:
Responsible for loading the. class file into memory and generating the corresponding class object for it

Composition of Class 4 loaders
1) root classloader : Bootstrap ClassLoader (String class we use)
Also known as the Boot class loader, which is responsible for loading the Java core class. For example, System Object string, etc., in the Rt.jar file in the Lib directory of the JRE in the JDK
2) Extension class loader : Extension ClassLoader
Responsible for loading JRE packages in the extended directory of the JRE. The Lib directory of the JRE in the JDK under the EXT directory
3) system ClassLoader: Systems ClassLoader (Load our own)
Responsible for loading the class file from the Java command when the JVM starts and the jar package and classpath referred to by the CLASSPATH environment variable


Reflection:

1 Reflection Mechanism
The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can invoke its arbitrary methods and properties, this
The ability to dynamically obtain information and to dynamically invoke the object's methods is called the reflection mechanism of the Java language.
That is, by using the class file object, use the member variable, constructor method, member method in the file

To dissect a class, you must first obtain the bytecode file object for that class.
And the anatomy uses the method in class. So first get to each byte code file corresponding to the class corresponding to the class type of object

2 How to get a class object: You must get the class bytecode file object, which is actually the object of class
Suppose the People.java is packaged in Com.hzw.pojo.People.
   1) through the GetClass () method of the object class
Create an object that gets the class object through the GetClass method:
People p = new people ();
class people = P.getclass ();//Returns the run-time class object for this object.

   2) The class object is obtained from the class name by the static method of Class forname ()
Class people = class.forname ("Com.hzw.pojo.People");

   3) Static property class for data type (basic and Reference)
Class people = People.class;
Class C = int.class; Automatic Boxing
Class C1 = String.class;
Note: In general, the second type is used in development because the second is a string, not a specific class name. This allows the string to be configured in the configuration file.

3 Class object

Class: Common Classes
Member variables: Field
Construction Method: Constructor
Member Methods: Method
Reflection: Get the three corresponding objects by using the class method, and then call their methods through the Filed/method/contructor object to really use the class's members, methods, constructs

4 Use of Reflections
1) The structure of the reflection uses:
Main methods:
Constructor<t> getcontructor (class<?> ... paramtypes)
Parameter: parametertypes-parameter Array (parameter of class type)
Return value: A Constructor object that reflects the specified public construction method for the class represented by this class object.


Constructor<?>[] getcontructors ()
Returns an array containing some Constructor objects that reflect all public constructors of the class represented by this class object.

constructor<t> getdeclaredcontructor (class<?> ... paramtypes)
parameter: parametertypes-parameter array   ; The
returns a Constructor object that reflects the specified construction method for the class or interface represented by this class object.

Constructor<?>[] getdeclaredcontructors ()
Returns an array of Constructor objects that reflect all of the constructor methods of the class declaration represented by this class object.

Instance:
Class C = class.forname ("Com.hzw.pojo.People"); Get the class file object
Contructor con = c.getcontructor ();//gets its specified constructor object, here is the default construct
Object obj = Con.newinstance ();//Create an instance from the constructor object
System.out.println (obj);//Print the object directly, that is, the ToString () method that invokes the object instance

2) Reflection of the members using:
Main methods:
Field Getdeclaredfield (String name)
Parameter: Name-field name
Returns a Field object that reflects the specified declared field of the class or interface represented by this class object.

Field[] Getdeclaredfields ()
Returns an array of field objects that reflect all the fields declared by the class or interface represented by this class object.

Field GetField (String name)
Parameter: Name-field name
Returns a Field object that reflects the specified public member field of the class or interface represented by this class object.

Field[] GetFields ()

Returns an array containing some field objects that reflect all accessible public fields for the class or interface represented by this class object.


3) Reflection of the Member method:

Main methods:
Method GetMethod (String name, Class<?> .... parametertypes)
Returns a method object that returns the public member methods of the class or interface represented by this class object .
Parameter: Name-method name Parametertypes-parameter list
  
Method[] GetMethods ()
Returns an array containing some method objects that reflect the public member methods of the class or interface represented by this class object .

Method Getdeclaredmethod (String name, Class<?> .... parametertypes)
Parameter: Name-method name Parametertypes-parameter array
Returns: A method object that reflects the specified declared method of the class or interface represented by this class object (which can be obtained without any modifier adornments).

Method[] Getdeclaredmethods ()
Returns an array of method objects that reflect all methods of the class or interface declaration represented by this class object, including public, protected, default (package) access, and private methods, but not inherited methods .

5 When you encounter an illegalaccessexception exception when using , to invoke the Setaccessible (Boolean flag) method, set the Cancel Java language access check.

 setaccessible(Boolean flag)   SecurityException
accessible true false indicates that the reflected object should implement a Java language access check.

Overview of the reflection of 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.