forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevModeClassLoader.java
More file actions
370 lines (330 loc) · 12.2 KB
/
DevModeClassLoader.java
File metadata and controls
370 lines (330 loc) · 12.2 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package act.app;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Act;
import act.controller.meta.ControllerClassMetaInfo;
import act.metric.Timer;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static act.app.App.F.*;
/**
* Dev mode application class loader, which is able to
* load classes directly from app src folder
*/
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 List<FsChangeDetector> detectors = new ArrayList<>();
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) {
if (sources.containsKey(className)) {
return true;
}
Class<?> clazz = app().classForName(className);
return null != source(clazz);
}
public Source source(Class<?> clazz) {
String className = clazz.getName();
Source source = sources.get(className);
if (null != source) {
return source;
}
Class<?> enclosingClass = clazz.getEnclosingClass();
return null == enclosingClass ? null : source(enclosingClass);
}
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) {
App app = app();
List<File> srcRoots = app.sourceDirs();
preloadSource(srcRoots, name);
return bytecodeFromSource(name, true);
}
private void addSourceRoot(List<File> sourceRoots, File base, ProjectLayout layout) {
if (null != base && base.isDirectory()) {
sourceRoots.add(layout.source(base));
}
}
@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() {
List<File> sourceRoots = app().allSourceDirs(true);
for (final File sourceRoot : sourceRoots) {
Files.filter(sourceRoot, 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(List<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() {
Timer timer = metric.startTimer("act:classload:scan:scanSources");
try {
logger.debug("start to scan sources...");
List<AppSourceCodeScanner> scanners = app().scannerManager().sourceCodeScanners();
Set<String> classesNeedByteCodeScan = C.newSet();
if (scanners.isEmpty()) {
//LOGGER.warn("No source code scanner found");
for (String className : sources.keySet()) {
classesNeedByteCodeScan.add(className);
}
} else {
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);
}
} finally {
timer.stop();
}
}
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() {
for (FsChangeDetector detector : detectors) {
detectChanges(detector);
}
super.detectChanges();
}
private void detectChanges(FsChangeDetector detector) {
if (null != detector) {
detector.detectChanges();
}
}
private void setupFsChangeDetectors() {
ProjectLayout layout = app().layout();
File appBase = app().base();
List<File> bases = C.newList(appBase);
bases.addAll(app().config().moduleBases());
boolean isTest = "test".equals(Act.profile());
for (File base : bases) {
addDetector(layout.source(base), JAVA_SOURCE, sourceChangeListener);
addDetector(layout.lib(base), JAR_FILE, libChangeListener);
File rsrc = layout.resource(base);
addDetector(rsrc, CONF_FILE.or(ROUTES_FILE), confChangeListener);
addDetector(rsrc, null, resourceChangeListener);
if (isTest) {
addDetector(layout.testSource(base), JAVA_SOURCE, sourceChangeListener);
addDetector(layout.testLib(base), JAR_FILE, libChangeListener);
File testRsrc = layout.testResource(base);
addDetector(testRsrc, CONF_FILE.or(ROUTES_FILE), confChangeListener);
addDetector(testRsrc, null, resourceChangeListener);
}
}
}
private void addDetector(File base, $.Predicate<String> predicate, FsEventListener listener) {
if (null != base && base.isDirectory()) {
detectors.add(new FsChangeDetector(base, predicate, listener));
}
}
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 ResourceChangeListener() {
@Override
public void on(FsEvent... events) {
super.on(events);
throw Act.requestRestart();
}
};
private final FsEventListener resourceChangeListener = new ResourceChangeListener();
private class ResourceChangeListener implements 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;
for (String path : paths) {
files[idx++] = new File(path);
}
switch (e.kind()) {
case CREATE:
case MODIFY:
app().builder().copyResources(files);
break;
case DELETE:
app().builder().removeResources(files);
break;
default:
assert false;
}
}
}
}
}