Search in sources :

Example 1 with BooleanLiteral

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;
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Example 2 with BooleanLiteral

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());
        }
    };
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) List(java.util.List)

Example 3 with BooleanLiteral

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!");
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableReference(org.evosuite.testcase.VariableReference) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) CastExpression(org.eclipse.jdt.core.dom.CastExpression) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 4 with BooleanLiteral

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;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 5 with BooleanLiteral

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;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) OrderedInfixExpression(org.autorefactor.jdt.internal.corext.dom.OrderedInfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Aggregations

BooleanLiteral (org.eclipse.jdt.core.dom.BooleanLiteral)13 Expression (org.eclipse.jdt.core.dom.Expression)6 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)6 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)4 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)4 SimpleName (org.eclipse.jdt.core.dom.SimpleName)4 StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)4 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)4 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)3 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)3 AST (org.eclipse.jdt.core.dom.AST)2 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 NullLiteral (org.eclipse.jdt.core.dom.NullLiteral)2