Search in sources :

Example 11 with Statement

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

the class SourceProvider method getExpressionRanges.

private List<IRegion> getExpressionRanges() {
    fMarkerMode = EXPRESSION_MODE;
    List<IRegion> result = new ArrayList<IRegion>(2);
    List<Statement> statements = fDeclaration.getBody().statements();
    ReturnStatement rs = null;
    int size = statements.size();
    ASTNode node;
    switch(size) {
        case 0:
            return result;
        case 1:
            node = statements.get(0);
            if (node.getNodeType() == ASTNode.RETURN_STATEMENT) {
                rs = (ReturnStatement) node;
            } else {
                result.add(createRange(node, node));
            }
            break;
        default:
            {
                node = statements.get(size - 1);
                if (node.getNodeType() == ASTNode.RETURN_STATEMENT) {
                    result.add(createRange(statements, size - 2));
                    rs = (ReturnStatement) node;
                } else {
                    result.add(createRange(statements, size - 1));
                }
                break;
            }
    }
    if (rs != null) {
        Expression exp = rs.getExpression();
        result.add(createRange(exp, exp));
    }
    return result;
}
Also used : 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) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) ArrayList(java.util.ArrayList) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IRegion(org.eclipse.jface.text.IRegion)

Example 12 with Statement

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

the class DelegateMethodCreator method createMethodInvocation.

/**
	 * Creates the corresponding statement for the method invocation, based on
	 * the return type.
	 *
	 * @param declaration the method declaration where the invocation statement
	 *            is inserted
	 * @param invocation the method invocation being encapsulated by the
	 *            resulting statement
	 * @return the corresponding statement
	 */
protected Statement createMethodInvocation(final MethodDeclaration declaration, final MethodInvocation invocation) {
    Assert.isNotNull(declaration);
    Assert.isNotNull(invocation);
    Statement statement = null;
    final Type type = declaration.getReturnType2();
    if (type == null)
        statement = createExpressionStatement(invocation);
    else {
        if (type instanceof PrimitiveType) {
            final PrimitiveType primitive = (PrimitiveType) type;
            if (primitive.getPrimitiveTypeCode().equals(PrimitiveType.VOID))
                statement = createExpressionStatement(invocation);
            else
                statement = createReturnStatement(invocation);
        } else
            statement = createReturnStatement(invocation);
    }
    return statement;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Example 13 with Statement

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

the class PromoteTempToFieldRefactoring method addFieldInitializationToConstructor.

private void addFieldInitializationToConstructor(ASTRewrite rewrite, MethodDeclaration constructor) {
    if (constructor.getBody() == null)
        constructor.setBody(getAST().newBlock());
    Statement newStatement = createNewAssignmentStatement(rewrite);
    rewrite.getListRewrite(constructor.getBody(), Block.STATEMENTS_PROPERTY).insertLast(newStatement, null);
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 14 with Statement

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

the class ConvertIterableLoopOperation method convert.

@Override
protected Statement convert(CompilationUnitRewrite cuRewrite, final TextEditGroup group, final LinkedProposalModel positionGroups) throws CoreException {
    final AST ast = cuRewrite.getAST();
    final ASTRewrite astRewrite = cuRewrite.getASTRewrite();
    final ImportRewrite importRewrite = cuRewrite.getImportRewrite();
    final ImportRemover remover = cuRewrite.getImportRemover();
    fEnhancedForLoop = ast.newEnhancedForStatement();
    String[] names = getVariableNameProposals();
    String name;
    if (fElementVariable != null) {
        name = fElementVariable.getName();
    } else {
        name = names[0];
    }
    final LinkedProposalPositionGroup pg = positionGroups.getPositionGroup(name, true);
    if (fElementVariable != null)
        pg.addProposal(name, null, 10);
    for (int i = 0; i < names.length; i++) {
        pg.addProposal(names[i], null, 10);
    }
    final Statement body = getForStatement().getBody();
    if (body != null) {
        final ListRewrite list;
        if (body instanceof Block) {
            list = astRewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY);
            for (final Iterator<Expression> iterator = fOccurrences.iterator(); iterator.hasNext(); ) {
                final Statement parent = (Statement) ASTNodes.getParent(iterator.next(), Statement.class);
                if (parent != null && list.getRewrittenList().contains(parent)) {
                    list.remove(parent, null);
                    remover.registerRemovedNode(parent);
                }
            }
        } else {
            list = null;
        }
        final String text = name;
        body.accept(new ASTVisitor() {

            private boolean replace(final Expression expression) {
                final SimpleName node = ast.newSimpleName(text);
                astRewrite.replace(expression, node, group);
                remover.registerRemovedNode(expression);
                pg.addPosition(astRewrite.track(node), false);
                return false;
            }

            @Override
            public final boolean visit(final MethodInvocation node) {
                final IMethodBinding binding = node.resolveMethodBinding();
                if (binding != null && (binding.getName().equals("next") || binding.getName().equals("nextElement"))) {
                    //$NON-NLS-1$ //$NON-NLS-2$
                    final Expression expression = node.getExpression();
                    if (expression instanceof Name) {
                        final IBinding result = ((Name) expression).resolveBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    } else if (expression instanceof FieldAccess) {
                        final IBinding result = ((FieldAccess) expression).resolveFieldBinding();
                        if (result != null && result.equals(fIteratorVariable))
                            return replace(node);
                    }
                }
                return super.visit(node);
            }

            @Override
            public final boolean visit(final SimpleName node) {
                if (fElementVariable != null) {
                    final IBinding binding = node.resolveBinding();
                    if (binding != null && binding.equals(fElementVariable)) {
                        final Statement parent = (Statement) ASTNodes.getParent(node, Statement.class);
                        if (parent != null && (list == null || list.getRewrittenList().contains(parent)))
                            pg.addPosition(astRewrite.track(node), false);
                    }
                }
                return false;
            }
        });
        fEnhancedForLoop.setBody(getBody(cuRewrite, group, positionGroups));
    }
    final SingleVariableDeclaration declaration = ast.newSingleVariableDeclaration();
    final SimpleName simple = ast.newSimpleName(name);
    pg.addPosition(astRewrite.track(simple), true);
    declaration.setName(simple);
    final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
    declaration.setType(importType(elementType, getForStatement(), importRewrite, getRoot()));
    if (fMakeFinal) {
        ModifierRewrite.create(astRewrite, declaration).setModifiers(Modifier.FINAL, 0, group);
    }
    remover.registerAddedImport(elementType.getQualifiedName());
    fEnhancedForLoop.setParameter(declaration);
    fEnhancedForLoop.setExpression(getExpression(astRewrite));
    for (Iterator<Expression> iterator = getForStatement().initializers().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        if (node instanceof VariableDeclarationExpression) {
            VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) node;
            remover.registerRemovedNode(variableDeclarationExpression.getType());
        } else {
            remover.registerRemovedNode(node);
        }
    }
    for (Iterator<Expression> iterator = getForStatement().updaters().iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        remover.registerRemovedNode(node);
    }
    return fEnhancedForLoop;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ImportRemover(org.eclipse.jdt.internal.corext.refactoring.structure.ImportRemover) AST(org.eclipse.jdt.core.dom.AST) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 15 with Statement

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

the class ConvertIterableLoopOperation method rewriteAST.

/**
	 * {@inheritDoc}
	 */
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
    final TextEditGroup group = createTextEditGroup(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, cuRewrite);
    final ASTRewrite astRewrite = cuRewrite.getASTRewrite();
    TightSourceRangeComputer rangeComputer;
    if (astRewrite.getExtendedSourceRangeComputer() instanceof TightSourceRangeComputer) {
        rangeComputer = (TightSourceRangeComputer) astRewrite.getExtendedSourceRangeComputer();
    } else {
        rangeComputer = new TightSourceRangeComputer();
    }
    rangeComputer.addTightSourceNode(getForStatement());
    astRewrite.setTargetSourceRangeComputer(rangeComputer);
    Statement statement = convert(cuRewrite, group, positionGroups);
    astRewrite.replace(getForStatement(), statement, group);
}
Also used : TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

Statement (org.eclipse.jdt.core.dom.Statement)68 ForStatement (org.eclipse.jdt.core.dom.ForStatement)42 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)40 IfStatement (org.eclipse.jdt.core.dom.IfStatement)39 Block (org.eclipse.jdt.core.dom.Block)37 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)35 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)32 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)32 DoStatement (org.eclipse.jdt.core.dom.DoStatement)31 Expression (org.eclipse.jdt.core.dom.Expression)31 ASTNode (org.eclipse.jdt.core.dom.ASTNode)29 AST (org.eclipse.jdt.core.dom.AST)25 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)25 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)23 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)18 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)17 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)15