Search in sources :

Example 11 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class LocalCorrectionsSubProcessor method addCasesOmittedProposals.

public static void addCasesOmittedProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof Expression && selectedNode.getLocationInParent() == SwitchStatement.EXPRESSION_PROPERTY) {
        AST ast = selectedNode.getAST();
        SwitchStatement parent = (SwitchStatement) selectedNode.getParent();
        for (Statement statement : (List<Statement>) parent.statements()) {
            if (statement instanceof SwitchCase && ((SwitchCase) statement).isDefault()) {
                // insert //$CASES-OMITTED$:
                ASTRewrite rewrite = ASTRewrite.create(ast);
                rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
                ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
                ASTNode casesOmittedComment = //$NON-NLS-1$
                rewrite.createStringPlaceholder("//$CASES-OMITTED$", ASTNode.EMPTY_STATEMENT);
                listRewrite.insertBefore(casesOmittedComment, statement, null);
                String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_cases_omitted;
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_CASES_OMITTED, image);
                proposals.add(proposal);
                break;
            }
        }
    }
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image) NoCommentSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) List(java.util.List) ArrayList(java.util.ArrayList)

Example 12 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class LocalCorrectionsSubProcessor method getInvalidOperatorProposals.

public static void getInvalidOperatorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    CompilationUnit root = context.getASTRoot();
    AST ast = root.getAST();
    ASTNode selectedNode = problem.getCoveringNode(root);
    while (selectedNode instanceof ParenthesizedExpression) {
        selectedNode = ((ParenthesizedExpression) selectedNode).getExpression();
    }
    if (selectedNode instanceof PrefixExpression) {
        // !x instanceof X -> !(x instanceof X)
        PrefixExpression expression = (PrefixExpression) selectedNode;
        if (expression.getOperator() == PrefixExpression.Operator.NOT) {
            ASTNode parent = expression.getParent();
            String label = null;
            switch(parent.getNodeType()) {
                case ASTNode.INSTANCEOF_EXPRESSION:
                    label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_instanceof_description;
                    break;
                case ASTNode.INFIX_EXPRESSION:
                    InfixExpression infixExpression = (InfixExpression) parent;
                    label = Messages.format(CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_description, infixExpression.getOperator().toString());
                    break;
            }
            if (label != null) {
                ASTRewrite rewrite = ASTRewrite.create(ast);
                rewrite.replace(selectedNode, rewrite.createMoveTarget(expression.getOperand()), null);
                ParenthesizedExpression newParentExpr = ast.newParenthesizedExpression();
                newParentExpr.setExpression((Expression) rewrite.createMoveTarget(parent));
                PrefixExpression newPrefixExpr = ast.newPrefixExpression();
                newPrefixExpr.setOperand(newParentExpr);
                newPrefixExpr.setOperator(PrefixExpression.Operator.NOT);
                rewrite.replace(parent, newPrefixExpr, null);
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
                ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVALID_OPERATOR, image);
                proposals.add(proposal);
            }
        }
    } else if (selectedNode instanceof InfixExpression && isBitOperation((((InfixExpression) selectedNode).getOperator()))) {
        // a & b == c -> (a & b) == c
        final CompareInBitWiseOpFinder opFinder = new CompareInBitWiseOpFinder(selectedNode);
        if (opFinder.getCompareExpression() != null) {
            // compare operation inside bit operations: set parents
            String label = CorrectionMessages.LocalCorrectionsSubProcessor_setparenteses_bitop_description;
            Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
            CUCorrectionProposal proposal = new CUCorrectionProposal(label, context.getCompilationUnit(), IProposalRelevance.INVALID_OPERATOR, image) {

                @Override
                protected void addEdits(IDocument document, TextEdit edit) throws CoreException {
                    InfixExpression compareExpression = opFinder.getCompareExpression();
                    InfixExpression expression = opFinder.getParentInfixExpression();
                    ASTNode left = compareExpression.getLeftOperand();
                    if (expression.getStartPosition() < left.getStartPosition()) {
                        edit.addChild(new InsertEdit(expression.getStartPosition(), String.valueOf('(')));
                        edit.addChild(new InsertEdit(ASTNodes.getExclusiveEnd(left), String.valueOf(')')));
                    }
                    ASTNode rigth = compareExpression.getRightOperand();
                    int selEnd = ASTNodes.getExclusiveEnd(expression);
                    if (selEnd > ASTNodes.getExclusiveEnd(rigth)) {
                        edit.addChild(new InsertEdit(rigth.getStartPosition(), String.valueOf('(')));
                        edit.addChild(new InsertEdit(selEnd, String.valueOf(')')));
                    }
                }
            };
            proposals.add(proposal);
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IDocument(org.eclipse.jface.text.IDocument)

Example 13 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal 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 14 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che by eclipse.

the class ModifierCorrectionSubProcessor method addOverridingDeprecatedMethodProposal.

public static void addOverridingDeprecatedMethodProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(cu.getJavaProject());
    MethodDeclaration methodDecl = (MethodDeclaration) selectedNode;
    AST ast = methodDecl.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    if (is50OrHigher) {
        Annotation annot = ast.newMarkerAnnotation();
        //$NON-NLS-1$
        annot.setTypeName(ast.newName("Deprecated"));
        rewrite.getListRewrite(methodDecl, methodDecl.getModifiersProperty()).insertFirst(annot, null);
    }
    Javadoc javadoc = methodDecl.getJavadoc();
    if (javadoc != null || !is50OrHigher) {
        if (!is50OrHigher) {
            javadoc = ast.newJavadoc();
            rewrite.set(methodDecl, MethodDeclaration.JAVADOC_PROPERTY, javadoc, null);
        }
        TagElement newTag = ast.newTagElement();
        newTag.setTagName(TagElement.TAG_DEPRECATED);
        JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
    }
    String label = CorrectionMessages.ModifierCorrectionSubProcessor_overrides_deprecated_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.OVERRIDES_DEPRECATED, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) TagElement(org.eclipse.jdt.core.dom.TagElement) Image(org.eclipse.swt.graphics.Image) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 15 with ASTRewriteCorrectionProposal

use of org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal in project che 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, image);
        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, image);
        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) 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) 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)

Aggregations

ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)125 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)93 Image (org.eclipse.swt.graphics.Image)70 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 AST (org.eclipse.jdt.core.dom.AST)38 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 ASTNode (org.eclipse.jdt.core.dom.ASTNode)33 Expression (org.eclipse.jdt.core.dom.Expression)32 ArrayList (java.util.ArrayList)29 CastExpression (org.eclipse.jdt.core.dom.CastExpression)24 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)23 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)23 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)23 Test (org.junit.Test)23 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)21 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)21 Block (org.eclipse.jdt.core.dom.Block)19 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)19 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)19 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)19