JavaGenerator extends Generator { @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); if (!javaOptions.generate_providence_core_types && ("net.morimekta.providence".equals(helper.getJavaPackage(program)) || "net.morimekta.providence.util".equals(helper.getJavaPackage(program)) || "net.morimekta.providence.model".equals(helper.getJavaPackage(program)))) { return; } 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(); } } } } JavaGenerator(FileManager manager,
                         GeneratorOptions generatorOptions,
                         JavaOptions javaOptions); @Override @SuppressWarnings("resource") void generate(@Nonnull ProgramTypeRegistry registry);  }
@Test public void testGenerate_defaults() throws GeneratorException, IOException { defaultSources(); JavaOptions options = new JavaOptions(); for (CProgram program : programs) { Generator generator = new JavaGenerator(fileManager, generatorOptions, options); generator.generate(programRegistry.registryForPath(program.getProgramFilePath())); } }
@Test public void testGenerate_jackson() throws GeneratorException, IOException { defaultSources(); JavaOptions options = new JavaOptions(); options.jackson = true; for (CProgram program : programs) { Generator generator = new JavaGenerator(fileManager, generatorOptions, options); generator.generate(programRegistry.registryForPath(program.getProgramFilePath())); } }
@Test public void testHazelcast() throws IOException { File src = tmp.newFolder("hz"); for (String res : ImmutableList.of( "/hazelcast/hazelcast.thrift")) { CProgram program = typeLoader.load(copyResourceTo(res, src)).getProgram(); programRegistry.putProgram(res, program); programs.add(program); } JavaOptions options = new JavaOptions(); options.hazelcast_portable = true; for (CProgram program : programs) { Generator generator = new JavaGenerator(fileManager, generatorOptions, options); generator.generate(programRegistry.registryForPath(program.getProgramFilePath())); } }