Search in sources :

Example 21 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.

the class CastCorrectionProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    AST ast = fNodeToCast.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ImportRewrite importRewrite = createImportRewrite((CompilationUnit) fNodeToCast.getRoot());
    Type newTypeNode = getNewCastTypeNode(rewrite, importRewrite);
    if (fNodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) {
        CastExpression expression = (CastExpression) fNodeToCast;
        rewrite.replace(expression.getType(), newTypeNode, null);
    } else {
        Expression expressionCopy = (Expression) rewrite.createCopyTarget(fNodeToCast);
        if (needsInnerParantheses(fNodeToCast)) {
            ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
            parenthesizedExpression.setExpression(expressionCopy);
            expressionCopy = parenthesizedExpression;
        }
        CastExpression castExpression = ast.newCastExpression();
        castExpression.setExpression(expressionCopy);
        castExpression.setType(newTypeNode);
        ASTNode replacingNode = castExpression;
        if (needsOuterParantheses(fNodeToCast)) {
            ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
            parenthesizedExpression.setExpression(castExpression);
            replacingNode = parenthesizedExpression;
        }
        rewrite.replace(fNodeToCast, replacingNode, null);
    }
    return rewrite;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 22 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.

the class CastCorrectionProposal method getNewCastTypeNode.

private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) {
    AST ast = rewrite.getAST();
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite);
    if (fCastType != null) {
        return importRewrite.addImport(fCastType, ast, context);
    }
    ASTNode node = fNodeToCast;
    ASTNode parent = node.getParent();
    if (parent instanceof CastExpression) {
        node = parent;
        parent = parent.getParent();
    }
    while (parent instanceof ParenthesizedExpression) {
        node = parent;
        parent = parent.getParent();
    }
    if (parent instanceof MethodInvocation) {
        MethodInvocation invocation = (MethodInvocation) node.getParent();
        if (invocation.getExpression() == node) {
            IBinding targetContext = ASTResolving.getParentMethodOrTypeBinding(node);
            ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext);
            if (bindings.length > 0) {
                ITypeBinding first = getCastFavorite(bindings, fNodeToCast.resolveTypeBinding());
                Type newTypeNode = importRewrite.addImport(first, ast, context);
                //$NON-NLS-1$
                addLinkedPosition(rewrite.track(newTypeNode), true, "casttype");
                for (int i = 0; i < bindings.length; i++) {
                    //$NON-NLS-1$
                    addLinkedPositionProposal("casttype", bindings[i]);
                }
                return newTypeNode;
            }
        }
    }
    //$NON-NLS-1$
    Type newCastType = ast.newSimpleType(ast.newSimpleName("Object"));
    //$NON-NLS-1$
    addLinkedPosition(rewrite.track(newCastType), true, "casttype");
    return newCastType;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 23 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.

the class AccessAnalyzer method visit.

@Override
public boolean visit(Assignment node) {
    Expression leftHandSide = node.getLeftHandSide();
    if (!considerBinding(resolveBinding(leftHandSide), leftHandSide))
        return true;
    checkParent(node);
    Expression rightHandSide = node.getRightHandSide();
    if (!fIsFieldFinal) {
        // Write access.
        AST ast = node.getAST();
        MethodInvocation invocation = ast.newMethodInvocation();
        invocation.setName(ast.newSimpleName(fSetter));
        fReferencingSetter = true;
        Expression receiver = getReceiver(leftHandSide);
        if (receiver != null)
            invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
        List<Expression> arguments = invocation.arguments();
        if (node.getOperator() == Assignment.Operator.ASSIGN) {
            arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide));
        } else {
            // This is the compound assignment case: field+= 10;
            InfixExpression exp = ast.newInfixExpression();
            exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
            MethodInvocation getter = ast.newMethodInvocation();
            getter.setName(ast.newSimpleName(fGetter));
            fReferencingGetter = true;
            if (receiver != null)
                getter.setExpression((Expression) fRewriter.createCopyTarget(receiver));
            exp.setLeftOperand(getter);
            Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
            if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) {
                ParenthesizedExpression p = ast.newParenthesizedExpression();
                p.setExpression(rhs);
                rhs = p;
            }
            exp.setRightOperand(rhs);
            arguments.add(exp);
        }
        fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
    }
    rightHandSide.accept(this);
    return false;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) 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) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 24 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.

the class ExpressionsFix method createRemoveUnnecessaryParenthesisFix.

public static ExpressionsFix createRemoveUnnecessaryParenthesisFix(CompilationUnit compilationUnit, ASTNode[] nodes) {
    // check sub-expressions in fully covered nodes
    final ArrayList<ParenthesizedExpression> changedNodes = new ArrayList<ParenthesizedExpression>();
    for (int i = 0; i < nodes.length; i++) {
        ASTNode covered = nodes[i];
        if (covered instanceof ParenthesizedExpression || covered instanceof InfixExpression)
            covered.accept(new UnnecessaryParenthesisVisitor(changedNodes));
    }
    if (changedNodes.isEmpty())
        return null;
    HashSet<ParenthesizedExpression> expressions = new HashSet<ParenthesizedExpression>(changedNodes);
    RemoveParenthesisOperation op = new RemoveParenthesisOperation(expressions);
    return new ExpressionsFix(FixMessages.ExpressionsFix_removeUnnecessaryParentheses_description, compilationUnit, new CompilationUnitRewriteOperation[] { op });
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) HashSet(java.util.HashSet)

Example 25 with ParenthesizedExpression

use of org.eclipse.jdt.core.dom.ParenthesizedExpression in project che by eclipse.

the class UnusedCodeFix method replaceCast.

private static void replaceCast(CastExpression castExpression, Expression replacement, ASTRewrite rewrite, TextEditGroup group) {
    boolean castEnclosedInNecessaryParentheses = castExpression.getParent() instanceof ParenthesizedExpression && NecessaryParenthesesChecker.needsParentheses(castExpression, castExpression.getParent().getParent(), castExpression.getParent().getLocationInParent());
    ASTNode toReplace = castEnclosedInNecessaryParentheses ? castExpression.getParent() : castExpression;
    ASTNode move;
    if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
        if (replacement.getParent() instanceof ParenthesizedExpression) {
            move = rewrite.createMoveTarget(replacement.getParent());
        } else if (castEnclosedInNecessaryParentheses) {
            toReplace = castExpression;
            move = rewrite.createMoveTarget(replacement);
        } else {
            ParenthesizedExpression parentheses = replacement.getAST().newParenthesizedExpression();
            parentheses.setExpression((Expression) rewrite.createMoveTarget(replacement));
            move = parentheses;
        }
    } else {
        move = rewrite.createMoveTarget(replacement);
    }
    rewrite.replace(toReplace, move, group);
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) Expression(org.eclipse.jdt.core.dom.Expression) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)30 CastExpression (org.eclipse.jdt.core.dom.CastExpression)20 ASTNode (org.eclipse.jdt.core.dom.ASTNode)19 Expression (org.eclipse.jdt.core.dom.Expression)17 AST (org.eclipse.jdt.core.dom.AST)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)11 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)8 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 Assignment (org.eclipse.jdt.core.dom.Assignment)5 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)5 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)4 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3 StructuralPropertyDescriptor (org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)3 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)3 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)3