Package com.laamella.javamodel

Source Code of com.laamella.javamodel.JavaFile

package com.laamella.javamodel;

import java.io.File;
import java.io.OutputStream;

import com.laamella.javamodel.io.OutputStreamFactory;
import com.laamella.javamodel.io.OutputStreamFactory.Streamer;

public class JavaFile extends Block {
    public final Imports imports = new Imports();
    public final String packageName;
    public final String name;

    public JavaFile(final String packageName, final String fileName) {
        this.packageName = packageName;
        this.name = fileName;
        add("package %s;", packageName).ln();
        addObject(imports);
    }

    public ClassBody class_(String opener, Object... args) {
        ln().open(opener, args);
        final ClassBody classBody = new ClassBody(getIndent(), imports);
        addObject(classBody);
        close();
        return classBody;
    }

    public void write(final OutputStreamFactory outputDir) throws Exception {
        checkIndentationMatches();
        final String packageDir = packageName.replace('.', File.separatorChar);
        outputDir.stream(packageDir, name + ".java", new Streamer() {
            public void stream(OutputStream outputStream) throws Exception {
                outputStream.write(JavaFile.this.toString().getBytes("utf-8"));
            }
        });
    }

}
TOP

Related Classes of com.laamella.javamodel.JavaFile

TOP
Copyright © 2018 www.massapi.com. 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.