Skip to content

Commit 98e8b22

Browse files
committed
transpiler and runtime fixes
for(final int xxx....) regex Matcher charSequence.subSequence$I$I
1 parent 8975a82 commit 98e8b22

File tree

7 files changed

+84
-46
lines changed

7 files changed

+84
-46
lines changed
124 Bytes
Binary file not shown.
124 Bytes
Binary file not shown.

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

Lines changed: 60 additions & 42 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/21/2018 -- CharSequence.subSequence() should be defined both subSequence$I$I and subSequence
125126
// BH 6/20/2018 -- fixes for (int var : new int[] {3,4,5}) becoming for var var
126127
// BH 6/19/2018 -- adds .j2s j2s.class.replacements=org.apache.log4j.->jalview.javascript.log4j.;
127128
// BH 5/15/2018 -- fix for a[pt++] |= 3 incrementing pt twice and disregarding a[][] (see test/Test_Or.java)
@@ -339,8 +340,8 @@ public void endVisit(Block node) {
339340
// look for trailing j2sNative block just before the end of a block
340341
getJ2sJavadoc(node, false);
341342
buffer.append("}");
342-
clearVariables(getVariableList('f'));
343-
clearVariables(getVariableList('n'));
343+
clearVariables('f');
344+
clearVariables('n');
344345
blockLevel--;
345346
super.endVisit(node);
346347
}
@@ -495,8 +496,11 @@ public boolean visit(EmptyStatement node) {
495496
}
496497

497498
public boolean visit(EnhancedForStatement node) {
498-
String varName = getQualifiedSimpleName(node.getParameter().getName());
499-
writeReplaceV("for (var V, $V = ", "V", varName);
499+
SimpleName name = node.getParameter().getName();
500+
String varName = getQualifiedSimpleName(name);
501+
buffer.append("for (var ");
502+
acceptVariableFinal(name, 1);
503+
writeReplaceV(", $V = ", "V", varName);
500504
Expression exp = node.getExpression();
501505
ITypeBinding typeBinding = exp.resolveTypeBinding();
502506
if (typeBinding.isArray()) {
@@ -672,8 +676,6 @@ public boolean visit(MethodDeclaration node) {
672676
buffer.append("alert('native method must be replaced! " + key + "');\r\n");
673677
log("native: " + key);
674678
}
675-
// didn't we just find out that there was nothing to do?
676-
// addNativeJavadoc(node.getJavadoc(), null);
677679
buffer.append("}\r\n");
678680
// clearVariables(getVariableList('n'));
679681
// blockLevel--;
@@ -689,16 +691,33 @@ public boolean visit(MethodDeclaration node) {
689691
public void endVisit(MethodDeclaration node) {
690692
if (NativeDoc.checkj2sIgnore(node))
691693
return;
694+
removeVariableFinals(node);
695+
super.endVisit(node);
696+
}
697+
698+
private void acceptVariableFinal(SimpleName name, int offset) {
699+
IBinding binding = name.resolveBinding();
700+
if (binding != null) {
701+
String identifier = name.getIdentifier();
702+
int level = blockLevel + offset;
703+
VariableAdapter.FinalVariable f = new VariableAdapter.FinalVariable(level, identifier,
704+
methodDeclareNameStack.size() == 0 ? null : methodDeclareNameStack.peek());
705+
addVariable(f, identifier, binding);
706+
//buffer.append("/*blockLevel " + blockLevel + " level " + level + "*/");
707+
}
708+
name.accept(this);
709+
}
710+
711+
private void removeVariableFinals(MethodDeclaration node) {
692712
IMethodBinding mBinding = node.resolveBinding();
693713
if (mBinding != null)
694714
methodDeclareNameStack.pop();
715+
@SuppressWarnings("unchecked")
716+
List<SingleVariableDeclaration> parameters = node.parameters();
717+
String methodSig = (mBinding == null ? null : mBinding.getKey());
695718
List<VariableAdapter.FinalVariable> finalVars = getVariableList('f');
696719
List<VariableAdapter.FinalVariable> visitedVars = getVariableList('v');
697720
List<VariableAdapter.FinalVariable> normalVars = getVariableList('n');
698-
@SuppressWarnings("unchecked")
699-
List<SingleVariableDeclaration> parameters = node.parameters();
700-
IMethodBinding resolveBinding = node.resolveBinding();
701-
String methodSig = (resolveBinding == null ? null : resolveBinding.getKey());
702721
for (int i = parameters.size() - 1; i >= 0; i--) {
703722
SingleVariableDeclaration varDecl = parameters.get(i);
704723
SimpleName name = varDecl.getName();
@@ -708,13 +727,26 @@ public void endVisit(MethodDeclaration node) {
708727
VariableAdapter.FinalVariable f = new VariableAdapter.FinalVariable(blockLevel + 1, identifier, methodSig);
709728
f.toVariableName = identifier;
710729
normalVars.remove(f);
730+
//buffer.append("/*remNorm " + f.variableName + "/to/" + f.toVariableName + "*/");
711731
if (Modifier.isFinal(binding.getModifiers())) {
712732
finalVars.remove(f);
733+
//buffer.append("/*remFinal " + f.variableName + "/to/" + f.toVariableName + "*/");
713734
}
714735
visitedVars.remove(f);
736+
//buffer.append("/*remVis " + f.variableName + "/to/" + f.toVariableName + "*/");
737+
}
738+
}
739+
}
740+
741+
private void clearVariables(char nf) {
742+
List<VariableAdapter.FinalVariable> vars = getVariableList(nf);
743+
for (int i = vars.size(); --i >= 0;) {
744+
VariableAdapter.FinalVariable var = vars.get(i);
745+
if (var.blockLevel >= blockLevel) {
746+
vars.remove(i);
747+
//buffer.append("/*remVar " + nf + " " + var.toVariableName + " */");
715748
}
716749
}
717-
super.endVisit(node);
718750
}
719751

720752
public boolean visit(MethodInvocation node) {
@@ -817,21 +849,12 @@ public void endVisit(ReturnStatement node) {
817849
super.endVisit(node);
818850
}
819851

852+
/**
853+
* method parameters or catch variables
854+
*/
820855
public boolean visit(SingleVariableDeclaration node) {
821856
SimpleName name = node.getName();
822-
IBinding binding = name.resolveBinding();
823-
if (binding != null) {
824-
String identifier = name.getIdentifier();
825-
VariableAdapter.FinalVariable f = null;
826-
if (methodDeclareNameStack.size() == 0) {
827-
f = new VariableAdapter.FinalVariable(blockLevel + 1, identifier, null);
828-
} else {
829-
String methodSig = methodDeclareNameStack.peek();
830-
f = new VariableAdapter.FinalVariable(blockLevel + 1, identifier, methodSig);
831-
}
832-
addVariable(f, identifier, binding);
833-
}
834-
name.accept(this);
857+
acceptVariableFinal(name, 1);
835858
return false;
836859
}
837860

@@ -1743,15 +1766,6 @@ private void addSuperConstructor(SuperConstructorInvocation node, IMethodBinding
17431766
addCallInit();
17441767
}
17451768

1746-
private void clearVariables(List<VariableAdapter.FinalVariable> vars) {
1747-
for (int i = vars.size(); --i >= 0;) {
1748-
VariableAdapter.FinalVariable var = vars.get(i);
1749-
if (var.blockLevel >= blockLevel) {
1750-
vars.remove(i);
1751-
}
1752-
}
1753-
}
1754-
17551769
private String getAnonymousName(ITypeBinding binding) {
17561770
String binaryName = null, bindingKey;
17571771
if ((binding.isAnonymous() || binding.isLocal()) && (binaryName = binding.getBinaryName()) == null
@@ -2826,12 +2840,13 @@ private String simpleNameInVarBinding(SimpleName node, char ch, IVariableBinding
28262840
if (currentBlockForVisit != -1) {
28272841
List<VariableAdapter.FinalVariable> finalVars = getVariableList('f');
28282842
List<VariableAdapter.FinalVariable> visitedVars = getVariableList('v');
2829-
int size = finalVars.size();
2830-
for (int i = 0; i < size; i++) {
2843+
String vname = varBinding.getName();
2844+
for (int i = 0, size = finalVars.size(); i < size; i++) {
28312845
VariableAdapter.FinalVariable vv = finalVars.get(size - i - 1);
2832-
if (vv.variableName.equals(varBinding.getName()) && vv.blockLevel <= currentBlockForVisit) {
2846+
if (vv.blockLevel <= currentBlockForVisit && vv.variableName.equals(vname)) {
28332847
if (!visitedVars.contains(vv)) {
28342848
visitedVars.add(vv);
2849+
//buffer.append("/* current " + currentBlockForVisit + " vlevel " + vv.blockLevel + " " + vv.variableName + "*/");
28352850
}
28362851
fieldVar = vv.toVariableName;
28372852
}
@@ -2954,11 +2969,7 @@ public boolean visit(VariableDeclarationFragment node) {
29542969
IBinding binding = name.resolveBinding();
29552970
if (binding == null)
29562971
return false;
2957-
String identifier = name.getIdentifier();
2958-
VariableAdapter.FinalVariable f = new VariableAdapter.FinalVariable(blockLevel, identifier,
2959-
methodDeclareNameStack.size() == 0 ? null : (String) methodDeclareNameStack.peek());
2960-
addVariable(f, identifier, binding);
2961-
name.accept(this);
2972+
acceptVariableFinal(name, 0);
29622973
Expression right = node.getInitializer();
29632974
ITypeBinding rightBinding = (right == null ? null : right.resolveTypeBinding());
29642975
if (rightBinding == null)
@@ -3608,8 +3619,11 @@ private void addVariable(VariableAdapter.FinalVariable f, String identifier, IBi
36083619
List<VariableAdapter.FinalVariable> normalVars = getVariableList('n');
36093620
f.toVariableName = identifier;
36103621
normalVars.add(f);
3611-
if (Modifier.isFinal(binding.getModifiers()))
3622+
//buffer.append("/*addVar n " + identifier + " */");
3623+
if (Modifier.isFinal(binding.getModifiers())) {
36123624
finalVars.add(f);
3625+
//buffer.append("/*addVar f " + identifier + " */");
3626+
}
36133627
}
36143628

36153629
/**
@@ -4109,6 +4123,10 @@ private String getMethodNameOrArrayForDeclaration(MethodDeclaration node, IMetho
41094123
boolean isConstructor, boolean addUnqualified) {
41104124
SimpleName nodeName = node.getName();
41114125
String methodName = (isConstructor ? "c$" : NameMapper.getJ2SName(nodeName));
4126+
if (methodName.equals("subSequence$I$I")) {
4127+
// for StringBuffer and StringBuilder to be like String
4128+
return "['subSequence','subSequence$I$I']";
4129+
}
41124130
String qname = getJ2SQualifiedName(methodName, null, mBinding, null, false);
41134131
ITypeBinding methodClass = mBinding.getDeclaringClass();
41144132
List<String> names = null;
139 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/util/regex/Matcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Clazz.newMeth(C$,"processReplacement$S",function(replacement){
179179
index++;
180180
}
181181
if(this.replacementParts!=null&&replacementPos!=res.length$()){
182-
this.replacementParts[this.replacementParts.length]=res.subSequence(replacementPos,res.length$());
182+
this.replacementParts[this.replacementParts.length]=res.subSequence$I$I(replacementPos,res.length$());
183183
}
184184
return res.toString();
185185
}
@@ -199,7 +199,7 @@ Clazz.newMeth(C$,"region$I$I",function(leftBound,rightBound){
199199
});
200200

201201
Clazz.newMeth(C$,"appendTail$StringBuffer",function(sb){
202-
return sb.append$S(this.charSeq.subSequence(this.appendPos,this.charSeq.length$()));
202+
return sb.append$S(this.charSeq.subSequence$I$I(this.appendPos,this.charSeq.length$()));
203203
},"StringBuffer");
204204

205205
Clazz.newMeth(C$,"replaceFirst$S",function(replacement){

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// j2sSwingJS.js
1+
// j2sClazz.js
22
// NOTE: updates to this file should be copies to j2sjmol.js
33

44
// latest author: Bob Hanson, St. Olaf College, hansonr@stolaf.edu
@@ -7,6 +7,7 @@
77

88
// Google closure compiler cannot handle Clazz.new or Clazz.super
99

10+
// BH 6/21/2018 1:08:58 PM missing mysterious Integer.objectValue()
1011
// BH 6/20/2018 6:00:23 AM missing printStackTrace(PrintStream)
1112
// BH 6/19/2018 8:49:57 AM fix for checkDeclared
1213
// BH 5/19/2018 8:22:25 PM fix for new int[] {'a'}
@@ -2768,6 +2769,7 @@ m$(Integer, "c$", function(v){
27682769
this.valueOf=function(){return v;};
27692770
}, 1);
27702771

2772+
27712773
Integer.MIN_VALUE=Integer.prototype.MIN_VALUE=-0x80000000;
27722774
Integer.MAX_VALUE=Integer.prototype.MAX_VALUE=0x7fffffff;
27732775
//Integer.TYPE=Integer.prototype.TYPE=Integer;
@@ -3216,6 +3218,14 @@ Boolean = java.lang.Boolean = Boolean || function(){
32163218
if (typeof arguments[0] != "object")this.c$(arguments[0]);
32173219
});
32183220

3221+
Integer.prototype.objectValue =
3222+
Short.prototype.objectValue =
3223+
Long.prototype.objectValue =
3224+
Float.prototype.objectValue =
3225+
Double.prototype.objectValue = function() {return this.valueOf()};
3226+
3227+
3228+
32193229
//if (supportsNativeObject) {
32203230
extendObject(Boolean);
32213231
//}

sources/net.sf.j2s.java.core/srcjs/swingjs2.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13085,7 +13085,7 @@ J2S._getResourcePath = function(path, isJavaPath) {
1308513085

1308613086
})(J2S);
1308713087

13088-
// j2sSwingJS.js
13088+
// j2sClazz.js
1308913089
// NOTE: updates to this file should be copies to j2sjmol.js
1309013090

1309113091
// latest author: Bob Hanson, St. Olaf College, hansonr@stolaf.edu
@@ -13094,6 +13094,7 @@ J2S._getResourcePath = function(path, isJavaPath) {
1309413094

1309513095
// Google closure compiler cannot handle Clazz.new or Clazz.super
1309613096

13097+
// BH 6/21/2018 1:08:58 PM missing mysterious Integer.objectValue()
1309713098
// BH 6/20/2018 6:00:23 AM missing printStackTrace(PrintStream)
1309813099
// BH 6/19/2018 8:49:57 AM fix for checkDeclared
1309913100
// BH 5/19/2018 8:22:25 PM fix for new int[] {'a'}
@@ -15855,6 +15856,7 @@ m$(Integer, "c$", function(v){
1585515856
this.valueOf=function(){return v;};
1585615857
}, 1);
1585715858

15859+
1585815860
Integer.MIN_VALUE=Integer.prototype.MIN_VALUE=-0x80000000;
1585915861
Integer.MAX_VALUE=Integer.prototype.MAX_VALUE=0x7fffffff;
1586015862
//Integer.TYPE=Integer.prototype.TYPE=Integer;
@@ -16303,6 +16305,14 @@ Boolean = java.lang.Boolean = Boolean || function(){
1630316305
if (typeof arguments[0] != "object")this.c$(arguments[0]);
1630416306
});
1630516307

16308+
Integer.prototype.objectValue =
16309+
Short.prototype.objectValue =
16310+
Long.prototype.objectValue =
16311+
Float.prototype.objectValue =
16312+
Double.prototype.objectValue = function() {return this.valueOf()};
16313+
16314+
16315+
1630616316
//if (supportsNativeObject) {
1630716317
extendObject(Boolean);
1630816318
//}

0 commit comments

Comments
 (0)