Skip to content

Commit 977b0e7

Browse files
committed
unnecessary extra anonymous function in Clazz.load
1 parent 2c1ab08 commit 977b0e7

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
6060

6161
// TODO: all calls to inner classes should be referenced to the outer class for Clazz.load
62+
// TODO: static calls to static methods do not trigger dependency
6263

6364
// BH 8/30/2017 -- all i/o working, including printf and FileOutputStream
6465
// BH 8/19/2017 -- String must implement CharSequence, so all .length() -> .length$()

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/DependencyASTVisitor.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ public String getDependencyScript(StringBuffer buf) {
226226
} else {
227227
buf = new StringBuffer();
228228
buf.append("Clazz.load (");
229-
if (musts.size() != 0 || requires.size() != 0) {
229+
230+
// add must/requires
231+
232+
if (musts.size() > 0 || requires.size() > 0) {
230233
buf.append("[");
231234
String[] ss = musts.toArray(new String[0]);
232235
Arrays.sort(ss);
@@ -241,15 +244,22 @@ public String getDependencyScript(StringBuffer buf) {
241244
} else {
242245
buf.append("null, ");
243246
}
244-
if (classNameSet.size() > 1) {
247+
248+
// add class names
249+
250+
boolean isArray = (classNameSet.size() > 1);
251+
if (isArray) {
245252
buf.append("[");
246253
}
247254
joinArrayClasses(buf, getClassNames(), null);
248-
if (classNameSet.size() > 1) {
255+
if (isArray) {
249256
buf.append("]");
250257
}
251258
buf.append(", ");
252-
if (optionals.size() != 0) {
259+
260+
// add optionals
261+
262+
if (optionals.size() > 0) {
253263
buf.append("[");
254264
String[] ss = optionals.toArray(new String[0]);
255265
Arrays.sort(ss);
@@ -258,9 +268,18 @@ public String getDependencyScript(StringBuffer buf) {
258268
} else {
259269
buf.append("null, ");
260270
}
261-
buf.append("function () {\r\n");
262-
buf.append(js);
263-
buf.append("});\r\n");
271+
272+
// check for anonymous wrapper if not multiple classes
273+
274+
if (isArray || !js.endsWith("})()\r\n") || !js.startsWith("\r\n(function")) {
275+
buf.append("function(){\r\n");
276+
buf.append(js);
277+
buf.append("}");
278+
} else {
279+
// just make outer function not anonymous
280+
buf.append("\r\n").append(js.substring(3, js.length() - 5));
281+
}
282+
buf.append(");\r\n");
264283
js = buf.toString();
265284
}
266285
return prefix + js;

0 commit comments

Comments
 (0)