Java Code Examples for org.codehaus.groovy.ast.stmt.BlockStatement#getStatements()

The following examples show how to use org.codehaus.groovy.ast.stmt.BlockStatement#getStatements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DependenciesVisitor.java    From synopsys-detect with Apache License 2.0 6 votes vote down vote up
@Override
public void visitArgumentlistExpression(final ArgumentListExpression argumentListExpression) {
    if (inDependenciesBlock) {
        final List<Expression> expressions = argumentListExpression.getExpressions();

        if (expressions.size() == 1 && expressions.get(0) instanceof ClosureExpression) {
            final ClosureExpression closureExpression = (ClosureExpression) expressions.get(0);
            if (closureExpression.getCode() instanceof BlockStatement) {
                final BlockStatement blockStatement = (BlockStatement) closureExpression.getCode();
                final List<Statement> statements = blockStatement.getStatements();
                for (final Statement statement : statements) {
                    addDependencyFromStatement(statement);
                }
            }
        }
    }

    super.visitArgumentlistExpression(argumentListExpression);
}
 
Example 2
Source File: StatementWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void writeBlockStatement(final BlockStatement block) {
    writeStatementLabel(block);

    int mark = controller.getOperandStack().getStackLength();
    CompileStack compileStack = controller.getCompileStack();
    compileStack.pushVariableScope(block.getVariableScope());
    for (Statement statement : block.getStatements()) {
        statement.visit(controller.getAcg());
    }
    compileStack.pop();

    // GROOVY-7647, GROOVY-9126
    if (block.getLastLineNumber() > 0 && !isMethodOrConstructorNonEmptyBlock(block)) {
        MethodVisitor mv = controller.getMethodVisitor();
        Label blockEnd = new Label();
        mv.visitLabel(blockEnd);
        mv.visitLineNumber(block.getLastLineNumber(), blockEnd);
    }

    controller.getOperandStack().popDownTo(mark);
}
 
Example 3
Source File: GroovyVirtualSourceProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ConstructorCallExpression getConstructorCallExpression(
        ConstructorNode constructorNode) {
    Statement code = constructorNode.getCode();
    if (!(code instanceof BlockStatement)) {
        return null;
    }
    BlockStatement block = (BlockStatement) code;
    List<Statement> stats = block.getStatements();
    if (stats == null || stats.isEmpty()) {
        return null;
    }
    Statement stat = stats.get(0);
    if (!(stat instanceof ExpressionStatement)) {
        return null;
    }
    Expression expr = ((ExpressionStatement) stat).getExpression();
    if (!(expr instanceof ConstructorCallExpression)) {
        return null;
    }
    return (ConstructorCallExpression) expr;
}
 
Example 4
Source File: OptimizingStatementWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitBlockStatement(final BlockStatement statement) {
    opt.push();
    boolean optAll = true;
    for (Statement stmt : statement.getStatements()) {
        opt.push();
        stmt.visit(this);
        optAll = optAll && opt.canOptimize();
        opt.pop(true);
    }
    if (statement.isEmpty()) {
        opt.chainCanOptimize(true);
        opt.pop(true);
    } else {
        opt.chainShouldOptimize(optAll);
        if (optAll) {
            addMeta(statement, opt);
        }
        opt.pop(optAll);
    }
}
 
Example 5
Source File: GeneralUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
public static boolean copyStatementsWithSuperAdjustment(final ClosureExpression pre, final BlockStatement body) {
    Statement preCode = pre.getCode();
    boolean changed = false;
    if (preCode instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) preCode;
        List<Statement> statements = block.getStatements();
        for (int i = 0, n = statements.size(); i < n; i += 1) {
            Statement statement = statements.get(i);
            // adjust the first statement if it's a super call
            if (i == 0 && statement instanceof ExpressionStatement) {
                ExpressionStatement es = (ExpressionStatement) statement;
                Expression preExp = es.getExpression();
                if (preExp instanceof MethodCallExpression) {
                    MethodCallExpression mce = (MethodCallExpression) preExp;
                    String name = mce.getMethodAsString();
                    if ("super".equals(name)) {
                        es.setExpression(new ConstructorCallExpression(ClassNode.SUPER, mce.getArguments()));
                        changed = true;
                    }
                }
            }
            body.addStatement(statement);
        }
    }
    return changed;
}
 
Example 6
Source File: ClassNode.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
    MethodNode constructor = getOrAddStaticConstructorNode();
    Statement statement = constructor.getCode();
    if (statement instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) statement;
        // add given statements for explicitly declared static fields just after enum-special fields
        // are found - the $VALUES binary expression marks the end of such fields.
        List<Statement> blockStatements = block.getStatements();
        ListIterator<Statement> litr = blockStatements.listIterator();
        while (litr.hasNext()) {
            Statement stmt = litr.next();
            if (stmt instanceof ExpressionStatement &&
                    ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
                BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
                if (bExp.getLeftExpression() instanceof FieldExpression) {
                    FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
                    if (fExp.getFieldName().equals("$VALUES")) {
                        for (Statement tmpStmt : staticFieldStatements) {
                            litr.add(tmpStmt);
                        }
                    }
                }
            }
        }
    }
}
 
Example 7
Source File: AutoNewLineTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitClosureExpression(final ClosureExpression expression) {
    super.visitClosureExpression(expression);
    if (inBuilderMethod) {
        Statement oldCode = expression.getCode();
        BlockStatement block = oldCode instanceof BlockStatement?
                ((BlockStatement)oldCode):
                new BlockStatement(Collections.singletonList(oldCode), new VariableScope());
        List<Statement> statements = block.getStatements();
        if (!statements.isEmpty()) {
            Statement first = statements.get(0);
            Statement last = statements.get(statements.size()-1);
            if (expression.getLineNumber()<first.getLineNumber()) {
                // there's a new line between { -> ... and the first statement
                statements.add(0,createNewLine(expression));
            }
            if (expression.getLastLineNumber()>last.getLastLineNumber()) {
                // there's a new line between { -> ... and the first statement
                statements.add(createNewLine(expression));
            }
        }
        expression.setCode(block);
    }
}
 
Example 8
Source File: ConstructorNodeUtils.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Return the first statement from the constructor code if it is a call to super or this, otherwise null.
 *
 * @param code
 * @return the first statement if a special call or null
 */
public static ConstructorCallExpression getFirstIfSpecialConstructorCall(Statement code) {
    if (code == null) return null;

    if (code instanceof BlockStatement) {
        final BlockStatement block = (BlockStatement) code;
        final List<Statement> statementList = block.getStatements();
        if (statementList.isEmpty()) return null;
        // handle blocks of blocks
        return getFirstIfSpecialConstructorCall(statementList.get(0));
    }

    if (!(code instanceof ExpressionStatement)) return null;

    Expression expression = ((ExpressionStatement) code).getExpression();
    if (!(expression instanceof ConstructorCallExpression)) return null;
    ConstructorCallExpression cce = (ConstructorCallExpression) expression;
    if (cce.isSpecialCall()) return cce;
    return null;
}
 
Example 9
Source File: ClassNode.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) {
    MethodNode method = getOrAddStaticConstructorNode();
    BlockStatement block = getCodeAsBlock(method);

    // while anything inside a static initializer block is appended
    // we don't want to append in the case we have a initialization
    // expression of a static field. In that case we want to add
    // before the other statements
    if (!fieldInit) {
        block.addStatements(staticStatements);
    } else {
        List<Statement> blockStatements = block.getStatements();
        staticStatements.addAll(blockStatements);
        blockStatements.clear();
        blockStatements.addAll(staticStatements);
    }
}
 
Example 10
Source File: RulesVisitor.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void visitBlockStatement(BlockStatement block) {
    block.setNodeMetaData(AST_NODE_METADATA_KEY, true);

    for (Statement statement : block.getStatements()) {
        statement.visit(this);
    }
}
 
Example 11
Source File: RulesVisitor.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void visitBlockStatement(BlockStatement block) {
    block.setNodeMetaData(AST_NODE_METADATA_KEY, true);

    for (Statement statement : block.getStatements()) {
        statement.visit(this);
    }
}
 
Example 12
Source File: SecureASTCustomizer.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitBlockStatement(final BlockStatement block) {
    assertStatementAuthorized(block);
    for (Statement statement : block.getStatements()) {
        statement.visit(this);
    }
}
 
Example 13
Source File: MarkupBuilderCodeTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void extractModelTypesFromStatement(final Statement code, final Map<String, ClassNode> model) {
    if (code instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) code;
        for (Statement statement : block.getStatements()) {
            extractModelTypesFromStatement(statement, model);
        }
    } else if (code instanceof ExpressionStatement) {
        Expression expression = ((ExpressionStatement) code).getExpression();
        if (expression instanceof DeclarationExpression) {
            VariableExpression var = ((DeclarationExpression) expression).getVariableExpression();
            model.put(var.getName(), var.getOriginType());
        }
    }
}
 
Example 14
Source File: AndroidGradleDependenciesVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Override
public void visitBlockStatement(BlockStatement block) {
    if (block.equals(rootBlockStatement)) {
        List<Statement> statements = block.getStatements();
        for (int i = 0; i < statements.size(); i++) {
            //workaround to exlude buildscript->dependencies
            buildScript = false;
            Statement st = statements.get(i);
            st.visit(this);
        }
    } else {
        super.visitBlockStatement(block);
    }
}
 
Example 15
Source File: ASTUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static ASTNode getVariableInBlockStatement(BlockStatement block, String variable) {
    for (Object object : block.getStatements()) {
        if (object instanceof ExpressionStatement) {
            ExpressionStatement expressionStatement = (ExpressionStatement) object;
            Expression expression = expressionStatement.getExpression();
            if (expression instanceof DeclarationExpression) {
                DeclarationExpression declaration = (DeclarationExpression) expression;
                if (variable.equals(declaration.getVariableExpression().getName())) {
                    return declaration.getVariableExpression();
                }
            }
        }
    }
    return null;
}
 
Example 16
Source File: CodeVisitorSupport.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitBlockStatement(BlockStatement block) {
    for (Statement statement : block.getStatements()) {
        statement.visit(this);
    }
}
 
Example 17
Source File: Verifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected void addInitialization(ClassNode node, ConstructorNode constructorNode) {
    Statement firstStatement = constructorNode.getFirstStatement();
    // if some transformation decided to generate constructor then it probably knows who it does
    if (firstStatement instanceof BytecodeSequence)
        return;

    ConstructorCallExpression first = getFirstIfSpecialConstructorCall(firstStatement);

    // in case of this(...) let the other constructor do the init
    if (first != null && (first.isThisCall())) return;

    List<Statement> statements = new ArrayList<Statement>();
    List<Statement> staticStatements = new ArrayList<Statement>();
    final boolean isEnum = node.isEnum();
    List<Statement> initStmtsAfterEnumValuesInit = new ArrayList<Statement>();
    Set<String> explicitStaticPropsInEnum = new HashSet<String>();
    if (isEnum) {
        for (PropertyNode propNode : node.getProperties()) {
            if (!propNode.isSynthetic() && propNode.getField().isStatic()) {
                explicitStaticPropsInEnum.add(propNode.getField().getName());
            }
        }
        for (FieldNode fieldNode : node.getFields()) {
            if (!fieldNode.isSynthetic() && fieldNode.isStatic() && fieldNode.getType() != node) {
                explicitStaticPropsInEnum.add(fieldNode.getName());
            }
        }
    }

    if (!Traits.isTrait(node)) {
        for (FieldNode fn : node.getFields()) {
            addFieldInitialization(statements, staticStatements, fn, isEnum,
                    initStmtsAfterEnumValuesInit, explicitStaticPropsInEnum);
        }
    }

    statements.addAll(node.getObjectInitializerStatements());

    BlockStatement block = getCodeAsBlock(constructorNode);
    List<Statement> otherStatements = block.getStatements();
    if (!otherStatements.isEmpty()) {
        if (first != null) {
            // it is super(..) since this(..) is already covered
            otherStatements.remove(0);
            statements.add(0, firstStatement);
        }
        Statement stmtThis$0 = getImplicitThis$0StmtIfInnerClass(otherStatements);
        if (stmtThis$0 != null) {
            // since there can be field init statements that depend on method/property dispatching
            // that uses this$0, it needs to bubble up before the super call itself (GROOVY-4471)
            statements.add(0, stmtThis$0);
        }
        statements.addAll(otherStatements);
    }
    BlockStatement newBlock = new BlockStatement(statements, block.getVariableScope());
    newBlock.setSourcePosition(block);
    constructorNode.setCode(newBlock);


    if (!staticStatements.isEmpty()) {
        if (isEnum) {
            /*
             * GROOVY-3161: initialize statements for explicitly declared static fields
             * inside an enum should come after enum values are initialized
             */
            staticStatements.removeAll(initStmtsAfterEnumValuesInit);
            node.addStaticInitializerStatements(staticStatements, true);
            if (!initStmtsAfterEnumValuesInit.isEmpty()) {
                node.positionStmtsAfterEnumInitStmts(initStmtsAfterEnumValuesInit);
            }
        } else {
            node.addStaticInitializerStatements(staticStatements, true);
        }
    }
}
 
Example 18
Source File: PathFinderVisitor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void fixNode(ASTNode node) {
    // FIXME http://jira.codehaus.org/browse/GROOVY-3263
    if (node instanceof MethodCallExpression && !((MethodCallExpression) node).isImplicitThis()) {
        MethodCallExpression call = (MethodCallExpression) node;
        if (call.getObjectExpression() == VariableExpression.THIS_EXPRESSION
                || call.getObjectExpression() == VariableExpression.SUPER_EXPRESSION) {
            // this is not bulletproof but fix most of the problems
            VariableExpression var = new VariableExpression(
                    call.getObjectExpression() == VariableExpression.THIS_EXPRESSION ? "this" : "super", // NOI18N
                    call.getObjectExpression().getType()); // NOI18N
            var.setLineNumber(call.getLineNumber());
            var.setColumnNumber(call.getColumnNumber());
            var.setLastLineNumber(call.getMethod().getLineNumber());
            var.setLastColumnNumber(call.getMethod().getColumnNumber());
            call.setObjectExpression(var);
        }
    // FIXME http://jira.codehaus.org/browse/GROOVY-3472
    } else if (node instanceof MethodNode || node instanceof ClosureExpression) {
        Statement code = null;
        if (node instanceof MethodNode) {
            code = ((MethodNode) node).getCode();
        } else {
            code = ((ClosureExpression) node).getCode();
        }

        if (code != null && code instanceof BlockStatement
                && ((code.getLineNumber() < 0 && code.getColumnNumber() < 0)
                || (code.getLastLineNumber() < 0 && code.getLastColumnNumber() < 0))) {
            BlockStatement block = (BlockStatement) code;
            List statements = block.getStatements();
            if (statements != null && !statements.isEmpty()) {
                if (code.getLineNumber() < 0 && code.getColumnNumber() < 0) {
                    Statement first = (Statement) statements.get(0);
                    code.setLineNumber(first.getLineNumber());
                    code.setColumnNumber(first.getColumnNumber());
                }
                if (code.getLastLineNumber() < 0 && code.getLastColumnNumber() < 0) {
                    // maybe not accurate
                    code.setLastLineNumber(node.getLastLineNumber());
                    int lastColumn = node.getLastColumnNumber();
                    if (lastColumn > 0) {
                        lastColumn--;
                    }
                    code.setLastColumnNumber(lastColumn);
                }
            }
        }
    }
}
 
Example 19
Source File: ReferenceDetectingVisitor.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void visitBlockStatement(BlockStatement block) {
    for (Statement statement : block.getStatements()) {
        statement.visit(this);
    }
}
 
Example 20
Source File: ReferenceDetectingVisitor.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void visitBlockStatement(BlockStatement block) {
    for (Statement statement : block.getStatements()) {
        statement.visit(this);
    }
}