随笔-128  评论-55  文章-5  trackbacks-0

什么是 ClassLoader?

在流行的商业化编程语言中,Java 语言由于在 Java 虚拟机 (JVM) 上运行而显得与众不同。这意味着已编译的程序是一种特殊的、独立于平台的格式,并非依赖于它们所运行的机器。在很大程度上,这种格式不同于传统的可执行程序格式。
与 C 或 C++ 编写的程序不同,Java 程序并不是一个可执行文件,而是由许多独立的类文件组成,每一个文件对应于一个 Java 类。

此外,这些类文件并非立即全部都装入内存,而是根据程序需要装入内存。ClassLoader 是 JVM 中将类装入内存的那部分。
而且,Java ClassLoader 就是用 Java 语言编写的。这意味着创建您自己的 ClassLoader 非常容易,不必了解 JVM 的微小细节。


为什么编写 ClassLoader?

如果 JVM 已经有一个 ClassLoader,那么为什么还要编写另一个呢?问得好。缺省的 ClassLoader 只知道如何从本地文件系统装入类文件。不过这只适合于常规情况,即已全部编译完 Java 程序,并且计算机处于等待状态。

但 Java 语言最具新意的事就是 JVM 可以非常容易地从那些非本地硬盘或从网络上获取类。例如,浏览者可以使用定制的 ClassLoader 从 Web 站点装入可执行内容。

有许多其它方式可以获取类文件。除了简单地从本地或网络装入文件以外,可以使用定制的 ClassLoader 完成以下任务:

在执行非置信代码之前,自动验证数字签名
使用用户提供的密码透明地解密代码
动态地创建符合用户特定需要的定制化构建类
任何您认为可以生成 Java 字节码的内容都可以集成到应用程序中。

 

ClassLoader的结构

概述

ClassLoader 的基本目标是对类的请求提供服务。当 JVM 需要使用类时,它根据名称向 ClassLoader 请求这个类,然后 ClassLoader 试图返回一个表示这个类的 Class 对象。

通过覆盖对应于这个过程不同阶段的方法,可以创建定制的 ClassLoader。

在本章的其余部分,您会学习 Java ClassLoader 的关键方法。您将了解每一个方法的作用以及它是如何适合装入类文件这个过程的。您也会知道,创建自己的 ClassLoader 时,需要编写什么代码。

在下一章中,您将会利用这些知识来使用我们的 ClassLoader 示例 -- CompilingClassLoader。


1. 方法 loadClass

ClassLoader.loadClass() 是 ClassLoader 的入口点。其特征如下:

Class loadClass( String name, boolean resolve );

name 参数指定了 JVM 需要的类的名称,该名称以包表示法表示,如 Foo 或 java.lang.Object。

resolve 参数告诉方法是否需要解析类。在准备执行类之前,应考虑类解析。并不总是需要解析。如果 JVM 只需要知道该类是否存在或找出该类的超类,那么就不需要解析。

在 Java 版本 1.1 和以前的版本中,loadClass 方法是创建定制的 ClassLoader 时唯一需要覆盖的方法。(Java 2 中 ClassLoader 的变动提供了关于 Java 1.2 中 findClass() 方法的信息。)

 

2. 方法 defineClass

defineClass 方法是 ClassLoader 的主要诀窍。该方法接受由原始字节组成的数组并把它转换成 Class 对象。原始数组包含如从文件系统或网络装入的数据。
defineClass 管理 JVM 的许多复杂、神秘和倚赖于实现的方面 -- 它把字节码分析成运行时数据结构、校验有效性等等。不必担心,您无需亲自编写它。事实上,即使您想要这么做也不能覆盖它,因为它已被标记成最终的。


3. 方法 findSystemClass

findSystemClass 方法从本地文件系统装入文件。它在本地文件系统中寻找类文件,如果存在,就使用 defineClass 将原始字节转换成 Class 对象,以将该文件转换成类。当运行 Java 应用程序时,这是 JVM 正常装入类的缺省机制。(Java 2 中 ClassLoader 的变动提供了关于 Java 版本 1.2 这个过程变动的详细信息。)
对于定制的 ClassLoader,只有在尝试其它方法装入类之后,再使用 findSystemClass。原因很简单:ClassLoader 是负责执行装入类的特殊步骤,不是负责所有类。例如,即使 ClassLoader 从远程的 Web 站点装入了某些类,仍然需要在本地机器上装入大量的基本 Java 库。而这些类不是我们所关心的,所以要 JVM 以缺省方式装入它们:从本地文件系统。这就是 findSystemClass 的用途。

其工作流程如下:

请求定制的 ClassLoader 装入类。
检查远程 Web 站点,查看是否有所需要的类。
如果有,那么好;抓取这个类,完成任务。
如果没有,假定这个类是在基本 Java 库中,那么调用 findSystemClass,使它从文件系统装入该类。

在大多数定制 ClassLoaders 中,首先调用 findSystemClass 以节省在本地就可以装入的许多 Java 库类而要在远程 Web 站点上查找所花的时间。然而,正如,在下一章节所看到的,直到确信能自动编译我们的应用程序代码时,才让 JVM 从本地文件系统装入类。


4. 方法 resolveClass

正如前面所提到的,可以不完全地(不带解析)装入类,也可以完全地(带解析)装入类。当编写我们自己的 loadClass 时,可以调用 resolveClass,这取决于 loadClass 的 resolve 参数的值。

 
5. 方法 findLoadedClass

findLoadedClass 充当一个缓存:当请求 loadClass 装入类时,它调用该方法来查看 ClassLoader 是否已装入这个类,这样可以避免重新装入已存在类所造成的麻烦。应首先调用该方法。

组装

让我们看一下如何组装所有方法。

我们的 loadClass 实现示例执行以下步骤。(这里,我们没有指定生成类文件是采用了哪种技术 -- 它可以是从 Net 上装入、或者从归档文件中提取、或者实时编译。无论是哪一种,那是种特殊的神奇方式,使我们获得了原始类文件字节。)


调用 findLoadedClass 来查看是否存在已装入的类。

如果没有,那么采用那种特殊的神奇方式来获取原始字节。

如果已有原始字节,调用 defineClass 将它们转换成 Class 对象。

如果没有原始字节,然后调用 findSystemClass 查看是否从本地文件系统获取类。

如果 resolve 参数是 true,那么调用 resolveClass 解析 Class 对象。

如果还没有类,返回 ClassNotFoundException。

否则,将类返回给调用程序。

推想
现在您已经了解了 ClassLoader 的工作原理,现在该构建一个了。在下一章中,我们将讨论 CCL。

 
CCL 揭密

我们的 ClassLoader (CCL) 的任务是确保代码被编译和更新。

下面描述了它的工作方式:

当请求一个类时,先查看它是否在磁盘的当前目录或相应的子目录。

如果该类不存在,但源码中有,那么调用 Java 编译器来生成类文件。

如果该类已存在,检查它是否比源码旧。如果是,调用 Java 编译器来重新生成类文件。

如果编译失败,或者由于其它原因不能从现有的源码中生成类文件,返回 ClassNotFoundException。

如果仍然没有该类,也许它在其它库中,所以调用 findSystemClass 来寻找该类。

如果还是没有,则返回 ClassNotFoundException。

否则,返回该类。

 

Java 编译的工作方式

在深入讨论之前,应该先退一步,讨论 Java 编译。通常,Java 编译器不只是编译您要求它编译的类。它还会编译其它类,如果这些类是您要求编译的类所需要的类。

CCL 逐个编译应用程序中的需要编译的每一个类。但一般来说,在编译器编译完第一个类后,CCL 会查找所有需要编译的类,然后编译它。为什么?Java 编译器类似于我们正在使用的规则:如果类不存在,或者与它的源码相比,它比较旧,那么它需要编译。其实,Java 编译器在 CCL 之前的一个步骤,它会做大部分的工作。

当 CCL 编译它们时,会报告它正在编译哪个应用程序上的类。在大多数的情况下,CCL 会在程序中的主类上调用编译器,它会做完所有要做的 -- 编译器的单一调用已足够了。

然而,有一种情形,在第一步时不会编译某些类。如果使用 Class.forName 方法,通过名称来装入类,Java 编译器会不知道这个类时所需要的。在这种情况下,您会看到 CCL 再次运行 Java 编译器来编译这个类。在源代码中演示了这个过程。

 

使用 CompilationClassLoader

要使用 CCL,必须以特殊方式调用程序。不能直接运行该程序,如:

% java Foo arg1 arg2

应以下列方式运行它:

% java CCLRun Foo arg1 arg2

CCLRun 是一个特殊的存根程序,它创建 CompilingClassLoader 并用它来装入程序的主类,以确保通过 CompilingClassLoader 来装入整个程序。CCLRun 使用 Java Reflection API 来调用特定类的主方法并把参数传递给它。有关详细信息,请参阅源代码


运行示例

源码包括了一组小类,它们演示了工作方式。主程序是 Foo 类,它创建类 Bar 的实例。类 Bar 创建另一个类 Baz 的实例,它在 baz 包内,这是为了展示 CCL 是如何处理子包里的代码。Bar 也是通过名称装入的,其名称为 Boo,这用来展示它也能与 CCL 工作。

每个类都声明已被装入并运行。现在用源代码来试一下。编译 CCLRun 和 CompilingClassLoader。确保不要编译其它类(Foo、Bar、Baz 和 Boo),否则将不会使用 CCL,因为这些类已经编译过了。


 % java CCLRun Foo arg1 arg2
CCL: Compiling Foo.java...
foo! arg1 arg2
bar! arg1 arg2
baz! arg1 arg2
CCL: Compiling Boo.java...
Boo!

请注意,首先调用编译器,Foo.java 管理 Bar 和 baz.Baz。直到 Bar 通过名称来装入 Boo 时,被调用它,这时 CCL 会再次调用编译器来编译它。

 

  1package cn.loader;
  2
  3import java.io.*;
  4
  5/*
  6
  7A CompilingClassLoader compiles your Java source on-the-fly.  It checks
  8for nonexistent .class files, or .class files that are older than their
  9corresponding source code.
 10
 11*/

 12
 13public class CompilingClassLoader extends ClassLoader
 14{
 15  // Given a filename, read the entirety of that file from disk
 16  // and return it as a byte array.
 17  private byte[] getBytes( String filename ) throws IOException {
 18    // Find out the length of the file
 19    File file = new File( filename );
 20    long len = file.length();
 21
 22    // Create an array that's just the right size for the file's
 23    // contents
 24    byte raw[] = new byte[(int)len];
 25
 26    // Open the file
 27    FileInputStream fin = new FileInputStream( file );
 28
 29    // Read all of it into the array; if we don't get all,
 30    // then it's an error.
 31    int r = fin.read( raw );
 32    if (r != len)
 33      throw new IOException( "Can't read all, "+r+" != "+len );
 34
 35    // Don't forget to close the file!
 36    fin.close();
 37
 38    // And finally return the file contents as an array
 39    return raw;
 40  }

 41
 42  // Spawn a process to compile the java source code file
 43  // specified in the 'javaFile' parameter.  Return a true if
 44  // the compilation worked, false otherwise.
 45  private boolean compile( String javaFile ) throws IOException {
 46    // Let the user know what's going on
 47    System.out.println( "CCL: Compiling "+javaFile+"" );
 48
 49    // Start up the compiler
 50    Process p = Runtime.getRuntime().exec( "javac "+javaFile );
 51
 52    // Wait for it to finish running
 53    try {
 54      p.waitFor();
 55    }
 catch( InterruptedException ie ) { System.out.println( ie ); }
 56
 57    // Check the return code, in case of a compilation error
 58    int ret = p.exitValue();
 59
 60    // Tell whether the compilation worked
 61    return ret==0;
 62  }

 63
 64  // The heart of the ClassLoader -- automatically compile
 65  // source as necessary when looking for class files
 66  public Class loadClass( String name, boolean resolve )
 67      throws ClassNotFoundException {
 68
 69    // Our goal is to get a Class object
 70    Class clas = null;
 71
 72    // First, see if we've already dealt with this one
 73    clas = findLoadedClass( name );
 74
 75    //System.out.println( "findLoadedClass: "+clas );
 76
 77    // Create a pathname from the class name
 78    // E.g. java.lang.Object => java/lang/Object
 79    String fileStub = name.replace( '.''/' );
 80
 81    // Build objects pointing to the source code (.java) and object
 82    // code (.class)
 83    String javaFilename = fileStub+".java";
 84    String classFilename = fileStub+".class";
 85
 86    File javaFile = new File( javaFilename );
 87    File classFile = new File( classFilename );
 88
 89    //System.out.println( "j "+javaFile.lastModified()+" c "+
 90    //  classFile.lastModified() );
 91
 92    // First, see if we want to try compiling.  We do if (a) there
 93    // is source code, and either (b0) there is no object code,
 94    // or (b1) there is object code, but it's older than the source
 95    if (javaFile.exists() && (!classFile.exists() || javaFile.lastModified() > classFile.lastModified())) {
 96
 97      try {
 98        // Try to compile it.  If this doesn't work, then
 99        // we must declare failure.  (It's not good enough to use
100        // and already-existing, but out-of-date, classfile)
101        if (!compile( javaFilename ) || !classFile.exists()) {
102          throw new ClassNotFoundException( "Compile failed: "+javaFilename );
103        }

104      }
 catch( IOException ie ) {
105
106        // Another place where we might come to if we fail
107        // to compile
108        throw new ClassNotFoundException( ie.toString() );
109      }

110    }

111
112    // Let's try to load up the raw bytes, assuming they were
113    // properly compiled, or didn't need to be compiled
114    try {
115
116      // read the bytes
117      byte raw[] = getBytes( classFilename );
118
119      // try to turn them into a class
120      clas = defineClass( name, raw, 0, raw.length );
121    }
 catch( IOException ie ) {
122      // This is not a failure!  If we reach here, it might
123      // mean that we are dealing with a class in a library,
124      // such as java.lang.Object
125    }

126
127    //System.out.println( "defineClass: "+clas );
128
129    // Maybe the class is in a library -- try loading
130    // the normal way
131    if (clas==null{
132      clas = findSystemClass( name );
133    }

134
135    //System.out.println( "findSystemClass: "+clas );
136
137    // Resolve the class, if any, but only if the "resolve"
138    // flag is set to true
139    if (resolve && clas != null)
140      resolveClass( clas );
141
142    // If we still don't have a class, it's an error
143    if (clas == null)
144      throw new ClassNotFoundException( name );
145
146    // Otherwise, return the class
147    return clas;
148  }

149}

150
151
152
153package cn.loader;
154
155import java.lang.reflect.*;
156
157/*
158
159CCLRun executes a Java program by loading it through a
160CompilingClassLoader.
161
162*/

163
164public class CCLRun
165{
166  static public void main( String args[] ) throws Exception {
167
168    // The first argument is the Java program (class) the user
169    // wants to run
170    String progClass = args[0];
171
172    // And the arguments to that program are just
173    // arguments 1..n, so separate those out into
174    // their own array
175    String progArgs[] = new String[args.length-1];
176    System.arraycopy( args, 1, progArgs, 0, progArgs.length );
177
178    // Create a CompilingClassLoader
179    CompilingClassLoader ccl = new CompilingClassLoader();
180
181    // Load the main class through our CCL
182    Class clas = ccl.loadClass( progClass );
183
184    // Use reflection to call its main() method, and to
185    // pass the arguments in.
186
187    // Get a class representing the type of the main method's argument
188    Class mainArgType[] = { (new String[0]).getClass() };
189
190    // Find the standard main method in the class
191    Method main = clas.getMethod( "main", mainArgType );
192
193    // Create a list containing the arguments -- in this case,
194    // an array of strings
195    Object argsArray[] = { progArgs };
196
197    // Call the method
198    main.invoke( null, argsArray );
199  }

200}

201
202
203
204
205package cn.loader;
206
207public class Foo
208{
209  static public void main( String args[] ) throws Exception {
210    System.out.println( "foo! "+args[0]+" "+args[1] );
211    new Bar( args[0], args[1] );
212  }

213}

214
215
216package cn.loader;
217
218import baz.*;
219
220public class Bar
221{
222  public Bar( String a, String b ) {
223    System.out.println( "bar! "+a+" "+b );
224    new Baz( a, b );
225
226    try {
227      Class booClass = Class.forName( "cn.loader.Boo" );
228      Object boo = booClass.newInstance();
229    }
 catch( Exception e ) {
230      e.printStackTrace();
231    }

232  }

233}

234
235
236package cn.loader;
237
238public class Boo
239{
240  public Boo() {
241    System.out.println( "Boo!" );
242  }

243}

244
245
246package baz;
247
248public class Baz
249{
250  public Baz( String a, String b ) {
251    System.out.println( "baz! "+a+" "+b );
252  }

253}

254
255
256


Author: orangelizq
email: orangelizq@163.com

欢迎大家访问我的个人网站 萌萌的IT人
posted on 2007-09-02 14:38 桔子汁 阅读(516) 评论(0)  编辑  收藏 所属分类: J2EE

只有注册用户登录后才能发表评论。


网站导航: