Search in sources :

Example 6 with ChangeMethodSignatureProposal

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

the class TypeMismatchSubProcessor method addIncompatibleThrowsProposals.

public static void addIncompatibleThrowsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws JavaModelException {
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    MethodDeclaration decl = (MethodDeclaration) selectedNode;
    IMethodBinding methodDeclBinding = decl.resolveBinding();
    if (methodDeclBinding == null) {
        return;
    }
    IMethodBinding overridden = Bindings.findOverriddenMethod(methodDeclBinding, false);
    if (overridden == null) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    ITypeBinding[] methodExceptions = methodDeclBinding.getExceptionTypes();
    ITypeBinding[] definedExceptions = overridden.getExceptionTypes();
    ArrayList<ITypeBinding> undeclaredExceptions = new ArrayList<ITypeBinding>();
    {
        ChangeDescription[] changes = new ChangeDescription[methodExceptions.length];
        for (int i = 0; i < methodExceptions.length; i++) {
            if (!isDeclaredException(methodExceptions[i], definedExceptions)) {
                changes[i] = new RemoveDescription();
                undeclaredExceptions.add(methodExceptions[i]);
            }
        }
        String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_removeexceptions_description, BasicElementLabels.getJavaElementName(methodDeclBinding.getName()));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_REMOVE);
        proposals.add(new ChangeMethodSignatureProposal(label, cu, astRoot, methodDeclBinding, null, changes, IProposalRelevance.REMOVE_EXCEPTIONS, image));
    }
    ITypeBinding declaringType = overridden.getDeclaringClass();
    ICompilationUnit targetCu = null;
    if (declaringType.isFromSource()) {
        targetCu = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringType);
    }
    if (targetCu != null) {
        ChangeDescription[] changes = new ChangeDescription[definedExceptions.length + undeclaredExceptions.size()];
        for (int i = 0; i < undeclaredExceptions.size(); i++) {
            //$NON-NLS-1$
            changes[i + definedExceptions.length] = new InsertDescription(undeclaredExceptions.get(i), "");
        }
        IMethodBinding overriddenDecl = overridden.getMethodDeclaration();
        String[] args = { BasicElementLabels.getJavaElementName(declaringType.getName()), BasicElementLabels.getJavaElementName(overridden.getName()) };
        String label = Messages.format(CorrectionMessages.TypeMismatchSubProcessor_addexceptions_description, args);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_ADD);
        proposals.add(new ChangeMethodSignatureProposal(label, targetCu, astRoot, overriddenDecl, null, changes, IProposalRelevance.ADD_EXCEPTIONS, image));
    }
}
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) ChangeMethodSignatureProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) InsertDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.InsertDescription) ArrayList(java.util.ArrayList) Image(org.eclipse.swt.graphics.Image) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ChangeDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.ChangeDescription) RemoveDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.RemoveDescription)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 ChangeMethodSignatureProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal)6 ChangeDescription (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.ChangeDescription)6 Image (org.eclipse.swt.graphics.Image)6 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 RemoveDescription (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.RemoveDescription)4 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)3 InsertDescription (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.InsertDescription)3 ArrayList (java.util.ArrayList)2 IType (org.eclipse.jdt.core.IType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 Expression (org.eclipse.jdt.core.dom.Expression)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)2 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)2 List (java.util.List)1 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)1