Skip to content

Commit 5e82ba1

Browse files
committed
Fix app byte code scan issue when there are multple level of embedded classes
1 parent 053fd48 commit 5e82ba1

5 files changed

Lines changed: 49 additions & 49 deletions

File tree

src/main/java/act/app/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public String toString() {
365365
* @return
366366
*/
367367
public String cuid() {
368-
return idGenerator.id();
368+
return idGenerator.genId();
369369
}
370370

371371
public <T extends AppService<T>> T service(Class<T> serviceClass) {

src/main/java/act/app/AppClassLoader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ protected void scanByteCode(Iterable<String> classes, _.Function<String, byte[]>
170170
logger.debug("scanning %s ...", className);
171171
dependencies.remove(className);
172172
byte[] ba = bytecodeProvider.apply(className);
173+
if (null == ba) {
174+
throw new NullPointerException();
175+
}
173176
List<ByteCodeVisitor> visitors = C.newList();
174177
List<AppByteCodeScanner> scanners = C.newList();
175178
for (AppByteCodeScanner scanner : scannerManager.byteCodeScanners()) {

src/main/java/act/app/DevModeClassLoader.java

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ private void scanSources() {
159159
l.add(scanner);
160160
}
161161
}
162-
EmbeddedClassFinder finder = new EmbeddedClassFinder();
163-
l.add(finder);
164162
Source source = source(className);
165163
String[] lines = source.code().split("[\\n\\r]+");
166164
for (int i = 0, j = lines.length; i < j; ++i) {
@@ -169,22 +167,28 @@ private void scanSources() {
169167
scanner.visit(i, line, className);
170168
}
171169
}
172-
if (!finder.embeddedClassNames.isEmpty()) {
173-
classesNeedByteCodeScan.addAll(finder.embeddedClassNames);
174-
}
175170
}
176171

177172
if (classesNeedByteCodeScan.isEmpty()) {
178173
return;
179174
}
180175

181-
// FIX ME: Embedded class is not scanned
176+
final Set<String> embeddedClassNames = C.newSet();
182177
scanByteCode(classesNeedByteCodeScan, new _.F1<String, byte[]>() {
183178
@Override
184179
public byte[] apply(String s) throws NotAppliedException, _.Break {
185-
return bytecodeFromSource(s, true);
180+
return bytecodeFromSource(s, embeddedClassNames);
186181
}
187182
});
183+
184+
if (!embeddedClassNames.isEmpty()) {
185+
scanByteCode(embeddedClassNames, new _.F1<String, byte[]>() {
186+
@Override
187+
public byte[] apply(String s) throws NotAppliedException, _.Break {
188+
return bytecodeFromSource(s, embeddedClassNames);
189+
}
190+
});
191+
}
188192
}
189193

190194
private byte[] bytecodeFromSource(String name, boolean compile) {
@@ -204,6 +208,24 @@ private byte[] bytecodeFromSource(String name, boolean compile) {
204208
return bytes;
205209
}
206210

211+
private byte[] bytecodeFromSource(String name, Set<String> embeddedClassNames) {
212+
Source source = source(name);
213+
if (null == source) {
214+
return null;
215+
}
216+
byte[] bytes = source.bytes();
217+
if (null == bytes) {
218+
compiler.compile(name);
219+
bytes = source.bytes();
220+
embeddedClassNames.addAll(C.list(source.innerClassNames()).map(S.F.prepend(name + "$")));
221+
}
222+
if (name.contains("$")) {
223+
String innerClassName = S.afterFirst(name, "$");
224+
return source.bytes(innerClassName);
225+
}
226+
return bytes;
227+
}
228+
207229
@Override
208230
public void detectChanges() {
209231
detectChanges(confChangeDetector);
@@ -294,34 +316,4 @@ public void on(FsEvent... events) {
294316
}
295317
};
296318

297-
private static class EmbeddedClassFinder implements AppSourceCodeScanner {
298-
299-
private List<String> embeddedClassNames = C.newList();
300-
private String className;
301-
private static final Pattern P = Pattern.compile("\\s*public\\s+static\\s+class\\s+([_a-zA-Z][a-zA-Z_0-9]*).*");
302-
303-
@Override
304-
public void visit(int lineNumber, String line, String className) {
305-
Matcher m = P.matcher(line);
306-
if (m.matches()) {
307-
embeddedClassNames.add(className + "$" + m.group(1));
308-
}
309-
}
310-
311-
@Override
312-
public boolean triggerBytecodeScanning() {
313-
return false;
314-
}
315-
316-
@Override
317-
public void setApp(App app) {
318-
319-
}
320-
321-
@Override
322-
public boolean start(String className) {
323-
this.className = className;
324-
return true;
325-
}
326-
}
327319
}

src/main/java/act/app/Source.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.File;
99
import java.util.Map;
10+
import java.util.Set;
1011
import java.util.StringTokenizer;
1112

1213
/**
@@ -107,6 +108,10 @@ public byte[] bytes(String innerClass) {
107108
return innerBytes.get(innerClass);
108109
}
109110

111+
public Set<String> innerClassNames() {
112+
return innerBytes.keySet();
113+
}
114+
110115
public File file() {
111116
return file;
112117
}

src/main/java/act/util/IdGenerator.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import org.joda.time.DateTime;
44
import org.osgl._;
5-
import org.osgl.util.E;
6-
import org.osgl.util.IO;
7-
import org.osgl.util.S;
5+
import org.osgl.util.*;
86

97
import java.io.File;
8+
import java.security.SecureRandom;
109
import java.util.Objects;
1110
import java.util.concurrent.atomic.AtomicLong;
1211

@@ -208,21 +207,22 @@ public IdGenerator(String startIdFile) {
208207
this.sequenceProvider = new SequenceProvider.AtomicLongSeq();
209208
}
210209

211-
public String id() {
210+
/**
211+
* Generate a unique ID across the cluster
212+
* @return
213+
*/
214+
public String genId() {
212215
StringBuilder sb = S.builder();
213216
sb.append(nodeIdProvider.nodeId()).append(startIdProvider.startId()).append(sequenceProvider.seqId());
214217
return sb.toString();
215218
}
216219

217220
public static void main(String[] args) {
218-
long ts = _.ms();
219-
IdGenerator generator = new IdGenerator(4);
220-
System.out.println(generator.id());
221-
for (int i = 0; i < 22147201; ++i) {
222-
generator.id();
221+
for (int i = 0; i < 10; ++i) {
222+
//System.out.println(Crypto.genRandomDigits(4));
223+
//System.out.println(Crypto.genRandomStr(8));
224+
System.out.println(Crypto.genSecret(8));
223225
}
224-
System.out.println(generator.id());
225-
System.out.println(_.ms() - ts);
226226
}
227227

228228
/**

0 commit comments

Comments
 (0)