Esempio n. 1
0
 /**
  * 为约束表达式建立约束
  *
  * @param exp
  * @return
  */
 public Constraint constraintModeling(InfixExpression exp) {
   Constraint constraint = null;
   exp.getOperator();
   exp.getLeftOperand();
   exp.getRightOperand();
   return constraint;
 }
Esempio n. 2
0
 public Expression newInfixOperatorExpr(
     String operator, Expression leftOperand, Expression rightOperand) {
   InfixExpression expression = ast.newInfixExpression();
   expression.setOperator(Operator.toOperator(operator));
   expression.setLeftOperand(leftOperand);
   expression.setRightOperand(rightOperand);
   return expression;
 }
 protected static boolean isRemainderExp(Expression remainderExp) {
   while (remainderExp instanceof ParenthesizedExpression) {
     remainderExp = ((ParenthesizedExpression) remainderExp).getExpression();
   }
   if (remainderExp instanceof InfixExpression) {
     InfixExpression exp = ((InfixExpression) remainderExp);
     return REMAINDER.equals(exp.getOperator()) && isNumber(exp.getRightOperand(), 2);
   }
   return false;
 }
 protected static boolean isOddnessCheck(InfixExpression oddnessCheck) {
   if (EQUALS.equals(oddnessCheck.getOperator())) {
     if (isRemainderExp(oddnessCheck.getLeftOperand())) {
       return isNumber(oddnessCheck.getRightOperand(), 1);
     }
     if (isRemainderExp(oddnessCheck.getRightOperand())) {
       return isNumber(oddnessCheck.getLeftOperand(), 1);
     }
   }
   return false;
 }
Esempio n. 5
0
 /**
  * Builds a new {@link InfixExpression} instance.
  *
  * @param leftOperand the left operand
  * @param operator the infix operator
  * @param rightOperand the right operand
  * @param extendedOperands the extended operands
  * @return a new infix expression
  */
 public InfixExpression infixExpr(
     Expression leftOperand,
     InfixExpression.Operator operator,
     Expression rightOperand,
     Collection<? extends Expression> extendedOperands) {
   final InfixExpression ie = ast.newInfixExpression();
   ie.setLeftOperand(leftOperand);
   ie.setOperator(operator);
   ie.setRightOperand(rightOperand);
   extendedOperands(ie).addAll(extendedOperands);
   return ie;
 }
  /*
   * Must be one of:
   * <ul>
   * <li>[indexBinding]++</li>
   * <li>++[indexBinding]</li>
   * <li>[indexBinding]+= 1</li>
   * <li>[indexBinding]= [indexBinding] + 1</li>
   * <li>[indexBinding]= 1 + [indexBinding]</li>
   * <ul>
   */
  private boolean validateUpdaters(ForStatement statement) {
    List<Expression> updaters = statement.updaters();
    if (updaters.size() != 1) return false;

    Expression updater = updaters.get(0);
    if (updater instanceof PostfixExpression) {
      PostfixExpression postfix = (PostfixExpression) updater;

      if (!PostfixExpression.Operator.INCREMENT.equals(postfix.getOperator())) return false;

      IBinding binding = getBinding(postfix.getOperand());
      if (!fIndexBinding.equals(binding)) return false;

      return true;
    } else if (updater instanceof PrefixExpression) {
      PrefixExpression prefix = (PrefixExpression) updater;

      if (!PrefixExpression.Operator.INCREMENT.equals(prefix.getOperator())) return false;

      IBinding binding = getBinding(prefix.getOperand());
      if (!fIndexBinding.equals(binding)) return false;

      return true;
    } else if (updater instanceof Assignment) {
      Assignment assignment = (Assignment) updater;
      Expression left = assignment.getLeftHandSide();
      IBinding binding = getBinding(left);
      if (!fIndexBinding.equals(binding)) return false;

      if (Assignment.Operator.PLUS_ASSIGN.equals(assignment.getOperator())) {
        return isOneLiteral(assignment.getRightHandSide());
      } else if (Assignment.Operator.ASSIGN.equals(assignment.getOperator())) {
        Expression right = assignment.getRightHandSide();
        if (!(right instanceof InfixExpression)) return false;

        InfixExpression infixExpression = (InfixExpression) right;
        Expression leftOperand = infixExpression.getLeftOperand();
        IBinding leftBinding = getBinding(leftOperand);
        Expression rightOperand = infixExpression.getRightOperand();
        IBinding rightBinding = getBinding(rightOperand);

        if (fIndexBinding.equals(leftBinding)) {
          return isOneLiteral(rightOperand);
        } else if (fIndexBinding.equals(rightBinding)) {
          return isOneLiteral(leftOperand);
        }
      }
    }
    return false;
  }
 private static Expression createInfixInvocationFromPostPrefixExpression(
     InfixExpression.Operator operator,
     Expression getterExpression,
     AST ast,
     ITypeBinding variableType,
     boolean is50OrHigher) {
   InfixExpression infix = ast.newInfixExpression();
   infix.setLeftOperand(getterExpression);
   infix.setOperator(operator);
   NumberLiteral number = ast.newNumberLiteral();
   number.setToken("1"); // $NON-NLS-1$
   infix.setRightOperand(number);
   ITypeBinding infixType = infix.resolveTypeBinding();
   return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
 }
 /**
  * Converts an assignment, postfix expression or prefix expression into an assignable equivalent
  * expression using the getter.
  *
  * @param node the assignment/prefix/postfix node
  * @param astRewrite the astRewrite to use
  * @param getterExpression the expression to insert for read accesses or <code>null</code> if such
  *     an expression does not exist
  * @param variableType the type of the variable that the result will be assigned to
  * @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used
  * @return an expression that can be assigned to the type variableType with node being replaced by
  *     a equivalent expression using the getter
  */
 public static Expression getAssignedValue(
     ASTNode node,
     ASTRewrite astRewrite,
     Expression getterExpression,
     ITypeBinding variableType,
     boolean is50OrHigher) {
   InfixExpression.Operator op = null;
   AST ast = astRewrite.getAST();
   if (isNotInBlock(node)) return null;
   if (node.getNodeType() == ASTNode.ASSIGNMENT) {
     Assignment assignment = ((Assignment) node);
     Expression rightHandSide = assignment.getRightHandSide();
     Expression copiedRightOp = (Expression) astRewrite.createCopyTarget(rightHandSide);
     if (assignment.getOperator() == Operator.ASSIGN) {
       ITypeBinding rightHandSideType = rightHandSide.resolveTypeBinding();
       copiedRightOp =
           createNarrowCastIfNessecary(
               copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
       return copiedRightOp;
     }
     if (getterExpression != null) {
       InfixExpression infix = ast.newInfixExpression();
       infix.setLeftOperand(getterExpression);
       infix.setOperator(ASTNodes.convertToInfixOperator(assignment.getOperator()));
       infix.setRightOperand(copiedRightOp);
       ITypeBinding infixType = infix.resolveTypeBinding();
       return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
     }
   } else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
     PostfixExpression po = (PostfixExpression) node;
     if (po.getOperator() == PostfixExpression.Operator.INCREMENT)
       op = InfixExpression.Operator.PLUS;
     if (po.getOperator() == PostfixExpression.Operator.DECREMENT)
       op = InfixExpression.Operator.MINUS;
   } else if (node.getNodeType() == ASTNode.PREFIX_EXPRESSION) {
     PrefixExpression pe = (PrefixExpression) node;
     if (pe.getOperator() == PrefixExpression.Operator.INCREMENT)
       op = InfixExpression.Operator.PLUS;
     if (pe.getOperator() == PrefixExpression.Operator.DECREMENT)
       op = InfixExpression.Operator.MINUS;
   }
   if (op != null && getterExpression != null) {
     return createInfixInvocationFromPostPrefixExpression(
         op, getterExpression, ast, variableType, is50OrHigher);
   }
   return null;
 }
 /*
  * @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;
 }
 @Override
 public boolean visit(InfixExpression node) {
   if (numberExpression == null) {
     if (!isRemainderExp(node)) {
       return true;
     }
     numberExpression = node.getLeftOperand();
   }
   return false;
 }
Esempio n. 11
0
  @Override
  public void endVisit(InfixExpression node) {
    Stack<VMExpression> exps = new Stack<VMExpression>();
    exps.push((VMExpression) expressions.pop());
    exps.push((VMExpression) expressions.pop());
    if (node.hasExtendedOperands()) {
      for (int i = 0; i < node.extendedOperands().size(); i++) {
        exps.push((VMExpression) expressions.pop());
      }
    }
    VMInfixExpression.Operator operator =
        VMInfixExpression.Operator.toOperator(node.getOperator().toString());

    VMInfixExpression infixExp = new VMInfixExpression(node, operator);
    while (!exps.empty()) {
      infixExp.getExpressions().add(exps.pop());
    }
    expressions.push(infixExp);
  };
Esempio n. 12
0
    @Override
    public void endVisit(InfixExpression node) {
      if (node.getOperator() == InfixExpression.Operator.CONDITIONAL_AND
          || node.getOperator() == InfixExpression.Operator.CONDITIONAL_OR) {
        boolean isAnd = node.getOperator() == InfixExpression.Operator.CONDITIONAL_AND;
        boolean isSame = true;
        BooleanConstantLE diffVal = isAnd ? BooleanConstantLE.FALSE : BooleanConstantLE.TRUE;
        BooleanConstantLE sameVal = isAnd ? BooleanConstantLE.TRUE : BooleanConstantLE.FALSE;

        List<Expression> operands = new LinkedList<Expression>();

        operands.add(node.getLeftOperand());
        operands.add(node.getRightOperand());
        operands.addAll(node.extendedOperands());

        for (Expression exp : operands) {
          BooleanConstantLE val = fa.getResultsAfter(exp).get(fa.getVariable(exp));

          if (val == diffVal)
            reporter.reportUserProblem(
                "The expression " + node + " will always be " + !isAnd + ".", node, getName());
          isSame = isSame && val == sameVal;
        }

        if (isSame)
          reporter.reportUserProblem(
              "The expression " + node + " will always be " + isAnd + ".", node, getName());
      }
    }
 /* (non-Javadoc)
  * @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.InfixExpression)
  */
 public boolean visit(InfixExpression node) {
   PTInfixExpression inf = InstantiationFactory.eINSTANCE.createPTInfixExpression();
   inf.setLeftOperand(perform(node.getLeftOperand()));
   PTInfixOperator inoper = getParseInfix(node.getOperator());
   if (inoper == null) {
     // It is not one we can handle.
     throw new InvalidExpressionException(
         MessageFormat.format(
             WorkbenchUtilityMessages.ParseTreeCreationFromAST_OperatorTooComplicatedToHandle_EXC_,
             new Object[] {node.getOperator().toString()}));
   }
   inf.setOperator(inoper);
   inf.setRightOperand(perform(node.getRightOperand()));
   List eops = inf.getExtendedOperands();
   List neops = node.extendedOperands();
   int nsize = neops.size();
   for (int i = 0; i < nsize; i++) {
     eops.add(perform((Expression) neops.get(i)));
   }
   expression = inf;
   return false;
 }
  /*
   * Must be one of:
   * <ul>
   * <li>[indexBinding] < [result].length;</li>
   * <li>[result].length > [indexBinding];</li>
   * <li>[indexBinding] < [lengthBinding];</li>
   * <li>[lengthBinding] > [indexBinding];</li>
   * </ul>
   */
  private boolean validateExpression(ForStatement statement) {
    Expression expression = statement.getExpression();
    if (!(expression instanceof InfixExpression)) return false;

    InfixExpression infix = (InfixExpression) expression;

    Expression left = infix.getLeftOperand();
    Expression right = infix.getRightOperand();
    if (left instanceof SimpleName && right instanceof SimpleName) {
      IVariableBinding lengthBinding = fLengthBinding;
      if (lengthBinding == null) return false;

      IBinding leftBinding = ((SimpleName) left).resolveBinding();
      IBinding righBinding = ((SimpleName) right).resolveBinding();

      if (fIndexBinding.equals(leftBinding)) {
        return lengthBinding.equals(righBinding);
      } else if (fIndexBinding.equals(righBinding)) {
        return lengthBinding.equals(leftBinding);
      }

      return false;
    } else if (left instanceof SimpleName) {
      if (!fIndexBinding.equals(((SimpleName) left).resolveBinding())) return false;

      if (!Operator.LESS.equals(infix.getOperator())) return false;

      return validateLengthQuery(right);
    } else if (right instanceof SimpleName) {
      if (!fIndexBinding.equals(((SimpleName) right).resolveBinding())) return false;

      if (!Operator.GREATER.equals(infix.getOperator())) return false;

      return validateLengthQuery(left);
    }

    return false;
  }
  public void testCapturedLocalVariable() {
    resolveSource(
        "Test",
        "class Test { void test(final int i) { Runnable r = new Runnable() { "
            + "public void run() { int i2 = i + 1; } }; } }");

    AnonymousClassDeclaration runnableNode =
        (AnonymousClassDeclaration) nodesByType.get(ASTNode.ANONYMOUS_CLASS_DECLARATION).get(0);
    ITypeBinding runnableBinding = runnableNode.resolveBinding();
    assertFalse(OuterReferenceResolver.needsOuterReference(runnableBinding));
    List<IVariableBinding> capturedVars = OuterReferenceResolver.getCapturedVars(runnableBinding);
    List<IVariableBinding> innerFields = OuterReferenceResolver.getInnerFields(runnableBinding);
    assertEquals(1, capturedVars.size());
    assertEquals(1, innerFields.size());
    assertEquals("i", capturedVars.get(0).getName());
    assertEquals("val$i", innerFields.get(0).getName());

    InfixExpression addition = (InfixExpression) nodesByType.get(ASTNode.INFIX_EXPRESSION).get(0);
    List<IVariableBinding> iPath = OuterReferenceResolver.getPath(addition.getLeftOperand());
    assertNotNull(iPath);
    assertEquals(1, iPath.size());
    assertEquals("val$i", iPath.get(0).getName());
  }
Esempio n. 16
0
  /**
   * 解析exp得到ExpressionNode
   *
   * @param exp
   * @return
   */
  public ExpressionNode getExpressionNode(Expression exp) {
    ExpressionNode result = null;
    ExpressionNode leftPart = null;
    ExpressionNode rightPart = null;
    ExpressionType type = null;
    ExpressionOperator operator = null;
    // InfixExpression.Operator operator;

    if (exp instanceof InfixExpression) {
      ((InfixExpression) exp).getOperator();
    } else if (exp instanceof SimpleName) {

    } else if (exp instanceof NumberLiteral) {

    }
    return result;
  }
  private void collectSideEffects(Expression expr, List<Expression> sideEffectExprs) {
    // local variable, parameter, enum constant, etc.
    // OR method starting with is*(), get*()
    // except atomic long, atomic integer, etc.
    switch (expr.getNodeType()) {
      case METHOD_INVOCATION:
        MethodInvocation mi = (MethodInvocation) expr;
        methodHasSideEffects(mi.resolveMethodBinding(), mi, sideEffectExprs);
        collectSideEffects(mi.getExpression(), sideEffectExprs);
        collectSideEffects(arguments(mi), sideEffectExprs);
        break;

      case SUPER_METHOD_INVOCATION:
        SuperMethodInvocation smi = (SuperMethodInvocation) expr;
        methodHasSideEffects(smi.resolveMethodBinding(), smi, sideEffectExprs);
        collectSideEffects(arguments(smi), sideEffectExprs);
        break;

      case CLASS_INSTANCE_CREATION:
        ClassInstanceCreation cic = (ClassInstanceCreation) expr;
        methodHasSideEffects(cic.resolveConstructorBinding(), cic, sideEffectExprs);
        collectSideEffects(cic.getExpression(), sideEffectExprs);
        collectSideEffects(arguments(cic), sideEffectExprs);
        break;

      case ARRAY_ACCESS:
        ArrayAccess aa = (ArrayAccess) expr;
        collectSideEffects(aa.getArray(), sideEffectExprs);
        collectSideEffects(aa.getIndex(), sideEffectExprs);
        break;
      case ARRAY_CREATION:
        ArrayCreation ac = (ArrayCreation) expr;
        collectSideEffects(ac.getInitializer(), sideEffectExprs);
        collectSideEffects(ac.dimensions(), sideEffectExprs);
        break;
      case ARRAY_INITIALIZER:
        ArrayInitializer ai = (ArrayInitializer) expr;
        collectSideEffects(expressions(ai), sideEffectExprs);
        break;
      case ASSIGNMENT:
        Assignment as = (Assignment) expr;
        collectSideEffects(as.getLeftHandSide(), sideEffectExprs);
        collectSideEffects(as.getRightHandSide(), sideEffectExprs);
        break;

      case CONDITIONAL_EXPRESSION:
        ConditionalExpression ce = (ConditionalExpression) expr;
        collectSideEffects(ce.getExpression(), sideEffectExprs);
        collectSideEffects(ce.getThenExpression(), sideEffectExprs);
        collectSideEffects(ce.getElseExpression(), sideEffectExprs);
        break;

      case FIELD_ACCESS:
        FieldAccess fa = (FieldAccess) expr;
        collectSideEffects(fa.getExpression(), sideEffectExprs);
        collectSideEffects(fa.getName(), sideEffectExprs);
        break;
      case SUPER_FIELD_ACCESS:
        SuperFieldAccess sfa = (SuperFieldAccess) expr;
        collectSideEffects(sfa.getQualifier(), sideEffectExprs);
        collectSideEffects(sfa.getName(), sideEffectExprs);
        break;
      case THIS_EXPRESSION:
        collectSideEffects(((ThisExpression) expr).getQualifier(), sideEffectExprs);
        break;
      case VARIABLE_DECLARATION_EXPRESSION:
        collectSideEffects((VariableDeclarationExpression) expr, sideEffectExprs);
        break;

      case INFIX_EXPRESSION:
        InfixExpression ie = (InfixExpression) expr;
        collectSideEffects(ie.getLeftOperand(), sideEffectExprs);
        collectSideEffects(ie.getRightOperand(), sideEffectExprs);
        collectSideEffects(extendedOperands(ie), sideEffectExprs);
        break;

      case CAST_EXPRESSION:
        collectSideEffects(((CastExpression) expr).getExpression(), sideEffectExprs);
        break;
      case INSTANCEOF_EXPRESSION:
        collectSideEffects(((InstanceofExpression) expr).getLeftOperand(), sideEffectExprs);
        break;
      case PARENTHESIZED_EXPRESSION:
        collectSideEffects(((ParenthesizedExpression) expr).getExpression(), sideEffectExprs);
        break;
      case POSTFIX_EXPRESSION:
        sideEffectExprs.add(expr);
        break;
      case PREFIX_EXPRESSION:
        PrefixExpression pe = (PrefixExpression) expr;
        PrefixExpression.Operator op = pe.getOperator();
        if (PrefixExpression.Operator.INCREMENT.equals(op)
            | PrefixExpression.Operator.DECREMENT.equals(op)) {
          sideEffectExprs.add(pe);
        } else {
          collectSideEffects(pe.getOperand(), sideEffectExprs);
        }
        break;

      default:
        // literals
        // names
    }
  }
 @CheckForNull
 protected Expression findNumberExpression(InfixExpression oddnessCheck) {
   NumberExpressionFinder finder = new NumberExpressionFinder();
   oddnessCheck.accept(finder);
   return finder.getNumberExpression();
 }
 private String parseInfixExpression(InfixExpression expression) {
   // will implement if someone really wants it...
   return expression.toString();
 }