Search in sources :

Example 1 with TypeChangeCorrectionProposal

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

Example 2 with TypeChangeCorrectionProposal

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

the class TypeMismatchSubProcessor method addIncompatibleReturnTypeProposals.

public static void addIncompatibleReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws JavaModelException {
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(selectedNode);
    if (decl == null) {
        return;
    }
    IMethodBinding methodDeclBinding = decl.resolveBinding();
    if (methodDeclBinding == null) {
        return;
    }
    ITypeBinding returnType = methodDeclBinding.getReturnType();
    IMethodBinding overridden = Bindings.findOverriddenMethod(methodDeclBinding, false);
    if (overridden == null || overridden.getReturnType() == returnType) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    IMethodBinding methodDecl = methodDeclBinding.getMethodDeclaration();
    ITypeBinding overriddenReturnType = overridden.getReturnType();
    if (!JavaModelUtil.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
        overriddenReturnType = overriddenReturnType.getErasure();
    }
    proposals.add(new TypeChangeCorrectionProposal(cu, methodDecl, astRoot, overriddenReturnType, false, IProposalRelevance.CHANGE_RETURN_TYPE));
    ICompilationUnit targetCu = cu;
    IMethodBinding overriddenDecl = overridden.getMethodDeclaration();
    ITypeBinding overridenDeclType = overriddenDecl.getDeclaringClass();
    if (overridenDeclType.isFromSource()) {
        targetCu = ASTResolving.findCompilationUnitForBinding(cu, astRoot, overridenDeclType);
        if (targetCu != null && ASTResolving.isUseableTypeInContext(returnType, overriddenDecl, false)) {
            TypeChangeCorrectionProposal proposal = new TypeChangeCorrectionProposal(targetCu, overriddenDecl, astRoot, returnType, false, IProposalRelevance.CHANGE_RETURN_TYPE_OF_OVERRIDDEN);
            if (overridenDeclType.isInterface()) {
                proposal.setDisplayName(Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturnofimplemented_description, BasicElementLabels.getJavaElementName(overriddenDecl.getName())));
            } else {
                proposal.setDisplayName(Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changereturnofoverridden_description, BasicElementLabels.getJavaElementName(overriddenDecl.getName())));
            }
            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) TypeChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.TypeChangeCorrectionProposal) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

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