-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFromMemoryClassLoader.java
More file actions
63 lines (55 loc) · 1.99 KB
/
Copy pathFromMemoryClassLoader.java
File metadata and controls
63 lines (55 loc) · 1.99 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
package run.myCode.compiler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FromMemoryClassLoader extends ClassLoader {
private static final boolean DEBUG = Boolean.getBoolean("run.mycode.debug");
private final Map<String, MemoryByteCode> m = new HashMap<>();
private final ClassLoader parent;
public FromMemoryClassLoader(ClassLoader parent) {
this.parent = parent;
// System.err.println("new loader");
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
try {
// System.out.println("Class search:" + name);
MemoryByteCode mbc = m.get(name);
if (mbc == null) {
mbc = m.get(name.replace(".", "/"));
if (mbc == null) {
return super.findClass(name);
}
}
if (DEBUG) {
System.out.println("Loading in-memory class " + name);
}
return defineClass(name, mbc.getBytes(), 0, mbc.getBytes().length);
} catch (ClassNotFoundException e) {
// System.err.println("Could not find: " + name);
if (this.parent != null) {
try {
return parent.loadClass(name);
} catch (ClassNotFoundException e1) {
// System.err.println("Really couldn't find " + name);
throw e1;
}
}
else {
throw e;
}
}
}
public void addClass(String name, MemoryByteCode mbc) {
// System.err.println("Added class: " + name);
// System.out.println("Added class:" + name);
m.put(name, mbc);
if (DEBUG) {
System.out.println("Added in-memory class " + name);
}
}
public List<String> getClassNames() {
return new ArrayList(m.keySet());
}
}