Search in sources :

Example 1 with JHelper

use of net.morimekta.providence.generator.format.java.utils.JHelper in project providence by morimekta.

the class JavaGenerator method generate.

@Override
@SuppressWarnings("resource")
public void generate(@Nonnull ProgramTypeRegistry registry) throws IOException, GeneratorException {
    CProgram program = registry.getProgram();
    String javaPackage = JUtils.getJavaPackage(program);
    JHelper helper = new JHelper(registry);
    String path = JUtils.getPackageClassPath(javaPackage);
    if (program.getConstants().size() > 0) {
        String file = helper.getConstantsClassName(program) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            constFomatter(writer, helper).appendProgramClass(program);
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    if (javaOptions.hazelcast_portable && program.getConstants().stream().anyMatch(t -> t.getName().equals("FACTORY_ID"))) {
        String file = helper.getHazelcastFactoryClassName(program) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            hazelcastFomatter(writer, helper).appendProgramClass(program);
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    for (PDeclaredDescriptor<?> type : program.getDeclaredTypes()) {
        String file = JUtils.getClassName(type) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            switch(type.getType()) {
                case MESSAGE:
                    messageFormatter(writer, helper).appendMessageClass((PMessageDescriptor<?, ?>) type);
                    break;
                case ENUM:
                    enumFormatter(writer).appendEnumClass((CEnumDescriptor) type);
                    break;
                default:
                    throw new GeneratorException("Unhandled declaration type.");
            }
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    for (CService service : program.getServices()) {
        String file = JUtils.getClassName(service) + ".java";
        OutputStream out = new BufferedOutputStream(getFileManager().create(path, file));
        try {
            IndentedPrintWriter writer = new IndentedPrintWriter(out);
            appendFileHeader(writer, helper, program);
            serviceFormatter(writer, helper).appendServiceClass(service);
            writer.flush();
        } finally {
            try {
                getFileManager().finalize(out);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) OutputStream(java.io.OutputStream) CService(net.morimekta.providence.reflect.contained.CService) HazelcastPortableProgramFormatter(net.morimekta.providence.generator.format.java.program.extras.HazelcastPortableProgramFormatter) BaseMessageFormatter(net.morimekta.providence.generator.format.java.shared.BaseMessageFormatter) CProgram(net.morimekta.providence.reflect.contained.CProgram) Generator(net.morimekta.providence.generator.Generator) GeneratorException(net.morimekta.providence.generator.GeneratorException) JUtils(net.morimekta.providence.generator.format.java.utils.JUtils) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter) BaseServiceFormatter(net.morimekta.providence.generator.format.java.shared.BaseServiceFormatter) IOException(java.io.IOException) PDeclaredDescriptor(net.morimekta.providence.descriptor.PDeclaredDescriptor) BufferedOutputStream(java.io.BufferedOutputStream) BaseProgramFormatter(net.morimekta.providence.generator.format.java.shared.BaseProgramFormatter) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) CEnumDescriptor(net.morimekta.providence.reflect.contained.CEnumDescriptor) FileManager(net.morimekta.providence.generator.util.FileManager) BaseEnumFormatter(net.morimekta.providence.generator.format.java.shared.BaseEnumFormatter) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) GeneratorOptions(net.morimekta.providence.generator.GeneratorOptions) Nonnull(javax.annotation.Nonnull) JHelper(net.morimekta.providence.generator.format.java.utils.JHelper) CProgram(net.morimekta.providence.reflect.contained.CProgram) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CService(net.morimekta.providence.reflect.contained.CService) IOException(java.io.IOException) GeneratorException(net.morimekta.providence.generator.GeneratorException) BufferedOutputStream(java.io.BufferedOutputStream) IndentedPrintWriter(net.morimekta.util.io.IndentedPrintWriter)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Nonnull (javax.annotation.Nonnull)1 PDeclaredDescriptor (net.morimekta.providence.descriptor.PDeclaredDescriptor)1 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)1 Generator (net.morimekta.providence.generator.Generator)1 GeneratorException (net.morimekta.providence.generator.GeneratorException)1 GeneratorOptions (net.morimekta.providence.generator.GeneratorOptions)1 HazelcastPortableProgramFormatter (net.morimekta.providence.generator.format.java.program.extras.HazelcastPortableProgramFormatter)1 BaseEnumFormatter (net.morimekta.providence.generator.format.java.shared.BaseEnumFormatter)1 BaseMessageFormatter (net.morimekta.providence.generator.format.java.shared.BaseMessageFormatter)1 BaseProgramFormatter (net.morimekta.providence.generator.format.java.shared.BaseProgramFormatter)1 BaseServiceFormatter (net.morimekta.providence.generator.format.java.shared.BaseServiceFormatter)1 JHelper (net.morimekta.providence.generator.format.java.utils.JHelper)1 JUtils (net.morimekta.providence.generator.format.java.utils.JUtils)1 FileManager (net.morimekta.providence.generator.util.FileManager)1 CEnumDescriptor (net.morimekta.providence.reflect.contained.CEnumDescriptor)1 CProgram (net.morimekta.providence.reflect.contained.CProgram)1 CService (net.morimekta.providence.reflect.contained.CService)1