Search in sources :

Example 71 with Block

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

the class ExtractMethodRefactoring method createMethodBody.

private Block createMethodBody(ASTNode[] selectedNodes, TextEditGroup substitute, int modifiers) {
    Block result = fAST.newBlock();
    ListRewrite statements = fRewriter.getListRewrite(result, Block.STATEMENTS_PROPERTY);
    // Locals that are not passed as an arguments since the extracted method only
    // writes to them
    IVariableBinding[] methodLocals = fAnalyzer.getMethodLocals();
    for (int i = 0; i < methodLocals.length; i++) {
        if (methodLocals[i] != null) {
            result.statements().add(createDeclaration(methodLocals[i], null));
        }
    }
    for (Iterator<ParameterInfo> iter = fParameterInfos.iterator(); iter.hasNext(); ) {
        ParameterInfo parameter = iter.next();
        if (parameter.isRenamed()) {
            for (int n = 0; n < selectedNodes.length; n++) {
                SimpleName[] oldNames = LinkedNodeFinder.findByBinding(selectedNodes[n], parameter.getOldBinding());
                for (int i = 0; i < oldNames.length; i++) {
                    fRewriter.replace(oldNames[i], fAST.newSimpleName(parameter.getNewName()), null);
                }
            }
        }
    }
    boolean extractsExpression = fAnalyzer.isExpressionSelected();
    ASTNode[] callNodes = createCallNodes(null, modifiers);
    ASTNode replacementNode;
    if (callNodes.length == 1) {
        replacementNode = callNodes[0];
    } else {
        replacementNode = fRewriter.createGroupNode(callNodes);
    }
    if (extractsExpression) {
        // if we have an expression then only one node is selected.
        ITypeBinding binding = fAnalyzer.getExpressionBinding();
        if (binding != null && (!binding.isPrimitive() || !"void".equals(binding.getName()))) {
            //$NON-NLS-1$
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression((Expression) fRewriter.createMoveTarget(selectedNodes[0] instanceof ParenthesizedExpression ? ((ParenthesizedExpression) selectedNodes[0]).getExpression() : selectedNodes[0]));
            statements.insertLast(rs, null);
        } else {
            ExpressionStatement st = fAST.newExpressionStatement((Expression) fRewriter.createMoveTarget(selectedNodes[0]));
            statements.insertLast(st, null);
        }
        fRewriter.replace(selectedNodes[0].getParent() instanceof ParenthesizedExpression ? selectedNodes[0].getParent() : selectedNodes[0], replacementNode, substitute);
    } else {
        if (selectedNodes.length == 1) {
            statements.insertLast(fRewriter.createMoveTarget(selectedNodes[0]), substitute);
            fRewriter.replace(selectedNodes[0], replacementNode, substitute);
        } else {
            ListRewrite source = fRewriter.getListRewrite(selectedNodes[0].getParent(), (ChildListPropertyDescriptor) selectedNodes[0].getLocationInParent());
            ASTNode toMove = source.createMoveTarget(selectedNodes[0], selectedNodes[selectedNodes.length - 1], replacementNode, substitute);
            statements.insertLast(toMove, substitute);
        }
        IVariableBinding returnValue = fAnalyzer.getReturnValue();
        if (returnValue != null) {
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression(fAST.newSimpleName(getName(returnValue)));
            statements.insertLast(rs, null);
        }
    }
    return result;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 72 with Block

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

the class ExtractTempRefactoring method getEnclosingBodyNode.

private Block getEnclosingBodyNode() throws JavaModelException {
    ASTNode node = getSelectedExpression().getAssociatedNode();
    // expression must be in a method or initializer body
    // make sure it is not in method or parameter annotation
    StructuralPropertyDescriptor location = null;
    while (node != null && !(node instanceof BodyDeclaration)) {
        location = node.getLocationInParent();
        node = node.getParent();
    }
    if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY) {
        return (Block) node.getStructuralProperty(location);
    }
    return null;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 73 with Block

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

the class ExtractTempRefactoring method insertAt.

private void insertAt(ASTNode target, Statement declaration) {
    ASTRewrite rewrite = fCURewrite.getASTRewrite();
    TextEditGroup groupDescription = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable);
    ASTNode parent = target.getParent();
    StructuralPropertyDescriptor locationInParent = target.getLocationInParent();
    while (locationInParent != Block.STATEMENTS_PROPERTY && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) {
        if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY || locationInParent == ForStatement.BODY_PROPERTY || locationInParent == EnhancedForStatement.BODY_PROPERTY || locationInParent == DoStatement.BODY_PROPERTY || locationInParent == WhileStatement.BODY_PROPERTY) {
            // create intermediate block if target was the body property of a control statement:
            Block replacement = rewrite.getAST().newBlock();
            ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
            replacementRewrite.insertFirst(declaration, null);
            replacementRewrite.insertLast(rewrite.createMoveTarget(target), null);
            rewrite.replace(target, replacement, groupDescription);
            return;
        }
        target = parent;
        parent = parent.getParent();
        locationInParent = target.getLocationInParent();
    }
    ListRewrite listRewrite = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) locationInParent);
    listRewrite.insertBefore(declaration, target, groupDescription);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 74 with Block

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

the class CallInliner method initializeRewriteState.

private void initializeRewriteState() {
    fFieldInitializer = false;
    ASTNode parent = fInvocation.getParent();
    do {
        if (parent instanceof FieldDeclaration) {
            fFieldInitializer = true;
            return;
        } else if (parent instanceof Block) {
            return;
        }
        parent = parent.getParent();
    } while (parent != null);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 75 with Block

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

the class TypeChangeCorrectionProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    CompilationUnit newRoot = fAstRoot;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    if (declNode != null) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewrite imports = createImportRewrite(newRoot);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
        Type type = imports.addImport(fNewType, ast, context);
        if (declNode instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) declNode;
            Type origReturnType = methodDecl.getReturnType2();
            rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
            DimensionRewrite.removeAllChildren(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            // add javadoc tag
            Javadoc javadoc = methodDecl.getJavadoc();
            if (javadoc != null && origReturnType != null && origReturnType.isPrimitiveType() && ((PrimitiveType) origReturnType).getPrimitiveTypeCode() == PrimitiveType.VOID) {
                TagElement returnTag = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
                if (returnTag == null) {
                    returnTag = ast.newTagElement();
                    returnTag.setTagName(TagElement.TAG_RETURN);
                    TextElement commentStart = ast.newTextElement();
                    returnTag.fragments().add(commentStart);
                    //$NON-NLS-1$
                    addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
                    ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
                    JavadocTagsSubProcessor.insertTag(tagsRewriter, returnTag, null);
                }
            }
        } else if (declNode instanceof AnnotationTypeMemberDeclaration) {
            AnnotationTypeMemberDeclaration methodDecl = (AnnotationTypeMemberDeclaration) declNode;
            rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
        } else if (declNode instanceof VariableDeclarationFragment) {
            ASTNode parent = declNode.getParent();
            if (parent instanceof FieldDeclaration) {
                FieldDeclaration fieldDecl = (FieldDeclaration) parent;
                if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
                    // split
                    VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
                    FieldDeclaration newField = ast.newFieldDeclaration(placeholder);
                    newField.setType(type);
                    AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) fieldDecl.getParent();
                    ListRewrite listRewrite = rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
                    if (fieldDecl.fragments().indexOf(declNode) == 0) {
                        // if it as the first in the list-> insert before
                        listRewrite.insertBefore(newField, parent, null);
                    } else {
                        listRewrite.insertAfter(newField, parent, null);
                    }
                } else {
                    rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
                    DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
                }
            } else if (parent instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
                if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
                    // split
                    VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
                    VariableDeclarationStatement newStat = ast.newVariableDeclarationStatement(placeholder);
                    newStat.setType(type);
                    ListRewrite listRewrite = rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
                    if (varDecl.fragments().indexOf(declNode) == 0) {
                        // if it as the first in the list-> insert before
                        listRewrite.insertBefore(newStat, parent, null);
                    } else {
                        listRewrite.insertAfter(newStat, parent, null);
                    }
                } else {
                    rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
                    DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
                }
            } else if (parent instanceof VariableDeclarationExpression) {
                VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
                rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
                DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            }
        } else if (declNode instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) declNode;
            rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
            DimensionRewrite.removeAllChildren(declNode, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
        }
        // set up linked mode
        //$NON-NLS-1$
        final String KEY_TYPE = "type";
        addLinkedPosition(rewrite.track(type), true, KEY_TYPE);
        if (fTypeProposals != null) {
            for (int i = 0; i < fTypeProposals.length; i++) {
                addLinkedPositionProposal(KEY_TYPE, fTypeProposals[i]);
            }
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

Block (org.eclipse.jdt.core.dom.Block)105 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)45 AST (org.eclipse.jdt.core.dom.AST)44 Statement (org.eclipse.jdt.core.dom.Statement)42 Expression (org.eclipse.jdt.core.dom.Expression)39 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)38 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ForStatement (org.eclipse.jdt.core.dom.ForStatement)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)34 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)30 IfStatement (org.eclipse.jdt.core.dom.IfStatement)30 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)28 DoStatement (org.eclipse.jdt.core.dom.DoStatement)26 Type (org.eclipse.jdt.core.dom.Type)26 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)21 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)18