Search in sources :

Example 11 with BooleanLiteral

use of org.eclipse.jdt.core.dom.BooleanLiteral in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveTypeClass.

/**
 * <p>
 * retrieveTypeClass
 * </p>
 *
 * @param argument
 *            a {@link java.lang.Object} object.
 * @return a {@link java.lang.Class} object.
 */
protected Class<?> retrieveTypeClass(Object argument) {
    assert argument != null;
    if (argument instanceof SimpleType) {
        SimpleType simpleType = (SimpleType) argument;
        return retrieveTypeClass(simpleType);
    }
    if (argument instanceof ITypeBinding) {
        ITypeBinding binding = (ITypeBinding) argument;
        return retrieveTypeClass(binding);
    }
    if (argument instanceof IVariableBinding) {
        IVariableBinding variableBinding = (IVariableBinding) argument;
        return retrieveTypeClass(variableBinding.getType());
    }
    if (argument instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) argument;
        return retrieveTypeClass(simpleName.resolveBinding());
    }
    if (argument instanceof StringLiteral) {
        return String.class;
    }
    if (argument instanceof NumberLiteral) {
        return retrieveTypeClass((NumberLiteral) argument);
    }
    if (argument instanceof PrimitiveType) {
        PrimitiveType primitiveType = (PrimitiveType) argument;
        String typeCode = primitiveType.getPrimitiveTypeCode().toString();
        Class<?> result = PRIMITIVE_TYPECODE_MAPPING.get(typeCode);
        assert result != null : "Could not resolve typecode " + typeCode + ".";
        return result;
    }
    if (argument instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) argument;
        return retrieveTypeClass(arrayType);
    }
    if (argument instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) argument;
        return retrieveTypeClass(parameterizedType.getType());
    }
    if (argument instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) argument;
        return retrieveTypeClass(varDeclFrgmnt.resolveBinding());
    }
    if (argument instanceof InfixExpression) {
        InfixExpression infixExpr = (InfixExpression) argument;
        ITypeBinding refTypeBinding = infixExpr.resolveTypeBinding();
        if (refTypeBinding != null) {
            return retrieveTypeClass(refTypeBinding);
        } else {
            throw new RuntimeException("Could not determine type class of infix expression '" + infixExpr + "'.");
        }
    }
    if (argument instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) argument;
        ITypeBinding typeBinding = methodInvocation.resolveTypeBinding();
        if (typeBinding != null) {
            return retrieveTypeClass(typeBinding);
        }
        Expression typeExpression = methodInvocation.getExpression();
        if (typeExpression instanceof MethodInvocation) {
            MethodInvocation parentMethodInvocation = (MethodInvocation) typeExpression;
            IMethodBinding parentMethodBinding = parentMethodInvocation.resolveMethodBinding();
            return retrieveTypeClass(parentMethodBinding.getDeclaringClass());
        } else {
            return retrieveTypeClass(typeExpression);
        }
    }
    if (argument instanceof ArrayAccess) {
        ArrayAccess arrayAccess = (ArrayAccess) argument;
        return retrieveTypeClass(arrayAccess.getArray());
    }
    if (argument instanceof Class<?>) {
        return (Class<?>) argument;
    }
    if (argument instanceof ClassInstanceCreation) {
        return retrieveTypeClass(((ClassInstanceCreation) argument).resolveTypeBinding());
    }
    if (argument instanceof BooleanLiteral) {
        return Boolean.TYPE;
    }
    throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) 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) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) 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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) GenericClass(org.evosuite.utils.GenericClass) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 12 with BooleanLiteral

use of org.eclipse.jdt.core.dom.BooleanLiteral in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getInversedExpression.

private static Expression getInversedExpression(ASTRewrite rewrite, Expression expression, SimpleNameRenameProvider provider) {
    AST ast = rewrite.getAST();
    // 
    if (expression instanceof BooleanLiteral) {
        return ast.newBooleanLiteral(!((BooleanLiteral) expression).booleanValue());
    }
    if (expression instanceof InfixExpression) {
        InfixExpression infixExpression = (InfixExpression) expression;
        InfixExpression.Operator operator = infixExpression.getOperator();
        if (operator == InfixExpression.Operator.LESS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER_EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.GREATER) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS_EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.LESS_EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.GREATER, provider);
        }
        if (operator == InfixExpression.Operator.GREATER_EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.LESS, provider);
        }
        if (operator == InfixExpression.Operator.EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.NOT_EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.NOT_EQUALS) {
            return getInversedInfixExpression(rewrite, infixExpression, InfixExpression.Operator.EQUALS, provider);
        }
        if (operator == InfixExpression.Operator.CONDITIONAL_AND) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_OR, provider);
        }
        if (operator == InfixExpression.Operator.CONDITIONAL_OR) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.CONDITIONAL_AND, provider);
        }
        if (operator == InfixExpression.Operator.AND) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.OR, provider);
        }
        if (operator == InfixExpression.Operator.OR) {
            return getInversedAndOrExpression(rewrite, infixExpression, InfixExpression.Operator.AND, provider);
        }
        if (operator == InfixExpression.Operator.XOR) {
            return getInversedNotExpression(rewrite, expression, ast);
        }
    }
    if (expression instanceof PrefixExpression) {
        PrefixExpression prefixExpression = (PrefixExpression) expression;
        if (prefixExpression.getOperator() == PrefixExpression.Operator.NOT) {
            Expression operand = prefixExpression.getOperand();
            if ((operand instanceof ParenthesizedExpression) && NecessaryParenthesesChecker.canRemoveParentheses(operand, expression.getParent(), expression.getLocationInParent())) {
                operand = ((ParenthesizedExpression) operand).getExpression();
            }
            Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, operand);
            if (renamedNameCopy instanceof InfixExpression) {
                InfixExpression infixExpression = (InfixExpression) renamedNameCopy;
                infixExpression.setOperator(((InfixExpression) operand).getOperator());
            }
            return renamedNameCopy;
        }
    }
    if (expression instanceof InstanceofExpression) {
        return getInversedNotExpression(rewrite, expression, ast);
    }
    if (expression instanceof ParenthesizedExpression) {
        ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
        Expression innerExpression = parenthesizedExpression.getExpression();
        while (innerExpression instanceof ParenthesizedExpression) {
            innerExpression = ((ParenthesizedExpression) innerExpression).getExpression();
        }
        if (innerExpression instanceof InstanceofExpression) {
            return getInversedExpression(rewrite, innerExpression, provider);
        }
        parenthesizedExpression = getParenthesizedExpression(ast, getInversedExpression(rewrite, innerExpression, provider));
        return parenthesizedExpression;
    }
    if (expression instanceof ConditionalExpression) {
        ConditionalExpression conditionalExpression = (ConditionalExpression) expression;
        ConditionalExpression newExpression = ast.newConditionalExpression();
        newExpression.setExpression((Expression) rewrite.createCopyTarget(conditionalExpression.getExpression()));
        newExpression.setThenExpression(getInversedExpression(rewrite, conditionalExpression.getThenExpression()));
        newExpression.setElseExpression(getInversedExpression(rewrite, conditionalExpression.getElseExpression()));
        return newExpression;
    }
    PrefixExpression prefixExpression = ast.newPrefixExpression();
    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
    Expression renamedNameCopy = getRenamedNameCopy(provider, rewrite, expression);
    if (NecessaryParenthesesChecker.needsParentheses(renamedNameCopy, prefixExpression, PrefixExpression.OPERAND_PROPERTY)) {
        renamedNameCopy = getParenthesizedExpression(ast, renamedNameCopy);
    }
    prefixExpression.setOperand(renamedNameCopy);
    return prefixExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 13 with BooleanLiteral

use of org.eclipse.jdt.core.dom.BooleanLiteral in project AutoRefactor by JnRouvignac.

the class ObsoleteLiteralRatherThanBooleanConstantCleanUp method replaceWithBooleanLiteral.

private void replaceWithBooleanLiteral(final QualifiedName visited, final boolean val) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteLiteralRatherThanBooleanConstantCleanUp_description);
    BooleanLiteral booleanLiteral = ast.newBooleanLiteral(val);
    ASTNodes.replaceButKeepComment(rewrite, visited, booleanLiteral, group);
}
Also used : BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

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