Search in sources :

Example 31 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 32 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 33 with ParenthesizedExpression

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

the class ASTBuilder method parenthesize.

/**
 * Builds a new {@link ParenthesizedExpression} instance.
 *
 * @param expression the expression to wrap with parentheses
 * @return a new parenthesized expression
 */
public ParenthesizedExpression parenthesize(Expression expression) {
    final ParenthesizedExpression pe = ast.newParenthesizedExpression();
    pe.setExpression(expression);
    return pe;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression)

Aggregations

ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)33 CastExpression (org.eclipse.jdt.core.dom.CastExpression)22 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 Expression (org.eclipse.jdt.core.dom.Expression)18 AST (org.eclipse.jdt.core.dom.AST)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)13 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)12 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)7 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)7 Assignment (org.eclipse.jdt.core.dom.Assignment)6 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)4 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3