Class loading mechanism and reflection

1. Java class loading mechanism

1. Overview

       After the Class file is loaded by the class loader, a meta-information object describing the Class structure will be formed in the JVM. Through the meta-information object, the structure information of the Class can be known: such as constructors, properties and methods. Java allows users to use the This Class-related meta-information object indirectly invokes the functions of the Class object.

      The virtual machine loads the data describing the class from the class file into the memory, verifies the data, converts, parses and initializes the data, and finally forms a Java type that can be directly used by the virtual machine. This is the class loading mechanism of the virtual machine.

2. Working Mechanism

      The class loader is to find the bytecode file of the class and construct the object component that the class represents inside the JVM. In Java, the class loader loads a class into the JVM and goes through the following steps:

     (1) Loading: Find and import Class files;

     (2) Link: merge the binary data of the class into the JRE;

        (a) Verification: Check the correctness of the loaded Class file data;

        (b) Preparation: allocate storage space to the static variables of the class;

        (c) Parsing: convert symbolic references to direct references;

     (3) Initialization: perform initialization operations on static variables and static code blocks of the class

 

 

      The dynamic extension of Java programs is achieved by dynamic loading and dynamic linking at runtime; for example, if you write an application that uses an interface, you can wait until the runtime to specify its actual implementation (polymorphism), and the parsing process can sometimes be performed in Executed after initialization; for example: dynamic binding (polymorphism);

        

      [Class initialization] 

      (1) When encountering the four bytecode instructions of new, getstatic, putstatic or invokestatic, if the class has not been initialized, its initialization needs to be triggered first. The most common Java code scenario that generates these 4 instructions is: when instantiating an object with the new keyword, reading or setting a static field of a class (modified by final, a static field that has put the result into the constant pool at compile time) fields), and when calling a static method of a class.

      (2) When using the method of the java.lang.reflect package to make a reflection call to a class, if the class has not been initialized, its initialization needs to be triggered first.

      (3) 当初始化一个类的时候,如果发现其父类还没有进行过初始化,则需要先触发其父类的初始化。

      (4)当虚拟机启动时,用户需要指定一个要执行的主类(包含main()方法的那个类),虚拟机会先初始化这个主类。

只有上述四种情况会触发初始化,也称为对一个类进行主动引用,除此以外,所有其他方式都不会触发初始化,称为被动引用

代码清单1

 

 

 

 

 

上述代码运行后,只会输出【---SuperClass init】, 而不会输出【SubClass init】,对于静态字段,只有直接定义这个字段的类才会被初始化,因此,通过子类来调用父类的静态字段,只会触发父类的初始化,但是这是要看不同的虚拟机的不同实现。

代码清单2

 

 

 

 

 

此处不会引起SuperClass的初始化,但是却触发了【[Ltest.SuperClass】的初始化,通过arr.toString()可以看出,对于用户代码来说,这不是一个合法的类名称,它是由虚拟机自动生成的,直接继承于Object的子类,创建动作由字节码指令newarray触发,此时数组越界检查也会伴随数组对象的所有调用过程,越界检查并不是封装在数组元素访问的类中,而是封装在数组访问的xaload,xastore字节码指令中.

代码清单3

 

 

 

 

 

对常量ConstClass.value 的引用实际都被转化为NotInitialization类对自身常量池的引用,这两个类被编译成class后不存在任何联系。

 

          【装载】

    在装载阶段,虚拟机需要完成以下3件事情

        (1) 通过一个类的全限定名来获取定义此类的二进制字节流

        (2) 将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构

        (3) 在Java堆中生成一个代表这个类的java.lang.Class对象,作为方法区这些数据的访问入口。

    虚拟机规范中并没有准确说明二进制字节流应该从哪里获取以及怎样获取,这里可以通过定义自己的类加载器去控制字节流的获取方式。

        

         【验证】

    虚拟机如果不检查输入的字节流,对其完全信任的话,很可能会因为载入了有害的字节流而导致系统奔溃。

 

         【准备】

    准备阶段是正式为类变量分配并设置类变量初始值的阶段,这些内存都将在方法区中进行分配,需要说明的是:

这时候进行内存分配的仅包括类变量(被static修饰的变量),而不包括实例变量,实例变量将会在对象实例化时随着对象一起分配在Java堆中;这里所说的初始值“通常情况”是数据类型的零值,假如:

public static int value = 123;

value在准备阶段过后的初始值为0而不是123,而把value赋值的putstatic指令将在初始化阶段才会被执行

  

二、类加载器与双亲委派模型

      类加载器

     (1) Bootstrap ClassLoader : 将存放于<JAVA_HOME>\lib目录中的,或者被-Xbootclasspath参数所指定的路径中的,并且是虚拟机识别的(仅按照文件名识别,如 rt.jar 名字不符合的类库即使放在lib目录中也不会被加载)类库加载到虚拟机内存中。启动类加载器无法被Java程序直接引用

     (2) Extension ClassLoader : 将<JAVA_HOME>\lib\ext目录下的,或者被java.ext.dirs系统变量所指定的路径中的所有类库加载。开发者可以直接使用扩展类加载器。

     (3) Application ClassLoader : 负责加载用户类路径(ClassPath)上所指定的类库,开发者可直接使用。

     

双亲委派模型

 

工作过程:如果一个类加载器接收到了类加载的请求,它首先把这个请求委托给他的父类加载器去完成,每个层次的类加载器都是如此,因此所有的加载请求都应该传送到顶层的启动类加载器中,只有当父加载器反馈自己无法完成这个加载请求(它在搜索范围中没有找到所需的类)时,子加载器才会尝试自己去加载。

     好处:java类随着它的类加载器一起具备了一种带有优先级的层次关系。例如类java.lang.Object,它存放在rt.jar中,无论哪个类加载器要加载这个类,最终都会委派给启动类加载器进行加载,因此Object类在程序的各种类加载器环境中都是同一个类。相反,如果用户自己写了一个名为java.lang.Object的类,并放在程序的Classpath中,那系统中将会出现多个不同的Object类,java类型体系中最基础的行为也无法保证,应用程序也会变得一片混乱。

 

       java.lang.ClassLoader中几个最重要的方法:

复制代码
//加载指定名称(包括包名)的二进制类型,供用户调用的接口
public Class<?> loadClass(String name);
//加载指定名称(包括包名)的二进制类型,同时指定是否解析(但是,这里的resolve参数不一定真正能达到解析的效果),供继承用
protected synchronized Class<?> loadClass(String name, boolean resolve);
protected Class<?> findClass(String name)
//定义类型,一般在findClass方法中读取到对应字节码后调用,可以看出不可继承(说明:JVM已经实现了对应的具体功能,解析对应的字节码,产生对应的内部数据结构放置到方法区,所以无需覆写,直接调用就可以了)
protected final Class<?> defineClass(String name, byte[] b, int off, int len) throws ClassFormatError{}
复制代码

如下是实现双亲委派模型的主要代码:

 

 

 

 

 

 

 

 

 

 

 

三、反射

      Reflection机制允许程序在正在执行的过程中,利用Reflection APIs取得任何已知名称的类的内部信息,包括:package、 type parameters、 superclass、 implemented interfaces、 inner classes、 outer classes、 fields、 constructors、 methods、 modifiers等,并可以在执行的过程中,动态生成instances、变更fields内容或唤起methods。

      1、获取构造方法

Class类提供了四个public方法,用于获取某个类的构造方法。

Constructor getConstructor(Class[] params)     

根据构造函数的参数,返回一个具体的具有public属性的构造函数

    Constructor getConstructors()     

返回所有具有public属性的构造函数数组

    Constructor getDeclaredConstructor(Class[] params)     

根据构造函数的参数,返回一个具体的构造函数(不分public和非public属性)

    Constructor getDeclaredConstructors()    

返回该类中所有的构造函数数组(不分public和非public属性)

 

 

 

 

 

 

 

 

 

 

 

2、获取类的成员方法

与获取构造方法的方式相同,存在四种获取成员方法的方式。 

Method getMethod(String name, Class[] params)    

根据方法名和参数,返回一个具体的具有public属性的方法

    Method[] getMethods()    

返回所有具有public属性的方法数组

    Method getDeclaredMethod(String name, Class[] params)    

根据方法名和参数,返回一个具体的方法(不分public和非public属性)

    Method[] getDeclaredMethods()    

返回该类中的所有的方法数组(不分public和非public属性)

  

 

 

 

 

 

 

 

 

 

 

 

 

3、获取类的成员变量(成员属性)

存在四种获取成员属性的方法

    Field getField(String name)    

根据变量名,返回一个具体的具有public属性的成员变量

    Field[] getFields()  

返回具有public属性的成员变量的数组

    Field getDeclaredField(String name)  

根据变量名,返回一个成员变量(不分public和非public属性)

    Field[] getDelcaredFields()    

返回所有成员变量组成的数组(不分public和非public属性)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326219295&siteId=291194637