Search in sources :

Example 6 with InsertDescription

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.InsertDescription in project eclipse.jdt.ls by eclipse.

the class UnresolvedElementsSubProcessor method doMoreArguments.

private static void doMoreArguments(IInvocationContext context, ASTNode invocationNode, List<Expression> arguments, ITypeBinding[] argTypes, IMethodBinding methodRef, Collection<ChangeCorrectionProposal> 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[] { org.eclipse.jdt.ls.core.internal.corrections.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);
        }
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.REMOVE_ARGUMENTS);
        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;
        }
        ChangeMethodSignatureProposal.ChangeDescription[] changeDesc = new ChangeMethodSignatureProposal.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[] { org.eclipse.jdt.ls.core.internal.corrections.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);
            }
        }
        ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_ADD_PARAMETER);
        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) InsertDescription(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.InsertDescription) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ChangeDescription(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.ChangeDescription) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 ChangeDescription (org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.ChangeDescription)6 InsertDescription (org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.InsertDescription)6 ArrayList (java.util.ArrayList)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 RemoveDescription (org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeMethodSignatureProposal.RemoveDescription)4 List (java.util.List)2 AST (org.eclipse.jdt.core.dom.AST)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 CatchClause (org.eclipse.jdt.core.dom.CatchClause)2 EmptyStatement (org.eclipse.jdt.core.dom.EmptyStatement)2 Expression (org.eclipse.jdt.core.dom.Expression)2 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2