use of org.eclipse.jdt.core.dom.BooleanLiteral in project AutoRefactor by JnRouvignac.
the class ASTHelper method getBooleanLiteral.
/**
* Returns the {@link Boolean} object value represented by the provided expression.
*
* @param expr the expression to analyze
* @return the {@link Boolean} object value if the provided expression represents one, null otherwise
*/
public static Boolean getBooleanLiteral(Expression expr) {
final BooleanLiteral bl = as(expr, BooleanLiteral.class);
if (bl != null) {
return bl.booleanValue();
}
final QualifiedName qn = as(expr, QualifiedName.class);
if (hasType(qn, "java.lang.Boolean")) {
return getBooleanObject(qn);
}
return null;
}
use of org.eclipse.jdt.core.dom.BooleanLiteral in project eclipse-cs by checkstyle.
the class SimplifyBooleanReturnQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@Override
public boolean visit(final IfStatement node) {
if (containsPosition(node, markerStartOffset)) {
final Boolean isThenStatementTrue = isReturnStatementTrue(node.getThenStatement());
if (isThenStatementTrue == null) {
// the AST structure of the if statement is not as expected
return true;
}
final Expression condition = removeNotFromCondition(node.getExpression());
final boolean isNotCondition = condition != node.getExpression();
final ReturnStatement replacement;
if (isThenStatementTrue ^ isNotCondition) {
// create replacement: return condition;
replacement = node.getAST().newReturnStatement();
replacement.setExpression(copy(condition));
} else {
// create replacement: return !(condition);
final AST ast = node.getAST();
replacement = ast.newReturnStatement();
final PrefixExpression not = ast.newPrefixExpression();
not.setOperator(Operator.NOT);
if (omitParantheses(condition)) {
not.setOperand(copy(condition));
} else {
final ParenthesizedExpression parentheses = ast.newParenthesizedExpression();
parentheses.setExpression(copy(condition));
not.setOperand(parentheses);
}
replacement.setExpression(not);
}
replace(node, replacement);
}
return true;
}
private Boolean isReturnStatementTrue(final Statement node) {
if (node instanceof ReturnStatement) {
final Expression expression = ((ReturnStatement) node).getExpression();
if (expression instanceof BooleanLiteral) {
return ((BooleanLiteral) expression).booleanValue();
}
} else if (node instanceof Block) {
// the return statement might be wrapped in a block statement
@SuppressWarnings("unchecked") final List<Statement> statements = ((Block) node).statements();
if (statements.size() > 0) {
return isReturnStatementTrue(statements.get(0));
}
}
return null;
}
private Expression removeNotFromCondition(final Expression condition) {
if (condition instanceof PrefixExpression) {
final PrefixExpression prefix = (PrefixExpression) condition;
if (PrefixExpression.Operator.NOT.equals(prefix.getOperator())) {
return prefix.getOperand();
}
}
return condition;
}
private boolean omitParantheses(final Expression condition) {
return OMIT_PARANETHESES_CLASSES.contains(condition.getClass());
}
};
}
use of org.eclipse.jdt.core.dom.BooleanLiteral in project evosuite by EvoSuite.
the class TestExtractingVisitor method retrieveVariableReference.
/**
* <p>
* retrieveVariableReference
* </p>
*
* @param argument
* a {@link java.lang.Object} object.
* @param varType
* a {@link java.lang.Class} object.
* @return a {@link org.evosuite.testcase.VariableReference} object.
*/
protected VariableReference retrieveVariableReference(Object argument, Class<?> varType) {
if (argument instanceof ClassInstanceCreation) {
return retrieveVariableReference((ClassInstanceCreation) argument, varType);
}
if (argument instanceof VariableDeclarationFragment) {
return retrieveVariableReference((VariableDeclarationFragment) argument);
}
if (argument instanceof SimpleName) {
SimpleName simpleName = (SimpleName) argument;
lineNumber = testReader.getLineNumber(simpleName.getStartPosition());
return retrieveVariableReference(simpleName.resolveBinding(), varType);
}
if (argument instanceof IVariableBinding) {
return retrieveVariableReference((IVariableBinding) argument, varType);
}
if (argument instanceof PrefixExpression) {
return retrieveVariableReference((PrefixExpression) argument);
}
if (argument instanceof InfixExpression) {
return retrieveVariableReference((InfixExpression) argument, varType);
}
if (argument instanceof ExpressionStatement) {
ExpressionStatement exprStmt = (ExpressionStatement) argument;
Expression expression = exprStmt.getExpression();
return retrieveVariableReference(expression, varType);
}
if (argument instanceof NullLiteral) {
return retrieveVariableReference((NullLiteral) argument, varType);
}
if (argument instanceof StringLiteral) {
return retrieveVariableReference((StringLiteral) argument);
}
if (argument instanceof NumberLiteral) {
return retrieveVariableReference((NumberLiteral) argument);
}
if (argument instanceof CharacterLiteral) {
return retrieveVariableReference((CharacterLiteral) argument);
}
if (argument instanceof BooleanLiteral) {
return retrieveVariableReference((BooleanLiteral) argument);
}
if (argument instanceof ITypeBinding) {
if (varType != null) {
return new ValidVariableReference(testCase.getReference(), varType);
}
return new ValidVariableReference(testCase.getReference(), retrieveTypeClass(argument));
}
if (argument instanceof QualifiedName) {
return retrieveVariableReference((QualifiedName) argument);
}
if (argument instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) argument;
VariableReference result = retrieveResultReference(methodInvocation);
nestedCallResults.push(result);
return result;
}
if (argument instanceof ArrayCreation) {
return retrieveVariableReference((ArrayCreation) argument);
}
if (argument instanceof VariableDeclaration) {
return retrieveVariableReference((VariableDeclaration) argument);
}
if (argument instanceof ArrayAccess) {
// argument).getArray(), null);
return retrieveVariableReference((ArrayAccess) argument);
}
if (argument instanceof Assignment) {
return retrieveVariableReference(((Assignment) argument).getLeftHandSide(), null);
}
if (argument instanceof CastExpression) {
CastExpression castExpression = (CastExpression) argument;
VariableReference result = retrieveVariableReference(castExpression.getExpression(), null);
Class<?> castClass = retrieveTypeClass(castExpression.resolveTypeBinding());
assert castClass.isAssignableFrom(toClass(result.getType()));
result.setType(castClass);
return result;
}
throw new UnsupportedOperationException("Argument type " + argument.getClass() + " not implemented!");
}
use of org.eclipse.jdt.core.dom.BooleanLiteral in project AutoRefactor by JnRouvignac.
the class ASTNodes method getBooleanLiteral.
/**
* Returns the {@link Boolean} object value represented by the provided
* expression.
*
* @param node the expression to analyze
* @return the {@link Boolean} object value if the provided expression
* represents one, null otherwise
*/
public static Boolean getBooleanLiteral(final ASTNode node) {
if (!(node instanceof Expression)) {
return null;
}
Expression expression = (Expression) node;
BooleanLiteral booleanLiteral = as(expression, BooleanLiteral.class);
if (booleanLiteral != null) {
return booleanLiteral.booleanValue();
}
QualifiedName booleanConstant = as(expression, QualifiedName.class);
if (booleanConstant != null) {
if (isField(booleanConstant, Boolean.class.getCanonicalName(), "TRUE")) {
// $NON-NLS-1$
return Boolean.TRUE;
}
if (isField(booleanConstant, Boolean.class.getCanonicalName(), "FALSE")) {
// $NON-NLS-1$
return Boolean.FALSE;
}
return getBooleanObject(booleanConstant);
}
SimpleName staticConstant = as(expression, SimpleName.class);
if (staticConstant != null) {
IBinding iBinding = staticConstant.resolveBinding();
if (iBinding != null && iBinding.getKind() == IBinding.VARIABLE) {
IVariableBinding iVariableBinding = (IVariableBinding) iBinding;
if (hasType(iVariableBinding.getDeclaringClass(), Boolean.class.getCanonicalName())) {
String fqn = staticConstant.getIdentifier();
if ("TRUE".equals(fqn)) {
// $NON-NLS-1$
return true;
}
if ("FALSE".equals(fqn)) {
// $NON-NLS-1$
return false;
}
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.BooleanLiteral 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;
}
Aggregations