use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteComparisonCleanUp method refactorComparingToZero.
private void refactorComparingToZero(final InfixExpression visited, final MethodInvocation comparisonMethod, final InfixExpression.Operator operator) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteComparisonCleanUp_description);
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, comparisonMethod));
newInfixExpression.setOperator(operator);
// $NON-NLS-1$
newInfixExpression.setRightOperand(ast.newNumberLiteral("0"));
ASTNodes.replaceButKeepComment(rewrite, visited, newInfixExpression, group);
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeRefactorIfStatement.
private boolean maybeRefactorIfStatement(final IfStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
if (node.getElseStatement() != null) {
InfixExpression condition = ASTNodes.as(node.getExpression(), InfixExpression.class);
List<Statement> thenStatements = ASTNodes.asList(node.getThenStatement());
List<Statement> elseStatements = ASTNodes.asList(node.getElseStatement());
if (condition != null && !condition.hasExtendedOperands() && ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS) && thenStatements != null && thenStatements.size() == 1 && elseStatements != null && elseStatements.size() == 1) {
OrderedInfixExpression<Expression, NullLiteral> nullityOrderedCondition = ASTNodes.orderedInfix(condition, Expression.class, NullLiteral.class);
if (nullityOrderedCondition != null && ASTNodes.isPassive(nullityOrderedCondition.getFirstOperand())) {
return maybeReplaceCode(node, condition, thenStatements, elseStatements, nullityOrderedCondition.getFirstOperand(), classesToUseWithImport, importsToAdd);
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeReplaceCode.
private boolean maybeReplaceCode(final IfStatement node, final InfixExpression condition, final List<Statement> thenStatements, final List<Statement> elseStatements, final Expression firstField, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
IfStatement checkNullityStatement;
IfStatement checkEqualsStatement;
if (ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS)) {
checkNullityStatement = ASTNodes.as(thenStatements.get(0), IfStatement.class);
checkEqualsStatement = ASTNodes.as(elseStatements.get(0), IfStatement.class);
} else {
checkEqualsStatement = ASTNodes.as(thenStatements.get(0), IfStatement.class);
checkNullityStatement = ASTNodes.as(elseStatements.get(0), IfStatement.class);
}
if (checkNullityStatement != null && checkNullityStatement.getElseStatement() == null && checkEqualsStatement != null && checkEqualsStatement.getElseStatement() == null) {
InfixExpression nullityCondition = ASTNodes.as(checkNullityStatement.getExpression(), InfixExpression.class);
ReturnStatement nullityStatement = ASTNodes.as(checkNullityStatement.getThenStatement(), ReturnStatement.class);
PrefixExpression equalsCondition = ASTNodes.as(checkEqualsStatement.getExpression(), PrefixExpression.class);
ReturnStatement equalsStatement = ASTNodes.as(checkEqualsStatement.getThenStatement(), ReturnStatement.class);
if (nullityCondition != null && !nullityCondition.hasExtendedOperands() && ASTNodes.hasOperator(nullityCondition, InfixExpression.Operator.NOT_EQUALS) && nullityStatement != null && equalsCondition != null && ASTNodes.hasOperator(equalsCondition, PrefixExpression.Operator.NOT) && equalsStatement != null) {
return maybeReplaceEquals(node, firstField, nullityCondition, nullityStatement, equalsCondition, equalsStatement, classesToUseWithImport, importsToAdd);
}
}
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteObjectsEqualsRatherThanEqualsAndNullCheckCleanUp method maybeReplaceEquals.
private boolean maybeReplaceEquals(final IfStatement node, final Expression firstField, final InfixExpression nullityCondition, final ReturnStatement returnStatement1, final PrefixExpression equalsCondition, final ReturnStatement returnStatement2, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
OrderedInfixExpression<Expression, NullLiteral> nullityOrderedCondition = ASTNodes.orderedInfix(nullityCondition, Expression.class, NullLiteral.class);
MethodInvocation equalsMethod = ASTNodes.as(equalsCondition.getOperand(), MethodInvocation.class);
if (nullityOrderedCondition != null && returnStatement1 != null && returnStatement2 != null && equalsMethod != null && equalsMethod.getExpression() != null && EQUALS_METHOD.equals(equalsMethod.getName().getIdentifier()) && equalsMethod.arguments() != null && equalsMethod.arguments().size() == 1) {
Expression secondField = nullityOrderedCondition.getFirstOperand();
if (secondField != null && (match(firstField, secondField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)) || match(secondField, firstField, equalsMethod.getExpression(), (ASTNode) equalsMethod.arguments().get(0)))) {
BooleanLiteral returnFalse1 = ASTNodes.as(returnStatement1.getExpression(), BooleanLiteral.class);
BooleanLiteral returnFalse2 = ASTNodes.as(returnStatement2.getExpression(), BooleanLiteral.class);
if (returnFalse1 != null && !returnFalse1.booleanValue() && returnFalse2 != null && !returnFalse2.booleanValue()) {
replaceEquals(node, firstField, classesToUseWithImport, importsToAdd, secondField, returnStatement1);
return false;
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method visit.
@Override
public boolean visit(final InfixExpression node) {
if (ASTNodes.hasOperator(node, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR) && !node.hasExtendedOperands()) {
InfixExpression firstCondition = ASTNodes.as(node.getLeftOperand(), InfixExpression.class);
InfixExpression secondCondition = ASTNodes.as(node.getRightOperand(), InfixExpression.class);
if (firstCondition != null && secondCondition != null && !firstCondition.hasExtendedOperands() && !secondCondition.hasExtendedOperands() && ASTNodes.hasOperator(firstCondition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND) && ASTNodes.hasOperator(secondCondition, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND) && ASTNodes.isPassive(firstCondition.getLeftOperand()) && ASTNodes.isPassive(firstCondition.getRightOperand()) && ASTNodes.isPassive(secondCondition.getLeftOperand()) && ASTNodes.isPassive(secondCondition.getRightOperand())) {
return maybeReplaceDuplicateExpression(node, firstCondition.getLeftOperand(), secondCondition.getLeftOperand(), firstCondition.getRightOperand(), secondCondition.getRightOperand()) && maybeReplaceDuplicateExpression(node, firstCondition.getLeftOperand(), secondCondition.getRightOperand(), firstCondition.getRightOperand(), secondCondition.getLeftOperand());
}
}
return true;
}
Aggregations