Search in sources :

Example 26 with CastExpression

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

use of org.eclipse.jdt.core.dom.CastExpression 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)

Aggregations

CastExpression (org.eclipse.jdt.core.dom.CastExpression)27 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)20 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 Expression (org.eclipse.jdt.core.dom.Expression)17 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)12 AST (org.eclipse.jdt.core.dom.AST)11 Type (org.eclipse.jdt.core.dom.Type)10 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)9 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)8 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)8 Assignment (org.eclipse.jdt.core.dom.Assignment)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)7 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 InstanceofExpression (org.eclipse.jdt.core.dom.InstanceofExpression)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)6 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)6