122122import org .eclipse .jdt .core .dom .WhileStatement ;
123123import 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 ;
0 commit comments