Search in sources :

Example 1 with NewMethodCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal 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 2 with NewMethodCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal in project che by eclipse.

the class UnresolvedElementsSubProcessor method getConstructorProposals.

public static void getConstructorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    ITypeBinding targetBinding = null;
    List<Expression> arguments = null;
    IMethodBinding recursiveConstructor = null;
    int type = selectedNode.getNodeType();
    if (type == ASTNode.CLASS_INSTANCE_CREATION) {
        ClassInstanceCreation creation = (ClassInstanceCreation) selectedNode;
        IBinding binding = creation.getType().resolveBinding();
        if (binding instanceof ITypeBinding) {
            targetBinding = (ITypeBinding) binding;
            arguments = creation.arguments();
        }
    } else if (type == ASTNode.SUPER_CONSTRUCTOR_INVOCATION) {
        ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
        if (typeBinding != null && !typeBinding.isAnonymous()) {
            targetBinding = typeBinding.getSuperclass();
            arguments = ((SuperConstructorInvocation) selectedNode).arguments();
        }
    } else if (type == ASTNode.CONSTRUCTOR_INVOCATION) {
        ITypeBinding typeBinding = Bindings.getBindingOfParentType(selectedNode);
        if (typeBinding != null && !typeBinding.isAnonymous()) {
            targetBinding = typeBinding;
            arguments = ((ConstructorInvocation) selectedNode).arguments();
            recursiveConstructor = ASTResolving.findParentMethodDeclaration(selectedNode).resolveBinding();
        }
    }
    if (targetBinding == null) {
        return;
    }
    IMethodBinding[] methods = targetBinding.getDeclaredMethods();
    ArrayList<IMethodBinding> similarElements = new ArrayList<IMethodBinding>();
    for (int i = 0; i < methods.length; i++) {
        IMethodBinding curr = methods[i];
        if (curr.isConstructor() && recursiveConstructor != curr) {
            // similar elements can contain a implicit default constructor
            similarElements.add(curr);
        }
    }
    addParameterMissmatchProposals(context, problem, similarElements, selectedNode, arguments, proposals);
    if (targetBinding.isFromSource()) {
        ITypeBinding targetDecl = targetBinding.getTypeDeclaration();
        ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, targetDecl);
        if (targetCU != null) {
            String[] args = new String[] { ASTResolving.getMethodSignature(ASTResolving.getTypeSignature(targetDecl), getParameterTypes(arguments), false) };
            String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createconstructor_description, args);
            Image image = JavaElementImageProvider.getDecoratedImage(JavaPluginImages.DESC_MISC_PUBLIC, JavaElementImageDescriptor.CONSTRUCTOR, JavaElementImageProvider.SMALL_SIZE);
            proposals.add(new NewMethodCorrectionProposal(label, targetCU, selectedNode, arguments, targetDecl, IProposalRelevance.CREATE_CONSTRUCTOR, image));
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) Image(org.eclipse.swt.graphics.Image) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NewMethodCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 NewMethodCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.NewMethodCorrectionProposal)2 Image (org.eclipse.swt.graphics.Image)2 ArrayList (java.util.ArrayList)1 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 Expression (org.eclipse.jdt.core.dom.Expression)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1