コード例 #1
0
  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());
  }
コード例 #2
0
  /** @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());
  }
コード例 #3
0
  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());
  }
コード例 #4
0
  /** @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());
  }
コード例 #5
0
  /** @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());
  }
コード例 #6
0
  /** @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());
  }
コード例 #7
0
  /** @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());
  }
コード例 #8
0
 /*
  * @see ASTVisitor#visit(Assignment)
  */
 public boolean visit(Assignment node) {
   node.getLeftHandSide().accept(this);
   this.fBuffer.append(node.getOperator().toString());
   node.getRightHandSide().accept(this);
   return false;
 }