use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class XORRatherThanDuplicateConditionsRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if ((hasOperator(node, CONDITIONAL_OR) || hasOperator(node, OR)) && !node.hasExtendedOperands()) {
final InfixExpression firstCondition = as(node.getLeftOperand(), InfixExpression.class);
final InfixExpression secondCondition = as(node.getRightOperand(), InfixExpression.class);
if (firstCondition != null && !firstCondition.hasExtendedOperands() && (hasOperator(firstCondition, CONDITIONAL_AND) || hasOperator(firstCondition, AND)) && secondCondition != null && !secondCondition.hasExtendedOperands() && (hasOperator(secondCondition, CONDITIONAL_AND) || hasOperator(secondCondition, AND))) {
final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
final AtomicBoolean isFourthExprPositive = new AtomicBoolean();
final Expression firstExpr = getBasisExpression(firstCondition.getLeftOperand(), isFirstExprPositive);
final Expression secondExpr = getBasisExpression(firstCondition.getRightOperand(), isSecondExprPositive);
final Expression thirdExpr = getBasisExpression(secondCondition.getLeftOperand(), isThirdExprPositive);
final Expression fourthExpr = getBasisExpression(secondCondition.getRightOperand(), isFourthExprPositive);
if (isPassive(firstExpr) && isPassive(secondExpr) && ((match(new ASTMatcher(), firstExpr, thirdExpr) && match(new ASTMatcher(), secondExpr, fourthExpr) && isFirstExprPositive.get() ^ isThirdExprPositive.get() && isSecondExprPositive.get() ^ isFourthExprPositive.get()) || (match(new ASTMatcher(), firstExpr, fourthExpr) && match(new ASTMatcher(), secondExpr, thirdExpr) && isFirstExprPositive.get() ^ isFourthExprPositive.get() && isSecondExprPositive.get() ^ isThirdExprPositive.get()))) {
replaceDuplicateExpr(node, firstExpr, secondExpr, isFirstExprPositive, isSecondExprPositive);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class RemoveUnnecessaryCastRefactoring method canRemoveCast.
private boolean canRemoveCast(CastExpression node) {
final ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case RETURN_STATEMENT:
final MethodDeclaration md = getAncestor(parent, MethodDeclaration.class);
return isAssignmentCompatible(node.getExpression(), md.getReturnType2());
case ASSIGNMENT:
final Assignment as = (Assignment) parent;
return isAssignmentCompatible(node.getExpression(), as) || isConstantExpressionAssignmentConversion(node);
case VARIABLE_DECLARATION_FRAGMENT:
final VariableDeclarationFragment vdf = (VariableDeclarationFragment) parent;
return isAssignmentCompatible(node.getExpression(), resolveTypeBinding(vdf)) || isConstantExpressionAssignmentConversion(node);
case INFIX_EXPRESSION:
final InfixExpression ie = (InfixExpression) parent;
final Expression lo = ie.getLeftOperand();
final Expression ro = ie.getRightOperand();
if (node.equals(lo)) {
return (isStringConcat(ie) || isAssignmentCompatible(node.getExpression(), ro)) && !isPrimitiveTypeNarrowing(node) && !hasOperator(ie, DIVIDE) && !hasOperator(ie, PLUS) && !hasOperator(ie, MINUS);
} else {
final boolean integralDivision = isIntegralDivision(ie);
return ((isNotRefactored(lo) && isStringConcat(ie)) || (!integralDivision && isAssignmentCompatibleInInfixExpression(node, ie)) || (integralDivision && canRemoveCastInIntegralDivision(node, ie))) && !isPrimitiveTypeNarrowing(node) && !isIntegralDividedByFloatingPoint(node, ie);
}
}
return false;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class CollapseIfStatementRefactoring method replaceIfNoElseStatement.
private boolean replaceIfNoElseStatement(IfStatement outerIf, IfStatement innerIf) {
if (innerIf.getElseStatement() != null) {
return VISIT_SUBTREE;
}
final ASTBuilder b = this.ctx.getASTBuilder();
final InfixExpression ie = b.infixExpr(parenthesizeOrExpr(b, outerIf.getExpression()), CONDITIONAL_AND, parenthesizeOrExpr(b, innerIf.getExpression()));
this.ctx.getRefactorings().replace(outerIf.getExpression(), ie);
this.ctx.getRefactorings().replace(outerIf.getThenStatement(), b.copy(innerIf.getThenStatement()));
return DO_NOT_VISIT_SUBTREE;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class OperatorEnumTest method simpleTestCompareExpressions.
@Test
public void simpleTestCompareExpressions() {
final AST ast = AST.newAST(AST.JLS4);
final Assignment op1 = ast.newAssignment();
op1.setOperator(Assignment.Operator.ASSIGN);
final InfixExpression op2 = ast.newInfixExpression();
op2.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
assertTrue(OperatorEnum.compareTo(op1, op2) < 0);
assertEquals("Comparing unknown objects result in no decision", 0, OperatorEnum.compareTo(op1, null));
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class BooleanEqualsRatherThanNullCheckRefactoring method visit.
@Override
public boolean visit(InfixExpression node) {
if (hasOperator(node, CONDITIONAL_AND) || hasOperator(node, CONDITIONAL_OR)) {
final Expression leftOperand = node.getLeftOperand();
final Expression rightOperand = node.getRightOperand();
final InfixExpression condition = as(leftOperand, InfixExpression.class);
final boolean isNullCheck = hasOperator(condition, EQUALS);
final boolean isAndExpr = hasOperator(node, CONDITIONAL_AND);
if (!node.hasExtendedOperands() && (isNullCheck ^ isAndExpr) && condition != null && (hasOperator(condition, EQUALS) || hasOperator(condition, NOT_EQUALS))) {
Expression firstExpr = null;
if (isNullLiteral(condition.getLeftOperand())) {
firstExpr = condition.getRightOperand();
} else if (isNullLiteral(condition.getRightOperand())) {
firstExpr = condition.getLeftOperand();
}
Expression secondExpr = null;
final PrefixExpression negateSecondExpr = as(rightOperand, PrefixExpression.class);
final boolean isPositiveExpr;
if (negateSecondExpr != null && hasOperator(negateSecondExpr, NOT)) {
secondExpr = negateSecondExpr.getOperand();
isPositiveExpr = false;
} else {
secondExpr = rightOperand;
isPositiveExpr = true;
}
if (firstExpr != null && hasType(firstExpr, "java.lang.Boolean") && isPassive(firstExpr) && match(new ASTMatcher(), firstExpr, secondExpr)) {
replaceNullCheck(node, firstExpr, isNullCheck, isAndExpr, isPositiveExpr);
return DO_NOT_VISIT_SUBTREE;
}
}
}
return VISIT_SUBTREE;
}
Aggregations