Search in sources :

Example 11 with ModifierChangeCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal in project flux by eclipse.

the class ModifierCorrectionSubProcessor method addMethodRequiresBodyProposals.

public static void addMethodRequiresBodyProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    AST ast = context.getASTRoot().getAST();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    MethodDeclaration decl = (MethodDeclaration) selectedNode;
    Modifier modifierNode;
    {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        modifierNode = removeModifier(decl, rewrite, Modifier.ABSTRACT);
        Block body = ast.newBlock();
        rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, body, null);
        if (!decl.isConstructor()) {
            Type returnType = decl.getReturnType2();
            if (returnType != null) {
                Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
                if (expression != null) {
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(expression);
                    body.statements().add(returnStatement);
                }
            }
        }
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_addmissingbody_description;
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_MISSING_BODY);
        proposals.add(proposal);
    }
    IMethodBinding binding = decl.resolveBinding();
    if (modifierNode == null && binding != null) {
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertoabstract_description, getMethodLabel(binding));
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        int included = binding.getDeclaringClass().isInterface() ? Modifier.NONE : Modifier.ABSTRACT;
        int excluded = Modifier.STATIC | Modifier.DEFAULT;
        ModifierChangeCorrectionProposal proposal = new ModifierChangeCorrectionProposal(label, cu, binding, decl, included, excluded, IProposalRelevance.ADD_ABSTRACT_MODIFIER);
        proposals.add(proposal);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 12 with ModifierChangeCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal in project flux by eclipse.

the class ModifierCorrectionSubProcessor method addNeedToEmulateProposal.

public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding instanceof IVariableBinding) {
        binding = ((IVariableBinding) binding).getVariableDeclaration();
        //			Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 13 with ModifierChangeCorrectionProposal

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

the class ModifierCorrectionSubProcessor method addNeedToEmulateProposal.

public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding instanceof IVariableBinding) {
        binding = ((IVariableBinding) binding).getVariableDeclaration();
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL, image));
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Image(org.eclipse.swt.graphics.Image)

Example 14 with ModifierChangeCorrectionProposal

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

the class ModifierCorrectionSubProcessor method addChangeOverriddenModifierProposal.

public static void addChangeOverriddenModifierProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int kind) throws JavaModelException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    IMethodBinding method = ((MethodDeclaration) selectedNode).resolveBinding();
    ITypeBinding curr = method.getDeclaringClass();
    if (kind == TO_VISIBLE && problem.getProblemId() != IProblem.OverridingNonVisibleMethod) {
        // e.g. IProblem.InheritedMethodReducesVisibility, IProblem.MethodReducesVisibility
        List<IMethodBinding> methods = Bindings.findOverriddenMethods(method, false, false);
        if (!methods.isEmpty()) {
            int includedModifiers = 0;
            for (IMethodBinding binding : methods) {
                int temp = JdtFlags.getVisibilityCode(binding);
                includedModifiers = JdtFlags.getHigherVisibility(temp, includedModifiers);
            }
            int excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
            String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodvisibility_description, new String[] { getVisibilityString(includedModifiers) });
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            proposals.add(new ModifierChangeCorrectionProposal(label, cu, method, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_1, image));
        }
    }
    IMethodBinding overriddenInClass = null;
    while (overriddenInClass == null && curr.getSuperclass() != null) {
        curr = curr.getSuperclass();
        overriddenInClass = Bindings.findOverriddenMethodInType(curr, method);
    }
    if (overriddenInClass != null) {
        final IMethodBinding overriddenDecl = overriddenInClass.getMethodDeclaration();
        final ICompilationUnit overriddenMethodCU = ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), overriddenDecl.getDeclaringClass());
        if (overriddenMethodCU != null) {
            //target method and compilation unit for the quick fix
            IMethodBinding targetMethod = overriddenDecl;
            ICompilationUnit targetCU = overriddenMethodCU;
            String label;
            int excludedModifiers;
            int includedModifiers;
            switch(kind) {
                case TO_VISIBLE:
                    if (JdtFlags.isPrivate(method)) {
                        // Propose to increase the visibility of this method, because decreasing to private is not possible.
                        targetMethod = method;
                        targetCU = cu;
                        excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                        includedModifiers = JdtFlags.getVisibilityCode(overriddenDecl);
                    } else if (JdtFlags.isPackageVisible(method) && !overriddenDecl.getDeclaringClass().getPackage().isEqualTo(method.getDeclaringClass().getPackage())) {
                        // method is package visible but not in the same package as overridden method
                        // propose to make the method protected
                        excludedModifiers = Modifier.PRIVATE;
                        includedModifiers = Modifier.PROTECTED;
                        // if it is already protected, ignore it
                        if (JdtFlags.isProtected(overriddenDecl)) {
                            return;
                        }
                    } else {
                        excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                        includedModifiers = JdtFlags.getVisibilityCode(method);
                        if (JdtFlags.getVisibilityCode(overriddenDecl) == JdtFlags.getVisibilityCode(method)) {
                            // don't propose the same visibility it already has
                            return;
                        }
                    }
                    label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changeoverriddenvisibility_description, new String[] { getMethodLabel(targetMethod), getVisibilityString(includedModifiers) });
                    break;
                case TO_NON_FINAL:
                    label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononfinal_description, getMethodLabel(targetMethod));
                    excludedModifiers = Modifier.FINAL;
                    includedModifiers = 0;
                    break;
                case TO_NON_STATIC:
                    label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, getMethodLabel(targetMethod));
                    excludedModifiers = Modifier.STATIC;
                    includedModifiers = 0;
                    break;
                default:
                    //$NON-NLS-1$
                    Assert.isTrue(false, "not supported");
                    return;
            }
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
            proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, targetMethod, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_2, image));
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Image(org.eclipse.swt.graphics.Image)

Example 15 with ModifierChangeCorrectionProposal

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

the class ModifierCorrectionSubProcessor method addAddMethodModifierProposal.

private static void addAddMethodModifierProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int modifier, String label) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    IBinding binding = ((MethodDeclaration) selectedNode).resolveBinding();
    if (binding instanceof IMethodBinding) {
        binding = ((IMethodBinding) binding).getMethodDeclaration();
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, modifier, 0, IProposalRelevance.ADD_METHOD_MODIFIER, image));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Image(org.eclipse.swt.graphics.Image)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 ModifierChangeCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal)16 IBinding (org.eclipse.jdt.core.dom.IBinding)10 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)10 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)10 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 Image (org.eclipse.swt.graphics.Image)8 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 AST (org.eclipse.jdt.core.dom.AST)4 Block (org.eclipse.jdt.core.dom.Block)4 Expression (org.eclipse.jdt.core.dom.Expression)4 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)4 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)4 SimpleType (org.eclipse.jdt.core.dom.SimpleType)4 Type (org.eclipse.jdt.core.dom.Type)4 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)4 IExtendedModifier (org.eclipse.jdt.core.dom.IExtendedModifier)3