Search in sources :

Example 36 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.

the class ModifierCorrectionSubProcessor method addRemoveInvalidModifiersProposal.

public static void addRemoveInvalidModifiersProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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);
                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);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, includedModifiers, excludedModifiers, relevance, image));
        if (problemId == IProblem.IllegalModifierCombinationFinalVolatileForField) {
            proposals.add(new ModifierChangeCorrectionProposal(CorrectionMessages.ModifierCorrectionSubProcessor_removefinal_description, cu, binding, selectedNode, 0, Modifier.FINAL, relevance + 1, image));
        }
        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, image));
                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, image));
                }
            }
        }
        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, image));
                }
            }
        }
    }
}
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.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Image(org.eclipse.swt.graphics.Image) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 37 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.

the class ModifierCorrectionSubProcessor method addNativeMethodProposals.

public static void addNativeMethodProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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;
    }
    {
        AST ast = astRoot.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        removeModifier(decl, rewrite, Modifier.NATIVE);
        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_removenative_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_NATIVE, image);
        proposals.add(proposal);
    }
    if (decl.getBody() != null) {
        ASTRewrite rewrite = ASTRewrite.create(decl.getAST());
        rewrite.remove(decl.getBody(), null);
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_removebody_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal2 = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_METHOD_BODY, image);
        proposals.add(proposal2);
    }
}
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) Image(org.eclipse.swt.graphics.Image) 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) 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)

Example 38 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.

the class ModifierCorrectionSubProcessor method addNonFinalLocalProposal.

public static void addNonFinalLocalProposal(IInvocationContext context, IProblemLocation 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));
    }
}
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 39 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.

the class NullAnnotationsCorrectionProcessor method findProblemFieldName.

private static SimpleName findProblemFieldName(ASTNode selectedNode, int problemID) {
    // with expected type declared @NonNull we first need to find the SimpleName inside:
    if (selectedNode instanceof FieldAccess)
        selectedNode = ((FieldAccess) selectedNode).getName();
    else if (selectedNode instanceof QualifiedName)
        selectedNode = ((QualifiedName) selectedNode).getName();
    if (selectedNode instanceof SimpleName) {
        SimpleName name = (SimpleName) selectedNode;
        if (problemID == IProblem.NullableFieldReference)
            return name;
        // not field dereference, but compatibility issue - is value a field reference?
        IBinding binding = name.resolveBinding();
        if ((binding instanceof IVariableBinding) && ((IVariableBinding) binding).isField())
            return name;
    }
    return null;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 40 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project che by eclipse.

the class RenameLinkedModeRefactoringSession method getModel.

public LinkedModeModel getModel() {
    CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_YES, null);
    LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
    ASTNode selectedNode = NodeFinder.perform(root, offset, 0);
    if (!(selectedNode instanceof SimpleName)) {
        return null;
    }
    SimpleName nameNode = (SimpleName) selectedNode;
    fOriginalName = nameNode.getIdentifier();
    final int pos = nameNode.getStartPosition();
    ASTNode[] sameNodes = LinkedNodeFinder.findByNode(root, nameNode);
    //TODO: copied from LinkedNamesAssistProposal#apply(..):
    // sort for iteration order, starting with the node @ offset
    Arrays.sort(sameNodes, new Comparator<ASTNode>() {

        public int compare(ASTNode o1, ASTNode o2) {
            return rank(o1) - rank(o2);
        }

        /**
             * Returns the absolute rank of an <code>ASTNode</code>. Nodes
             * preceding <code>pos</code> are ranked last.
             *
             * @param node the node to compute the rank for
             * @return the rank of the node with respect to the invocation offset
             */
        private int rank(ASTNode node) {
            int relativeRank = node.getStartPosition() + node.getLength() - pos;
            if (relativeRank < 0)
                return Integer.MAX_VALUE + relativeRank;
            else
                return relativeRank;
        }
    });
    for (int i = 0; i < sameNodes.length; i++) {
        ASTNode elem = sameNodes[i];
        RegionImpl position = new RegionImpl();
        position.setOffset(elem.getStartPosition());
        position.setLength(elem.getLength());
        group.addPositions(position);
    }
    LinkedModeModelImpl model = new LinkedModeModelImpl();
    model.addGroups(group);
    return model;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)

Aggregations

SimpleName (org.eclipse.jdt.core.dom.SimpleName)291 ASTNode (org.eclipse.jdt.core.dom.ASTNode)122 IBinding (org.eclipse.jdt.core.dom.IBinding)70 Expression (org.eclipse.jdt.core.dom.Expression)67 AST (org.eclipse.jdt.core.dom.AST)63 ArrayList (java.util.ArrayList)60 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)57 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)55 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)53 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)46 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)43 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)41 Name (org.eclipse.jdt.core.dom.Name)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 Type (org.eclipse.jdt.core.dom.Type)35 Block (org.eclipse.jdt.core.dom.Block)34 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)33 Assignment (org.eclipse.jdt.core.dom.Assignment)32