Search in sources :

Example 11 with IMethodBinding

use of org.eclipse.jdt.core.dom.IMethodBinding 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 12 with IMethodBinding

use of org.eclipse.jdt.core.dom.IMethodBinding 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)

Example 13 with IMethodBinding

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

the class GenerateForLoopAssistProposal method getIteratorBasedForInitializer.

/**
	 * Generates the initializer for an iterator based <code>for</code> loop, which declares and
	 * initializes the variable to loop over.
	 * 
	 * @param rewrite the instance of {@link ASTRewrite}
	 * @param loopVariableName the proposed name of the loop variable
	 * @return a {@link VariableDeclarationExpression} to use as initializer
	 */
private VariableDeclarationExpression getIteratorBasedForInitializer(ASTRewrite rewrite, SimpleName loopVariableName) {
    AST ast = rewrite.getAST();
    //$NON-NLS-1$
    IMethodBinding iteratorMethodBinding = Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {});
    // initializing fragment
    VariableDeclarationFragment varDeclarationFragment = ast.newVariableDeclarationFragment();
    varDeclarationFragment.setName(loopVariableName);
    MethodInvocation iteratorExpression = ast.newMethodInvocation();
    iteratorExpression.setName(ast.newSimpleName(iteratorMethodBinding.getName()));
    iteratorExpression.setExpression((Expression) rewrite.createCopyTarget(fCurrentExpression));
    varDeclarationFragment.setInitializer(iteratorExpression);
    // declaration
    VariableDeclarationExpression varDeclarationExpression = ast.newVariableDeclarationExpression(varDeclarationFragment);
    varDeclarationExpression.setType(getImportRewrite().addImport(iteratorMethodBinding.getReturnType(), ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
    return varDeclarationExpression;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 14 with IMethodBinding

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

the class GenerateForLoopAssistProposal method extractElementType.

/**
	 * Extracts the type parameter of the variable contained in fCurrentExpression or the elements
	 * type to iterate over an array using <code>foreach</code>.
	 * 
	 * @param ast the current {@link AST} instance
	 * @return the {@link ITypeBinding} of the elements to iterate over
	 */
private ITypeBinding extractElementType(AST ast) {
    if (fExpressionType.isArray()) {
        return Bindings.normalizeForDeclarationUse(fExpressionType.getElementType(), ast);
    }
    // extract elements type directly out of the bindings
    //$NON-NLS-1$
    IMethodBinding iteratorMethodBinding = Bindings.findMethodInHierarchy(fExpressionType, "iterator", new ITypeBinding[] {});
    IMethodBinding iteratorNextMethodBinding = Bindings.findMethodInHierarchy(iteratorMethodBinding.getReturnType(), "next", //$NON-NLS-1$
    new ITypeBinding[] {});
    ITypeBinding currentElementBinding = iteratorNextMethodBinding.getReturnType();
    return Bindings.normalizeForDeclarationUse(currentElementBinding, ast);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 15 with IMethodBinding

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

the class TypeMismatchSubProcessor method addChangeSenderTypeProposals.

public static void addChangeSenderTypeProposals(IInvocationContext context, Expression nodeToCast, ITypeBinding castTypeBinding, boolean isAssignedNode, int relevance, Collection<ICommandAccess> proposals) throws JavaModelException {
    IBinding callerBinding = Bindings.resolveExpressionBinding(nodeToCast, false);
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ICompilationUnit targetCu = null;
    ITypeBinding declaringType = null;
    IBinding callerBindingDecl = callerBinding;
    if (callerBinding instanceof IVariableBinding) {
        IVariableBinding variableBinding = (IVariableBinding) callerBinding;
        if (variableBinding.isEnumConstant()) {
            return;
        }
        if (!variableBinding.isField()) {
            targetCu = cu;
        } else {
            callerBindingDecl = variableBinding.getVariableDeclaration();
            ITypeBinding declaringClass = variableBinding.getDeclaringClass();
            if (declaringClass == null) {
                // array length
                return;
            }
            declaringType = declaringClass.getTypeDeclaration();
        }
    } else if (callerBinding instanceof IMethodBinding) {
        IMethodBinding methodBinding = (IMethodBinding) callerBinding;
        if (!methodBinding.isConstructor()) {
            declaringType = methodBinding.getDeclaringClass().getTypeDeclaration();
            callerBindingDecl = methodBinding.getMethodDeclaration();
        }
    } else if (callerBinding instanceof ITypeBinding && nodeToCast.getLocationInParent() == SingleMemberAnnotation.TYPE_NAME_PROPERTY) {
        declaringType = (ITypeBinding) callerBinding;
        //$NON-NLS-1$
        callerBindingDecl = Bindings.findMethodInType(declaringType, "value", (String[]) null);
        if (callerBindingDecl == null) {
            return;
        }
    }
    if (declaringType != null && declaringType.isFromSource()) {
        targetCu = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringType);
    }
    if (targetCu != null && ASTResolving.isUseableTypeInContext(castTypeBinding, callerBindingDecl, false)) {
        proposals.add(new TypeChangeCorrectionProposal(targetCu, callerBindingDecl, astRoot, castTypeBinding, isAssignedNode, relevance));
    }
    // add interface to resulting type
    if (!isAssignedNode) {
        ITypeBinding nodeType = nodeToCast.resolveTypeBinding();
        if (castTypeBinding.isInterface() && nodeType != null && nodeType.isClass() && !nodeType.isAnonymous() && nodeType.isFromSource()) {
            ITypeBinding typeDecl = nodeType.getTypeDeclaration();
            ICompilationUnit nodeCu = ASTResolving.findCompilationUnitForBinding(cu, astRoot, typeDecl);
            if (nodeCu != null && ASTResolving.isUseableTypeInContext(castTypeBinding, typeDecl, true)) {
                proposals.add(new ImplementInterfaceProposal(nodeCu, typeDecl, astRoot, castTypeBinding, relevance - 1));
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImplementInterfaceProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ImplementInterfaceProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TypeChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.TypeChangeCorrectionProposal) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)164 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)103 ASTNode (org.eclipse.jdt.core.dom.ASTNode)46 Expression (org.eclipse.jdt.core.dom.Expression)34 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)28 ArrayList (java.util.ArrayList)27 IBinding (org.eclipse.jdt.core.dom.IBinding)24 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)20 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)20 CastExpression (org.eclipse.jdt.core.dom.CastExpression)19 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 SimpleName (org.eclipse.jdt.core.dom.SimpleName)19 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)18 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)18 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)17 Image (org.eclipse.swt.graphics.Image)16 AST (org.eclipse.jdt.core.dom.AST)15 Type (org.eclipse.jdt.core.dom.Type)15