Search in sources :

Example 1 with ModifierChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class ModifierCorrectionSubProcessor method addAbstractMethodProposals.

public static void addAbstractMethodProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    MethodDeclaration decl;
    if (selectedNode instanceof SimpleName) {
        decl = (MethodDeclaration) selectedNode.getParent();
    } else if (selectedNode instanceof MethodDeclaration) {
        decl = (MethodDeclaration) selectedNode;
    } else {
        return;
    }
    ASTNode parentType = ASTResolving.findParentType(decl);
    TypeDeclaration parentTypeDecl = null;
    boolean parentIsAbstractClass = false;
    boolean parentIsInterface = false;
    if (parentType instanceof TypeDeclaration) {
        parentTypeDecl = (TypeDeclaration) parentType;
        parentIsAbstractClass = !parentTypeDecl.isInterface() && Modifier.isAbstract(parentTypeDecl.getModifiers());
        parentIsInterface = parentTypeDecl.isInterface();
    }
    boolean hasNoBody = decl.getBody() == null;
    int id = problem.getProblemId();
    if (id == IProblem.AbstractMethodInAbstractClass || id == IProblem.EnumAbstractMethodMustBeImplemented || id == IProblem.AbstractMethodInEnum || parentIsAbstractClass) {
        AST ast = astRoot.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        removeModifier(decl, rewrite, Modifier.ABSTRACT);
        if (hasNoBody) {
            Block newBody = ast.newBlock();
            rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, newBody, null);
            Type returnType = decl.getReturnType2();
            if (returnType != null) {
                Expression expr = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
                if (expr != null) {
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(expr);
                    newBody.statements().add(returnStatement);
                }
            }
        }
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_removeabstract_description;
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.REMOVE_ABSTRACT_MODIFIER);
        proposals.add(proposal);
    }
    if (!hasNoBody && id == IProblem.BodyForAbstractMethod) {
        AST ast = decl.getAST();
        {
            ASTRewrite rewrite = ASTRewrite.create(ast);
            rewrite.remove(decl.getBody(), null);
            int excluded;
            if (parentIsInterface) {
                excluded = ~(Modifier.PUBLIC | Modifier.ABSTRACT);
            } else {
                excluded = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.ABSTRACT);
            }
            ModifierRewrite.create(rewrite, decl).setModifiers(0, excluded, null);
            String label = CorrectionMessages.ModifierCorrectionSubProcessor_removebody_description;
            ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.REMOVE_METHOD_BODY);
            proposals.add(proposal);
        }
        if (JavaModelUtil.is1d8OrHigher(cu.getJavaProject()) && parentIsInterface) {
            {
                // insert proposal to add static modifier
                String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, decl.getName());
                int included = Modifier.STATIC;
                int excluded = Modifier.ABSTRACT | Modifier.DEFAULT;
                proposals.add(new ModifierChangeCorrectionProposal(label, cu, decl.resolveBinding(), decl, included, excluded, IProposalRelevance.ADD_STATIC_MODIFIER));
            }
            {
                // insert proposal to add default modifier
                String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertodefault_description, decl.getName());
                int included = Modifier.DEFAULT;
                int excluded = Modifier.ABSTRACT | Modifier.STATIC;
                proposals.add(new ModifierChangeCorrectionProposal(label, cu, decl.resolveBinding(), decl, included, excluded, IProposalRelevance.ADD_DEFAULT_MODIFIER));
            }
        }
    }
    if (id == IProblem.AbstractMethodInAbstractClass && parentTypeDecl != null) {
        addMakeTypeAbstractProposal(context, parentTypeDecl, proposals);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTRewriteCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.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.ls.core.internal.corrections.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) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 2 with ModifierChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class ModifierCorrectionSubProcessor method addSealedMissingModifierProposal.

public static void addSealedMissingModifierProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    if (proposals == null) {
        return;
    }
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    if (!(((SimpleName) selectedNode).getParent() instanceof TypeDeclaration)) {
        return;
    }
    TypeDeclaration typeDecl = (TypeDeclaration) ((SimpleName) selectedNode).getParent();
    boolean isInterface = typeDecl.isInterface();
    ICompilationUnit cu = context.getCompilationUnit();
    ITypeBinding typeDeclBinding = typeDecl.resolveBinding();
    int relevance = IProposalRelevance.CHANGE_MODIFIER_TO_FINAL;
    String label;
    if (!isInterface) {
        // Add final modifier
        label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifierto_final_description, typeDecl.getName());
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, typeDeclBinding, typeDecl, Modifier.FINAL, 0, relevance));
    }
    // Add sealed modifier
    label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifierto_sealed_description, typeDecl.getName());
    proposals.add(new ModifierChangeCorrectionProposal(label, cu, typeDeclBinding, typeDecl, Modifier.SEALED, 0, relevance));
    // Add non-sealed modifier
    label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifierto_nonsealed_description, typeDecl.getName());
    proposals.add(new ModifierChangeCorrectionProposal(label, cu, typeDeclBinding, typeDecl, Modifier.NON_SEALED, 0, relevance));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 3 with ModifierChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.

public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals, int kind, int relevance) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    IBinding binding = null;
    switch(selectedNode.getNodeType()) {
        case ASTNode.SIMPLE_NAME:
            binding = ((SimpleName) selectedNode).resolveBinding();
            break;
        case ASTNode.QUALIFIED_NAME:
            binding = ((QualifiedName) selectedNode).resolveBinding();
            break;
        case ASTNode.SIMPLE_TYPE:
            binding = ((SimpleType) selectedNode).resolveBinding();
            break;
        case ASTNode.NAME_QUALIFIED_TYPE:
            binding = ((NameQualifiedType) selectedNode).resolveBinding();
            break;
        case ASTNode.METHOD_INVOCATION:
            binding = ((MethodInvocation) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.SUPER_METHOD_INVOCATION:
            binding = ((SuperMethodInvocation) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.FIELD_ACCESS:
            binding = ((FieldAccess) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            binding = ((SuperFieldAccess) selectedNode).getName().resolveBinding();
            break;
        case ASTNode.CLASS_INSTANCE_CREATION:
            binding = ((ClassInstanceCreation) selectedNode).resolveConstructorBinding();
            break;
        case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            binding = ((SuperConstructorInvocation) selectedNode).resolveConstructorBinding();
            break;
        default:
            return;
    }
    ITypeBinding typeBinding = null;
    String name;
    IBinding bindingDecl;
    boolean isLocalVar = false;
    if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
        binding = ((IVariableBinding) binding).getType();
    }
    if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
        binding = ((IMethodBinding) binding).getReturnType();
    }
    if (binding instanceof IMethodBinding) {
        IMethodBinding methodDecl = (IMethodBinding) binding;
        if (methodDecl.isDefaultConstructor()) {
            UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
            return;
        }
        bindingDecl = methodDecl.getMethodDeclaration();
        typeBinding = methodDecl.getDeclaringClass();
        // $NON-NLS-1$
        name = BasicElementLabels.getJavaElementName(methodDecl.getName() + "()");
    } else if (binding instanceof IVariableBinding) {
        IVariableBinding varDecl = (IVariableBinding) binding;
        typeBinding = varDecl.getDeclaringClass();
        name = BasicElementLabels.getJavaElementName(binding.getName());
        isLocalVar = !varDecl.isField();
        bindingDecl = varDecl.getVariableDeclaration();
    } else if (binding instanceof ITypeBinding) {
        typeBinding = (ITypeBinding) binding;
        bindingDecl = typeBinding.getTypeDeclaration();
        name = BasicElementLabels.getJavaElementName(binding.getName());
    } else {
        return;
    }
    if (typeBinding != null && typeBinding.isFromSource() || isLocalVar) {
        int includedModifiers = 0;
        int excludedModifiers = 0;
        String label;
        switch(kind) {
            case TO_VISIBLE:
                excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                includedModifiers = getNeededVisibility(selectedNode, typeBinding, binding);
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
                break;
            case TO_STATIC:
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, name);
                includedModifiers = Modifier.STATIC;
                if (bindingDecl.getKind() == IBinding.METHOD) {
                    excludedModifiers = Modifier.DEFAULT | Modifier.ABSTRACT;
                }
                break;
            case TO_NON_STATIC:
                if (typeBinding != null && typeBinding.isInterface()) {
                    return;
                }
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononstatic_description, name);
                excludedModifiers = Modifier.STATIC;
                break;
            case TO_NON_PRIVATE:
                int visibility;
                if (cu.getParent().getElementName().equals(typeBinding.getPackage().getName())) {
                    visibility = Modifier.NONE;
                    excludedModifiers = Modifier.PRIVATE;
                } else {
                    visibility = Modifier.PUBLIC;
                    includedModifiers = Modifier.PUBLIC;
                    excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
                }
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(visibility) });
                break;
            case TO_NON_FINAL:
                if (typeBinding != null && typeBinding.isInterface()) {
                    return;
                }
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononfinal_description, name);
                excludedModifiers = Modifier.FINAL;
                break;
            default:
                // $NON-NLS-1$
                throw new IllegalArgumentException("not supported");
        }
        ICompilationUnit targetCU = isLocalVar ? cu : ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), typeBinding.getTypeDeclaration());
        if (targetCU != null) {
            proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, bindingDecl, selectedNode, includedModifiers, excludedModifiers, relevance));
        }
    }
    if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
        UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IBinding(org.eclipse.jdt.core.dom.IBinding) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess)

Example 4 with ModifierChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class ModifierCorrectionSubProcessor method addRemoveInvalidModifiersProposal.

// 
// public static void addNonFinalLocalProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ICommandAccess> 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_TO_FINAL, image));
// }
// }
// 
public static void addRemoveInvalidModifiersProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals, int relevance) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof MethodDeclaration) {
        selectedNode = ((MethodDeclaration) selectedNode).getName();
    }
    if (!(selectedNode instanceof SimpleName)) {
        return;
    }
    IBinding binding = ((SimpleName) selectedNode).resolveBinding();
    if (binding != null) {
        String methodName = BasicElementLabels.getJavaElementName(binding.getName());
        String label = null;
        int problemId = problem.getProblemId();
        int excludedModifiers = 0;
        int includedModifiers = 0;
        switch(problemId) {
            case IProblem.CannotHideAnInstanceMethodWithAStaticMethod:
            case IProblem.UnexpectedStaticModifierForMethod:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemethodtononstatic_description, methodName);
                break;
            case IProblem.UnexpectedStaticModifierForField:
                excludedModifiers = Modifier.STATIC;
                label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changefieldmodifiertononstatic_description, methodName);
                break;
            case IProblem.IllegalModifierCombinationFinalVolatileForField:
                excludedModifiers = Modifier.VOLATILE;
                label = CorrectionMessages.ModifierCorrectionSubProcessor_removevolatile_description;
                break;
            case IProblem.IllegalModifierForInterfaceMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT);
                break;
            case IProblem.IllegalModifierForInterfaceMethod18:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP | Modifier.DEFAULT | Modifier.STATIC);
                if (Modifier.isAbstract(binding.getModifiers())) {
                    excludedModifiers = excludedModifiers | Modifier.STRICTFP;
                }
                break;
            case IProblem.IllegalModifierForInterface:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForInterfaceField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
                break;
            case IProblem.IllegalModifierForMemberInterface:
            case IProblem.IllegalVisibilityModifierForInterfaceMemberType:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STATIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForMemberClass:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForLocalClass:
                excludedModifiers = ~(Modifier.ABSTRACT | Modifier.FINAL | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForArgument:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForField:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | Modifier.TRANSIENT);
                break;
            case IProblem.IllegalModifierForMethod:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.ABSTRACT | Modifier.FINAL | Modifier.NATIVE | Modifier.STRICTFP | Modifier.SYNCHRONIZED);
                break;
            case IProblem.IllegalModifierForConstructor:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
                break;
            case IProblem.IllegalModifierForVariable:
                excludedModifiers = ~Modifier.FINAL;
                break;
            case IProblem.IllegalModifierForEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.STRICTFP);
                break;
            case IProblem.IllegalModifierForEnumConstant:
                excludedModifiers = ~Modifier.NONE;
                break;
            case IProblem.IllegalModifierForEnumConstructor:
                excludedModifiers = ~Modifier.PRIVATE;
                break;
            case IProblem.IllegalModifierForMemberEnum:
                excludedModifiers = ~(Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED | Modifier.STATIC | Modifier.STRICTFP);
                break;
            default:
                // $NON-NLS-1$
                Assert.isTrue(false, "not supported");
                return;
        }
        if (label == null) {
            label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_removeinvalidmodifiers_description, methodName);
        }
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, includedModifiers, excludedModifiers, relevance));
        if (problemId == IProblem.IllegalModifierCombinationFinalVolatileForField) {
            proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_removefinal_description, cu, binding, selectedNode, 0, Modifier.FINAL, relevance + 1));
        }
        if (problemId == IProblem.UnexpectedStaticModifierForField && binding instanceof IVariableBinding) {
            ITypeBinding declClass = ((IVariableBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostaticfinal_description, cu, binding, selectedNode, Modifier.FINAL, Modifier.VOLATILE, relevance + 1));
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1));
                }
            }
        }
        if (problemId == IProblem.UnexpectedStaticModifierForMethod && binding instanceof IMethodBinding) {
            ITypeBinding declClass = ((IMethodBinding) binding).getDeclaringClass();
            if (declClass.isMember()) {
                ASTNode parentType = context.getASTRoot().findDeclaringNode(declClass);
                if (parentType != null) {
                    proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_addstatictoparenttype_description, cu, declClass, parentType, Modifier.STATIC, 0, relevance - 1));
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ModifierChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 5 with ModifierChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class ModifierCorrectionSubProcessor method addChangeOverriddenModifierProposal.

public static void addChangeOverriddenModifierProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> 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) });
            proposals.add(new ModifierChangeCorrectionProposal(label, cu, method, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_1));
        }
    }
    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;
            }
            proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, targetMethod, selectedNode, includedModifiers, excludedModifiers, IProposalRelevance.CHANGE_OVERRIDDEN_MODIFIER_2));
        }
    }
}
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.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 ModifierChangeCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.ModifierChangeCorrectionProposal)5 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)3 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)3 SimpleName (org.eclipse.jdt.core.dom.SimpleName)3 IBinding (org.eclipse.jdt.core.dom.IBinding)2 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)2 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)2 AST (org.eclipse.jdt.core.dom.AST)1 Block (org.eclipse.jdt.core.dom.Block)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 Expression (org.eclipse.jdt.core.dom.Expression)1 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)1 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)1 SimpleType (org.eclipse.jdt.core.dom.SimpleType)1 SuperFieldAccess (org.eclipse.jdt.core.dom.SuperFieldAccess)1