Repository navigation
Expand file tree
/
Copy pathDevModeClassLoader.java
More file actions
336 lines (299 loc) · 10.8 KB
/
Copy pathDevModeClassLoader.java
File metadata and controls
336 lines (299 loc) · 10.8 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package act.app;
import act.Act;
import act.ActComponent;
import act.controller.meta.ControllerClassMetaInfo;
import act.util.Files;
import act.util.FsChangeDetector;
import act.util.FsEvent;
import act.util.FsEventListener;
import org.osgl.$;
import org.osgl.exception.NotAppliedException;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.S;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Dev mode application class loader, which is able to
* load classes directly from app src folder
*/
@ActComponent
public class DevModeClassLoader extends AppClassLoader {
private final static Logger logger = L.get(DevModeClassLoader.class);
private Map<String, Source> sources = C.newMap();
private final AppCompiler compiler;
private FsChangeDetector confChangeDetector;
private FsChangeDetector libChangeDetector;
private FsChangeDetector resourceChangeDetector;
private FsChangeDetector sourceChangeDetector;
public DevModeClassLoader(App app) {
super(app);
compiler = new AppCompiler(this);
}
@Override
protected void releaseResources() {
sources.clear();
compiler.destroy();
super.releaseResources();
}
public boolean isSourceClass(String className) {
return sources.containsKey(className);
}
public ControllerClassMetaInfo controllerClassMetaInfo(String controllerClassName) {
return super.controllerClassMetaInfo(controllerClassName);
}
@Override
protected void preload() {
preloadSources();
super.preload();
setupFsChangeDetectors();
}
@Override
protected void preloadClasses() {
// do not preload classes in dev mode
}
@Override
protected void scan() {
super.scan();
compileSources();
scanSources();
}
@Override
protected byte[] loadAppClassFromDisk(String name) {
File srcRoot = app().layout().source(app().base());
preloadSource(srcRoot, name);
return bytecodeFromSource(name, true);
}
@Override
protected byte[] appBytecode(String name, boolean compileSource) {
byte[] bytecode = super.appBytecode(name, compileSource);
return null == bytecode && compileSource ? bytecodeFromSource(name, compileSource) : bytecode;
}
public Source source(String className) {
if (className.contains("$")) {
String name0 = S.before(className, "$");
return sources.get(name0);
}
return sources.get(className);
}
private void preloadSources() {
final File sourceRoot = app().layout().source(app().base());
Files.filter(sourceRoot, App.F.JAVA_SOURCE, new $.Visitor<File>() {
@Override
public void visit(File file) throws $.Break {
Source source = Source.ofFile(sourceRoot, file);
if (null != source) {
if (null == sources) {
sources = C.newMap();
}
sources.put(source.className(), source);
}
}
});
if ("test".equals(app().profile())) {
final File testSourceRoot = app().layout().testSource(app().base());
Files.filter(testSourceRoot, App.F.JAVA_SOURCE, new $.Visitor<File>() {
@Override
public void visit(File file) throws $.Break {
Source source = Source.ofFile(sourceRoot, file);
if (null != source) {
if (null == sources) {
sources = C.newMap();
}
sources.put(source.className(), source);
}
}
});
}
}
private void preloadSource(File sourceRoot, String className) {
if (null != sources) {
Source source = sources.get(className);
if (null != source) {
return;
}
}
Source source = Source.ofClass(sourceRoot, className);
if (null != source) {
if (null == sources) {
sources = C.newMap();
}
sources.put(source.className(), source);
}
}
private void compileSources() {
logger.debug("start to compile sources ...");
compiler.compile(sources.values());
}
private void scanSources() {
logger.debug("start to scan sources...");
List<AppSourceCodeScanner> scanners = app().scannerManager().sourceCodeScanners();
if (scanners.isEmpty()) {
logger.warn("No source code scanner found");
return;
}
Set<String> classesNeedByteCodeScan = C.newSet();
for (String className : sources.keySet()) {
classesNeedByteCodeScan.add(className);
logger.debug("scanning %s ...", className);
List<AppSourceCodeScanner> l = C.newList();
for (AppSourceCodeScanner scanner : scanners) {
if (scanner.start(className)) {
//logger.trace("scanner %s added to the list", scanner.getClass().getName());
l.add(scanner);
}
}
Source source = source(className);
String[] lines = source.code().split("[\\n\\r]+");
for (int i = 0, j = lines.length; i < j; ++i) {
String line = lines[i];
for (AppSourceCodeScanner scanner : l) {
scanner.visit(i, line, className);
}
}
}
if (classesNeedByteCodeScan.isEmpty()) {
return;
}
final Set<String> embeddedClassNames = C.newSet();
scanByteCode(classesNeedByteCodeScan, new $.F1<String, byte[]>() {
@Override
public byte[] apply(String s) throws NotAppliedException, $.Break {
return bytecodeFromSource(s, embeddedClassNames);
}
});
while (!embeddedClassNames.isEmpty()) {
Set<String> embeddedClassNameCopy = C.newSet(embeddedClassNames);
scanByteCode(embeddedClassNameCopy, new $.F1<String, byte[]>() {
@Override
public byte[] apply(String s) throws NotAppliedException, $.Break {
return bytecodeFromSource(s, embeddedClassNames);
}
});
embeddedClassNames.removeAll(embeddedClassNameCopy);
}
}
private byte[] bytecodeFromSource(String name, boolean compile) {
Source source = source(name);
if (null == source) {
return null;
}
byte[] bytes = source.bytes();
if (null == bytes && compile) {
compiler.compile(name);
bytes = source.bytes();
}
if (name.contains("$")) {
String innerClassName = S.afterFirst(name, "$");
return source.bytes(innerClassName);
}
return bytes;
}
private byte[] bytecodeFromSource(String name, Set<String> embeddedClassNames) {
Source source = source(name);
if (null == source) {
return null;
}
byte[] bytes = source.bytes();
if (null == bytes) {
compiler.compile(name);
bytes = source.bytes();
}
if (!name.contains("$")) {
embeddedClassNames.addAll(C.list(source.innerClassNames()).map(S.F.prepend(name + "$")));
} else {
String innerClassName = S.afterFirst(name, "$");
return source.bytes(innerClassName);
}
return bytes;
}
@Override
public void detectChanges() {
detectChanges(confChangeDetector);
detectChanges(libChangeDetector);
detectChanges(resourceChangeDetector);
detectChanges(sourceChangeDetector);
super.detectChanges();
}
private void detectChanges(FsChangeDetector detector) {
if (null != detector) {
detector.detectChanges();
}
}
private void setupFsChangeDetectors() {
ProjectLayout layout = app().layout();
File appBase = app().base();
File src = layout.source(appBase);
if (null != src) {
sourceChangeDetector = new FsChangeDetector(src, App.F.JAVA_SOURCE, sourceChangeListener);
}
File lib = layout.lib(appBase);
if (null != lib && lib.canRead()) {
libChangeDetector = new FsChangeDetector(lib, App.F.JAR_FILE, libChangeListener);
}
File conf = layout.conf(appBase);
if (null != conf && conf.canRead()) {
confChangeDetector = new FsChangeDetector(conf, App.F.CONF_FILE.or(App.F.ROUTES_FILE), confChangeListener);
}
File rsrc = layout.resource(appBase);
if (null != rsrc && rsrc.canRead()) {
resourceChangeDetector = new FsChangeDetector(rsrc, null, resourceChangeListener);
}
}
private final FsEventListener sourceChangeListener = new FsEventListener() {
@Override
public void on(FsEvent... events) {
throw Act.requestRefreshClassLoader();
}
};
private final FsEventListener libChangeListener = new FsEventListener() {
@Override
public void on(FsEvent... events) {
int len = events.length;
if (len < 0) return;
throw Act.requestRefreshClassLoader();
}
};
private final FsEventListener confChangeListener = new FsEventListener() {
@Override
public void on(FsEvent... events) {
throw Act.requestRestart();
}
};
private final FsEventListener resourceChangeListener = new FsEventListener() {
@Override
public void on(FsEvent... events) {
int len = events.length;
for (int i = 0; i < len; ++i) {
FsEvent e = events[i];
List<String> paths = e.paths();
File[] files = new File[paths.size()];
int idx = 0;
boolean routeChanged = false;
for (String path : paths) {
if (path.endsWith("/routes") || path.endsWith("\\routes")) {
routeChanged = true;
}
files[idx++] = new File(path);
}
switch (e.kind()) {
case CREATE:
case MODIFY:
app().builder().copyResources(files);
if (routeChanged) {
throw Act.requestRestart();
}
break;
case DELETE:
app().builder().removeResources(files);
break;
default:
assert false;
}
}
}
};
}