public void testBug128818() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" if (true) {\n"); buf.append(" } // comment\n"); buf.append(" else\n"); buf.append(" return;\n"); buf.append("}\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST3(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); FunctionDeclaration function = findMethodDeclaration(astRoot, "foo"); IfStatement statement = (IfStatement) function.getBody().statements().get(0); rewrite.set(statement, IfStatement.ELSE_STATEMENT_PROPERTY, ast.newBlock(), null); String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" if (true) {\n"); buf.append(" } // comment\n"); buf.append(" else {\n"); buf.append("}\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }
public void testCommentAtEnd() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function E() \n"); buf.append("{\n"); buf.append("}//comment"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST3(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); ListRewrite listRewrite = rewrite.getListRewrite(astRoot, JavaScriptUnit.STATEMENTS_PROPERTY); FunctionDeclaration newFunction = ast.newFunctionDeclaration(); newFunction.setName(ast.newSimpleName("B")); listRewrite.insertLast(newFunction, null); String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function E() \n"); buf.append("{\n"); buf.append("}//comment\n"); buf.append("\n"); buf.append("function B() {\n"); buf.append("}"); assertEqualString(preview, buf.toString()); }
private void replaceExpressionsWithConstant() throws JavaScriptModelException { ASTRewrite astRewrite = fCuRewrite.getASTRewrite(); AST ast = astRewrite.getAST(); IASTFragment[] fragmentsToReplace = getFragmentsToReplace(); for (int i = 0; i < fragmentsToReplace.length; i++) { IASTFragment fragment = fragmentsToReplace[i]; SimpleName ref = ast.newSimpleName(fConstantName); Name replacement = ref; if (qualifyReferencesWithDeclaringClassName()) { replacement = ast.newQualifiedName(ast.newSimpleName(getContainingTypeBinding().getName()), ref); } TextEditGroup description = fCuRewrite.createGroupDescription( RefactoringCoreMessages.ExtractConstantRefactoring_replace); fragment.replace(astRewrite, replacement, description); if (fLinkedProposalModel != null) fLinkedProposalModel .getPositionGroup(KEY_NAME, true) .addPosition(astRewrite.track(ref), false); } }
public void testAssignment() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" var i, j;\n"); buf.append(" i= 0;\n"); buf.append(" i-= j= 3;\n"); buf.append("}\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 3", statements.size() == 3); { // change left side & right side ExpressionStatement stmt = (ExpressionStatement) statements.get(1); Assignment assignment = (Assignment) stmt.getExpression(); SimpleName name = ast.newSimpleName("j"); rewrite.replace(assignment.getLeftHandSide(), name, null); FunctionInvocation invocation = ast.newFunctionInvocation(); invocation.setName(ast.newSimpleName("goo")); invocation.setExpression(ast.newSimpleName("other")); rewrite.replace(assignment.getRightHandSide(), invocation, null); } { // change operator and operator of inner ExpressionStatement stmt = (ExpressionStatement) statements.get(2); Assignment assignment = (Assignment) stmt.getExpression(); rewrite.set( assignment, Assignment.OPERATOR_PROPERTY, Assignment.Operator.DIVIDE_ASSIGN, null); Assignment inner = (Assignment) assignment.getRightHandSide(); rewrite.set( inner, Assignment.OPERATOR_PROPERTY, Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" var i, j;\n"); buf.append(" j= other.goo();\n"); buf.append(" i/= j>>>= 3;\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }
public void testCatchClause() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" try {\n"); buf.append(" } catch (e) {\n"); buf.append(" }\n"); buf.append("}\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 3", statements.size() == 1); List catchClauses = ((TryStatement) statements.get(0)).catchClauses(); assertTrue("Number of catchClauses not 1", catchClauses.size() == 1); { // change exception type CatchClause clause = (CatchClause) catchClauses.get(0); SingleVariableDeclaration exception = clause.getException(); SingleVariableDeclaration newException = ast.newSingleVariableDeclaration(); newException.setName(ast.newSimpleName("ex")); rewrite.replace(exception, newException, null); } { // change body CatchClause clause = (CatchClause) catchClauses.get(0); Block body = clause.getBody(); Block newBody = ast.newBlock(); ReturnStatement returnStatement = ast.newReturnStatement(); newBody.statements().add(returnStatement); rewrite.replace(body, newBody, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" try {\n"); buf.append(" } catch (ex) {\n"); buf.append(" return;\n"); buf.append(" }\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }
public boolean visit(SuperConstructorInvocation node) { if (matches(node.resolveConstructorBinding())) { SimpleName name = fAST.newSimpleName("xxxxx"); // $NON-NLS-1$ name.setSourceRange(node.getStartPosition(), 5); fResult.add(name); } return super.visit(node); }
public boolean visit(ThrowStatement node) { if (matches(node.getExpression().resolveTypeBinding())) { SimpleName name = fAST.newSimpleName("xxxxx"); // $NON-NLS-1$ name.setSourceRange(node.getStartPosition(), 5); fResult.add(name); } return super.visit(node); }
public void testArrayAccess() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo(o) {\n"); buf.append(" o[3 /* comment*/ - 1]= this.o[3 - 1];\n"); buf.append("}\n"); buf.append(""); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 1", statements.size() == 1); { // replace left hand side index, replace right hand side index by left side index ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); ArrayAccess left = (ArrayAccess) assignment.getLeftHandSide(); ArrayAccess right = (ArrayAccess) assignment.getRightHandSide(); NumberLiteral name = ast.newNumberLiteral("1"); rewrite.replace(left.getIndex(), name, null); ASTNode placeHolder = rewrite.createCopyTarget(left.getIndex()); rewrite.replace(right.getIndex(), placeHolder, null); SimpleName newName = ast.newSimpleName("o"); rewrite.replace(right.getArray(), newName, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo(o) {\n"); buf.append(" o[1]= o[3 /* comment*/ - 1];\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }
// TODO fix public void XtestThisExpression() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" return this;\n"); buf.append(" return Outer.this;\n"); buf.append("}\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 2", statements.size() == 2); { // add qualifier ReturnStatement returnStatement = (ReturnStatement) statements.get(0); ThisExpression thisExpression = (ThisExpression) returnStatement.getExpression(); SimpleName newExpression = ast.newSimpleName("X"); rewrite.set(thisExpression, ThisExpression.QUALIFIER_PROPERTY, newExpression, null); } { // remove qualifier ReturnStatement returnStatement = (ReturnStatement) statements.get(1); ThisExpression thisExpression = (ThisExpression) returnStatement.getExpression(); rewrite.remove(thisExpression.getQualifier(), null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" return X.this;\n"); buf.append(" return this;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
/** {@inheritDoc} */ public void rewriteAST(CompilationUnitRewrite cuRewrite, List textEditGroups) throws CoreException { TextEditGroup group = createTextEditGroup(FixMessages.ExpressionsFix_addParanoiacParenthesis_description); textEditGroups.add(group); ASTRewrite rewrite = cuRewrite.getASTRewrite(); AST ast = cuRewrite.getRoot().getAST(); for (int i = 0; i < fExpressions.length; i++) { // add parenthesis around expression Expression expression = fExpressions[i]; ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression(); parenthesizedExpression.setExpression((Expression) rewrite.createCopyTarget(expression)); rewrite.replace(expression, parenthesizedExpression, group); } }
/** @deprecated using deprecated code */ public void testFieldAccess() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" foo().i= goo().i;\n"); buf.append(" }\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); // TypeDeclaration type= findTypeDeclaration(astRoot, "E"); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 1", statements.size() == 1); { // replace field expression, replace field name ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); FieldAccess leftFieldAccess = (FieldAccess) assignment.getLeftHandSide(); FieldAccess rightFieldAccess = (FieldAccess) assignment.getRightHandSide(); FunctionInvocation invocation = ast.newFunctionInvocation(); invocation.setName(ast.newSimpleName("xoo")); rewrite.replace(leftFieldAccess.getExpression(), invocation, null); SimpleName newName = ast.newSimpleName("x"); rewrite.replace(leftFieldAccess.getName(), newName, null); SimpleName rightHand = ast.newSimpleName("b"); rewrite.replace(rightFieldAccess.getExpression(), rightHand, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" xoo().x= b.i;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
/** @deprecated using deprecated code */ public void testPostfixExpression() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= x--;\n"); buf.append(" }\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); // TypeDeclaration type= findTypeDeclaration(astRoot, "E"); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 1", statements.size() == 1); { // modify operand and operation ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); PostfixExpression postExpression = (PostfixExpression) assignment.getRightHandSide(); NumberLiteral newOperation = ast.newNumberLiteral("10"); rewrite.replace(postExpression.getOperand(), newOperation, null); rewrite.set( postExpression, PostfixExpression.OPERATOR_PROPERTY, PostfixExpression.Operator.INCREMENT, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= 10++;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
/** @deprecated using deprecated code */ public void testParenthesizedExpression() throws Exception { // System.out.println(getClass().getName()+"::" + getName() +" disabled (bug 23362)"); IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= (1 + 2) * 3;\n"); buf.append(" }\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); // TypeDeclaration type= findTypeDeclaration(astRoot, "E"); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 1", statements.size() == 1); { // replace expression ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); InfixExpression multiplication = (InfixExpression) assignment.getRightHandSide(); ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) multiplication.getLeftOperand(); SimpleName name = ast.newSimpleName("x"); rewrite.replace(parenthesizedExpression.getExpression(), name, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= (x) * 3;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
public void testBug95839() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" object.method(\n"); buf.append(" param1, // text about param1\n"); buf.append(" param2 // text about param2\n"); buf.append(" );\n"); buf.append("}\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST3(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); FunctionDeclaration function = findMethodDeclaration(astRoot, "foo"); ExpressionStatement statement = (ExpressionStatement) function.getBody().statements().get(0); FunctionInvocation inv = (FunctionInvocation) statement.getExpression(); ListRewrite listRewrite = rewrite.getListRewrite(inv, FunctionInvocation.ARGUMENTS_PROPERTY); listRewrite.insertLast(ast.newSimpleName("param3"), null); String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" object.method(\n"); buf.append(" param1, // text about param1\n"); buf.append(" param2 // text about param2\n"); buf.append(", param3\n"); buf.append(" );\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }
/** @deprecated using deprecated code */ public void testConditionalExpression() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= (k == 0) ? 1 : 2;\n"); buf.append(" }\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); // TypeDeclaration type= findTypeDeclaration(astRoot, "E"); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 1", statements.size() == 1); { // change compare expression, then expression & else expression ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); ConditionalExpression condExpression = (ConditionalExpression) assignment.getRightHandSide(); BooleanLiteral literal = ast.newBooleanLiteral(true); rewrite.replace(condExpression.getExpression(), literal, null); SimpleName newThenExpre = ast.newSimpleName("x"); rewrite.replace(condExpression.getThenExpression(), newThenExpre, null); InfixExpression infixExpression = ast.newInfixExpression(); infixExpression.setLeftOperand(ast.newNumberLiteral("1")); infixExpression.setRightOperand(ast.newNumberLiteral("2")); infixExpression.setOperator(InfixExpression.Operator.PLUS); rewrite.replace(condExpression.getElseExpression(), infixExpression, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= true ? x : 1 + 2;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
/** @deprecated using deprecated code */ public void testMethodInvocation() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" foo(1, 2).goo();\n"); buf.append(" foo(1, 2).goo();\n"); buf.append(" foo(1, 2).goo();\n"); buf.append(" }\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); // TypeDeclaration type= findTypeDeclaration(astRoot, "E"); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 3", statements.size() == 3); { // remove expression, add param, change name ExpressionStatement stmt = (ExpressionStatement) statements.get(0); FunctionInvocation invocation = (FunctionInvocation) stmt.getExpression(); rewrite.remove(invocation.getExpression(), null); SimpleName name = ast.newSimpleName("x"); rewrite.replace(invocation.getName(), name, null); ASTNode arg = ast.newNumberLiteral("1"); rewrite .getListRewrite(invocation, FunctionInvocation.ARGUMENTS_PROPERTY) .insertLast(arg, null); } { // insert expression, delete params ExpressionStatement stmt = (ExpressionStatement) statements.get(1); FunctionInvocation invocation = (FunctionInvocation) stmt.getExpression(); FunctionInvocation leftInvocation = (FunctionInvocation) invocation.getExpression(); SimpleName newExpression = ast.newSimpleName("x"); rewrite.set(leftInvocation, FunctionInvocation.EXPRESSION_PROPERTY, newExpression, null); List args = leftInvocation.arguments(); rewrite.remove((ASTNode) args.get(0), null); rewrite.remove((ASTNode) args.get(1), null); } { // remove expression, add it as parameter ExpressionStatement stmt = (ExpressionStatement) statements.get(2); FunctionInvocation invocation = (FunctionInvocation) stmt.getExpression(); ASTNode placeHolder = rewrite.createCopyTarget(invocation.getExpression()); rewrite.set(invocation, FunctionInvocation.EXPRESSION_PROPERTY, null, null); rewrite .getListRewrite(invocation, FunctionInvocation.ARGUMENTS_PROPERTY) .insertLast(placeHolder, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" x(1);\n"); buf.append(" x.foo().goo();\n"); buf.append(" goo(foo(1, 2));\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
/** @deprecated using deprecated code */ public void testInfixExpression() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= 1 + 2;\n"); buf.append(" j= 1 + 2 + 3 + 4 + 5;\n"); buf.append(" k= 1 + 2 + 3 + 4 + 5;\n"); buf.append(" }\n"); IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null); JavaScriptUnit astRoot = createAST(cu); ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST()); AST ast = astRoot.getAST(); assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); // TypeDeclaration type= findTypeDeclaration(astRoot, "E"); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 3", statements.size() == 3); { // change left side & right side & operand ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); InfixExpression expr = (InfixExpression) assignment.getRightHandSide(); SimpleName leftOp = ast.newSimpleName("k"); rewrite.replace(expr.getLeftOperand(), leftOp, null); SimpleName rightOp = ast.newSimpleName("j"); rewrite.replace(expr.getRightOperand(), rightOp, null); // change operand rewrite.set(expr, InfixExpression.OPERATOR_PROPERTY, InfixExpression.Operator.MINUS, null); } { // remove an ext. operand, add one and replace one ExpressionStatement stmt = (ExpressionStatement) statements.get(1); Assignment assignment = (Assignment) stmt.getExpression(); InfixExpression expr = (InfixExpression) assignment.getRightHandSide(); List extendedOperands = expr.extendedOperands(); assertTrue("Number of extendedOperands not 3", extendedOperands.size() == 3); rewrite.remove((ASTNode) extendedOperands.get(0), null); SimpleName newOp1 = ast.newSimpleName("k"); rewrite.replace((ASTNode) extendedOperands.get(1), newOp1, null); SimpleName newOp2 = ast.newSimpleName("n"); rewrite .getListRewrite(expr, InfixExpression.EXTENDED_OPERANDS_PROPERTY) .insertLast(newOp2, null); } { // change operand ExpressionStatement stmt = (ExpressionStatement) statements.get(2); Assignment assignment = (Assignment) stmt.getExpression(); InfixExpression expr = (InfixExpression) assignment.getRightHandSide(); rewrite.set(expr, InfixExpression.OPERATOR_PROPERTY, InfixExpression.Operator.TIMES, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= k - j;\n"); buf.append(" j= 1 + 2 + k + 5 + n;\n"); buf.append(" k= 1 * 2 * 3 * 4 * 5;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
private void createConstantDeclaration() throws CoreException { Type type = getConstantType(); IExpressionFragment fragment = getSelectedExpression(); String initializerSource = fCu.getBuffer().getText(fragment.getStartPosition(), fragment.getLength()); AST ast = fCuRewrite.getAST(); VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment(); variableDeclarationFragment.setName(ast.newSimpleName(fConstantName)); variableDeclarationFragment.setInitializer( (Expression) fCuRewrite .getASTRewrite() .createStringPlaceholder(initializerSource, ASTNode.SIMPLE_NAME)); FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment); fieldDeclaration.setType(type); Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility); if (accessModifier != null) fieldDeclaration.modifiers().add(ast.newModifier(accessModifier)); fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaScriptProject()) .createComments; if (createComments) { String comment = CodeGeneration.getFieldComment( fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu)); if (comment != null && comment.length() > 0) { JSdoc doc = (JSdoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JSDOC); fieldDeclaration.setJavadoc(doc); } } AbstractTypeDeclaration parent = getContainingTypeDeclarationNode(); ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty()); TextEditGroup msg = fCuRewrite.createGroupDescription( RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant); if (insertFirst()) { listRewrite.insertFirst(fieldDeclaration, msg); } else { listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg); } if (fLinkedProposalModel != null) { ASTRewrite rewrite = fCuRewrite.getASTRewrite(); LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true); nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true); String[] nameSuggestions = guessConstantNames(); if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) { nameGroup.addProposal(fConstantName, null, nameSuggestions.length + 1); } for (int i = 0; i < nameSuggestions.length; i++) { nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i); } LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true); typeGroup.addPosition(rewrite.track(type), true); ITypeBinding typeBinding = fragment.getAssociatedExpression().resolveTypeBinding(); if (typeBinding != null) { ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding); for (int i = 0; i < relaxingTypes.length; i++) { typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i); } } ModifierCorrectionSubProcessor.installLinkedVisibilityProposals( fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), false); } }