Search in sources :

Example 6 with ChangeDescription

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

Example 7 with ChangeDescription

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

the class UnresolvedElementsSubProcessor method createSignatureChangeDescription.

private static ChangeDescription[] createSignatureChangeDescription(int[] indexOfDiff, int nDiffs, ITypeBinding[] paramTypes, List<Expression> arguments, ITypeBinding[] argTypes) {
    ChangeDescription[] changeDesc = new ChangeDescription[paramTypes.length];
    for (int i = 0; i < nDiffs; i++) {
        int diffIndex = indexOfDiff[i];
        Expression arg = arguments.get(diffIndex);
        String name = getExpressionBaseName(arg);
        ITypeBinding argType = argTypes[diffIndex];
        if (argType.isWildcardType()) {
            argType = ASTResolving.normalizeWildcardType(argType, true, arg.getAST());
            if (argType == null) {
                return null;
            }
        }
        changeDesc[diffIndex] = new EditDescription(argType, name);
    }
    return changeDesc;
}
Also used : EditDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.EditDescription) 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)

Aggregations

ChangeDescription (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.ChangeDescription)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 ChangeMethodSignatureProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal)6 Image (org.eclipse.swt.graphics.Image)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 RemoveDescription (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.RemoveDescription)4 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3 Expression (org.eclipse.jdt.core.dom.Expression)3 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)3 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 EditDescription (org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.EditDescription)2 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)2 List (java.util.List)1