Please refer to
thank you!
Abstract: JNode is an open source operating system based on Java. In this paper, the source code of its ClassLoader is analyzed, the lowest level implementation of dynamic class loading is discussed, especially the native code, which is always provided as dll file, is expounded completely. It may be helpful for understanding the Java language mechanism and the Java Virtual Machine Specification through an actual case, it also provides some references on implementing ClassLoader on special hardware. (awful english at that time...)
Java virtual machine is designed to cover the architectural difference; java programs are compiled into byte code and executed on JVM, ignoring what the platform is. Java programs do not need to compile all the classes needed before execution. The dynamic class loading mechanism can load the needed classed whenever needed.
|
文件: |
classloader.pdf |
大小: |
182KB |
下载: |
下载 | |
By studying the JDK source code (the above file), we can find that the implementation of java class loader is hidden in the /jre/bin/java.dll; no source code is available. What it provides is a group of interfaces that
JNode is an open source OS; it incorporates a JVM in it. By reading its source code, we can get some insight of the class loading mechanism.
Class Loader Model in JVM Specification
JVM Specification prescribe the rules of implementing class loader model, JNode conforms to these rules too.
(1) loading a class includes loading, linking and initializaion, while linking is the central part including verification, preparation and resolving. Loading read the class file into the vitural machine memory and transforms it into interal structure; linking verfiies the correctness of the class, prepares space for members and replaces symbol reference into direct reference; initializaion phrase initializes members of the class.
(2) Java programs run on virtual machines. Every virtual machine has a system class loader that does not depend on other loaders; it is initialized with the virtual machine, implemented by native code rather that user code.
(3) All class loaders are organized into a tree structure, using the system class loader as the root. This is similar to the process organization in Linux. Class loading adopts the assigning mechanism, at first the virtual machine tries to load the class using all existing class loaders, from the lowest level to the root. If no loaders is suitable, then use the user define class loader.
(4) Class file are read into method space, the class is instantiated in the heap.
VMSystem is static class that models the virture machine of JVM. Its static member VmSystemClassLoader is generated with the VMSystem; it is the root of all the class loaders in the system, just like the initial process of Linux. Whenever a classloader is instantiated, it is designated a parent loader as a parameter. Classloader has the type of VMClassLoader, as figure 1 shows. The loadClass function is the interface to load the class. The next figure shows the process of function invoktion.
Figure2
When encounter a class not yet resolved, the virtual machine try to load it using existing class loaders, starting from the lowest level (Parent.loadClass in Figure2). The higher the level, the more class that the loader can load. If all the existing class loaders are not suitable, the loadclass function return to the befinning and use findclass function (figure2) to find new class loaders.
The function defineClass is the actual function that do the work. It read in the class file into method space, verify the correctness, initialize the members(discussed above). When analyzing the class, it treat normal class and array class differently.
figure3
loadArrayClass is responsible for loading array class, while loader.loadClass invoke loadNormalClass to load common classes. Loading normal class is simple, just follow the steps defined in JVM specification. For array class, the loader seperated the array members into 3 types: normal class, array and objects. For normal class, create the normal class and then create the array.
return VmType.GetPrimitiveArray Class(compName.charAt(0))
For array member, create it recursively. For object members, create the object class firstly, and then
array class is delt with recursively. If the array memer is normal class, then
To load a primitive class, loadNormalClass typically follows several stpes: invoke getClassData to read the class file into a byteBuffer structure, invokes classDecoder.decodeClass to transform it into internal structure VmType. Note that if no existing class loaders work, the loader would invoke findclass to invoke defineclass, which at last invokes decodeClass too. S0 decodeClass is the lowest level function.
This function does the following things: check the magic number "CAFEBABE" and version number; construct the constant pool VMCP, using a group of constant type VMXX for each constant member; read into class name and direct super class name, initialize the VMType according to the type; read in and initialize the interface table VMType.interfaceTable; read in all members and allocates space for static members, storing in the sharestatic or isolatedstatic area; read in the method arrtribute and code, store in VMType.methodTable. This is the preparation phrase. The resolving phrase includes loading and resolving all parent classes; compute the offset of all non static members (VmInstantField in JNode), replace all the symbol reference with direct reference, store in the corresponding space; construct all the virtual function table; construct all the parent class array, sort in from the oldest; read in all finalize methods.
[1] Weidan Wu. "Analysis of Dynamic Clas s Loading in JNode". 2006
|
文件: |
JNode中的类动态装载机制分析.pdf |
大小: |
210KB |
下载: |
下载 | |
阅读(394) | 评论(0) | 转发(0) |