コード例 #1
0
 private static int getExpressionPrecedence(Expression expression) {
   if (expression instanceof PostfixExpression || expression instanceof FunctionInvocation) {
     return 0;
   }
   if (expression instanceof PrefixExpression) {
     return 1;
   }
   if (expression instanceof ClassInstanceCreation) {
     return 2;
   }
   if (expression instanceof InfixExpression) {
     InfixExpression infixExpression = (InfixExpression) expression;
     InfixExpression.Operator operator = infixExpression.getOperator();
     return getInfixOperatorPrecedence(operator);
   }
   if (expression instanceof InstanceofExpression) {
     return 6;
   }
   if (expression instanceof ConditionalExpression) {
     return 13;
   }
   if (expression instanceof Assignment) {
     return 14;
   }
   return -1;
 }
コード例 #2
0
    public void postVisit(ASTNode node) {
      // check that parent is && or ||
      if (!(node.getParent() instanceof InfixExpression)) return;

      // we want to add parenthesis around arithmetic operators and instanceof
      boolean needParenthesis = false;
      if (node instanceof InfixExpression) {
        InfixExpression expression = (InfixExpression) node;
        InfixExpression.Operator operator = expression.getOperator();

        InfixExpression parentExpression = (InfixExpression) node.getParent();
        InfixExpression.Operator parentOperator = parentExpression.getOperator();

        if (parentOperator == operator) return;

        needParenthesis =
            (operator == InfixExpression.Operator.LESS)
                || (operator == InfixExpression.Operator.GREATER)
                || (operator == InfixExpression.Operator.LESS_EQUALS)
                || (operator == InfixExpression.Operator.GREATER_EQUALS)
                || (operator == InfixExpression.Operator.EQUALS)
                || (operator == InfixExpression.Operator.NOT_EQUALS)
                || (operator == InfixExpression.Operator.CONDITIONAL_AND)
                || (operator == InfixExpression.Operator.CONDITIONAL_OR);
      }
      if (node instanceof InstanceofExpression) {
        needParenthesis = true;
      }
      if (!needParenthesis) {
        return;
      }
      fNodes.add(node);
    }
コード例 #3
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());
  }
コード例 #4
0
 /*
  * @see ASTVisitor#visit(InfixExpression)
  */
 public boolean visit(InfixExpression node) {
   node.getLeftOperand().accept(this);
   this.fBuffer.append(' '); // for cases like x= i - -1; or x= i++ + ++i;
   this.fBuffer.append(node.getOperator().toString());
   this.fBuffer.append(' ');
   node.getRightOperand().accept(this);
   final List extendedOperands = node.extendedOperands();
   if (extendedOperands.size() != 0) {
     this.fBuffer.append(' ');
     for (Iterator it = extendedOperands.iterator(); it.hasNext(); ) {
       this.fBuffer.append(node.getOperator().toString()).append(' ');
       Expression e = (Expression) it.next();
       e.accept(this);
     }
   }
   return false;
 }
コード例 #5
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());
  }
コード例 #6
0
 /* (omit javadoc for this method)
  * Method declared on ASTNode.
  */
 ASTNode clone0(AST target) {
   InfixExpression result = new InfixExpression(target);
   result.setSourceRange(this.getStartPosition(), this.getLength());
   result.setOperator(getOperator());
   result.setLeftOperand((Expression) getLeftOperand().clone(target));
   result.setRightOperand((Expression) getRightOperand().clone(target));
   if (this.extendedOperands != null) {
     // be careful not to trigger lazy creation of list
     result.extendedOperands().addAll(ASTNode.copySubtrees(target, this.extendedOperands()));
   }
   return result;
 }
コード例 #7
0
 public void postVisit(ASTNode node) {
   if (!(node instanceof ParenthesizedExpression)) {
     return;
   }
   ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) node;
   Expression expression = parenthesizedExpression.getExpression();
   while (expression instanceof ParenthesizedExpression) {
     expression = ((ParenthesizedExpression) expression).getExpression();
   }
   // if this is part of another expression, check for this and parent precedences
   if (parenthesizedExpression.getParent() instanceof Expression) {
     Expression parentExpression = (Expression) parenthesizedExpression.getParent();
     int expressionPrecedence = getExpressionPrecedence(expression);
     int parentPrecedence = getExpressionPrecedence(parentExpression);
     if ((expressionPrecedence > parentPrecedence)
         && !(parenthesizedExpression.getParent() instanceof ParenthesizedExpression)) {
       return;
     }
     // check for case when precedences for expression and parent are same
     if ((expressionPrecedence == parentPrecedence)
         && (parentExpression instanceof InfixExpression)) {
       // we have expr infix (expr infix expr) removing the parenthesis is equal to (expr infix
       // expr) infix expr
       InfixExpression parentInfix = (InfixExpression) parentExpression;
       Operator parentOperator = parentInfix.getOperator();
       if (parentInfix.getLeftOperand() == parenthesizedExpression) {
         fNodes.add(node);
       } else if (isAssoziative(parentOperator)) {
         if (parentOperator == InfixExpression.Operator.PLUS) {
           if (isStringExpression(parentInfix.getLeftOperand())
               || isStringExpression(parentInfix.getRightOperand())) {
             return;
           }
           for (Iterator J = parentInfix.extendedOperands().iterator(); J.hasNext(); ) {
             Expression operand = (Expression) J.next();
             if (isStringExpression(operand)) {
               return;
             }
           }
         }
         fNodes.add(node);
       }
       return;
     } else if (expressionPrecedence == parentPrecedence
         && parentExpression instanceof ConditionalExpression) {
       if (((ConditionalExpression) parentExpression).getElseExpression()
           != parenthesizedExpression) return;
     }
   }
   fNodes.add(node);
 }
コード例 #8
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());
  }