Search in sources :

Example 91 with ImportRewriteContext

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext in project eclipse.jdt.ls 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, TypeLocation.CAST);
    }
    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, TypeLocation.CAST);
                return newTypeNode;
            }
        }
    }
    // $NON-NLS-1$
    Type newCastType = ast.newSimpleType(ast.newSimpleName("Object"));
    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

ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)91 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)80 AST (org.eclipse.jdt.core.dom.AST)65 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)60 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)56 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)55 ASTNode (org.eclipse.jdt.core.dom.ASTNode)50 Type (org.eclipse.jdt.core.dom.Type)48 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)34 Expression (org.eclipse.jdt.core.dom.Expression)31 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)31 SimpleName (org.eclipse.jdt.core.dom.SimpleName)29 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)23 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)22 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)22 CastExpression (org.eclipse.jdt.core.dom.CastExpression)18 Javadoc (org.eclipse.jdt.core.dom.Javadoc)18 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)17