Search in sources :

Example 56 with AST

use of org.eclipse.jdt.core.dom.AST in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getPickOutStringProposals.

private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    // we work with String's
    if (!(node instanceof StringLiteral)) {
        return false;
    }
    // user should select part of String
    int selectionPos = context.getSelectionOffset();
    int selectionLen = context.getSelectionLength();
    if (selectionLen == 0) {
        return false;
    }
    int valueStart = node.getStartPosition() + 1;
    int valueEnd = node.getStartPosition() + node.getLength() - 1;
    // selection must be inside node and the quotes and not contain the full value
    if (selectionPos < valueStart || selectionPos + selectionLen > valueEnd || valueEnd - valueStart == selectionLen) {
        return false;
    }
    // prepare string parts positions
    StringLiteral stringLiteral = (StringLiteral) node;
    String stringValue = stringLiteral.getEscapedValue();
    int firstPos = selectionPos - node.getStartPosition();
    int secondPos = firstPos + selectionLen;
    // prepare new string literals
    AST ast = node.getAST();
    StringLiteral leftLiteral = ast.newStringLiteral();
    StringLiteral centerLiteral = ast.newStringLiteral();
    StringLiteral rightLiteral = ast.newStringLiteral();
    try {
        leftLiteral.setEscapedValue('"' + stringValue.substring(1, firstPos) + '"');
        centerLiteral.setEscapedValue('"' + stringValue.substring(firstPos, secondPos) + '"');
        rightLiteral.setEscapedValue('"' + stringValue.substring(secondPos, stringValue.length() - 1) + '"');
    } catch (IllegalArgumentException e) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // prepare new expression instead of StringLiteral
    InfixExpression expression = ast.newInfixExpression();
    expression.setOperator(InfixExpression.Operator.PLUS);
    if (firstPos != 1) {
        expression.setLeftOperand(leftLiteral);
    }
    if (firstPos == 1) {
        expression.setLeftOperand(centerLiteral);
    } else {
        expression.setRightOperand(centerLiteral);
    }
    if (secondPos < stringValue.length() - 1) {
        if (firstPos == 1) {
            expression.setRightOperand(rightLiteral);
        } else {
            expression.extendedOperands().add(rightLiteral);
        }
    }
    // use new expression instead of old StirngLiteral
    rewrite.replace(stringLiteral, expression, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_pickSelectedString;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.PICK_SELECTED_STRING);
    //$NON-NLS-1$
    proposal.addLinkedPosition(rewrite.track(centerLiteral), true, "CENTER_STRING");
    resultingCollections.add(proposal);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 57 with AST

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

the class SourceProvider method replaceParameterWithExpression.

private void replaceParameterWithExpression(ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
    Expression[] arguments = context.arguments;
    try {
        ITextFileBuffer buffer = RefactoringFileBuffers.acquire(context.compilationUnit);
        for (int i = 0; i < arguments.length; i++) {
            Expression expression = arguments[i];
            String expressionString = null;
            if (expression instanceof SimpleName) {
                expressionString = ((SimpleName) expression).getIdentifier();
            } else {
                try {
                    expressionString = buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
                } catch (BadLocationException exception) {
                    JavaPlugin.log(exception);
                    continue;
                }
            }
            ParameterData parameter = getParameterData(i);
            List<SimpleName> references = parameter.references();
            for (Iterator<SimpleName> iter = references.iterator(); iter.hasNext(); ) {
                ASTNode element = iter.next();
                Expression newExpression = (Expression) rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
                AST ast = rewriter.getAST();
                ITypeBinding explicitCast = ASTNodes.getExplicitCast(expression, (Expression) element);
                if (explicitCast != null) {
                    CastExpression cast = ast.newCastExpression();
                    if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
                        newExpression = createParenthesizedExpression(newExpression, ast);
                    }
                    cast.setExpression(newExpression);
                    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(expression, importRewrite);
                    cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
                    expression = newExpression = cast;
                }
                if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
                    newExpression = createParenthesizedExpression(newExpression, ast);
                }
                rewriter.replace(element, newExpression, null);
            }
        }
    } finally {
        RefactoringFileBuffers.release(context.compilationUnit);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 58 with AST

use of org.eclipse.jdt.core.dom.AST 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 59 with AST

use of org.eclipse.jdt.core.dom.AST 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 60 with AST

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

AST (org.eclipse.jdt.core.dom.AST)169 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)78 Expression (org.eclipse.jdt.core.dom.Expression)77 ASTNode (org.eclipse.jdt.core.dom.ASTNode)70 Type (org.eclipse.jdt.core.dom.Type)52 SimpleName (org.eclipse.jdt.core.dom.SimpleName)51 Block (org.eclipse.jdt.core.dom.Block)44 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)43 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 CastExpression (org.eclipse.jdt.core.dom.CastExpression)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)38 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)38 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)34 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)33 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)30 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)30