use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteTernaryOperatorRatherThanDuplicateConditionsCleanUp method getBasisExpression.
private Expression getBasisExpression(final Expression originalExpression, final AtomicBoolean isExprPositive) {
Expression basisExpression;
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 UpdateSetRatherThanTestingFirstCleanUp method visit.
@Override
public boolean visit(final IfStatement visited) {
Statement elseStatement = visited.getElseStatement();
Statement thenStatement = visited.getThenStatement();
PrefixExpression prefixExpression = ASTNodes.as(visited.getExpression(), PrefixExpression.class);
if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.NOT)) {
return maybeReplaceSetContains(visited, prefixExpression.getOperand(), thenStatement, elseStatement, false);
}
return maybeReplaceSetContains(visited, visited.getExpression(), elseStatement, thenStatement, true);
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ASTNodes method getIntegerLiteral.
/**
* Integer literal.
*
* @param input The input
* @return Integer literal.
*/
public static Long getIntegerLiteral(final Expression input) {
if (input == null) {
return null;
}
Object number = input.resolveConstantExpressionValue();
if (number instanceof Short) {
return (long) ((Short) number).intValue();
}
if (number instanceof Integer) {
return (long) ((Integer) number).intValue();
}
if (number instanceof Long) {
return (Long) number;
}
InfixExpression operation = as(input, InfixExpression.class);
if (operation != null && hasOperator(operation, // All numerical operators
InfixExpression.Operator.AND, InfixExpression.Operator.DIVIDE, InfixExpression.Operator.LEFT_SHIFT, InfixExpression.Operator.MINUS, InfixExpression.Operator.OR, InfixExpression.Operator.PLUS, InfixExpression.Operator.REMAINDER, InfixExpression.Operator.RIGHT_SHIFT_SIGNED, InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED, InfixExpression.Operator.TIMES, InfixExpression.Operator.XOR)) {
List<Expression> operands = allOperands(operation);
Long leftValue = getIntegerLiteral(operands.remove(0));
if (leftValue == null) {
return null;
}
long result = leftValue;
for (Expression operand : operands) {
Long newValue = getIntegerLiteral(operand);
if (newValue == null) {
return null;
}
if (hasOperator(operation, InfixExpression.Operator.PLUS)) {
result = result + newValue;
} else if (hasOperator(operation, InfixExpression.Operator.MINUS)) {
result = result - newValue;
} else if (hasOperator(operation, InfixExpression.Operator.TIMES)) {
result = result * newValue;
} else if (hasOperator(operation, InfixExpression.Operator.AND)) {
result = result & newValue;
} else if (hasOperator(operation, InfixExpression.Operator.OR)) {
result = result | newValue;
} else if (hasOperator(operation, InfixExpression.Operator.XOR)) {
result = result ^ newValue;
} else if (hasOperator(operation, InfixExpression.Operator.LEFT_SHIFT)) {
result = result << newValue;
} else if (hasOperator(operation, InfixExpression.Operator.REMAINDER)) {
result = result % newValue;
} else if (hasOperator(operation, InfixExpression.Operator.RIGHT_SHIFT_SIGNED)) {
result = result >> newValue;
} else if (hasOperator(operation, InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED)) {
result = result >>> newValue;
} else if (hasOperator(operation, InfixExpression.Operator.DIVIDE) && result % newValue == 0) {
result = result / newValue;
} else {
return null;
}
}
return result;
}
PrefixExpression negativeContant = as(input, PrefixExpression.class);
if (negativeContant != null && hasOperator(negativeContant, PrefixExpression.Operator.MINUS)) {
Long value = getIntegerLiteral(negativeContant.getOperand());
if (value != null) {
return -value;
}
}
return null;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ASTNodeFactory method getNegatedOperation.
private Expression getNegatedOperation(final InfixExpression booleanOperation, final InfixExpression.Operator negatedOperator, final boolean isMove) {
List<Expression> allOperands = ASTNodes.allOperands(booleanOperation);
List<Expression> allTargetOperands;
if (ASTNodes.hasOperator(booleanOperation, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
allTargetOperands = new ArrayList<>(allOperands.size());
for (Expression booleanOperand : allOperands) {
Expression negatedOperand = negate(booleanOperand, isMove);
if (negatedOperand != null) {
allTargetOperands.add(negatedOperand);
} else {
PrefixExpression prefixExpression = newPrefixExpression(PrefixExpression.Operator.NOT, isMove ? createMoveTarget(booleanOperand) : createCopyTarget(booleanOperand));
allTargetOperands.add(prefixExpression);
}
}
} else {
allTargetOperands = new ArrayList<>(allOperands.size());
if (isMove) {
for (Expression anOperand : allOperands) {
allTargetOperands.add(createMoveTarget(anOperand));
}
} else {
for (Expression anOperand : allOperands) {
allTargetOperands.add(createCopyTarget(anOperand));
}
}
}
return newInfixExpression(negatedOperator, allTargetOperands);
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class AbstractPrimitiveRatherThanWrapperCleanUp method isNotNull.
private boolean isNotNull(final Expression expression) {
if (expression instanceof ParenthesizedExpression) {
ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
return isNotNull(parenthesizedExpression.getExpression());
}
if (expression instanceof ConditionalExpression) {
ConditionalExpression prefixExpression = (ConditionalExpression) expression;
return isNotNull(prefixExpression.getThenExpression()) && isNotNull(prefixExpression.getElseExpression());
}
if (getLiteralClass().equals(expression.getClass())) {
return true;
}
if (expression instanceof QualifiedName) {
QualifiedName qualifiedName = (QualifiedName) expression;
return ASTNodes.hasType(qualifiedName.getQualifier(), getWrapperFullyQualifiedName()) && (ASTNodes.isField(qualifiedName, getWrapperFullyQualifiedName(), getSafeInConstants()) || ASTNodes.isField(qualifiedName, getPrimitiveTypeName(), getSafeInConstants()));
}
if (expression instanceof InfixExpression) {
InfixExpression infixExpression = (InfixExpression) expression;
return getInfixInSafeOperators().contains(infixExpression.getOperator());
}
if (expression instanceof PrefixExpression) {
PrefixExpression prefixExpression = (PrefixExpression) expression;
return getPrefixInSafeOperators().contains(prefixExpression.getOperator());
}
if (expression instanceof PostfixExpression) {
PostfixExpression postfixExpression = (PostfixExpression) expression;
return getPostfixInSafeOperators().contains(postfixExpression.getOperator());
}
if (expression instanceof CastExpression) {
CastExpression castExpression = (CastExpression) expression;
return ASTNodes.hasType(castExpression.getType().resolveBinding(), getPrimitiveTypeName()) || ASTNodes.hasType(castExpression.getType().resolveBinding(), getWrapperFullyQualifiedName()) && isNotNull(castExpression.getExpression());
}
if (expression instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) expression;
return // $NON-NLS-1$
ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", getPrimitiveTypeName()) || getParsingMethodName(getWrapperFullyQualifiedName()) != null && (// $NON-NLS-1$
ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName()) || // $NON-NLS-1$
ASTNodes.usesGivenSignature(methodInvocation, getWrapperFullyQualifiedName(), "valueOf", String.class.getCanonicalName(), int.class.getSimpleName()));
}
if (expression instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression;
List<Expression> classInstanceCreationArguments = classInstanceCreation.arguments();
if (classInstanceCreationArguments.size() == 1) {
Expression arg0 = classInstanceCreationArguments.get(0);
return ASTNodes.hasType(arg0, String.class.getCanonicalName());
}
}
return false;
}
Aggregations