Skip to content

Commit 23e7e8b

Browse files
authored
Merge pull request #128 from BobHanson/hanson1
Hanson1
2 parents 77392df + ffdc322 commit 23e7e8b

File tree

73 files changed

+15437
-771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+15437
-771
lines changed
18.5 KB
Binary file not shown.
321 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190829124003
1+
20190923090813
18.5 KB
Binary file not shown.
321 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190829124003
1+
20190923090813

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptCompiler.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class Java2ScriptCompiler {
7979

8080
private IJavaProject project;
8181

82+
private boolean isDebugging;
83+
8284
static boolean isActive(IJavaProject project) {
8385
try {
8486
return new File(project.getProject().getLocation().toOSString(), J2S_OPTIONS_FILE_NAME).exists();
@@ -152,6 +154,8 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
152154
siteFolder = projectFolder + "/" + siteFolder;
153155
j2sPath = siteFolder + "/swingjs/j2s";
154156

157+
if (isDebugging)
158+
System.out.println("Java2ScriptCompiler writing to " + j2sPath);
155159
// method declarations and invocations are only logged
156160
// when the designated files are deleted prior to building
157161

@@ -199,7 +203,7 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
199203
if (nonqualifiedPackages.length() == 0)
200204
nonqualifiedPackages = null;
201205
// includes @j2sDebug blocks
202-
boolean isDebugging = "debug".equals(getProperty("j2s.compiler.mode"));
206+
isDebugging = "debug".equals(getProperty("j2s.compiler.mode"));
203207

204208
String classReplacements = getProperty("j2s.class.replacements");
205209

@@ -375,7 +379,10 @@ private void createJSFile(String j2sPath, String packageName, String elementName
375379
}
376380
}
377381
}
378-
writeToFile(new File(j2sPath, elementName + ".js"), js);
382+
File f = new File(j2sPath, elementName + ".js");
383+
if (isDebugging)
384+
System.out.println("Java2ScriptCompiler creating " + f);
385+
writeToFile(f, js);
379386
}
380387

381388
private String getFileContents(File file) {

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
import org.eclipse.jdt.core.dom.WhileStatement;
135135
import org.eclipse.jdt.core.dom.WildcardType;
136136

137+
// BH 2019.09.07 adds optimization for lambda methods that do not have finals
137138
// BH 2019.08.29 fix for boxing of binary representation 0b01... (Google Closure Compiler bug)
138139
// BH 2019.05.13 fix for Math.getExponent, ulp, nextDown, nextUp, nextAfter needing qualification
139140
// BH 2019.05.13 fix for Function reference in new Foo()::test(...)
@@ -163,6 +164,12 @@ public class Java2ScriptVisitor extends ASTVisitor {
163164
*/
164165
private static final boolean ALLOW_NEW_LAMBDA = false;
165166

167+
/**
168+
* If there are no finals for a lambda method, then we can reuse the object.
169+
* This can be huge for preventing repetitive object creation
170+
*/
171+
private static final boolean ALLOW_LAMBDA_OBJECT_REUSE = true;
172+
166173
private static final int NOT_LAMBDA = 0;
167174
private static final int LAMBDA_METHOD = 1;
168175
private static final int LAMBDA_CREATION = 3;
@@ -657,8 +664,10 @@ private String getClassJavaNameForType(Type type) {
657664
* anonymous class names
658665
* @param isLambda
659666
* @param isClassTrulyLocal
667+
*
668+
* @return anonymous name only if there are no finals
660669
*/
661-
private void processLocalInstance(ASTNode node, ASTNode anonymousClassDeclaration, ITypeBinding binding,
670+
private String processLocalInstance(ASTNode node, ASTNode anonymousClassDeclaration, ITypeBinding binding,
662671
ITypeBinding innerClass, String javaInnerClassName, int lambdaType, boolean isClassTrulyLocal) {
663672

664673
// In the case of local classes, the declaration is dissociated from the
@@ -701,6 +710,7 @@ private void processLocalInstance(ASTNode node, ASTNode anonymousClassDeclaratio
701710
anonymousSuperclassName, anonName);
702711
if (lambdaType != LAMBDA_METHOD && !isClassTrulyLocal)
703712
buffer.append(")"); // end of line (..., ...)
713+
return finals == null ? anonName : null;
704714
}
705715

706716
/**
@@ -6808,15 +6818,19 @@ private boolean addLambdaMethodReference(MethodReference node, Expression exp) {
68086818
*/
68096819
private boolean addLambda$class$Method(MethodReference node, ITypeBinding binding, Expression exp,
68106820
ITypeBinding declaringClass, boolean checkFinals) {
6821+
6822+
68116823
allowClazzNewLambda = (ALLOW_NEW_LAMBDA && getLastCharInBuffer() != '=');
68126824
int pt = buffer.length();
68136825
buffer.append("(function($class$){");
6814-
processLocalInstance(node, null, binding, null, null, LAMBDA_METHOD, false);
6826+
String anonName = processLocalInstance(node, null, binding, null, null, LAMBDA_METHOD, false);
68156827
buffer.append("})(");
68166828
appendFinalMethodQualifier(exp, declaringClass, null, FINAL_ESCAPECACHE | FINAL_LAMBDA);
68176829
buffer.append(")");
68186830
if (checkFinals && allowClazzNewLambda)
68196831
buffer.setLength(pt);
6832+
if (anonName != null && ALLOW_LAMBDA_OBJECT_REUSE)
6833+
addLambdaReuse(pt, anonName);
68206834
return !allowClazzNewLambda;
68216835

68226836
}
@@ -6871,12 +6885,28 @@ public boolean visit(LambdaExpression node) {
68716885
private boolean addLambda$class$Expr(LambdaExpression node, ITypeBinding binding, boolean checkFinals) {
68726886
allowClazzNewLambda = (ALLOW_NEW_LAMBDA && getLastCharInBuffer() != '=');
68736887
int pt = buffer.length();
6874-
processLocalInstance(node, null, binding, null, null, LAMBDA_EXPRESSION, false);
6888+
String anonName = processLocalInstance(node, null, binding, null, null, LAMBDA_EXPRESSION, false);
68756889
if (checkFinals && allowClazzNewLambda)
68766890
buffer.setLength(pt);
6891+
if (anonName != null && ALLOW_LAMBDA_OBJECT_REUSE)
6892+
addLambdaReuse(pt, anonName);
68776893
return !allowClazzNewLambda;
68786894
}
68796895

6896+
/**
6897+
* allow reuse of Lambda method and expression objects when they involve no finals
6898+
*
6899+
* @param pt
6900+
* @param anonName
6901+
*/
6902+
private void addLambdaReuse(int pt, String anonName) {
6903+
String tmp = buffer.substring(pt);
6904+
buffer.setLength(pt);
6905+
anonName = getFinalJ2SClassName(anonName, FINAL_P);
6906+
buffer.append("(" + anonName + "$||(" + anonName + "$=(")
6907+
.append(tmp).append(")))");
6908+
}
6909+
68806910
private char getLambdaType(ITypeBinding binding) {
68816911
String name = removeBracketsAndFixNullPackageName(getJavaClassNameQualified(binding));
68826912
if (!name.startsWith("java.util.function.") || name.indexOf("To") >= 0)
18.5 KB
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/io/File.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ public boolean isDirectory() {
803803
// return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
804804
// != 0);
805805
//
806-
return true;
806+
// BH 2019.09.23 return true;
807+
return false;
807808
}
808809

809810
/**

0 commit comments

Comments
 (0)