Search in sources :

Example 46 with PrefixExpression

use of org.eclipse.jdt.core.dom.PrefixExpression 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 47 with PrefixExpression

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

the class ASTResolving method getPossibleReferenceBinding.

private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
    ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case ASTNode.ASSIGNMENT:
            Assignment assignment = (Assignment) parent;
            if (node.equals(assignment.getLeftHandSide())) {
                // field write access: xx= expression
                return assignment.getRightHandSide().resolveTypeBinding();
            }
            // read access
            return assignment.getLeftHandSide().resolveTypeBinding();
        case ASTNode.INFIX_EXPRESSION:
            InfixExpression infix = (InfixExpression) parent;
            InfixExpression.Operator op = infix.getOperator();
            if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("boolean");
            } else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("int");
            }
            if (node.equals(infix.getLeftOperand())) {
                // xx operation expression
                ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
                if (rigthHandBinding != null) {
                    return rigthHandBinding;
                }
            } else {
                // expression operation xx
                ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
                if (leftHandBinding != null) {
                    return leftHandBinding;
                }
            }
            if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.INSTANCEOF_EXPRESSION:
            InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
            return instanceofExpression.getRightOperand().resolveBinding();
        case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
            VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
            if (frag.getInitializer().equals(node)) {
                return frag.getName().resolveTypeBinding();
            }
            break;
        case ASTNode.SUPER_METHOD_INVOCATION:
            SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
            IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
            if (superMethodBinding != null) {
                return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
            }
            break;
        case ASTNode.METHOD_INVOCATION:
            MethodInvocation methodInvocation = (MethodInvocation) parent;
            IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
            if (methodBinding != null) {
                return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
            }
            break;
        case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            {
                SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
                IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
                if (superBinding != null) {
                    return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
                }
                break;
            }
        case ASTNode.CONSTRUCTOR_INVOCATION:
            {
                ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
                IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
                if (constrBinding != null) {
                    return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
                }
                break;
            }
        case ASTNode.CLASS_INSTANCE_CREATION:
            {
                ClassInstanceCreation creation = (ClassInstanceCreation) parent;
                IMethodBinding creationBinding = creation.resolveConstructorBinding();
                if (creationBinding != null) {
                    return getParameterTypeBinding(node, creation.arguments(), creationBinding);
                }
                break;
            }
        case ASTNode.PARENTHESIZED_EXPRESSION:
            return guessBindingForReference(parent);
        case ASTNode.ARRAY_ACCESS:
            if (((ArrayAccess) parent).getIndex().equals(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            } else {
                ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
                if (parentBinding == null) {
                    // $NON-NLS-1$
                    parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
                }
                return parentBinding.createArrayType(1);
            }
        case ASTNode.ARRAY_CREATION:
            if (((ArrayCreation) parent).dimensions().contains(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.ARRAY_INITIALIZER:
            ASTNode initializerParent = parent.getParent();
            int dim = 1;
            while (initializerParent instanceof ArrayInitializer) {
                initializerParent = initializerParent.getParent();
                dim++;
            }
            Type creationType = null;
            if (initializerParent instanceof ArrayCreation) {
                creationType = ((ArrayCreation) initializerParent).getType();
            } else if (initializerParent instanceof VariableDeclaration) {
                VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
                creationType = ASTNodes.getType(varDecl);
                dim -= varDecl.getExtraDimensions();
            } else if (initializerParent instanceof MemberValuePair) {
                String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
                IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
                if (annotMember != null) {
                    return getReducedDimensionBinding(annotMember.getReturnType(), dim);
                }
            }
            if (creationType instanceof ArrayType) {
                ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
                if (creationTypeBinding != null) {
                    return Bindings.getComponentType(creationTypeBinding, dim);
                }
            }
            break;
        case ASTNode.CONDITIONAL_EXPRESSION:
            ConditionalExpression expression = (ConditionalExpression) parent;
            if (node.equals(expression.getExpression())) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            if (node.equals(expression.getElseExpression())) {
                return expression.getThenExpression().resolveTypeBinding();
            }
            return expression.getElseExpression().resolveTypeBinding();
        case ASTNode.POSTFIX_EXPRESSION:
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("int");
        case ASTNode.PREFIX_EXPRESSION:
            if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("int");
        case ASTNode.IF_STATEMENT:
        case ASTNode.WHILE_STATEMENT:
        case ASTNode.DO_STATEMENT:
            if (node instanceof Expression) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            break;
        case ASTNode.SWITCH_STATEMENT:
            if (((SwitchStatement) parent).getExpression().equals(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.RETURN_STATEMENT:
            MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
            if (decl != null && !decl.isConstructor()) {
                return decl.getReturnType2().resolveBinding();
            }
            LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
            if (lambdaExpr != null) {
                IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
                if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
                    return lambdaMethodBinding.getReturnType();
                }
            }
            break;
        case ASTNode.CAST_EXPRESSION:
            return ((CastExpression) parent).getType().resolveBinding();
        case ASTNode.THROW_STATEMENT:
        case ASTNode.CATCH_CLAUSE:
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("java.lang.Exception");
        case ASTNode.FIELD_ACCESS:
            if (node.equals(((FieldAccess) parent).getName())) {
                return getPossibleReferenceBinding(parent);
            }
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            return getPossibleReferenceBinding(parent);
        case ASTNode.QUALIFIED_NAME:
            if (node.equals(((QualifiedName) parent).getName())) {
                return getPossibleReferenceBinding(parent);
            }
            break;
        case ASTNode.SWITCH_CASE:
            if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
                return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
            }
            break;
        case ASTNode.ASSERT_STATEMENT:
            if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("java.lang.String");
        case ASTNode.SINGLE_MEMBER_ANNOTATION:
            {
                // $NON-NLS-1$
                IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
                if (annotMember != null) {
                    return annotMember.getReturnType();
                }
                break;
            }
        case ASTNode.MEMBER_VALUE_PAIR:
            {
                String name = ((MemberValuePair) parent).getName().getIdentifier();
                IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
                if (annotMember != null) {
                    return annotMember.getReturnType();
                }
                break;
            }
        default:
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) 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) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Annotation(org.eclipse.jdt.core.dom.Annotation) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) 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) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 48 with PrefixExpression

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

the class NecessaryParenthesesChecker method needsParentheses.

/**
 * Does the <code>expression</code> need parentheses when inserted into <code>parent</code> at
 * <code>locationInParent</code> ?
 *
 * @param expression the expression
 * @param parent the parent node
 * @param locationInParent location of expression in the parent
 * @param leftOperandType the type of the left operand in <code>parent</code> if
 *            <code>parent</code> is an infix expression with no bindings and
 *            <code>expression</code> is the right operand in it, <code>null</code> otherwise
 * @return <code>true</code> if <code>expression</code> needs parentheses, <code>false</code>
 *         otherwise.
 *
 * @since 3.9
 */
private static boolean needsParentheses(Expression expression, ASTNode parent, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
    if (!expressionTypeNeedsParentheses(expression))
        return false;
    if (!locationNeedsParentheses(locationInParent)) {
        return false;
    }
    if (parent instanceof Expression) {
        Expression parentExpression = (Expression) parent;
        if (expression instanceof PrefixExpression) {
            // see bug 405096
            return needsParenthesesForPrefixExpression(parentExpression, ((PrefixExpression) expression).getOperator());
        }
        int expressionPrecedence = OperatorPrecedence.getExpressionPrecedence(expression);
        int parentPrecedence = OperatorPrecedence.getExpressionPrecedence(parentExpression);
        if (expressionPrecedence > parentPrecedence)
            // (opEx) opParent and opEx binds more -> parentheses not needed
            return false;
        if (expressionPrecedence < parentPrecedence)
            // (opEx) opParent and opEx binds less -> parentheses needed
            return true;
        if (parentExpression instanceof InfixExpression) {
            return needsParenthesesInInfixExpression(expression, (InfixExpression) parentExpression, locationInParent, leftOperandType);
        }
        if (parentExpression instanceof ConditionalExpression && locationInParent == ConditionalExpression.EXPRESSION_PROPERTY) {
            return true;
        }
        return false;
    }
    return true;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 49 with PrefixExpression

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

the class ObsoleteRemoveUnnecessaryCastCleanUp method createPrimitive.

private void createPrimitive(final CastExpression visited, final PrefixExpression.Operator prefixOperator, final NumberLiteral literal, final char postfix) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveUnnecessaryCastCleanUp_description);
    NumberLiteral numberLiteral = ast.newNumberLiteral(literal.getToken() + postfix);
    if (prefixOperator != null) {
        PrefixExpression prefixExpression = ast.newPrefixExpression();
        prefixExpression.setOperator(prefixOperator);
        prefixExpression.setOperand(numberLiteral);
        ASTNodes.replaceButKeepComment(rewrite, visited, prefixExpression, group);
    } else {
        ASTNodes.replaceButKeepComment(rewrite, visited, numberLiteral, group);
    }
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 50 with PrefixExpression

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

the class ObsoleteRemoveUnnecessaryCastCleanUp method visit.

@Override
public boolean visit(final CastExpression visited) {
    Expression expression = visited.getExpression();
    PrefixExpression prefixExpression = ASTNodes.as(expression, PrefixExpression.class);
    NumberLiteral literal = ASTNodes.as(expression, NumberLiteral.class);
    PrefixExpression.Operator prefixOperator = null;
    if (prefixExpression != null) {
        if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.MINUS)) {
            prefixOperator = PrefixExpression.Operator.MINUS;
            literal = ASTNodes.as(prefixExpression.getOperand(), NumberLiteral.class);
        } else if (ASTNodes.hasOperator(prefixExpression, PrefixExpression.Operator.PLUS)) {
            prefixOperator = PrefixExpression.Operator.PLUS;
            literal = ASTNodes.as(prefixExpression.getOperand(), NumberLiteral.class);
        } else {
            literal = null;
        }
    }
    if (literal != null && (literal.getToken().matches(".*[^lLdDfF]") || literal.getToken().matches("0x.*[^lL]"))) {
        // $NON-NLS-1$ //$NON-NLS-2$
        if (ASTNodes.hasType(visited.getType().resolveBinding(), long.class.getSimpleName())) {
            createPrimitive(visited, prefixOperator, literal, 'L');
            return false;
        }
        if (ASTNodes.hasType(visited.getType().resolveBinding(), float.class.getSimpleName())) {
            createPrimitive(visited, prefixOperator, literal, 'F');
            return false;
        }
        if (ASTNodes.hasType(visited.getType().resolveBinding(), double.class.getSimpleName())) {
            createPrimitive(visited, prefixOperator, literal, 'D');
            return false;
        }
    }
    if (canRemoveCast(visited)) {
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteRemoveUnnecessaryCastCleanUp_description);
        ASTNodes.replaceButKeepComment(rewrite, visited, ASTNodes.createMoveTarget(rewrite, visited.getExpression()), group);
        return false;
    }
    return true;
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Aggregations

PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)64 Expression (org.eclipse.jdt.core.dom.Expression)51 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)50 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)31 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)24 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)16 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)14 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 AST (org.eclipse.jdt.core.dom.AST)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Assignment (org.eclipse.jdt.core.dom.Assignment)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 Operator (org.eclipse.jdt.core.dom.InfixExpression.Operator)7 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6