use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoletePushNegationDownCleanUp method visit.
@Override
public boolean visit(final PrefixExpression node) {
if (!ASTNodes.hasOperator(node, PrefixExpression.Operator.NOT)) {
return true;
}
Expression replacement = getOppositeExpression(node.getOperand());
if (replacement != null) {
TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoletePushNegationDownCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodes.replaceButKeepComment(rewrite, node, replacement, group);
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoletePushNegationDownCleanUp method getOppositeExpression.
private Expression getOppositeExpression(final Expression negativeExpression) {
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ASTNodeFactory ast = cuRewrite.getASTBuilder();
Expression operand = ASTNodes.getUnparenthesedExpression(negativeExpression);
if (operand instanceof PrefixExpression) {
PrefixExpression pe = (PrefixExpression) operand;
if (ASTNodes.hasOperator(pe, PrefixExpression.Operator.NOT)) {
return ASTNodes.createMoveTarget(rewrite, pe.getOperand());
}
} else if (operand instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) operand;
InfixExpression.Operator reverseOp = (InfixExpression.Operator) OperatorEnum.getOperator(infixExpression).getReverseBooleanOperator();
if (reverseOp != null) {
List<Expression> allOperands = new ArrayList<>(ASTNodes.allOperands(infixExpression));
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
for (ListIterator<Expression> it = allOperands.listIterator(); it.hasNext(); ) {
Expression anOperand = it.next();
Expression oppositeOperand = getOppositeExpression(anOperand);
it.set(oppositeOperand != null ? oppositeOperand : ast.negate(anOperand, true));
}
} else {
allOperands = ASTNodes.createMoveTarget(rewrite, allOperands);
}
return ast.newParenthesizedExpression(ast.newInfixExpression(reverseOp, allOperands));
}
} else {
Boolean constant = ASTNodes.getBooleanLiteral(operand);
if (constant != null) {
return ast.newBooleanLiteral(!constant);
}
}
return null;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteRedundantComparatorCleanUp method isReturnedExpressionToRemove.
private boolean isReturnedExpressionToRemove(final SimpleName name1, final SimpleName name2, final Expression returnExpression, final boolean isForward) {
PrefixExpression negativeExpression = ASTNodes.as(returnExpression, PrefixExpression.class);
if (negativeExpression != null && ASTNodes.hasOperator(negativeExpression, PrefixExpression.Operator.MINUS)) {
return isReturnedExpressionToRemove(name1, name2, negativeExpression.getOperand(), !isForward);
}
MethodInvocation compareToMethod = ASTNodes.as(returnExpression, MethodInvocation.class);
if (compareToMethod != null && compareToMethod.getExpression() != null) {
ITypeBinding comparisonType = compareToMethod.getExpression().resolveTypeBinding();
if (comparisonType != null) {
List<Expression> arguments = compareToMethod.arguments();
if (compareToMethod.getExpression() != null && // $NON-NLS-1$
ASTNodes.usesGivenSignature(// $NON-NLS-1$
compareToMethod, // $NON-NLS-1$
comparisonType.getQualifiedName(), // $NON-NLS-1$
"compareTo", comparisonType.getQualifiedName())) {
return isRefactorComparisonToRefactor(name1, name2, compareToMethod.getExpression(), arguments.get(0), isForward);
}
String primitiveType = Bindings.getUnboxedTypeName(comparisonType.getQualifiedName());
if (primitiveType != null && // $NON-NLS-1$
ASTNodes.usesGivenSignature(// $NON-NLS-1$
compareToMethod, // $NON-NLS-1$
comparisonType.getQualifiedName(), // $NON-NLS-1$
"compare", primitiveType, primitiveType)) {
return isRefactorComparisonToRefactor(name1, name2, arguments.get(0), arguments.get(1), isForward);
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class BooleanEqualsRatherThanNullCheckCleanUp method visit.
@Override
public boolean visit(final InfixExpression visited) {
if (ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
Expression leftOperand = visited.getLeftOperand();
Expression rightOperand = visited.getRightOperand();
InfixExpression condition = ASTNodes.as(leftOperand, InfixExpression.class);
boolean isNullCheck = ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS);
boolean isAndExpression = ASTNodes.hasOperator(visited, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND);
if (!visited.hasExtendedOperands() && isNullCheck ^ isAndExpression && condition != null && ASTNodes.hasOperator(condition, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS)) {
Expression firstExpression = null;
if (ASTNodes.is(condition.getLeftOperand(), NullLiteral.class)) {
firstExpression = condition.getRightOperand();
} else if (ASTNodes.is(condition.getRightOperand(), NullLiteral.class)) {
firstExpression = condition.getLeftOperand();
}
Expression secondExpression = null;
PrefixExpression negateSecondExpression = ASTNodes.as(rightOperand, PrefixExpression.class);
boolean isPositiveExpression;
if (negateSecondExpression != null && ASTNodes.hasOperator(negateSecondExpression, PrefixExpression.Operator.NOT)) {
secondExpression = negateSecondExpression.getOperand();
isPositiveExpression = false;
} else {
secondExpression = rightOperand;
isPositiveExpression = true;
}
if (firstExpression != null && ASTNodes.hasType(firstExpression, Boolean.class.getCanonicalName()) && ASTNodes.isPassive(firstExpression) && ASTNodes.match(firstExpression, secondExpression)) {
replaceNullCheck(visited, firstExpression, isNullCheck, isAndExpression, isPositiveExpression);
return false;
}
}
}
return true;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class DoWhileRatherThanWhileCleanUp method peremptoryValue.
private Object peremptoryValue(final ASTNode visited, final Expression condition) {
Object constantCondition = condition.resolveConstantExpressionValue();
if (constantCondition != null) {
return constantCondition;
}
Long integerLiteral = ASTNodes.getIntegerLiteral(condition);
if (integerLiteral != null) {
return integerLiteral;
}
SimpleName variable = ASTNodes.as(condition, SimpleName.class);
if (variable != null && variable.resolveBinding() != null && variable.resolveBinding().getKind() == IBinding.VARIABLE) {
List<ASTNode> precedingStatements = getPrecedingCode(visited);
Collections.reverse(precedingStatements);
for (ASTNode precedingStatement : precedingStatements) {
if (isConditionalCode(precedingStatement)) {
return null;
}
VarDefinitionsUsesVisitor visitor = new VarDefinitionsUsesVisitor((IVariableBinding) variable.resolveBinding(), precedingStatement, true);
if (visitor.getWrites().size() > 1) {
return null;
}
for (SimpleName astNode : visitor.getReads()) {
ASTNode parent = astNode.getParent();
while (parent instanceof ParenthesizedExpression) {
parent = astNode.getParent();
}
if (parent instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) parent, PrefixExpression.Operator.INCREMENT, PrefixExpression.Operator.DECREMENT) || parent instanceof PostfixExpression && ASTNodes.hasOperator((PostfixExpression) parent, PostfixExpression.Operator.INCREMENT, PostfixExpression.Operator.DECREMENT)) {
return null;
}
}
if (!visitor.getWrites().isEmpty()) {
SimpleName write = visitor.getWrites().get(0);
ASTNode parent = write;
while (parent != precedingStatement) {
if (isConditionalCode(parent)) {
return null;
}
parent = parent.getParent();
}
switch(write.getParent().getNodeType()) {
case ASTNode.ASSIGNMENT:
Assignment assignment = (Assignment) write.getParent();
if (ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN)) {
return peremptoryValue(precedingStatement, assignment.getRightHandSide());
}
break;
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
VariableDeclarationFragment fragment = (VariableDeclarationFragment) write.getParent();
if (fragment.getInitializer() != null) {
return peremptoryValue(precedingStatement, fragment.getInitializer());
}
break;
case ASTNode.SINGLE_VARIABLE_DECLARATION:
SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration) write.getParent();
if (singleVariableDeclaration.getInitializer() != null) {
return peremptoryValue(precedingStatement, singleVariableDeclaration.getInitializer());
}
break;
default:
break;
}
return null;
}
}
return null;
}
InfixExpression infixExpression = ASTNodes.as(condition, InfixExpression.class);
if (infixExpression != null) {
if (!infixExpression.hasExtendedOperands() && ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.GREATER, InfixExpression.Operator.GREATER_EQUALS, InfixExpression.Operator.LESS, InfixExpression.Operator.LESS_EQUALS)) {
Object leftOperand = peremptoryValue(visited, infixExpression.getLeftOperand());
Object rightOperand = peremptoryValue(visited, infixExpression.getRightOperand());
if (leftOperand instanceof Number && rightOperand instanceof Number) {
Number leftNumber = (Number) leftOperand;
Number rightNumber = (Number) rightOperand;
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.EQUALS)) {
return leftNumber.longValue() == rightNumber.longValue();
}
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.NOT_EQUALS)) {
return leftNumber.longValue() != rightNumber.longValue();
}
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.GREATER)) {
return leftNumber.longValue() > rightNumber.longValue();
}
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.GREATER_EQUALS)) {
return leftNumber.longValue() >= rightNumber.longValue();
}
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.LESS)) {
return leftNumber.longValue() < rightNumber.longValue();
}
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.LESS_EQUALS)) {
return leftNumber.longValue() <= rightNumber.longValue();
}
}
return null;
}
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.AND)) {
for (Expression operand : ASTNodes.allOperands(infixExpression)) {
final Object hasAlwaysValue = peremptoryValue(visited, operand);
if (!Boolean.TRUE.equals(hasAlwaysValue)) {
return hasAlwaysValue;
}
}
return Boolean.TRUE;
}
if (ASTNodes.hasOperator(infixExpression, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.OR)) {
for (Expression operand : ASTNodes.allOperands(infixExpression)) {
final Object hasAlwaysValue = peremptoryValue(visited, operand);
if (!Boolean.FALSE.equals(hasAlwaysValue)) {
return hasAlwaysValue;
}
}
return Boolean.FALSE;
}
}
return false;
}
Aggregations