public void writeTo(OutputStream out) throws IOException {
        String className = mUnit.getName();
        String targetPackage = mUnit.getTargetPackage();
        if (targetPackage != null) {
            className = targetPackage + '.' + className;
        }

        ClassFile classFile = new ClassFile(className);
        classFile.getModifiers().setFinal(true);

        String sourceFile = mUnit.getSourcePath();
        if (sourceFile != null) {
            classFile.setSourceFile(sourceFile);
        }

        final Template t = getParseTree();

        t.accept(new TreeWalker() {
            public Object visit(FunctionCallExpression node) {
                if (node.getSubstitutionParam() != null) {
                    mSubBlockCount++;
                    mGenerateSubFormat = true;
                }
                return super.visit(node);
            }

            public Object visit(TemplateCallExpression node) {
                if (node.getSubstitutionParam() != null) {
                    mSubBlockCount++;
                    mGenerateSubFormat = true;
                }
                return super.visit(node);
            }

            public Object visit(Variable node) {
                if (node.isField()) {
                    mGenerateSubFormat = true;
                }
                return super.visit(node);
            }
        });

        generateTemplate(t, className, classFile);

        classFile.writeTo(out);
        out.flush();
    }