forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFClassLoader.java
More file actions
286 lines (262 loc) · 8.42 KB
/
Copy pathJFClassLoader.java
File metadata and controls
286 lines (262 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package javaforce;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.*;
/** Class Loader from a ClassPath.
*
* You could use URLClassLoader with file://... but this is easier.
*
* see https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/jdk/internal/loader/BuiltinClassLoader.java
* see https://github.com/apache/tomcat/blob/main/java/org/apache/catalina/loader/WebappClassLoaderBase.java
*
* @author pquiring
*/
public class JFClassLoader extends ClassLoader {
private static class Folder {
public Object lock = new Object();
public HashMap<String, byte[]> files = new HashMap<>();
public HashMap<String, Class<?>> classes = new HashMap<>();
};
private ArrayList<Folder> cp_folders = new ArrayList<>();
private HashMap<String, Folder> cp_files = new HashMap<>();
public static boolean debug = false;
/** Build a Class Loader from the classpath.
*
* classpath may include directories and jar files.
*
*/
public JFClassLoader(String[] classpath) {
super(ClassLoader.getPlatformClassLoader());
File[] files = new File[classpath.length];
int idx = 0;
for(String cp : classpath) {
files[idx++] = new File(cp);
}
init(files);
}
/** Build a Class Loader from the classpath.
*
* classpath may include directories and jar files.
*
*/
public JFClassLoader(File[] classpath) {
super(ClassLoader.getPlatformClassLoader());
init(classpath);
}
private void init(File[] classpath) {
//need to get PlatformClassLoader
if (debug) {
JFLog.log("platform = " + ClassLoader.getPlatformClassLoader());
JFLog.log("system = " + ClassLoader.getSystemClassLoader());
ClassLoader parent = this.getParent();
while (parent != null) {
JFLog.log("parent = " + parent);
parent = parent.getParent();
}
}
for(File file : classpath) {
if (!file.exists()) {
JFLog.log("Error:ClassPath element not found:" + file.getAbsolutePath());
continue;
}
if (file.isDirectory()) {
Folder folder = new Folder();
cp_folders.add(folder);
doFileFolder(file, folder, "");
continue;
}
if (file.getName().endsWith(".jar")) {
Folder jar_folder = new Folder();
cp_folders.add(jar_folder);
doJarFolder(file, jar_folder, "");
continue;
}
JFLog.log("Unknown ClassPath element:" + file);
}
}
private void doFileFolder(File folder, Folder file_folder, String path) {
try {
File[] files = folder.listFiles();
for(File file : files) {
String name = file.getName();
if (file.isDirectory()) {
String full;
if (path.length() == 0) full = name; else full = path + "/" + name;
doFileFolder(file, file_folder, full);
} else if (file.getName().endsWith(".jar")) {
Folder jar_folder = new Folder();
cp_folders.add(jar_folder);
doJarFolder(file, jar_folder, "");
} else if (file.getName().endsWith(".class")) {
String full;
if (path.length() == 0) full = name; else full = path + "/" + name;
FileInputStream fis = new FileInputStream(file);
byte[] data = fis.readAllBytes();
fis.close();
String cls = convert_class(full);
if (debug) JFLog.log("class:" + cls);
file_folder.files.put(cls, data);
cp_files.put(cls, file_folder);
} else {
String full;
if (path.length() == 0) full = name; else full = path + "/" + name;
FileInputStream fis = new FileInputStream(file);
byte[] data = fis.readAllBytes();
fis.close();
String cls = convert_resource(full);
if (debug) JFLog.log("resource:" + cls);
file_folder.files.put(cls, data);
cp_files.put(cls, file_folder);
}
}
} catch (Exception e) {
JFLog.log(e);
}
}
private void doJarFolder(File jar, Folder jar_folder, String path) {
try {
ZipInputStream zis = new ZipInputStream(new FileInputStream(jar));
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
String name = ze.getName(); //includes directory path
if (ze.isDirectory()) {
continue;
}
if (name.endsWith(".class")) {
String full;
if (path.length() == 0) full = name; else full = path + "/" + name;
byte[] data = zis.readAllBytes();
String cls = convert_class(full);
if (debug) JFLog.log("jar.class:" + cls);
jar_folder.files.put(cls, data);
cp_files.put(cls, jar_folder);
}
}
zis.close();
} catch (Exception e) {
JFLog.log(e);
}
}
private String convert_class(String in) {
in = in.substring(0, in.length() - 6); //remove .class
in = in.replaceAll("[/]", ".");
if (JF.isWindows()) {
in = in.replaceAll("[\\\\]", ".");
}
return in;
}
private String convert_resource(String in) {
if (JF.isWindows()) {
in = in.replaceAll("[\\\\]", "/");
}
return in;
}
public Class<?> findClass(String name) throws ClassNotFoundException {
return findClass(null, name);
}
public Class<?> findClass(String module_name, String name) {
//try bootloader first
if (!name.startsWith("javaforce.")) {
try {
Class<?> cls;
if (module_name == null)
cls = super.findClass(name);
else
cls = super.findClass(module_name, name);
if (cls != null) {
if (debug) JFLog.log("bootClass:" + name);
return cls;
}
} catch (Exception e) {}
}
Folder folder = cp_files.get(name);
if (folder == null) return null;
synchronized (folder.lock) {
Class<?> cls = folder.classes.get(name);
if (cls != null) return cls;
byte[] data = getData(name);
if (data == null) return null;
if (debug) JFLog.log("defineClass:" + name);
try {
cls = defineClass(name, data, 0, data.length);
if (cls != null) {
folder.classes.put(name, cls);
}
return cls;
} catch (Throwable t) {
JFLog.log(t);
return null;
}
}
}
private byte[] getData(String name) {
Folder folder = cp_files.get(name);
if (folder == null) {
JFLog.log("JFClassLoader:class not found:" + name);
return null;
}
return folder.files.get(name);
}
public URL findResource(final String name) {
return super.findResource(name);
}
public Enumeration<URL> findResources(String name) throws IOException {
return super.findResources(name);
}
public URL getResource(String name) {
return super.getResource(name);
}
public Enumeration<URL> getResources(String name) throws IOException {
return super.getResources(name);
}
public InputStream getResourceAsStream(String name) {
byte[] data = getData(name);
if (data == null) return super.getResourceAsStream(name);
return new ByteArrayInputStream(data);
}
public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
return super.loadClass(name, resolve);
}
public static String inspectClass(Class<?> cls, boolean recursive) {
StringBuilder sb = new StringBuilder();
sb.append("inspectClass{");
sb.append("class=" + cls);
Class<?> supercls = cls.getSuperclass();
if (supercls != null) {
sb.append(",super=" + supercls);
}
Object[] ifaces = cls.getInterfaces();
for(Object iface : ifaces) {
sb.append(",interface=" + iface);
}
sb.append("}");
if (recursive && supercls != null) {
sb.append(inspectClass(supercls, recursive));
}
return sb.toString();
}
public static String inspectObject(Object obj, boolean recursive) {
StringBuilder sb = new StringBuilder();
sb.append("inspectObject{object=" + obj);
Class<?> cls = obj.getClass();
sb.append(",class=" + cls);
Class<?> supercls = cls.getSuperclass();
sb.append(",super=" + supercls);
Object[] ifaces = cls.getInterfaces();
for(Object iface : ifaces) {
sb.append(",interface=" + iface);
}
sb.append("}");
if (recursive && supercls != null) {
sb.append(inspectClass(supercls, recursive));
}
return sb.toString();
}
public String toString() {
return "JFClassLoader";
}
}