Search in sources :

Example 1 with ImplementInterfaceProposal

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

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1 ImplementInterfaceProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.ImplementInterfaceProposal)1 TypeChangeCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.TypeChangeCorrectionProposal)1