use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method replaceDuplicateExpression.
private void replaceDuplicateExpression(final InfixExpression node, final Expression firstExpression, final Expression secondExpression, final AtomicBoolean isFirstExprPositive, final AtomicBoolean isSecondExprPositive) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteXORRatherThanDuplicateConditionsCleanUp_description);
InfixExpression newInfixExpression = ast.newInfixExpression();
newInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
newInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
if (isFirstExprPositive.get() == isSecondExprPositive.get()) {
newInfixExpression.setOperator(InfixExpression.Operator.EQUALS);
} else {
newInfixExpression.setOperator(InfixExpression.Operator.XOR);
}
ASTNodes.replaceButKeepComment(rewrite, node, newInfixExpression, group);
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isGreatNumberValid.
private boolean isGreatNumberValid(final CollectedData data, final CastExpression newHash) {
OrderedInfixExpression<Expression, InfixExpression> orderedBitwise = ASTNodes.orderedInfix(newHash.getExpression(), Expression.class, InfixExpression.class);
if (ASTNodes.hasType(newHash, int.class.getSimpleName()) && orderedBitwise != null && ASTNodes.hasType(newHash.getExpression(), long.class.getSimpleName(), double.class.getSimpleName()) && InfixExpression.Operator.XOR.equals(orderedBitwise.getOperator())) {
SimpleName field = getField(orderedBitwise.getFirstOperand());
InfixExpression moveExpression = orderedBitwise.getSecondOperand();
if (field != null && moveExpression != null && !ASTNodes.isSameVariable(field, data.getPrimeId()) && !ASTNodes.isSameVariable(field, data.getResultId()) && ASTNodes.hasOperator(moveExpression, InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED)) {
SimpleName againFieldName = getField(moveExpression.getLeftOperand());
Long hash = ASTNodes.getIntegerLiteral(moveExpression.getRightOperand());
if (againFieldName != null && ASTNodes.isSameVariable(againFieldName, field) && Long.valueOf(32L).equals(hash)) {
if (data.isTempValueUsed()) {
data.getFields().add(ASTNodes.getUnparenthesedExpression(againFieldName));
return true;
}
if (ASTNodes.isSameVariable(data.getTempVar(), field)) {
data.setTempValueUsed(true);
return true;
}
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteOperandFactorizationCleanUp method visit.
@Override
public boolean visit(final InfixExpression visited) {
if (!visited.hasExtendedOperands() && ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR)) {
InfixExpression firstCondition = ASTNodes.as(visited.getLeftOperand(), InfixExpression.class);
InfixExpression secondCondition = ASTNodes.as(visited.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.isPrimitive(firstCondition.getLeftOperand()) && ASTNodes.isPrimitive(firstCondition.getRightOperand()) && ASTNodes.isPrimitive(secondCondition.getLeftOperand()) && ASTNodes.isPrimitive(secondCondition.getRightOperand()) && ASTNodes.isPassiveWithoutFallingThrough(firstCondition.getLeftOperand()) && ASTNodes.isPassiveWithoutFallingThrough(firstCondition.getRightOperand()) && ASTNodes.isPassiveWithoutFallingThrough(secondCondition.getLeftOperand()) && ASTNodes.isPassiveWithoutFallingThrough(secondCondition.getRightOperand())) {
return maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getLeftOperand(), secondCondition.getLeftOperand(), firstCondition.getRightOperand(), secondCondition.getRightOperand()) && maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getLeftOperand(), secondCondition.getRightOperand(), firstCondition.getRightOperand(), secondCondition.getLeftOperand()) && maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getRightOperand(), secondCondition.getLeftOperand(), firstCondition.getLeftOperand(), secondCondition.getRightOperand()) && maybeReplaceDuplicateExpression(visited, firstCondition, firstCondition.getRightOperand(), secondCondition.getRightOperand(), firstCondition.getLeftOperand(), secondCondition.getLeftOperand());
}
}
return true;
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteOperandFactorizationCleanUp method replaceDuplicateExpression.
private void replaceDuplicateExpression(final InfixExpression visited, final InfixExpression firstCondition, final Expression firstExpression, final Expression secondExpression, final Expression secondOppositeExpression) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOperandFactorizationCleanUp_description);
InfixExpression newInnerInfixExpression = ast.newInfixExpression();
newInnerInfixExpression.setOperator(visited.getOperator());
newInnerInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, secondExpression));
newInnerInfixExpression.setRightOperand(ASTNodes.createMoveTarget(rewrite, secondOppositeExpression));
InfixExpression newMainInfixExpression = ast.newInfixExpression();
newMainInfixExpression.setOperator(firstCondition.getOperator());
newMainInfixExpression.setLeftOperand(ASTNodes.createMoveTarget(rewrite, firstExpression));
newMainInfixExpression.setRightOperand(ASTNodeFactory.parenthesizeIfNeeded(ast, newInnerInfixExpression));
ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodeFactory.parenthesizeIfNeeded(ast, newMainInfixExpression), group);
}
use of org.eclipse.jdt.core.dom.InfixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantTruthCleanUp method maybeRefactorInfixExpression.
private boolean maybeRefactorInfixExpression(final ASTNode visited, final InfixExpression originalCondition) {
if (!originalCondition.hasExtendedOperands() && ASTNodes.hasOperator(originalCondition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.XOR) && // fearing we would introduce a previously non existing NPE.
(ASTNodes.isPrimitive(originalCondition.getLeftOperand(), boolean.class.getSimpleName()) || ASTNodes.isPrimitive(originalCondition.getRightOperand(), boolean.class.getSimpleName()))) {
Expression leftExpression = originalCondition.getLeftOperand();
Expression rightExpression = originalCondition.getRightOperand();
boolean isEquals = ASTNodes.hasOperator(originalCondition, InfixExpression.Operator.EQUALS);
return maybeRemoveConstantOperand(visited, leftExpression, rightExpression, isEquals) && maybeRemoveConstantOperand(visited, rightExpression, leftExpression, isEquals);
}
return true;
}
Aggregations