use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class CFGBuilder method addVariableAccess.
/**
* @return whether the current variable access can throw an exception.
*/
private boolean addVariableAccess(final CFGBasicBlock basicBlock, final Expression node, final int flags, final ThrowerBlocks throwers) {
if (node == null) {
return false;
}
switch(node.getNodeType()) {
case ASTNode.ARRAY_ACCESS:
ArrayAccess aa = (ArrayAccess) node;
addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
throwers.addThrow(aa, newException(node, ArrayIndexOutOfBoundsException.class.getCanonicalName()));
return true;
case ASTNode.ARRAY_CREATION:
ArrayCreation ac = (ArrayCreation) node;
boolean acMightThrow1 = addVariableAccess(basicBlock, ac.getInitializer(), flags, throwers);
boolean acMightThrow2 = addVariableAccesses(basicBlock, ac.dimensions(), flags, throwers);
return acMightThrow1 || acMightThrow2;
case ASTNode.ARRAY_INITIALIZER:
ArrayInitializer ai = (ArrayInitializer) node;
return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
case ASTNode.ASSIGNMENT:
Assignment a = (Assignment) node;
boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), VariableAccess.WRITE, throwers);
boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), VariableAccess.READ, throwers);
return aMightThrow1 || aMightThrow2;
case ASTNode.BOOLEAN_LITERAL:
case ASTNode.CHARACTER_LITERAL:
case ASTNode.NULL_LITERAL:
case ASTNode.NUMBER_LITERAL:
case ASTNode.STRING_LITERAL:
case ASTNode.TYPE_LITERAL:
// Nothing to do
return false;
case ASTNode.CAST_EXPRESSION:
CastExpression cae = (CastExpression) node;
return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
case ASTNode.CLASS_INSTANCE_CREATION:
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) node;
addVariableAccess(basicBlock, classInstanceCreation.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, classInstanceCreation.arguments(), flags, throwers);
IMethodBinding cicBinding = classInstanceCreation.resolveConstructorBinding();
if (cicBinding != null) {
ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
throwers.addThrow(classInstanceCreation, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression coe = (ConditionalExpression) node;
boolean mightThrow1 = addVariableAccess(basicBlock, coe.getExpression(), flags, throwers);
boolean mightThrow2 = addVariableAccess(basicBlock, coe.getThenExpression(), flags, throwers);
boolean mightThrow3 = addVariableAccess(basicBlock, coe.getElseExpression(), flags, throwers);
return mightThrow1 || mightThrow2 || mightThrow3;
case ASTNode.FIELD_ACCESS:
FieldAccess fa = (FieldAccess) node;
boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
basicBlock.addVariableAccess(new VariableAccess(fa, flags));
if (is(flags, VariableAccess.READ)) {
throwers.addThrow(fa, newException(node, NullPointerException.class.getCanonicalName()));
mightThrow = true;
}
return mightThrow;
case ASTNode.INFIX_EXPRESSION:
InfixExpression infixExpression = (InfixExpression) node;
boolean ieMightThrow1 = addVariableAccess(basicBlock, infixExpression.getLeftOperand(), flags, throwers);
boolean ieMightThrow2 = addVariableAccess(basicBlock, infixExpression.getRightOperand(), flags, throwers);
return ieMightThrow1 || ieMightThrow2;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression ioe = (InstanceofExpression) node;
return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) node;
addVariableAccess(basicBlock, methodInvocation.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, methodInvocation.arguments(), flags, throwers);
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
throwers.addThrow(methodInvocation, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.SIMPLE_NAME:
SimpleName sn = (SimpleName) node;
basicBlock.addVariableAccess(new VariableAccess(sn, flags));
if (is(flags, VariableAccess.READ)) {
throwers.addThrow(sn, newException(node, NullPointerException.class.getCanonicalName()));
return true;
}
return false;
case ASTNode.QUALIFIED_NAME:
QualifiedName qn = (QualifiedName) node;
basicBlock.addVariableAccess(new VariableAccess(qn, flags));
throwers.addThrow(qn, newException(node, NullPointerException.class.getCanonicalName()));
return true;
case ASTNode.PARENTHESIZED_EXPRESSION:
ParenthesizedExpression pe = (ParenthesizedExpression) node;
return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
case ASTNode.POSTFIX_EXPRESSION:
PostfixExpression poe = (PostfixExpression) node;
return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
case ASTNode.PREFIX_EXPRESSION:
PrefixExpression pre = (PrefixExpression) node;
return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
case ASTNode.SUPER_FIELD_ACCESS:
SuperFieldAccess sfa = (SuperFieldAccess) node;
boolean sfaMightThrow1 = addVariableAccess(basicBlock, sfa.getQualifier(), flags, throwers);
boolean sfaMightThrow2 = addVariableAccess(basicBlock, sfa.getName(), flags, throwers);
return sfaMightThrow1 || sfaMightThrow2;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation smi = (SuperMethodInvocation) node;
addVariableAccess(basicBlock, smi.getQualifier(), flags, throwers);
addVariableAccess(basicBlock, smi.getName(), flags, throwers);
IMethodBinding sMethodBinding = smi.resolveMethodBinding();
if (sMethodBinding != null) {
ITypeBinding[] declaredThrows = sMethodBinding.getExceptionTypes();
throwers.addThrow(smi, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.THIS_EXPRESSION:
ThisExpression te = (ThisExpression) node;
// TODO JNR remember use of "this" here
return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return addDeclarations(basicBlock, (VariableDeclarationExpression) node, throwers);
default:
throw new NotImplementedException(node);
}
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ASTNodeFactory method negate.
/**
* Negates the provided expression and applies the provided copy operation on
* the returned expression:
*
* isValid => !isValid
* !isValid => isValid
* true => false
* false => true
* i > 0 => i <= 0
* isValid || isEnabled => !isValid && !isEnabled
* !isValid || !isEnabled => isValid && isEnabled
* isValid ? (i > 0) : !isEnabled => isValid ? (i <= 0) : isEnabled
* @param booleanExpression the expression to negate
* @param isMove False if the returned nodes need to be new nodes
*
* @return the negated expression, as move or copy
*/
public Expression negate(final Expression booleanExpression, final boolean isMove) {
Expression unparenthesedExpression = ASTNodes.getUnparenthesedExpression(booleanExpression);
if (unparenthesedExpression instanceof PrefixExpression) {
PrefixExpression prefixExpression = (PrefixExpression) unparenthesedExpression;
if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.NOT)) {
Expression otherExpression = prefixExpression.getOperand();
PrefixExpression otherPrefixExpression = ASTNodes.as(otherExpression, PrefixExpression.class);
if (otherPrefixExpression != null && ASTNodes.hasOperator(otherPrefixExpression, PrefixExpression.Operator.NOT)) {
return negate(otherPrefixExpression.getOperand(), isMove);
}
return isMove ? createMoveTarget(otherExpression) : createCopyTarget(otherExpression);
}
} else if (unparenthesedExpression instanceof InfixExpression) {
InfixExpression booleanOperation = (InfixExpression) unparenthesedExpression;
InfixExpression.Operator negatedOperator = ASTNodes.negatedInfixOperator(booleanOperation.getOperator());
if (negatedOperator != null) {
return getNegatedOperation(booleanOperation, negatedOperator, isMove);
}
} else if (unparenthesedExpression instanceof ConditionalExpression) {
ConditionalExpression aConditionalExpression = (ConditionalExpression) unparenthesedExpression;
return newConditionalExpression(isMove ? createMoveTarget(aConditionalExpression.getExpression()) : createCopyTarget(aConditionalExpression.getExpression()), negate(aConditionalExpression.getThenExpression(), isMove), negate(aConditionalExpression.getElseExpression(), isMove));
} else {
Boolean constant = ASTNodes.getBooleanLiteral(unparenthesedExpression);
if (constant != null) {
return newBooleanLiteral(!constant);
}
}
if (isMove) {
return not(createMoveTarget(unparenthesedExpression));
}
return not(createCopyTarget(unparenthesedExpression));
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteDoubleNegationCleanUp method visit.
@Override
public boolean visit(final InfixExpression visited) {
if (!visited.hasExtendedOperands() && ASTNodes.hasOperator(visited, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.XOR)) {
Expression leftExpression = visited.getLeftOperand();
Expression rightExpression = visited.getRightOperand();
Expression leftNegatedExpression = null;
PrefixExpression leftPrefix = ASTNodes.as(leftExpression, PrefixExpression.class);
if (leftPrefix != null && ASTNodes.hasOperator(leftPrefix, PrefixExpression.Operator.NOT)) {
leftNegatedExpression = leftPrefix.getOperand();
}
Expression rightNegatedExpression = null;
PrefixExpression rightPrefix = ASTNodes.as(rightExpression, PrefixExpression.class);
if (rightPrefix != null && ASTNodes.hasOperator(rightPrefix, PrefixExpression.Operator.NOT)) {
rightNegatedExpression = rightPrefix.getOperand();
}
if (leftNegatedExpression != null || rightNegatedExpression != null) {
removeDoubleNegation(visited, leftExpression, rightExpression, leftNegatedExpression, rightNegatedExpression);
return false;
}
}
return true;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteLambdaExpressionRatherThanComparatorCleanUp method isReturnedExpressionToRefactor.
private boolean isReturnedExpressionToRefactor(final Expression returnExpression, final AtomicReference<Expression> criteria, final AtomicBoolean isForward, final SimpleName name1, final SimpleName name2) {
PrefixExpression negativeExpression = ASTNodes.as(returnExpression, PrefixExpression.class);
if (negativeExpression != null && ASTNodes.hasOperator(negativeExpression, PrefixExpression.Operator.MINUS)) {
isForward.lazySet(!isForward.get());
return isReturnedExpressionToRefactor(negativeExpression.getOperand(), criteria, isForward, name1, name2);
}
MethodInvocation compareToMethod = ASTNodes.as(returnExpression, MethodInvocation.class);
if (compareToMethod != null && compareToMethod.getExpression() != null) {
ITypeBinding comparisonType = compareToMethod.getExpression().resolveTypeBinding();
if (comparisonType != null) {
if (compareToMethod.getExpression() != null && ASTNodes.usesGivenSignature(compareToMethod, comparisonType.getQualifiedName(), "compareTo", comparisonType.getQualifiedName())) {
// $NON-NLS-1$
return isRefactorComparisonToRefactor(criteria, isForward, name1, name2, compareToMethod.getExpression(), (Expression) compareToMethod.arguments().get(0));
}
String primitiveType = Bindings.getUnboxedTypeName(comparisonType.getQualifiedName());
if (primitiveType != null && ASTNodes.usesGivenSignature(compareToMethod, comparisonType.getQualifiedName(), "compare", primitiveType, primitiveType)) {
// $NON-NLS-1$
return isRefactorComparisonToRefactor(criteria, isForward, name1, name2, (Expression) compareToMethod.arguments().get(0), (Expression) compareToMethod.arguments().get(1));
}
}
}
return false;
}
use of org.eclipse.jdt.core.dom.PrefixExpression 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;
}
Aggregations