use of org.eclipse.jdt.core.dom.PrefixExpression in project AutoRefactor by JnRouvignac.
the class ASTSemanticMatcher method matchNegative.
/**
* Match the boolean opposite.
*
* @param node Node to check
* @param otherObject Node to compare
* @return True if it is the boolean opposite.
*/
public boolean matchNegative(final ASTNode node, final Object otherObject) {
Object other = unbracket(otherObject);
if (node instanceof ParenthesizedExpression) {
return matchNegative(((ParenthesizedExpression) node).getExpression(), other);
}
if (node instanceof PrefixExpression) {
PrefixExpression pe = (PrefixExpression) node;
if (ASTNodes.hasOperator(pe, PrefixExpression.Operator.NOT)) {
if (other instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) other, PrefixExpression.Operator.NOT)) {
return matchNegative(pe.getOperand(), ((PrefixExpression) other).getOperand());
}
return safeSubtreeMatch(pe.getOperand(), other);
}
} else if (other instanceof PrefixExpression && ASTNodes.hasOperator((PrefixExpression) other, PrefixExpression.Operator.NOT)) {
return safeSubtreeMatch(node, ((PrefixExpression) other).getOperand());
}
if (other instanceof ASTNode) {
Boolean value = ASTNodes.getBooleanLiteral(node);
Boolean otherValue = ASTNodes.getBooleanLiteral((ASTNode) other);
if (value != null && otherValue != null) {
return value ^ otherValue;
}
}
if (!(node instanceof InfixExpression) || !(other instanceof InfixExpression)) {
return false;
}
InfixExpression infixExpression1 = (InfixExpression) node;
InfixExpression infixExpression2 = (InfixExpression) other;
Expression leftOperand1 = infixExpression1.getLeftOperand();
Expression rightOperand1 = infixExpression1.getRightOperand();
Expression leftOperand2 = infixExpression2.getLeftOperand();
Expression rightOperand2 = infixExpression2.getRightOperand();
if (infixExpression1.getOperator().equals(infixExpression2.getOperator()) && !infixExpression1.hasExtendedOperands() && !infixExpression2.hasExtendedOperands() && ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS, InfixExpression.Operator.XOR)) {
return matchOneNegativeOther(leftOperand1, leftOperand2, rightOperand2, rightOperand1) || matchOneNegativeOther(rightOperand2, rightOperand1, leftOperand1, leftOperand2) || ASTNodes.isPassiveWithoutFallingThrough(leftOperand1) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand1) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand2) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand2) && (matchOneNegativeOther(leftOperand1, leftOperand2, rightOperand2, rightOperand1) || matchOneNegativeOther(rightOperand2, rightOperand1, leftOperand1, leftOperand2));
}
InfixExpression.Operator negatedOperator = ASTNodes.negatedInfixOperator(infixExpression1.getOperator());
if (infixExpression2.getOperator().equals(negatedOperator)) {
if (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.CONDITIONAL_AND, InfixExpression.Operator.CONDITIONAL_OR, InfixExpression.Operator.AND, InfixExpression.Operator.OR)) {
return isOperandsMatching(infixExpression1, infixExpression2, false);
}
if (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.EQUALS, InfixExpression.Operator.NOT_EQUALS)) {
return isOperandsMatching(infixExpression1, infixExpression2, true);
}
if (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.GREATER, InfixExpression.Operator.GREATER_EQUALS, InfixExpression.Operator.LESS, InfixExpression.Operator.LESS_EQUALS) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand1) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand1) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand2) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand2)) {
return safeSubtreeMatch(leftOperand1, leftOperand2) && safeSubtreeMatch(rightOperand1, rightOperand2);
}
return false;
}
return (ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.GREATER) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.GREATER_EQUALS) || ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.GREATER_EQUALS) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.GREATER) || ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.LESS) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.LESS_EQUALS) || ASTNodes.hasOperator(infixExpression1, InfixExpression.Operator.LESS_EQUALS) && ASTNodes.hasOperator(infixExpression2, InfixExpression.Operator.LESS)) && !infixExpression1.hasExtendedOperands() && !infixExpression2.hasExtendedOperands() && ASTNodes.isPassiveWithoutFallingThrough(leftOperand1) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand1) && ASTNodes.isPassiveWithoutFallingThrough(leftOperand2) && ASTNodes.isPassiveWithoutFallingThrough(rightOperand2) && safeSubtreeMatch(leftOperand1, rightOperand2) && safeSubtreeMatch(rightOperand1, leftOperand2);
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project evosuite by EvoSuite.
the class TestExtractingVisitor method visit.
/**
* {@inheritDoc}
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public boolean visit(VariableDeclarationStatement variableDeclStmt) {
Class<?> varType = retrieveTypeClass(variableDeclStmt.getType());
if (varType.isPrimitive() || PRIMITIVE_CLASSES.contains(varType)) {
// Can only happen to primitive types and String,
// otherwise it is a constructor call which is handled elsewhere
logger.debug("Variable has not been treated elsewhere...");
VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
Expression expression = varDeclFrgmnt.getInitializer();
VariableReference varRef = retrieveVariableReference(expression, null);
varRef.setOriginalCode(variableDeclStmt.toString());
// TODO Use the name here as well?
// String name = varDeclFrgmt.getName().getIdentifier();
// new BoundVariableReferenceImpl(testCase, varType, name);
testCase.addVariable(varDeclFrgmnt.resolveBinding(), varRef);
return true;
}
if (varType.isArray()) {
// if (varType.getComponentType().isPrimitive() ||
// varType.getComponentType().equals(String.class)) {
// ... or to primitive and string arrays
VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) variableDeclStmt.fragments().get(0);
Expression expression = varDeclFrgmnt.getInitializer();
if (expression instanceof ArrayInitializer) {
ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
arrayReference.setOriginalCode(variableDeclStmt.toString());
testCase.addStatement(arrayStatement);
testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayReference);
ArrayInitializer arrayInitializer = (ArrayInitializer) expression;
for (int idx = 0; idx < arrayInitializer.expressions().size(); idx++) {
Expression expr = (Expression) arrayInitializer.expressions().get(idx);
VariableReference valueRef;
if (expr instanceof NumberLiteral) {
valueRef = retrieveVariableReference((NumberLiteral) expr, varType.getComponentType());
} else if (expr instanceof PrefixExpression) {
valueRef = retrieveVariableReference((PrefixExpression) expr, varType.getComponentType());
} else {
valueRef = retrieveVariableReference(expr, null);
}
valueRef.setOriginalCode(expr.toString());
VariableReference arrayElementRef = new ArrayIndex(testCase.getReference(), arrayReference, idx);
arrayElementRef.setOriginalCode(expr.toString());
arrayStatement.getVariableReferences().add(arrayElementRef);
AssignmentStatement arrayAssignment = new AssignmentStatement(testCase.getReference(), arrayElementRef, valueRef);
testCase.addStatement(arrayAssignment);
}
// }
return true;
}
if (expression instanceof ArrayCreation) {
ArrayCreation arrayCreation = ((ArrayCreation) expression);
List paramTypes = new ArrayList();
for (int idx = 0; idx < arrayCreation.dimensions().size(); idx++) {
paramTypes.add(int.class);
}
List<VariableReference> lengthsVarRefs = convertParams(arrayCreation.dimensions(), paramTypes);
ArrayReference arrayReference = new ValidArrayReference(testCase.getReference(), varType);
arrayReference.setOriginalCode(variableDeclStmt.toString());
ArrayStatement arrayStatement = new ArrayStatement(testCase.getReference(), arrayReference);
arrayStatement.setLengths(getLengths(variableDeclStmt, lengthsVarRefs));
testCase.addVariable(varDeclFrgmnt.resolveBinding(), arrayStatement.getReturnValue());
testCase.addStatement(arrayStatement);
}
}
return true;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse.jdt.ls by eclipse.
the class InvertBooleanUtility method getInversedNotExpression.
private static Expression getInversedNotExpression(ASTRewrite rewrite, Expression expression, AST ast) {
PrefixExpression prefixExpression = ast.newPrefixExpression();
prefixExpression.setOperator(PrefixExpression.Operator.NOT);
ParenthesizedExpression parenthesizedExpression = getParenthesizedExpression(ast, (Expression) rewrite.createCopyTarget(expression));
prefixExpression.setOperand(parenthesizedExpression);
return prefixExpression;
}
use of org.eclipse.jdt.core.dom.PrefixExpression in project eclipse.jdt.ls by eclipse.
the class AccessAnalyzer method visit.
@Override
public boolean visit(PrefixExpression node) {
Expression operand = node.getOperand();
if (!considerBinding(resolveBinding(operand), operand)) {
return true;
}
PrefixExpression.Operator operator = node.getOperator();
if (operator != PrefixExpression.Operator.INCREMENT && operator != PrefixExpression.Operator.DECREMENT) {
return true;
}
checkParent(node);
fRewriter.replace(node, createInvocation(node.getAST(), node.getOperand(), node.getOperator().toString()), createGroupDescription(PREFIX_ACCESS));
return false;
}
Aggregations