use of org.eclipse.jdt.core.dom.PrefixExpression 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.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteXORRatherThanDuplicateConditionsCleanUp method getBasisExpression.
private Expression getBasisExpression(final Expression originalExpression, final AtomicBoolean isExprPositive) {
Expression basisExpression = null;
PrefixExpression negateExpression = ASTNodes.as(originalExpression, PrefixExpression.class);
if (ASTNodes.hasOperator(negateExpression, PrefixExpression.Operator.NOT)) {
basisExpression = negateExpression.getOperand();
isExprPositive.lazySet(false);
} else {
basisExpression = originalExpression;
isExprPositive.lazySet(true);
}
return basisExpression;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp method refactorCondition.
private void refactorCondition(final IfStatement node, final Expression duplicateExpression, final Expression notDuplicateExpression, final Statement positiveStatement, final Statement negativeStatement) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteOppositeConditionRatherThanDuplicateConditionCleanUp_description);
Statement negativeStmtCopy;
if (negativeStatement instanceof IfStatement) {
Block newBlock = ast.newBlock();
newBlock.statements().add(ASTNodes.createMoveTarget(rewrite, negativeStatement));
negativeStmtCopy = newBlock;
} else {
negativeStmtCopy = ASTNodes.createMoveTarget(rewrite, negativeStatement);
}
Expression secondCond;
Statement secondStmtCopy;
Statement thirdStmtCopy;
PrefixExpression negativeCond = ASTNodes.as(notDuplicateExpression, PrefixExpression.class);
if (negativeCond != null && ASTNodes.hasOperator(negativeCond, PrefixExpression.Operator.NOT)) {
secondCond = negativeCond.getOperand();
secondStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
} else {
secondCond = notDuplicateExpression;
secondStmtCopy = ASTNodes.createMoveTarget(rewrite, node.getThenStatement());
thirdStmtCopy = ASTNodes.createMoveTarget(rewrite, positiveStatement);
}
ASTNodes.replaceButKeepComment(rewrite, node.getExpression(), ast.negate(duplicateExpression, true), group);
ASTNodes.replaceButKeepComment(rewrite, node.getThenStatement(), negativeStmtCopy, group);
IfStatement newIfStatement = ast.newIfStatement();
newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(secondCond)));
newIfStatement.setThenStatement(secondStmtCopy);
newIfStatement.setElseStatement(thirdStmtCopy);
ASTNodes.replaceButKeepComment(rewrite, node.getElseStatement(), newIfStatement, group);
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class OppositeComparisonRatherThanNegativeExpressionCleanUp method visit.
@Override
public boolean visit(final PrefixExpression visited) {
if (ASTNodes.hasOperator(visited, PrefixExpression.Operator.MINUS)) {
MethodInvocation methodInvocation = ASTNodes.as(visited.getOperand(), MethodInvocation.class);
if (methodInvocation != null && methodInvocation.getExpression() != null && methodInvocation.arguments().size() == 1) {
Expression argument = (Expression) methodInvocation.arguments().get(0);
String[] classes = { Double.class.getCanonicalName(), Float.class.getCanonicalName(), Short.class.getCanonicalName(), Integer.class.getCanonicalName(), Long.class.getCanonicalName(), Character.class.getCanonicalName(), Byte.class.getCanonicalName(), Boolean.class.getCanonicalName() };
for (String klass : classes) {
if (ASTNodes.usesGivenSignature(methodInvocation, klass, "compareTo", klass)) {
// $NON-NLS-1$
if (ASTNodes.hasType(argument, klass)) {
reverseObjects(visited, methodInvocation);
return false;
}
return true;
}
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse-pmd by acanda.
the class UseCollectionIsEmptyQuickFix method apply.
/**
* Replaces {@code x.size() == 0} or {@code 0 == x.size()} with {@code x.isEmpty()}. Replaces {@code x.size() != 0}
* or {@code 0 != x.size()} with {@code !x.isEmpty()}.
*/
@Override
protected boolean apply(final InfixExpression node) {
final MethodInvocation size;
if (node.getLeftOperand() instanceof MethodInvocation) {
size = (MethodInvocation) node.getLeftOperand();
} else if (node.getRightOperand() instanceof MethodInvocation) {
size = (MethodInvocation) node.getRightOperand();
} else {
return false;
}
final AST ast = node.getAST();
final MethodInvocation invocation = (MethodInvocation) ast.createInstance(MethodInvocation.class);
invocation.setExpression(ASTUtil.copy(size.getExpression()));
final SimpleName isEmpty = (SimpleName) ast.createInstance(SimpleName.class);
isEmpty.setIdentifier("isEmpty");
invocation.setName(isEmpty);
final Expression replacement;
if (isNotEmpty(node)) {
final PrefixExpression not = (PrefixExpression) ast.createInstance(PrefixExpression.class);
not.setOperator(org.eclipse.jdt.core.dom.PrefixExpression.Operator.NOT);
not.setOperand(invocation);
replacement = not;
} else {
replacement = invocation;
}
ASTUtil.replace(node, replacement);
return true;
}
Aggregations