@Override public boolean visit(PostfixExpression exp) { if(exp.getOperand() instanceof SimpleName) { String varName = exp.getOperand().toString(); VariableOperation op = null; if(exp.getOperator() == PostfixExpression.Operator.INCREMENT) op = new VariableOperation(varName, VariableOperation.Type.INC); else if(exp.getOperator() == PostfixExpression.Operator.DECREMENT) op = new VariableOperation(varName, VariableOperation.Type.DEC); if(op != null) current.addOperation(op); } return true; }
@Override public boolean visit(PrefixExpression exp) { if(exp.getOperand() instanceof SimpleName) { String varName = exp.getOperand().toString(); VariableOperation op = null; if(exp.getOperator() == PrefixExpression.Operator.INCREMENT) op = new VariableOperation(varName, VariableOperation.Type.INC); else if(exp.getOperator() == PrefixExpression.Operator.DECREMENT) op = new VariableOperation(varName, VariableOperation.Type.DEC); if(op != null) current.addOperation(op); } return true; }
public boolean visit(InfixExpression exp) { Operator op = exp.getOperator(); if(isCompareOperator(op)) { String leftExp = exp.getLeftOperand().toString(); String rightExp = exp.getRightOperand().toString(); Set<String> incVars = current.getOperations(VariableOperation.Type.INC, VariableOperation.Type.DEC); if(exp.getLeftOperand() instanceof SimpleName && incVars.contains(leftExp)) aux(leftExp, op, exp.getRightOperand()); if(exp.getRightOperand() instanceof SimpleName && incVars.contains(rightExp)) aux(rightExp, op, exp.getLeftOperand()); } return true; }
private static boolean isAcumulationAssign(Assignment assignment, InfixExpression.Operator op, Predicate<Expression> acceptExpression) { if(!( assignment.getRightHandSide() instanceof InfixExpression && assignment.getLeftHandSide() instanceof SimpleName && assignment.getOperator() == Assignment.Operator.ASSIGN)) return false; InfixExpression exp = (InfixExpression) assignment.getRightHandSide(); if(exp.getOperator() != op) return false; String assignVar = assignment.getLeftHandSide().toString(); if( exp.getLeftOperand() instanceof SimpleName && exp.getLeftOperand().toString().equals(assignVar) && acceptExpression.test(exp.getRightOperand())) return true; if( exp.getRightOperand() instanceof SimpleName && exp.getRightOperand().toString().equals(assignVar) && acceptExpression.test(exp.getLeftOperand())) return true; return false; }
private static boolean isAssociative(InfixExpression.Operator operator, ITypeBinding infixExprType, boolean isAllOperandsHaveSameType) { if (operator == InfixExpression.Operator.PLUS) { return isStringType(infixExprType) || isIntegerType(infixExprType) && isAllOperandsHaveSameType; } if (operator == InfixExpression.Operator.TIMES) { return isIntegerType(infixExprType) && isAllOperandsHaveSameType; } if (operator == InfixExpression.Operator.CONDITIONAL_AND || operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.AND || operator == InfixExpression.Operator.OR || operator == InfixExpression.Operator.XOR) { return true; } return false; }
/** * Returns the type of infix expression based on its operands and operator. * * @param operator the operator of infix expression * @param leftOperandType the type of left operand of infix expression * @param rightOperandType the type of right operand of infix expression * @return the type of infix expression if the type of both the operands is same or if the type * of either operand of a + operator is String, <code>null</code> otherwise. * * @since 3.9 */ private static ITypeBinding getInfixExpressionType(InfixExpression.Operator operator, ITypeBinding leftOperandType, ITypeBinding rightOperandType) { if (leftOperandType == rightOperandType) { return leftOperandType; } if (operator == InfixExpression.Operator.PLUS) { if (isStringType(leftOperandType)) { return leftOperandType; } else if (isStringType(rightOperandType)) { return rightOperandType; } } // If the left and right operand types are different, we assume that parentheses are needed. // This is to avoid complications of numeric promotions and for readability of complicated code. return null; }
/** * Returns the precedence of an infix operator. Operators * with higher precedence are executed before expressions * with lower precedence. * <br> * i.e. in: <br> * <code>3 + 4 - 5 * 6;</code><br> * the precedence order is * <ul> * <li>*</li> * <li>+</li> * <li>-</li> * </ul> * 1. 5,6 -(*)-> 30<br> * 2. 3,4 -(+)-> 7<br> * 3. 7,30 -(-)-> -23<br> * * @param operator the expression to determine the precedence for * @return the precedence the higher to stronger the binding to its operands */ public static int getOperatorPrecedence(Operator operator) { if (operator == Operator.CONDITIONAL_OR) { return CONDITIONAL_OR; } else if (operator == Operator.CONDITIONAL_AND) { return CONDITIONAL_AND; } else if (operator == Operator.OR) { return BITWISE_INCLUSIVE_OR; } else if (operator == Operator.XOR) { return BITWISE_EXCLUSIVE_OR; } else if (operator == Operator.AND) { return BITWISE_AND; } else if (operator == Operator.EQUALS || operator == Operator.NOT_EQUALS) { return EQUALITY; } else if (operator == Operator.LESS || operator == Operator.LESS_EQUALS || operator == Operator.GREATER || operator == Operator.GREATER_EQUALS) { return RELATIONAL; } else if (operator == Operator.LEFT_SHIFT || operator == Operator.RIGHT_SHIFT_SIGNED || operator == Operator.RIGHT_SHIFT_UNSIGNED) { return SHIFT; } else if (operator == Operator.PLUS || operator == Operator.MINUS) { return ADDITIVE; } else if (operator == Operator.REMAINDER || operator == Operator.DIVIDE || operator == Operator.TIMES) { return MULTIPLICATIVE; } return Integer.MAX_VALUE; }
protected String getNumber(PrefixExpression prefixExpression) { String number = null; Expression operand = prefixExpression.getOperand(); if (operand.getNodeType() == ASTNode.NUMBER_LITERAL) { org.eclipse.jdt.core.dom.PrefixExpression.Operator operator = prefixExpression .getOperator(); if (org.eclipse.jdt.core.dom.PrefixExpression.Operator.MINUS .equals(operator)) { number = "-" + operand.toString(); } else if (org.eclipse.jdt.core.dom.PrefixExpression.Operator.PLUS .equals(operator) || org.eclipse.jdt.core.dom.PrefixExpression.Operator.DECREMENT .equals(operator) || org.eclipse.jdt.core.dom.PrefixExpression.Operator.INCREMENT .equals(operator)) { number = operand.toString(); } else { number = "0"; } } return number; }
private static boolean isAssociative( InfixExpression.Operator operator, ITypeBinding infixExprType, boolean isAllOperandsHaveSameType) { if (operator == InfixExpression.Operator.PLUS) return isStringType(infixExprType) || isIntegerType(infixExprType) && isAllOperandsHaveSameType; if (operator == InfixExpression.Operator.TIMES) return isIntegerType(infixExprType) && isAllOperandsHaveSameType; if (operator == InfixExpression.Operator.CONDITIONAL_AND || operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.AND || operator == InfixExpression.Operator.OR || operator == InfixExpression.Operator.XOR) return true; return false; }
/** * Returns the type of infix expression based on its operands and operator. * * @param operator the operator of infix expression * @param leftOperandType the type of left operand of infix expression * @param rightOperandType the type of right operand of infix expression * @return the type of infix expression if the type of both the operands is same or if the type of * either operand of a + operator is String, <code>null</code> otherwise. * @since 3.9 */ private static ITypeBinding getInfixExpressionType( InfixExpression.Operator operator, ITypeBinding leftOperandType, ITypeBinding rightOperandType) { if (leftOperandType == rightOperandType) { return leftOperandType; } if (operator == InfixExpression.Operator.PLUS) { if (isStringType(leftOperandType)) { return leftOperandType; } else if (isStringType(rightOperandType)) { return rightOperandType; } } // If the left and right operand types are different, we assume that parentheses are needed. // This is to avoid complications of numeric promotions and for readability of complicated code. return null; }
private static Expression getInversedAndOrExpression( ASTRewrite rewrite, InfixExpression infixExpression, Operator newOperator, SimpleNameRenameProvider provider) { InfixExpression newExpression = rewrite.getAST().newInfixExpression(); newExpression.setOperator(newOperator); int newOperatorPrecedence = OperatorPrecedence.getOperatorPrecedence(newOperator); // Expression leftOperand = getInversedExpression(rewrite, infixExpression.getLeftOperand(), provider); newExpression.setLeftOperand(parenthesizeIfRequired(leftOperand, newOperatorPrecedence)); Expression rightOperand = getInversedExpression(rewrite, infixExpression.getRightOperand(), provider); newExpression.setRightOperand(parenthesizeIfRequired(rightOperand, newOperatorPrecedence)); List<Expression> extraOperands = infixExpression.extendedOperands(); List<Expression> newExtraOperands = newExpression.extendedOperands(); for (int i = 0; i < extraOperands.size(); i++) { Expression extraOperand = getInversedExpression(rewrite, extraOperands.get(i), provider); newExtraOperands.add(parenthesizeIfRequired(extraOperand, newOperatorPrecedence)); } return newExpression; }
private static Expression combineOperands( ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) { if (existing == null && removeParentheses) { while (originalNode instanceof ParenthesizedExpression) { originalNode = ((ParenthesizedExpression) originalNode).getExpression(); } } Expression newRight = (Expression) rewrite.createMoveTarget(originalNode); if (originalNode instanceof InfixExpression) { ((InfixExpression) newRight).setOperator(((InfixExpression) originalNode).getOperator()); } if (existing == null) { return newRight; } AST ast = rewrite.getAST(); InfixExpression infix = ast.newInfixExpression(); infix.setOperator(operator); infix.setLeftOperand(existing); infix.setRightOperand(newRight); return infix; }
private static boolean isAssociative(InfixExpression.Operator operator, ITypeBinding infixExprType, boolean isAllOperandsHaveSameType) { if (operator == InfixExpression.Operator.PLUS) return isStringType(infixExprType) || isIntegerType(infixExprType) && isAllOperandsHaveSameType; if (operator == InfixExpression.Operator.TIMES) return isIntegerType(infixExprType) && isAllOperandsHaveSameType; if (operator == InfixExpression.Operator.CONDITIONAL_AND || operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.AND || operator == InfixExpression.Operator.OR || operator == InfixExpression.Operator.XOR) return true; return false; }
@Override protected void addMemberCheckNull(Object member, boolean addSeparator) { IfStatement ifStatement= fAst.newIfStatement(); ifStatement.setExpression(createInfixExpression(createMemberAccessExpression(member, true, true), Operator.NOT_EQUALS, fAst.newNullLiteral())); Block thenBlock= fAst.newBlock(); flushBuffer(null); String[] arrayString= getContext().getTemplateParser().getBody(); for (int i= 0; i < arrayString.length; i++) { addElement(processElement(arrayString[i], member), thenBlock); } if (addSeparator) addElement(getContext().getTemplateParser().getSeparator(), thenBlock); flushBuffer(thenBlock); if (thenBlock.statements().size() == 1 && !getContext().isForceBlocks()) { ifStatement.setThenStatement((Statement)ASTNode.copySubtree(fAst, (ASTNode)thenBlock.statements().get(0))); } else { ifStatement.setThenStatement(thenBlock); } toStringMethod.getBody().statements().add(ifStatement); }
@Override protected void addMemberCheckNull(Object member, boolean addSeparator) { IfStatement ifStatement= fAst.newIfStatement(); ifStatement.setExpression(createInfixExpression(createMemberAccessExpression(member, true, true), Operator.NOT_EQUALS, fAst.newNullLiteral())); Block thenBlock= fAst.newBlock(); flushTemporaryExpression(); String[] arrayString= getContext().getTemplateParser().getBody(); for (int i= 0; i < arrayString.length; i++) { addElement(processElement(arrayString[i], member), thenBlock); } if (addSeparator) addElement(getContext().getTemplateParser().getSeparator(), thenBlock); flushTemporaryExpression(); if (thenBlock.statements().size() == 1 && !getContext().isForceBlocks()) { ifStatement.setThenStatement((Statement)ASTNode.copySubtree(fAst, (ASTNode)thenBlock.statements().get(0))); } else { ifStatement.setThenStatement(thenBlock); } toStringMethod.getBody().statements().add(ifStatement); }
private Statement createAddQualifiedHashCode(IVariableBinding binding) { MethodInvocation invoc= fAst.newMethodInvocation(); invoc.setExpression(getThisAccessForHashCode(binding.getName())); invoc.setName(fAst.newSimpleName(METHODNAME_HASH_CODE)); InfixExpression expr= fAst.newInfixExpression(); expr.setOperator(Operator.EQUALS); expr.setLeftOperand(getThisAccessForHashCode(binding.getName())); expr.setRightOperand(fAst.newNullLiteral()); ConditionalExpression cexpr= fAst.newConditionalExpression(); cexpr.setThenExpression(fAst.newNumberLiteral("0")); //$NON-NLS-1$ cexpr.setElseExpression(invoc); cexpr.setExpression(parenthesize(expr)); return prepareAssignment(parenthesize(cexpr)); }
private Expression createShiftAssignment(Expression shift1, Expression shift2) { // (int)(element ^ (element >>> 32)); // see implementation in Arrays.hashCode(), Double.hashCode() and // Long.hashCode() CastExpression ce= fAst.newCastExpression(); ce.setType(fAst.newPrimitiveType(PrimitiveType.INT)); InfixExpression unsignedShiftRight= fAst.newInfixExpression(); unsignedShiftRight.setLeftOperand(shift1); unsignedShiftRight.setRightOperand(fAst.newNumberLiteral("32")); //$NON-NLS-1$ unsignedShiftRight.setOperator(Operator.RIGHT_SHIFT_UNSIGNED); InfixExpression xor= fAst.newInfixExpression(); xor.setLeftOperand(shift2); xor.setRightOperand(parenthesize(unsignedShiftRight)); xor.setOperator(InfixExpression.Operator.XOR); ce.setExpression(parenthesize(xor)); return ce; }
private Statement prepareAssignment(Expression rightHand) { // result = PRIME*result + (...) InfixExpression mul= fAst.newInfixExpression(); mul.setLeftOperand(fAst.newSimpleName(VARIABLE_NAME_PRIME)); mul.setRightOperand(fAst.newSimpleName(VARIABLE_NAME_RESULT)); mul.setOperator(Operator.TIMES); Assignment ass= fAst.newAssignment(); ass.setLeftHandSide(fAst.newSimpleName(VARIABLE_NAME_RESULT)); InfixExpression plus= fAst.newInfixExpression(); plus.setLeftOperand(mul); plus.setOperator(Operator.PLUS); plus.setRightOperand(rightHand); ass.setRightHandSide(plus); return fAst.newExpressionStatement(ass); }
private Statement createOuterComparison() { MethodInvocation outer1= fAst.newMethodInvocation(); outer1.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE)); MethodInvocation outer2= fAst.newMethodInvocation(); outer2.setName(fAst.newSimpleName(METHODNAME_OUTER_TYPE)); outer2.setExpression(fAst.newSimpleName(VARIABLE_NAME_EQUALS_CASTED)); MethodInvocation outerEql= fAst.newMethodInvocation(); outerEql.setName(fAst.newSimpleName(METHODNAME_EQUALS)); outerEql.setExpression(outer1); outerEql.arguments().add(outer2); PrefixExpression not= fAst.newPrefixExpression(); not.setOperand(outerEql); not.setOperator(PrefixExpression.Operator.NOT); IfStatement notEqNull= fAst.newIfStatement(); notEqNull.setExpression(not); notEqNull.setThenStatement(getThenStatement(getReturnFalse())); return notEqNull; }
private Statement createArrayComparison(String name) { MethodInvocation invoc= fAst.newMethodInvocation(); invoc.setName(fAst.newSimpleName(METHODNAME_EQUALS)); invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS)); invoc.arguments().add(getThisAccessForEquals(name)); invoc.arguments().add(getOtherAccess(name)); PrefixExpression pe= fAst.newPrefixExpression(); pe.setOperator(PrefixExpression.Operator.NOT); pe.setOperand(invoc); IfStatement ifSt= fAst.newIfStatement(); ifSt.setExpression(pe); ifSt.setThenStatement(getThenStatement(getReturnFalse())); return ifSt; }
private Statement createMultiArrayComparison(String name) { MethodInvocation invoc= fAst.newMethodInvocation(); invoc.setName(fAst.newSimpleName(METHODNAME_DEEP_EQUALS)); invoc.setExpression(getQualifiedName(JAVA_UTIL_ARRAYS)); invoc.arguments().add(getThisAccessForEquals(name)); invoc.arguments().add(getOtherAccess(name)); PrefixExpression pe= fAst.newPrefixExpression(); pe.setOperator(PrefixExpression.Operator.NOT); pe.setOperand(invoc); IfStatement ifSt= fAst.newIfStatement(); ifSt.setExpression(pe); ifSt.setThenStatement(getThenStatement(getReturnFalse())); return ifSt; }
private static Expression getInversedAndOrExpression(ASTRewrite rewrite, InfixExpression infixExpression, Operator newOperator, SimpleNameRenameProvider provider) { InfixExpression newExpression= rewrite.getAST().newInfixExpression(); newExpression.setOperator(newOperator); int newOperatorPrecedence= OperatorPrecedence.getOperatorPrecedence(newOperator); // Expression leftOperand= getInversedExpression(rewrite, infixExpression.getLeftOperand(), provider); newExpression.setLeftOperand(parenthesizeIfRequired(leftOperand, newOperatorPrecedence)); Expression rightOperand= getInversedExpression(rewrite, infixExpression.getRightOperand(), provider); newExpression.setRightOperand(parenthesizeIfRequired(rightOperand, newOperatorPrecedence)); List<Expression> extraOperands= infixExpression.extendedOperands(); List<Expression> newExtraOperands= newExpression.extendedOperands(); for (int i= 0; i < extraOperands.size(); i++) { Expression extraOperand= getInversedExpression(rewrite, extraOperands.get(i), provider); newExtraOperands.add(parenthesizeIfRequired(extraOperand, newOperatorPrecedence)); } return newExpression; }
private static void breakInfixOperationAtOperation(ASTRewrite rewrite, Expression expression, Operator operator, int operatorOffset, boolean removeParentheses, Expression[] res) { if (expression.getStartPosition() + expression.getLength() <= operatorOffset) { // add to the left res[0]= combineOperands(rewrite, res[0], expression, removeParentheses, operator); return; } if (operatorOffset <= expression.getStartPosition()) { // add to the right res[1]= combineOperands(rewrite, res[1], expression, removeParentheses, operator); return; } if (!(expression instanceof InfixExpression)) { throw new IllegalArgumentException("Cannot break up non-infix expression"); //$NON-NLS-1$ } InfixExpression infixExpression= (InfixExpression) expression; if (infixExpression.getOperator() != operator) { throw new IllegalArgumentException("Incompatible operator"); //$NON-NLS-1$ } breakInfixOperationAtOperation(rewrite, infixExpression.getLeftOperand(), operator, operatorOffset, removeParentheses, res); breakInfixOperationAtOperation(rewrite, infixExpression.getRightOperand(), operator, operatorOffset, removeParentheses, res); List<Expression> extended= infixExpression.extendedOperands(); for (int i= 0; i < extended.size(); i++) { breakInfixOperationAtOperation(rewrite, extended.get(i), operator, operatorOffset, removeParentheses, res); } }
private static Expression combineOperands(ASTRewrite rewrite, Expression existing, Expression originalNode, boolean removeParentheses, Operator operator) { if (existing == null && removeParentheses) { while (originalNode instanceof ParenthesizedExpression) { originalNode= ((ParenthesizedExpression)originalNode).getExpression(); } } Expression newRight= (Expression)rewrite.createMoveTarget(originalNode); if (originalNode instanceof InfixExpression) { ((InfixExpression)newRight).setOperator(((InfixExpression)originalNode).getOperator()); } if (existing == null) { return newRight; } AST ast= rewrite.getAST(); InfixExpression infix= ast.newInfixExpression(); infix.setOperator(operator); infix.setLeftOperand(existing); infix.setRightOperand(newRight); return infix; }
private static boolean isAssociative(InfixExpression expression) { Operator operator= expression.getOperator(); if (operator == InfixExpression.Operator.PLUS) return isExpressionStringType(expression) || isExpressionIntegerType(expression) && isAllOperandsHaveSameType(expression); if (operator == InfixExpression.Operator.TIMES) return isExpressionIntegerType(expression) && isAllOperandsHaveSameType(expression); if (operator == InfixExpression.Operator.CONDITIONAL_AND || operator == InfixExpression.Operator.CONDITIONAL_OR || operator == InfixExpression.Operator.AND || operator == InfixExpression.Operator.OR || operator == InfixExpression.Operator.XOR) return true; return false; }
@SuppressWarnings("unchecked") private Expression makeFixedExpression(ASTRewrite rewrite, IsNANVisitor visitor) { AST ast = rewrite.getAST(); MethodInvocation fixedMethod = ast.newMethodInvocation(); fixedMethod.setName(ast.newSimpleName("isNaN")); if (visitor.isPrimitive) { // make a reference to Double or Float SimpleName staticType = ast.newSimpleName(doubleOrFloat(visitor.isDouble)); fixedMethod.setExpression(staticType); fixedMethod.arguments().add(rewrite.createMoveTarget(visitor.testedVariable)); } else { // call isNaN directly on the boxed variable fixedMethod.setExpression((Expression) rewrite.createMoveTarget(visitor.testedVariable)); } if (!visitor.isEquals) { PrefixExpression not = ast.newPrefixExpression(); not.setOperator(PrefixExpression.Operator.NOT); not.setOperand(fixedMethod); return not; } return fixedMethod; }