Package org.codehaus.groovy.control

Examples of org.codehaus.groovy.control.Janitor


       
        boolean rewriteAssert = true;
        // don't rewrite assertions with message
        rewriteAssert = statement.getMessageExpression() == ConstantExpression.NULL;
        AssertionTracker oldTracker = assertionTracker;
        Janitor janitor = new Janitor();
        final Label tryStart = new Label();
        if (rewriteAssert){
            assertionTracker = new AssertionTracker();
            try {
                // because source position seems to be more reliable for statements
                // than for expressions, we get the source text for the whole statement
                assertionTracker.sourceText = new SourceText(statement, controller.getSourceUnit(), janitor);
                mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/powerassert/ValueRecorder");
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "<init>", "()V");
                //TODO: maybe use more specialized type here
                controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
                assertionTracker.recorderIndex = controller.getCompileStack().defineTemporaryVariable("recorder", true);
                mv.visitLabel(tryStart);
            } catch (SourceTextNotAvailableException e) {
                // don't rewrite assertions w/o source text
                rewriteAssert = false;
            }
        }
       
        statement.getBooleanExpression().visit(controller.getAcg());

        Label exceptionThrower = operandStack.jump(IFEQ);

        // do nothing, but clear the value recorder
        if (rewriteAssert) {
            //clean up assertion recorder
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    "org/codehaus/groovy/runtime/powerassert/ValueRecorder",
                    "clear",
                    "()V");
        }
        Label afterAssert = new Label();
        mv.visitJumpInsn(GOTO, afterAssert);
        mv.visitLabel(exceptionThrower);
       
        if (rewriteAssert) {
            mv.visitLdcInsn(assertionTracker.sourceText.getNormalizedText());
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKESTATIC,
                    "org/codehaus/groovy/runtime/powerassert/AssertionRenderer",
                    "render",
                    "(Ljava/lang/String;Lorg/codehaus/groovy/runtime/powerassert/ValueRecorder;)Ljava/lang/String;"
            );
        } else {
            writeSourcelessAssertText(statement);
        }
        operandStack.push(ClassHelper.STRING_TYPE);
        AssertionTracker savedTracker = assertionTracker;
        assertionTracker = null;
       
        // now the optional exception expression
        statement.getMessageExpression().visit(controller.getAcg());
        operandStack.box();
        assertFailedMethod.call(mv);
        operandStack.remove(2); // assertFailed called static with 2 arguments
       
        if (rewriteAssert) {
            final Label tryEnd = new Label();
            mv.visitLabel(tryEnd);
            mv.visitJumpInsn(GOTO, afterAssert);
            // finally block to clean assertion recorder
            final Label catchAny = new Label();
            mv.visitLabel(catchAny);
            mv.visitVarInsn(ALOAD, savedTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    "org/codehaus/groovy/runtime/powerassert/ValueRecorder",
                    "clear",
                    "()V");
            mv.visitInsn(ATHROW);
            // add catch any block to exception table
            controller.getCompileStack().addExceptionBlock(tryStart, tryEnd, catchAny, null);
        }
       
        mv.visitLabel(afterAssert);
        if (rewriteAssert) {
            controller.getCompileStack().removeVar(savedTracker.recorderIndex);
        }
        assertionTracker = oldTracker;
        // close possibly open file handles from getting a sample for
        // power asserts
        janitor.cleanup();
    }
View Full Code Here


    private Janitor janitor;

    @Before
    public void setUp() throws Exception {
        readerSource = new AbstractReaderSourceSubclass();
        janitor = new Janitor();
    }
View Full Code Here

        boolean rewriteAssert = true;
        // don't rewrite assertions with message
        rewriteAssert = statement.getMessageExpression() == ConstantExpression.NULL;
        AssertionTracker oldTracker = assertionTracker;
        Janitor janitor = new Janitor();
        final Label tryStart = new Label();
        if (rewriteAssert) {
            assertionTracker = new AssertionTracker();
            try {
                // because source position seems to be more reliable for statements
                // than for expressions, we get the source text for the whole statement
                assertionTracker.sourceText = new SourceText(statement, source, janitor);
                mv.visitTypeInsn(NEW, "org/codehaus/groovy/transform/powerassert/ValueRecorder");
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/transform/powerassert/ValueRecorder", "<init>", "()V");
                assertionTracker.recorderIndex = compileStack.defineTemporaryVariable("recorder", true);
                mv.visitLabel(tryStart);
            } catch (SourceTextNotAvailableException e) {
                // don't rewrite assertions w/o source text
                rewriteAssert = false;
                assertionTracker = oldTracker;
            }
        }

        BooleanExpression booleanExpression = statement.getBooleanExpression();
        booleanExpression.visit(this);

        Label exceptionThrower = new Label();
        mv.visitJumpInsn(IFEQ, exceptionThrower);

        // do nothing, but clear the value recorder
        if (rewriteAssert) {
            //clean up assertion recorder
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    "org/codehaus/groovy/transform/powerassert/ValueRecorder",
                    "clear",
                    "()V");
        }
        Label afterAssert = new Label();
        mv.visitJumpInsn(GOTO, afterAssert);
        mv.visitLabel(exceptionThrower);

        if (rewriteAssert) {
            mv.visitLdcInsn(assertionTracker.sourceText.getNormalizedText());
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKESTATIC,
                    "org/codehaus/groovy/transform/powerassert/AssertionRenderer",
                    "render",
                    "(Ljava/lang/String;Lorg/codehaus/groovy/transform/powerassert/ValueRecorder;)Ljava/lang/String;"
            );
        } else {
            writeSourclessAssertText(statement);
        }
        AssertionTracker savedTracker = assertionTracker;
        assertionTracker = null;

        // now the optional exception expression
        visitAndAutoboxBoolean(statement.getMessageExpression());
        assertFailedMethod.call(mv);

        if (rewriteAssert) {
            final Label tryEnd = new Label();
            mv.visitLabel(tryEnd);
            mv.visitJumpInsn(GOTO, afterAssert);
            // finally block to clean assertion recorder
            final Label catchAny = new Label();
            mv.visitLabel(catchAny);
            mv.visitVarInsn(ALOAD, savedTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    "org/codehaus/groovy/transform/powerassert/ValueRecorder",
                    "clear",
                    "()V");
            mv.visitInsn(ATHROW);
            // add catch any block to exception table
            compileStack.addExceptionBlock(tryStart, tryEnd, catchAny, null);
        }

        mv.visitLabel(afterAssert);
        assertionTracker = oldTracker;
        // close possibly open file handles from getting a sample for
        // power asserts
        janitor.cleanup();
    }
View Full Code Here

    @Deprecated
    public static final VariableExpression THIS_EXPR = new VariableExpression("this");


    public static void warning(final SourceUnit sourceUnit, final ASTNode node, final String warningMessage) {
        final String sample = sourceUnit.getSample(node.getLineNumber(), node.getColumnNumber(), new Janitor());
        GrailsConsole.getInstance().warning(warningMessage + "\n\n" + sample);
    }
View Full Code Here

    private Janitor janitor;

    @Before
    public void setUp() throws Exception {
        readerSource = new AbstractReaderSourceSubclass();
        janitor = new Janitor();
    }
View Full Code Here

       
        boolean rewriteAssert = true;
        // don't rewrite assertions with message
        rewriteAssert = statement.getMessageExpression() == ConstantExpression.NULL;
        AssertionTracker oldTracker = assertionTracker;
        Janitor janitor = new Janitor();
        final Label tryStart = new Label();
        if (rewriteAssert){
            assertionTracker = new AssertionTracker();
            try {
                // because source position seems to be more reliable for statements
                // than for expressions, we get the source text for the whole statement
                assertionTracker.sourceText = new SourceText(statement, controller.getSourceUnit(), janitor);
                mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/powerassert/ValueRecorder");
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "<init>", "()V", false);
                //TODO: maybe use more specialized type here
                controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
                assertionTracker.recorderIndex = controller.getCompileStack().defineTemporaryVariable("recorder", true);
                mv.visitLabel(tryStart);
            } catch (SourceTextNotAvailableException e) {
                // set assertionTracker to null to deactivate AssertionWriter#record calls
                assertionTracker = null;
                // don't rewrite assertions w/o source text
                rewriteAssert = false;
            }
        }
       
        statement.getBooleanExpression().visit(controller.getAcg());

        Label exceptionThrower = operandStack.jump(IFEQ);

        // do nothing, but clear the value recorder
        if (rewriteAssert) {
            //clean up assertion recorder
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "clear", "()V", false);
        }
        Label afterAssert = new Label();
        mv.visitJumpInsn(GOTO, afterAssert);
        mv.visitLabel(exceptionThrower);
       
        if (rewriteAssert) {
            mv.visitLdcInsn(assertionTracker.sourceText.getNormalizedText());
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/powerassert/AssertionRenderer", "render", "(Ljava/lang/String;Lorg/codehaus/groovy/runtime/powerassert/ValueRecorder;)Ljava/lang/String;", false);
        } else {
            writeSourcelessAssertText(statement);
        }
        operandStack.push(ClassHelper.STRING_TYPE);
        AssertionTracker savedTracker = assertionTracker;
        assertionTracker = null;
       
        // now the optional exception expression
        statement.getMessageExpression().visit(controller.getAcg());
        operandStack.box();
        assertFailedMethod.call(mv);
        operandStack.remove(2); // assertFailed called static with 2 arguments
       
        if (rewriteAssert) {
            final Label tryEnd = new Label();
            mv.visitLabel(tryEnd);
            mv.visitJumpInsn(GOTO, afterAssert);
            // finally block to clean assertion recorder
            final Label catchAny = new Label();
            mv.visitLabel(catchAny);
            mv.visitVarInsn(ALOAD, savedTracker.recorderIndex);
            mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/runtime/powerassert/ValueRecorder", "clear", "()V", false);
            mv.visitInsn(ATHROW);
            // add catch any block to exception table
            controller.getCompileStack().addExceptionBlock(tryStart, tryEnd, catchAny, null);
        }
       
        mv.visitLabel(afterAssert);
        if (rewriteAssert) {
            controller.getCompileStack().removeVar(savedTracker.recorderIndex);
        }
        assertionTracker = oldTracker;
        // close possibly open file handles from getting a sample for
        // power asserts
        janitor.cleanup();
    }
View Full Code Here

        boolean rewriteAssert = true;
        // don't rewrite assertions with message
        rewriteAssert = statement.getMessageExpression() == ConstantExpression.NULL;
        AssertionTracker oldTracker = assertionTracker;
        Janitor janitor = new Janitor();
        final Label tryStart = new Label();
        if (rewriteAssert) {
            assertionTracker = new AssertionTracker();
            try {
                // because source position seems to be more reliable for statements
                // than for expressions, we get the source text for the whole statement
                assertionTracker.sourceText = new SourceText(statement, source, janitor);
                mv.visitTypeInsn(NEW, "org/codehaus/groovy/transform/powerassert/ValueRecorder");
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKESPECIAL, "org/codehaus/groovy/transform/powerassert/ValueRecorder", "<init>", "()V");
                assertionTracker.recorderIndex = compileStack.defineTemporaryVariable("recorder", true);
                mv.visitLabel(tryStart);
            } catch (SourceTextNotAvailableException e) {
                // don't rewrite assertions w/o source text
                rewriteAssert = false;
                assertionTracker = oldTracker;
            }
        }

        BooleanExpression booleanExpression = statement.getBooleanExpression();
        booleanExpression.visit(this);

        Label exceptionThrower = new Label();
        mv.visitJumpInsn(IFEQ, exceptionThrower);

        // do nothing, but clear the value recorder
        if (rewriteAssert) {
            //clean up assertion recorder
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    "org/codehaus/groovy/transform/powerassert/ValueRecorder",
                    "clear",
                    "()V");
        }
        Label afterAssert = new Label();
        mv.visitJumpInsn(GOTO, afterAssert);
        mv.visitLabel(exceptionThrower);

        if (rewriteAssert) {
            mv.visitLdcInsn(assertionTracker.sourceText.getNormalizedText());
            mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKESTATIC,
                    "org/codehaus/groovy/transform/powerassert/AssertionRenderer",
                    "render",
                    "(Ljava/lang/String;Lorg/codehaus/groovy/transform/powerassert/ValueRecorder;)Ljava/lang/String;"
            );
        } else {
            writeSourclessAssertText(statement);
        }
        AssertionTracker savedTracker = assertionTracker;
        assertionTracker = null;

        // now the optional exception expression
        visitAndAutoboxBoolean(statement.getMessageExpression());
        assertFailedMethod.call(mv);

        if (rewriteAssert) {
            final Label tryEnd = new Label();
            mv.visitLabel(tryEnd);
            mv.visitJumpInsn(GOTO, afterAssert);
            // finally block to clean assertion recorder
            final Label catchAny = new Label();
            mv.visitLabel(catchAny);
            mv.visitVarInsn(ALOAD, savedTracker.recorderIndex);
            mv.visitMethodInsn(
                    INVOKEVIRTUAL,
                    "org/codehaus/groovy/transform/powerassert/ValueRecorder",
                    "clear",
                    "()V");
            mv.visitInsn(ATHROW);
            // add catch any block to exception table
            compileStack.addExceptionBlock(tryStart, tryEnd, catchAny, null);
        }

        mv.visitLabel(afterAssert);
        assertionTracker = oldTracker;
        // close possibly open file handles from getting a sample for
        // power asserts
        janitor.cleanup();
    }
View Full Code Here

TOP

Related Classes of org.codehaus.groovy.control.Janitor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.