@@ -487,6 +487,7 @@ public static final class StringBufferFinder extends ASTVisitor {
487487 private Map <ExpressionStatement , ChangeStringBufferToTextBlock > conversions = new HashMap <>();
488488 private static final String APPEND = "append" ; //$NON-NLS-1$
489489 private static final String TO_STRING = "toString" ; //$NON-NLS-1$
490+ private static final String INDEX_OF = "indexOf" ; //$NON-NLS-1$
490491 private BodyDeclaration fLastBodyDecl ;
491492 private final Set <String > fExcludedNames ;
492493 private final Set <IBinding > fRemovedDeclarations = new HashSet <>();
@@ -503,6 +504,7 @@ private class CheckValidityVisitor extends ASTVisitor {
503504 private int lastStatementEnd ;
504505 private IBinding varBinding ;
505506 private List <MethodInvocation > toStringList = new ArrayList <>();
507+ private List <MethodInvocation > indexOfList = new ArrayList <>();
506508 private List <SimpleName > argList = new ArrayList <>();
507509
508510 public CheckValidityVisitor (int lastStatementEnd , IBinding varBinding ) {
@@ -522,6 +524,10 @@ public List<MethodInvocation> getToStringList() {
522524 return toStringList ;
523525 }
524526
527+ public List <MethodInvocation > getIndexOfList () {
528+ return indexOfList ;
529+ }
530+
525531 public List <SimpleName > getArgList () {
526532 return argList ;
527533 }
@@ -547,6 +553,10 @@ public boolean visit(SimpleName name) {
547553 valid = true ;
548554 toStringList .add (invocation );
549555 return true ;
556+ } else if (invocation .getName ().getFullyQualifiedName ().equals (INDEX_OF )) {
557+ valid = true ;
558+ indexOfList .add (invocation );
559+ return true ;
550560 } else {
551561 valid = false ;
552562 }
@@ -645,6 +655,7 @@ public boolean visit(ClassInstanceCreation node) {
645655 if (exp instanceof MethodInvocation ) {
646656 class MethodVisitor extends ASTVisitor {
647657 private boolean valid = false ;
658+ private boolean indexOfSeen = false ;
648659 public boolean isValid () {
649660 return valid ;
650661 }
@@ -653,11 +664,19 @@ public boolean visit(SimpleName simpleName) {
653664 if (simpleName .getFullyQualifiedName ().equals (APPEND ) && simpleName .getLocationInParent () == MethodInvocation .NAME_PROPERTY ) {
654665 return true ;
655666 }
656- if (simpleName .getLocationInParent () == MethodInvocation .EXPRESSION_PROPERTY && simpleName .getFullyQualifiedName ().equals (originalVarName .getFullyQualifiedName ())
657- && ((MethodInvocation )simpleName .getParent ()).getName ().getFullyQualifiedName ().equals (APPEND )) {
658- extractConcatenatedAppends (simpleName );
659- valid = true ;
660- return false ;
667+ if (simpleName .getLocationInParent () == MethodInvocation .EXPRESSION_PROPERTY && simpleName .getFullyQualifiedName ().equals (originalVarName .getFullyQualifiedName ())) {
668+ if (((MethodInvocation )simpleName .getParent ()).getName ().getFullyQualifiedName ().equals (APPEND )) {
669+ if (!indexOfSeen ) {
670+ extractConcatenatedAppends (simpleName );
671+ valid = true ;
672+ } else {
673+ valid = false ;
674+ }
675+ return false ;
676+ } else if (((MethodInvocation )simpleName .getParent ()).getName ().getFullyQualifiedName ().equals (INDEX_OF )) {
677+ indexOfSeen = true ;
678+ return false ;
679+ }
661680 }
662681 return true ;
663682 }
@@ -724,12 +743,13 @@ public boolean visit(SimpleName simpleName) {
724743 List <Statement > statements = new ArrayList <>(statementList );
725744 List <StringLiteral > literals = new ArrayList <>(fLiterals );
726745 List <MethodInvocation > toStringList = new ArrayList <>(checkValidityVisitor .getToStringList ());
746+ List <MethodInvocation > indexOfList = new ArrayList <>(checkValidityVisitor .getIndexOfList ());
727747 List <SimpleName > argList = new ArrayList <>(checkValidityVisitor .getArgList ());
728748 BodyDeclaration bodyDecl = ASTNodes .getFirstAncestorOrNull (node , BodyDeclaration .class );
729749 if (statements .get (0 ) instanceof VariableDeclarationStatement ) {
730750 fRemovedDeclarations .add (originalVarName .resolveBinding ());
731751 }
732- ChangeStringBufferToTextBlock operation = new ChangeStringBufferToTextBlock (toStringList , argList , statements , literals ,
752+ ChangeStringBufferToTextBlock operation = new ChangeStringBufferToTextBlock (toStringList , indexOfList , argList , statements , literals ,
733753 fRemovedDeclarations .contains (originalVarName .resolveBinding ()) ? assignmentToConvert : null , fExcludedNames , fLastBodyDecl , nonNLS );
734754 fLastBodyDecl = bodyDecl ;
735755 fOperations .add (operation );
@@ -864,6 +884,7 @@ private static boolean hasNLSMarker(ASTNode node, ICompilationUnit cu) throws Ab
864884 public static class ChangeStringBufferToTextBlock extends CompilationUnitRewriteOperation {
865885
866886 private final List <MethodInvocation > fToStringList ;
887+ private final List <MethodInvocation > fIndexOfList ;
867888 private final List <SimpleName > fArgList ;
868889 private final List <Statement > fStatements ;
869890 private final List <StringLiteral > fLiterals ;
@@ -873,9 +894,10 @@ public static class ChangeStringBufferToTextBlock extends CompilationUnitRewrite
873894 private final boolean fNonNLS ;
874895 private ExpressionStatement fAssignmentToConvert ;
875896
876- public ChangeStringBufferToTextBlock (final List <MethodInvocation > toStringList , final List <SimpleName > argList , List <Statement > statements ,
877- List <StringLiteral > literals , ExpressionStatement assignmentToConvert , Set <String > excludedNames , BodyDeclaration lastBodyDecl , boolean nonNLS ) {
897+ public ChangeStringBufferToTextBlock (final List <MethodInvocation > toStringList , final List <MethodInvocation > indexOfList , final List <SimpleName > argList ,
898+ List <Statement > statements , List < StringLiteral > literals , ExpressionStatement assignmentToConvert , Set <String > excludedNames , BodyDeclaration lastBodyDecl , boolean nonNLS ) {
878899 this .fToStringList = toStringList ;
900+ this .fIndexOfList = indexOfList ;
879901 this .fArgList = argList ;
880902 this .fStatements = statements ;
881903 this .fLiterals = literals ;
@@ -976,6 +998,7 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) {
976998 buf .append ("\" \" \" " ); //$NON-NLS-1$
977999 AST ast = fStatements .get (0 ).getAST ();
9781000 if (fToStringList .size () == 1 &&
1001+ fIndexOfList .isEmpty () &&
9791002 !fNonNLS &&
9801003 ASTNodes .getLeadingComments (fStatements .get (0 )).size () == 0 &&
9811004 (fToStringList .get (0 ).getLocationInParent () == Assignment .RIGHT_HAND_SIDE_PROPERTY ||
@@ -1024,6 +1047,15 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) {
10241047 SimpleName name = ast .newSimpleName (newVarName );
10251048 rewrite .replace (toStringCall , name , group );
10261049 }
1050+ for (MethodInvocation indexOfCall : fIndexOfList ) {
1051+ MethodInvocation newCall = ast .newMethodInvocation ();
1052+ newCall .setName (ast .newSimpleName ("indexOf" )); //$NON-NLS-1$
1053+ SimpleName caller = ast .newSimpleName (newVarName );
1054+ newCall .setExpression (caller );
1055+ List <Expression > arguments = indexOfCall .arguments ();
1056+ newCall .arguments ().add (rewrite .createCopyTarget (arguments .get (0 )));
1057+ rewrite .replace (indexOfCall , newCall , group );
1058+ }
10271059 for (SimpleName arg : fArgList ) {
10281060 SimpleName name = ast .newSimpleName (newVarName );
10291061 rewrite .replace (arg , name , group );
0 commit comments