Search in sources :

Example 46 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class TypeMismatchSubProcessor method addTypeMismatchInForEachProposals.

public static void addTypeMismatchInForEachProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null || selectedNode.getLocationInParent() != EnhancedForStatement.EXPRESSION_PROPERTY) {
        return;
    }
    EnhancedForStatement forStatement = (EnhancedForStatement) selectedNode.getParent();
    ITypeBinding expressionBinding = forStatement.getExpression().resolveTypeBinding();
    if (expressionBinding == null) {
        return;
    }
    ITypeBinding expectedBinding;
    if (expressionBinding.isArray()) {
        expectedBinding = expressionBinding.getComponentType();
    } else {
        //$NON-NLS-1$
        IMethodBinding iteratorMethod = Bindings.findMethodInHierarchy(expressionBinding, "iterator", new String[0]);
        if (iteratorMethod == null) {
            return;
        }
        ITypeBinding[] typeArguments = iteratorMethod.getReturnType().getTypeArguments();
        if (typeArguments.length != 1) {
            return;
        }
        expectedBinding = typeArguments[0];
    }
    AST ast = astRoot.getAST();
    expectedBinding = Bindings.normalizeForDeclarationUse(expectedBinding, ast);
    SingleVariableDeclaration parameter = forStatement.getParameter();
    ICompilationUnit cu = context.getCompilationUnit();
    if (parameter.getName().getLength() == 0) {
        SimpleName simpleName = null;
        if (parameter.getType() instanceof SimpleType) {
            SimpleType type = (SimpleType) parameter.getType();
            if (type.getName() instanceof SimpleName) {
                simpleName = (SimpleName) type.getName();
            }
        } else if (parameter.getType() instanceof NameQualifiedType) {
            simpleName = ((NameQualifiedType) parameter.getType()).getName();
        }
        if (simpleName != null) {
            String name = simpleName.getIdentifier();
            int relevance = StubUtility.hasLocalVariableName(cu.getJavaProject(), name) ? 10 : 7;
            String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_create_loop_variable_description, BasicElementLabels.getJavaElementName(name));
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
            proposals.add(new NewVariableCorrectionProposal(label, cu, NewVariableCorrectionProposal.LOCAL, simpleName, null, relevance, image));
            return;
        }
    }
    String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_incompatible_for_each_type_description, new String[] { BasicElementLabels.getJavaElementName(parameter.getName().getIdentifier()), BindingLabelProvider.getBindingLabel(expectedBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS) });
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.INCOMPATIBLE_FOREACH_TYPE, image);
    ImportRewrite importRewrite = proposal.createImportRewrite(astRoot);
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(ASTResolving.findParentBodyDeclaration(selectedNode), importRewrite);
    Type newType = importRewrite.addImport(expectedBinding, ast, importRewriteContext);
    rewrite.replace(parameter.getType(), newType, null);
    proposals.add(proposal);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NewVariableCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewVariableCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Example 47 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class UnresolvedElementsSubProcessor method addQualifierToOuterProposal.

private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode, IMethodBinding binding, Collection<ICommandAccess> proposals) {
    ITypeBinding declaringType = binding.getDeclaringClass();
    ITypeBinding parentType = Bindings.getBindingOfParentType(invocationNode);
    ITypeBinding currType = parentType;
    boolean isInstanceMethod = !Modifier.isStatic(binding.getModifiers());
    while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
        if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
            return;
        }
        currType = currType.getDeclaringClass();
    }
    if (currType == null || currType == parentType) {
        return;
    }
    ASTRewrite rewrite = ASTRewrite.create(invocationNode.getAST());
    String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description, ASTResolving.getTypeSignature(currType));
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE, image);
    ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(invocationNode, imports);
    AST ast = invocationNode.getAST();
    String qualifier = imports.addImport(currType, importRewriteContext);
    Name name = ASTNodeFactory.newName(ast, qualifier);
    Expression newExpression;
    if (isInstanceMethod) {
        ThisExpression expr = ast.newThisExpression();
        expr.setQualifier(name);
        newExpression = expr;
    } else {
        newExpression = name;
    }
    rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);
    proposals.add(proposal);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Image(org.eclipse.swt.graphics.Image) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 48 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class UnresolvedElementsSubProcessor method createTypeRefChangeProposal.

private static CUCorrectionProposal createTypeRefChangeProposal(ICompilationUnit cu, String fullName, Name node, int relevance, int maxProposals) {
    ImportRewrite importRewrite = null;
    String simpleName = fullName;
    String packName = Signature.getQualifier(fullName);
    if (packName.length() > 0) {
        // no imports for primitive types, type variables
        importRewrite = StubUtility.createImportRewrite((CompilationUnit) node.getRoot(), true);
        // can be null in package-info.java
        BodyDeclaration scope = ASTResolving.findParentBodyDeclaration(node);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(scope != null ? scope : node, importRewrite);
        simpleName = importRewrite.addImport(fullName, context);
    }
    if (!isLikelyTypeName(simpleName)) {
        relevance -= 2;
    }
    ASTRewriteCorrectionProposal proposal;
    if (importRewrite != null && node.isSimpleName() && simpleName.equals(((SimpleName) node).getIdentifier())) {
        // import only
        // import only
        String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description, arg);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
        int boost = QualifiedTypeNameHistory.getBoost(fullName, 0, maxProposals);
        proposal = new AddImportCorrectionProposal(label, cu, relevance + 100 + boost, image, packName, simpleName, (SimpleName) node);
        proposal.setCommandId(ADD_IMPORT_ID);
    } else {
        String label;
        if (packName.length() == 0) {
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_nopack_description, BasicElementLabels.getJavaElementName(simpleName));
        } else {
            String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_description, arg);
        }
        ASTRewrite rewrite = ASTRewrite.create(node.getAST());
        rewrite.replace(node, rewrite.createStringPlaceholder(simpleName, ASTNode.SIMPLE_TYPE), null);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance, image);
    }
    if (importRewrite != null) {
        proposal.setImportRewrite(importRewrite);
    }
    return proposal;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AddImportCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 49 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class QuickAssistProcessor method getArrayInitializerToArrayCreation.

private static boolean getArrayInitializerToArrayCreation(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof ArrayInitializer)) {
        return false;
    }
    ArrayInitializer initializer = (ArrayInitializer) node;
    ASTNode parent = initializer.getParent();
    while (parent instanceof ArrayInitializer) {
        initializer = (ArrayInitializer) parent;
        parent = parent.getParent();
    }
    ITypeBinding typeBinding = initializer.resolveTypeBinding();
    if (!(parent instanceof VariableDeclaration) || typeBinding == null || !typeBinding.isArray()) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    String label = CorrectionMessages.QuickAssistProcessor_typetoarrayInitializer_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_TYPE_TO_ARRAY_INITIALIZER, image);
    ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, imports);
    String typeName = imports.addImport(typeBinding, importRewriteContext);
    ArrayCreation creation = ast.newArrayCreation();
    creation.setInitializer((ArrayInitializer) rewrite.createMoveTarget(initializer));
    creation.setType((ArrayType) ASTNodeFactory.newType(ast, typeName));
    rewrite.replace(initializer, creation, null);
    resultingCollections.add(proposal);
    return true;
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Image(org.eclipse.swt.graphics.Image) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 50 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext 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

ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)57 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)47 AST (org.eclipse.jdt.core.dom.AST)33 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)32 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)30 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)23 ASTNode (org.eclipse.jdt.core.dom.ASTNode)21 Type (org.eclipse.jdt.core.dom.Type)19 SimpleName (org.eclipse.jdt.core.dom.SimpleName)18 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)17 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)15 Image (org.eclipse.swt.graphics.Image)14 Expression (org.eclipse.jdt.core.dom.Expression)13 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)12 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)11 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8