Search in sources :

Example 46 with ITypeBinding

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

the class UnresolvedElementsSubProcessor method addNewMethodProposals.

private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender, List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName, Collection<ICommandAccess> proposals) throws JavaModelException {
    ITypeBinding nodeParentType = Bindings.getBindingOfParentType(invocationNode);
    ITypeBinding binding = null;
    if (sender != null) {
        binding = sender.resolveTypeBinding();
    } else {
        binding = nodeParentType;
        if (isSuperInvocation && binding != null) {
            binding = binding.getSuperclass();
        }
    }
    if (binding != null && binding.isFromSource()) {
        ITypeBinding senderDeclBinding = binding.getTypeDeclaration();
        ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
        if (targetCU != null) {
            String label;
            Image image;
            ITypeBinding[] parameterTypes = getParameterTypes(arguments);
            if (parameterTypes != null) {
                String sig = ASTResolving.getMethodSignature(methodName, parameterTypes, false);
                if (ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
                    if (nodeParentType == senderDeclBinding) {
                        label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig);
                        image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE);
                    } else {
                        label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) });
                        image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
                    }
                    proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
                }
                if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings.findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) {
                    // no covering method
                    ASTNode anonymDecl = astRoot.findDeclaringNode(senderDeclBinding);
                    if (anonymDecl != null) {
                        senderDeclBinding = Bindings.getBindingOfParentType(anonymDecl.getParent());
                        if (!senderDeclBinding.isAnonymous() && ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
                            String[] args = new String[] { sig, ASTResolving.getTypeSignature(senderDeclBinding) };
                            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args);
                            image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PROTECTED);
                            proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
                        }
                    }
                }
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NewMethodCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal) Image(org.eclipse.swt.graphics.Image)

Example 47 with ITypeBinding

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

the class UnresolvedElementsSubProcessor method addSimilarTypeProposals.

private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
    SimilarElement[] elements = SimilarElementsRequestor.findSimilarElement(cu, node, kind);
    // try to resolve type in context -> highest severity
    String resolvedTypeName = null;
    ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
    if (binding != null) {
        ITypeBinding simpleBinding = binding;
        if (simpleBinding.isArray()) {
            simpleBinding = simpleBinding.getElementType();
        }
        simpleBinding = simpleBinding.getTypeDeclaration();
        if (!simpleBinding.isRecovered()) {
            resolvedTypeName = simpleBinding.getQualifiedName();
            CUCorrectionProposal proposal = createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
            proposals.add(proposal);
            if (proposal instanceof AddImportCorrectionProposal)
                proposal.setRelevance(relevance + elements.length + 2);
            if (binding.isParameterizedType() && (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType) && !(node.getParent().getParent() instanceof Type)) {
                proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
            }
        }
    } else {
        ASTNode normalizedNode = ASTNodes.getNormalizedNode(node);
        if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
            ITypeBinding normBinding = ASTResolving.guessBindingForTypeReference(normalizedNode);
            if (normBinding != null && !normBinding.isRecovered()) {
                proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
            }
        }
    }
    // add all similar elements
    for (int i = 0; i < elements.length; i++) {
        SimilarElement elem = elements[i];
        if ((elem.getKind() & SimilarElementsRequestor.ALL_TYPES) != 0) {
            String fullName = elem.getName();
            if (!fullName.equals(resolvedTypeName)) {
                proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
            }
        }
    }
}
Also used : AddImportCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal) SimpleType(org.eclipse.jdt.core.dom.SimpleType) IType(org.eclipse.jdt.core.IType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Example 48 with ITypeBinding

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

the class UnresolvedElementsSubProcessor method addParameterMissmatchProposals.

private static void addParameterMissmatchProposals(IInvocationContext context, IProblemLocation problem, List<IMethodBinding> similarElements, ASTNode invocationNode, List<Expression> arguments, Collection<ICommandAccess> proposals) throws CoreException {
    int nSimilarElements = similarElements.size();
    ITypeBinding[] argTypes = getArgumentTypes(arguments);
    if (argTypes == null || nSimilarElements == 0) {
        return;
    }
    for (int i = 0; i < nSimilarElements; i++) {
        IMethodBinding elem = similarElements.get(i);
        int diff = elem.getParameterTypes().length - argTypes.length;
        if (diff == 0) {
            int nProposals = proposals.size();
            doEqualNumberOfParameters(context, invocationNode, problem, arguments, argTypes, elem, proposals);
            if (nProposals != proposals.size()) {
                // only suggest for one method (avoid duplicated proposals)
                return;
            }
        } else if (diff > 0) {
            doMoreParameters(context, invocationNode, argTypes, elem, proposals);
        } else {
            doMoreArguments(context, invocationNode, arguments, argTypes, elem, proposals);
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 49 with ITypeBinding

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

the class UnresolvedElementsSubProcessor method doMoreArguments.

private static void doMoreArguments(IInvocationContext context, ASTNode invocationNode, List<Expression> arguments, ITypeBinding[] argTypes, IMethodBinding methodRef, Collection<ICommandAccess> proposals) throws CoreException {
    ITypeBinding[] paramTypes = methodRef.getParameterTypes();
    int k = 0, nSkipped = 0;
    int diff = argTypes.length - paramTypes.length;
    int[] indexSkipped = new int[diff];
    for (int i = 0; i < argTypes.length; i++) {
        if (k < paramTypes.length && canAssign(argTypes[i], paramTypes[k])) {
            // match
            k++;
        } else {
            if (nSkipped >= diff) {
                // too different
                return;
            }
            indexSkipped[nSkipped++] = i;
        }
    }
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    // remove arguments
    {
        ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
        for (int i = diff - 1; i >= 0; i--) {
            rewrite.remove(arguments.get(indexSkipped[i]), null);
        }
        String[] arg = new String[] { ASTResolving.getMethodSignature(methodRef) };
        String label;
        if (diff == 1) {
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeargument_description, arg);
        } else {
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removearguments_description, arg);
        }
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_ARGUMENTS, image);
        proposals.add(proposal);
    }
    IMethodBinding methodDecl = methodRef.getMethodDeclaration();
    ITypeBinding declaringType = methodDecl.getDeclaringClass();
    // add parameters
    if (!declaringType.isFromSource()) {
        return;
    }
    ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringType);
    if (targetCU != null) {
        if (isImplicitConstructor(methodDecl)) {
            return;
        }
        ChangeDescription[] changeDesc = new ChangeDescription[argTypes.length];
        ITypeBinding[] changeTypes = new ITypeBinding[diff];
        for (int i = diff - 1; i >= 0; i--) {
            int idx = indexSkipped[i];
            Expression arg = arguments.get(idx);
            String name = getExpressionBaseName(arg);
            ITypeBinding newType = Bindings.normalizeTypeBinding(argTypes[idx]);
            if (newType == null) {
                //$NON-NLS-1$
                newType = astRoot.getAST().resolveWellKnownType("java.lang.Object");
            }
            if (newType.isWildcardType()) {
                newType = ASTResolving.normalizeWildcardType(newType, true, astRoot.getAST());
            }
            if (!ASTResolving.isUseableTypeInContext(newType, methodDecl, false)) {
                return;
            }
            changeDesc[idx] = new InsertDescription(newType, name);
            changeTypes[i] = newType;
        }
        String[] arg = new String[] { ASTResolving.getMethodSignature(methodDecl), getTypeNames(changeTypes) };
        String label;
        if (methodDecl.isConstructor()) {
            if (diff == 1) {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparam_constr_description, arg);
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparams_constr_description, arg);
            }
        } else {
            if (diff == 1) {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparam_description, arg);
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addparams_description, arg);
            }
        }
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
        ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_ADD_PARAMETER, image);
        proposals.add(proposal);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ChangeMethodSignatureProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal) InsertDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.InsertDescription) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) 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) ChangeDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.ChangeDescription) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 50 with ITypeBinding

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

the class UnresolvedElementsSubProcessor method doMoreParameters.

private static void doMoreParameters(IInvocationContext context, ASTNode invocationNode, ITypeBinding[] argTypes, IMethodBinding methodBinding, Collection<ICommandAccess> proposals) throws CoreException {
    ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
    int k = 0, nSkipped = 0;
    int diff = paramTypes.length - argTypes.length;
    int[] indexSkipped = new int[diff];
    for (int i = 0; i < paramTypes.length; i++) {
        if (k < argTypes.length && canAssign(argTypes[k], paramTypes[i])) {
            // match
            k++;
        } else {
            if (nSkipped >= diff) {
                // too different
                return;
            }
            indexSkipped[nSkipped++] = i;
        }
    }
    ITypeBinding declaringType = methodBinding.getDeclaringClass();
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    // add arguments
    {
        String[] arg = new String[] { ASTResolving.getMethodSignature(methodBinding) };
        String label;
        if (diff == 1) {
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addargument_description, arg);
        } else {
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addarguments_description, arg);
        }
        AddArgumentCorrectionProposal proposal = new AddArgumentCorrectionProposal(label, context.getCompilationUnit(), invocationNode, indexSkipped, paramTypes, IProposalRelevance.ADD_ARGUMENTS);
        proposal.setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD));
        proposals.add(proposal);
    }
    // remove parameters
    if (!declaringType.isFromSource()) {
        return;
    }
    ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringType);
    if (targetCU != null) {
        IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
        ITypeBinding[] declParameterTypes = methodDecl.getParameterTypes();
        ChangeDescription[] changeDesc = new ChangeDescription[declParameterTypes.length];
        ITypeBinding[] changedTypes = new ITypeBinding[diff];
        for (int i = diff - 1; i >= 0; i--) {
            int idx = indexSkipped[i];
            changeDesc[idx] = new RemoveDescription();
            changedTypes[i] = declParameterTypes[idx];
        }
        String[] arg = new String[] { ASTResolving.getMethodSignature(methodDecl), getTypeNames(changedTypes) };
        String label;
        if (methodDecl.isConstructor()) {
            if (diff == 1) {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparam_constr_description, arg);
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparams_constr_description, arg);
            }
        } else {
            if (diff == 1) {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparam_description, arg);
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_removeparams_description, arg);
            }
        }
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE);
        ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_REMOVE_PARAMETER, image);
        proposals.add(proposal);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ChangeMethodSignatureProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal) AddArgumentCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddArgumentCorrectionProposal) Image(org.eclipse.swt.graphics.Image) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ChangeDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.ChangeDescription) RemoveDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.RemoveDescription)

Aggregations

ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)388 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)110 ASTNode (org.eclipse.jdt.core.dom.ASTNode)72 Expression (org.eclipse.jdt.core.dom.Expression)69 Type (org.eclipse.jdt.core.dom.Type)55 SimpleName (org.eclipse.jdt.core.dom.SimpleName)52 ArrayList (java.util.ArrayList)50 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)49 AST (org.eclipse.jdt.core.dom.AST)43 IBinding (org.eclipse.jdt.core.dom.IBinding)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)36 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 CastExpression (org.eclipse.jdt.core.dom.CastExpression)33 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)32 Name (org.eclipse.jdt.core.dom.Name)31 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)29 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)29 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)26