-
Notifications
You must be signed in to change notification settings - Fork 0
/
iClassLoader.java
236 lines (202 loc) · 6.44 KB
/
iClassLoader.java
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
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
public class iClassLoader {
static int id_counter = 0;
int id;
iClassLoader() {
id = id_counter++;
}
iClass findClassImpl(String name) throws Throwable {
// if (parent != null) return parent.findClass(name);
throw new ClassNotFoundException(name.toString());
}
public void log(String format, Object... args) {
if (ScopeImpl.debuglog)
System.out.print(String.format("[ClassLoader:%s#%d] %s\n", toString(), id, String.format(format, args)));
}
final iClass findClass(String name) throws ClassNotFoundException {
log("findClass %s", name);
// new Throwable().printStackTrace();
iClass clz;
try {
clz = findClassImpl(name);
} catch (ClassNotFoundException e) {
throw e;
} catch (Throwable t) {
throw new RuntimeException(t);
}
log("findClass2 %s=%s", name, clz);
return clz;
}
}
class BaseClassLoader extends iClassLoader {
BaseClassLoader() {
super();
}
@Override
public String toString() {
return "Base";
}
}
class JavaFileiClassLoader extends iClassLoader {
MultiiClassLoader parent;
Path p;
CompilationUnit cu;
ScopeImpl scope;
JavaFileiClassLoader(MultiiClassLoader parent, Path p) throws Throwable {
super();
this.parent = parent;
this.p = p;
cu = StaticJavaParser.parse(p);
cu.getImports().addFirst(new ImportDeclaration("java.lang", false, true));
scope = (ScopeImpl) ScopeImpl.newRootScope(cu);
scope.loader = this;
}
@Override
iClass findClassImpl(String name) throws Throwable {
return scope.findClassLocal(name);
}
final iClass findClassExt(String name) throws Throwable {
log("findClassExt %s", name);
iClass clz = parent.findClassExt(this, name);
log("findClassExt2 %s=%s", name, clz);
return clz;
}
@Override
public String toString() {
return "Java@" + p;
}
}
class ClassFileiClassLoader extends iClassLoader {
MultiiClassLoader parent;
Path p;
ClassFileClassLoader cl;
ClassFileiClassLoader(MultiiClassLoader parent, Path p) throws Throwable {
super();
this.parent = parent;
this.p = p;
cl = new ClassFileClassLoader();
}
@Override
iClass findClassImpl(String name) throws Throwable {
return iClassWrapped.from(ScopeImpl.newRootScope(null), cl.loadClass(name));
}
@Override
public String toString() {
return "Class@" + p;
}
class ClassFileClassLoader extends ClassLoader {
ClassFileClassLoader() throws Throwable {
byte[] b = Files.readAllBytes(p);
defineClass(null, b, 0, b.length, null);
}
}
}
class JarFileiClassLoader extends iClassLoader {
MultiiClassLoader parent;
Path p;
URLClassLoader cl;
JarFileiClassLoader(MultiiClassLoader parent, Path p) throws Throwable {
super();
this.parent = parent;
this.p = p;
cl = new URLClassLoader(new URL[] {p.toUri().toURL()});
}
@Override
iClass findClassImpl(String name) throws Throwable {
return iClassWrapped.from(ScopeImpl.newRootScope(null), cl.loadClass(name));
}
@Override
public String toString() {
return "Jar@" + p;
}
}
class MultiiClassLoader extends iClassLoader {
List<iClassLoader> xs;
MultiiClassLoader() {
super();
// this.parent = parent;
this.xs = new LinkedList<>();
}
MultiiClassLoader add(iClassLoader x) {
if (ScopeImpl.debuglog)
System.out.printf("%s add %s\n", this, x);
xs.add(x);
return this;
}
MultiiClassLoader add(Path p) throws Throwable {
if (Files.isDirectory(p)) {
Files.list(p).filter(z -> !Files.isDirectory(z)).forEach(f -> {
String n = f.getFileName().toString();
if (n.endsWith(".java") || n.endsWith(".class"))
try {
add(f);
} catch (Throwable t) {
throw new RuntimeException(t);
}
});
return this;
}
String n = p.getFileName().toString();
if (n.endsWith(".java"))
add(new JavaFileiClassLoader(this, p));
else if (n.endsWith(".class"))
add(new ClassFileiClassLoader(this, p));
else if (n.endsWith(".jar"))
add(new JarFileiClassLoader(this, p));
else
throw new UnsupportedOperationException(String.format("wtf is '%s'?", p));
return this;
}
@Override
iClass findClassImpl(String name) throws Throwable {
for (iClassLoader x : xs) {
try {
return x.findClass(name);
} catch (ClassNotFoundException e) {
}
}
return super.findClassImpl(name);
}
iClass findClassExtImpl(iClassLoader z, String name) throws Throwable {
for (iClassLoader x : xs) {
if (x != z)
try {
return x.findClass(name);
} catch (ClassNotFoundException e) {
}
}
return super.findClassImpl(name);
}
final iClass findClassExt(iClassLoader x, String name) throws Throwable {
log("findClassExt %s", name);
iClass clz = findClassExtImpl(x, name);
log("findClassExt2 %s=%s", name, clz);
return clz;
}
@Override
public String toString() {
return "Multi" + xs.toString();
}
}
class ClassName {
LinkedList<String> parts;
ClassName(String name) {
parts = new LinkedList<>(Arrays.asList(name.split("\\.")));
}
String name() {
return String.join(".", parts);
}
@Override
public String toString() {
return "ClassName" + parts;
}
}