KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RodentClassLoader


1 // In file RodentClassLoader.java
2

3 import java.io.*;
4 import java.util.Hashtable JavaDoc;
5
6 public class RodentClassLoader extends ClassLoader JavaDoc {
7
8     public synchronized Class JavaDoc loadClass(String JavaDoc typeName, boolean resolveIt)
9     throws ClassNotFoundException JavaDoc {
10
11         // See if type as already been loaded by
12
// this class loader
13
Class JavaDoc result = findLoadedClass(typeName);
14
15         if (result != null) {
16             // Return an already-loaded class
17
return result;
18         }
19
20         // Check with the primordial class loader
21
try {
22             result = super.findSystemClass(typeName);
23             // Return a system class
24
return result;
25         }
26         catch (ClassNotFoundException JavaDoc e) {
27         }
28
29         // Don't attempt to load a system file except
30
// through the primordial class loader
31
if (typeName.startsWith("java.")) {
32             throw new ClassNotFoundException JavaDoc();
33         }
34
35         // Try to load it from subdirectory hole.
36
byte typeData[] = getTypeFromHole(typeName);
37         if (typeData == null) {
38             throw new ClassNotFoundException JavaDoc();
39         }
40
41         // Parse it
42
result = defineClass(typeName, typeData, 0, typeData.length);
43         if (result == null) {
44             throw new ClassFormatError JavaDoc();
45         }
46
47         if (resolveIt) {
48             resolveClass(result);
49         }
50
51         // Return class from hole
52
return result;
53     }
54
55     private byte[] getTypeFromHole(String JavaDoc typeName) {
56
57         FileInputStream fis;
58         String JavaDoc fileName =
59         "hole" + File.separatorChar +
60             typeName.replace('.', File.separatorChar) + ".class";
61
62         try {
63             fis = new FileInputStream(fileName);
64         }
65         catch (FileNotFoundException e) {
66             return null;
67         }
68
69         BufferedInputStream bis = new BufferedInputStream(fis);
70         ByteArrayOutputStream out = new ByteArrayOutputStream();
71
72         try {
73             int c = bis.read();
74             while (c != -1) {
75                 out.write(c);
76                 c = bis.read();
77             }
78         }
79         catch (IOException e) {
80             return null;
81         }
82         return out.toByteArray();
83     }
84 }
85
86 // fini
87
Popular Tags