Skip to content

Commit f363aa8

Browse files
committed
// BH 6/24/2018 -- synchronized
synchronized(a = new Object()) {...} ---> ...; only if an assignment or not a simple function call to Object.getTreeLock()
1 parent e52b875 commit f363aa8

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed
165 Bytes
Binary file not shown.
165 Bytes
Binary file not shown.

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
import org.eclipse.jdt.core.dom.WhileStatement;
123123
import org.eclipse.jdt.core.dom.WildcardType;
124124

125+
// BH 6/24/2018 -- synchronized(a = new Object()) {...} ---> ...; only if an assignment or not a simple function call to Object.getTreeLock()
125126
// BH 6/23/2018 -- synchronized(a = new Object()) {...} ---> if(!(a = new Object()) {throw new NullPointerException()}else{...}
126127
// BH 6/21/2018 -- CharSequence.subSequence() should be defined both subSequence$I$I and subSequence
127128
// BH 6/20/2018 -- fixes for (int var : new int[] {3,4,5}) becoming for var var
@@ -910,11 +911,30 @@ public boolean visit(SwitchCase node) {
910911
}
911912

912913
public boolean visit(SynchronizedStatement node) {
913-
// we wrap this with a simple if() statement,
914-
// checking that it is not null
915-
buffer.append("if(!(");
916-
node.getExpression().accept(this);
917-
buffer.append(")){throw new NullPointerException()}else");
914+
// we could wrap this with a simple if() statement,
915+
// checking that it is not null, but that seems to me
916+
// to be unnecessary. When would one ever intentionally
917+
// produce a null pointer exception from synchronized(...)?
918+
919+
Expression e = node.getExpression();
920+
if (e instanceof Name
921+
|| e instanceof TypeLiteral
922+
|| e instanceof ThisExpression)
923+
return false;
924+
buffer.append("/*sync " + e.getClass().getName() + "*/");
925+
// get actual JavaScript code
926+
int pt = buffer.length();
927+
e.accept(this);
928+
String expr = buffer.substring(pt, buffer.length());
929+
buffer.setLength(pt);
930+
// ignore (treeLock())
931+
if (e instanceof MethodInvocation && expr.indexOf(".getTreeLock()") >= 0){
932+
MethodInvocation m = (MethodInvocation) e;
933+
m.getExpression().getName();
934+
return false;
935+
}
936+
buffer.append(expr);
937+
buffer.append(";");
918938
node.getBody().accept(this);
919939
return false;
920940
}

0 commit comments

Comments
 (0)