Java Code Examples for org.codehaus.groovy.ast.ClassNode#addStaticInitializerStatements()

The following examples show how to use org.codehaus.groovy.ast.ClassNode#addStaticInitializerStatements() . 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: Verifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected void addInitialization(final ClassNode node) {
    boolean addSwapInit = moveOptimizedConstantsInitialization(node);

    for (ConstructorNode cn : node.getDeclaredConstructors()) {
        addInitialization(node, cn);
    }

    if (addSwapInit) {
        BytecodeSequence seq = new BytecodeSequence(new BytecodeInstruction() {
            @Override
            public void visit(MethodVisitor mv) {
                mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(node), SWAP_INIT, "()V", false);
            }
        });

        List<Statement> swapCall = new ArrayList<>(1);
        swapCall.add(seq);
        node.addStaticInitializerStatements(swapCall, true);
    }
}
 
Example 2
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);
        }
    }
}