什么是 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 會再次調用編譯器來編譯它。
1
package cn.loader;
2
3
import java.io.*;
4
5
/**//*
6
7
A CompilingClassLoader compiles your Java source on-the-fly. It checks
8
for nonexistent .class files, or .class files that are older than their
9
corresponding source code.
10
11
*/
12
13
public 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
153
package cn.loader;
154
155
import java.lang.reflect.*;
156
157
/**//*
158
159
CCLRun executes a Java program by loading it through a
160
CompilingClassLoader.
161
162
*/
163
164
public 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
205
package cn.loader;
206
207
public 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
216
package cn.loader;
217
218
import baz.*;
219
220
public 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
236
package cn.loader;
237
238
public class Boo
239

{
240
public Boo()
{
241
System.out.println( "Boo!" );
242
}
243
}
244
245
246
package baz;
247
248
public 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
posted on 2007-09-02 14:38
桔子汁 閱讀(531)
評論(0) 編輯 收藏 所屬分類:
J2EE