Search in sources :

Example 51 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite 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 52 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite 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 53 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite 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 54 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite 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)

Example 55 with ASTRewrite

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

the class AdvancedQuickAssistProcessor method getExchangeOperandsProposals.

private static boolean getExchangeOperandsProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    // check that user invokes quick assist on infix expression
    if (!(node instanceof InfixExpression)) {
        return false;
    }
    InfixExpression infixExpression = (InfixExpression) node;
    Operator operator = infixExpression.getOperator();
    if (operator != InfixExpression.Operator.CONDITIONAL_AND && operator != InfixExpression.Operator.AND && operator != InfixExpression.Operator.CONDITIONAL_OR && operator != InfixExpression.Operator.OR && operator != InfixExpression.Operator.EQUALS && operator != InfixExpression.Operator.NOT_EQUALS && operator != InfixExpression.Operator.LESS && operator != InfixExpression.Operator.LESS_EQUALS && operator != InfixExpression.Operator.GREATER && operator != InfixExpression.Operator.GREATER_EQUALS && operator != InfixExpression.Operator.PLUS && operator != InfixExpression.Operator.TIMES && operator != InfixExpression.Operator.XOR) {
        return false;
    }
    int offset = isOperatorSelected(infixExpression, context.getSelectionOffset(), context.getSelectionLength());
    if (offset == -1) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    AST ast = infixExpression.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare left and right expressions
    Expression leftExpression = null;
    Expression rightExpression = null;
    InfixExpression currentExpression = infixExpression;
    leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getLeftOperand(), false, operator);
    if (infixExpression.getRightOperand().getStartPosition() <= context.getSelectionOffset()) {
        leftExpression = combineOperands(rewrite, leftExpression, infixExpression.getRightOperand(), false, operator);
    } else {
        rightExpression = combineOperands(rewrite, rightExpression, infixExpression.getRightOperand(), false, operator);
    }
    for (Iterator<Expression> iter = currentExpression.extendedOperands().iterator(); iter.hasNext(); ) {
        Expression extendedOperand = iter.next();
        if (extendedOperand.getStartPosition() <= context.getSelectionOffset()) {
            leftExpression = combineOperands(rewrite, leftExpression, extendedOperand, false, operator);
        } else {
            rightExpression = combineOperands(rewrite, rightExpression, extendedOperand, false, operator);
        }
    }
    if (NecessaryParenthesesChecker.needsParentheses(leftExpression, infixExpression, InfixExpression.RIGHT_OPERAND_PROPERTY)) {
        leftExpression = getParenthesizedExpression(ast, leftExpression);
    }
    if (NecessaryParenthesesChecker.needsParentheses(rightExpression, infixExpression, InfixExpression.LEFT_OPERAND_PROPERTY)) {
        rightExpression = getParenthesizedExpression(ast, rightExpression);
    }
    if (operator == InfixExpression.Operator.LESS) {
        operator = InfixExpression.Operator.GREATER;
    } else if (operator == InfixExpression.Operator.LESS_EQUALS) {
        operator = InfixExpression.Operator.GREATER_EQUALS;
    } else if (operator == InfixExpression.Operator.GREATER) {
        operator = InfixExpression.Operator.LESS;
    } else if (operator == InfixExpression.Operator.GREATER_EQUALS) {
        operator = InfixExpression.Operator.LESS_EQUALS;
    }
    // create new infix expression
    InfixExpression newInfix = ast.newInfixExpression();
    newInfix.setOperator(operator);
    newInfix.setLeftOperand(rightExpression);
    newInfix.setRightOperand(leftExpression);
    rewrite.replace(infixExpression, newInfix, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_exchangeOperands_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.EXCHANGE_OPERANDS, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Aggregations

ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)180 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)93 Image (org.eclipse.swt.graphics.Image)80 AST (org.eclipse.jdt.core.dom.AST)78 ASTNode (org.eclipse.jdt.core.dom.ASTNode)69 Expression (org.eclipse.jdt.core.dom.Expression)54 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)44 Block (org.eclipse.jdt.core.dom.Block)38 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 SimpleName (org.eclipse.jdt.core.dom.SimpleName)34 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)34 CastExpression (org.eclipse.jdt.core.dom.CastExpression)33 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)32 Type (org.eclipse.jdt.core.dom.Type)30 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)30 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)29 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)29 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)29 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)29