1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.ui.swing.util;
20
21 import java.io.BufferedInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.util.Hashtable;
26
27 /***
28 * ClassGrabber is a class loader used by WindowGrabber.
29 * It simply loads classes by filename and nothing more.
30 * It exists mainly because the java 1.1 class loading
31 * framework doesn't easily allow the creation of class
32 * loaders nor the loading of arbitrary classes.
33 *
34 * @author michael@mpowers.net
35 * @version $Revision: 904 $
36 * $Date: 2006-02-18 23:19:05 +0000 (Sat, 18 Feb 2006) $
37 */
38 public class ClassGrabber extends ClassLoader
39 {
40 Hashtable classMap = new Hashtable();
41
42 public ClassGrabber()
43 {
44 super();
45 }
46
47 protected Class loadClass(String name, boolean resolve)
48 throws ClassNotFoundException
49 {
50 Class c = (Class) classMap.get( name );
51
52 if ( c != null ) return c;
53
54 try
55 {
56 c = findSystemClass( name );
57 }
58 catch ( Exception exc1 )
59 {
60
61
62 }
63
64 if ( c != null ) return c;
65
66 try
67 {
68 c = findLoadedClass( name );
69 }
70 catch ( Exception exc1 )
71 {
72
73
74 }
75
76 if ( c != null ) return c;
77
78 try
79 {
80 InputStream input = new BufferedInputStream( new FileInputStream( name ) );
81 ByteArrayOutputStream output = new ByteArrayOutputStream( 200 );
82 int ch;
83 while ( ( ch = input.read() ) != -1 )
84 {
85 output.write( ch );
86 }
87 byte[] data = output.toByteArray();
88 c = defineClass( null, data, 0, data.length );
89 }
90 catch ( Exception exc )
91 {
92 System.err.print( "getResource: " + name + ": " );
93 System.err.println( exc );
94 c = null;
95 }
96
97 if ( c != null )
98 {
99 classMap.put( name, c );
100 if ( resolve ) resolveClass( c );
101 }
102
103 return c;
104 }
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126