private byte[] generateJava() throws Exception { JDefinedClass classModel = codeModel._class(JMod.PUBLIC | JMod.FINAL, className, ClassType.CLASS); Class<?> contractInterface = Class.forName(contractInterfaceName); classModel._implements(contractInterface); classModel._extends(AbstractDataTransferObject.class); List<FieldModel> fields = determineFields(contractInterface); renderConstantsClass(classModel); renderElementsClass(classModel, fields); renderClassLevelAnnotations(classModel, fields); renderFields(classModel, fields); renderFutureElementsField(classModel); renderPrivateJaxbConstructor(classModel, fields); renderBuilderConstructor(classModel, fields); renderGetters(classModel, fields); renderBuilderClass(classModel, fields, contractInterface); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); codeModel.build(new SingleStreamCodeWriter(outputStream)); return outputStream.toByteArray(); }
/** * Test method for * {@link ServiceStubGenerator#generateStubInterface(Class, String, String, com.sun.codemodel.CodeWriter)} * . * * @throws NotRestInterfaceException * @throws Exception */ @Test public void test() throws NotRestInterfaceException, Exception { @Cleanup final ByteArrayOutputStream out = new ByteArrayOutputStream(); final SingleStreamCodeWriter writer = new SingleStreamCodeWriter(out); stubGenerator.generateStubInterface(TestWebserviceWithPath.class, TestWebserviceWithPath.class.getSimpleName() + "Stub", TestWebserviceWithPath.class .getPackage().getName() + ".stub", writer); writer.close(); assertEquals(FileUtils.readFileToString( new File("src/test/resources/com/strandls/alchemy/rest/client/stubgenerator" + "/TestWebserviceWithPathStub.txt")).trim(), new String(out.toByteArray(), Charset.defaultCharset()).trim()); }
/** * Test method for * {@link ServiceStubGenerator#generateStubInterface(Class, String, String, com.sun.codemodel.CodeWriter)} * . * * Ensures generic types are handled correctly. * * @throws NotRestInterfaceException * @throws Exception */ @Test public void testGenericTypes() throws NotRestInterfaceException, Exception { @Cleanup final ByteArrayOutputStream out = new ByteArrayOutputStream(); final SingleStreamCodeWriter writer = new SingleStreamCodeWriter(out); stubGenerator.generateStubInterface(TestWebserviceWithGenericTypes.class, TestWebserviceWithGenericTypes.class.getSimpleName() + "Stub", TestWebserviceWithGenericTypes.class.getPackage().getName() + ".stub", writer); writer.close(); System.out.println(new String(out.toByteArray(), Charset.defaultCharset()).trim()); assertEquals(FileUtils.readFileToString( new File("src/test/resources/com/strandls/alchemy/rest/client/stubgenerator" + "/TestWebserviceWithGenericTypes.txt")).trim(), new String(out.toByteArray(), Charset.defaultCharset()).trim()); }
public void testAnnotationImplements() throws Exception{ JCodeModel jmod = new JCodeModel(); // adding a test package JPackage pack = jmod._package("testAnnotationImplements"); // building an interface JDefinedClass interface1 = pack._interface("Interface1"); // adding annotations JDefinedClass annotation1 = pack._annotationTypeDeclaration("Annot1"); try{ //this is perfectly legal in CodeModel: annotation1._implements(interface1); fail("No Exception was thrown for Illegal behavior"); } catch ( IllegalArgumentException ie){ } jmod.build(new SingleStreamCodeWriter(System.out)); }
public String writeToString(ITypeLibrary lib) { write(lib); ByteArrayOutputStream os = new ByteArrayOutputStream(); CodeWriter wr = new SingleStreamCodeWriter(os); try { getModel().build(wr); return new String(os.toByteArray(), "UTF-8"); } catch (IOException e) { throw new IllegalStateException(); } }
@Test public void schemaHelper_ExtractsPojo_NestedUsingClasspath() throws Exception { URL url = Resources.getResource(path + "A.json"); String text = Resources.toString(url, Charsets.UTF_8); ApiBodyMetadata mapSchemaToPojo = SchemaHelper.mapSchemaToPojo(null, text, "com.test", "Fallback", null); assertNotNull(mapSchemaToPojo); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { mapSchemaToPojo.getCodeModel().build(new SingleStreamCodeWriter(bos)); printModel(bos); } catch (IOException e) { assertThat(e.getMessage(), is(nullValue())); } }
protected String serializeModel(JCodeModel jCodeModel) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { jCodeModel.build(new SingleStreamCodeWriter(bos)); } catch (IOException e) { assertThat(e.getMessage(), is(nullValue())); } return bos.toString(); }
private String serialiseModel(JCodeModel codeModel) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { jCodeModel.build(new SingleStreamCodeWriter(bos)); return bos.toString(); } catch (IOException e) { //do nothing } return ""; }
/** * Adding nested Annotations in JModel * @throws Exception */ // unused //@Test public void testNestedAnnotations() throws Exception{ JCodeModel jmod = new JCodeModel(); // adding a test package JPackage pack = jmod._package("testNestedAnnotations"); // building an interface JDefinedClass interface1 = pack._interface("Interface1"); // adding annotations JDefinedClass annotation1 = pack._annotationTypeDeclaration("Annot1"); JDefinedClass annotation2 = pack._annotationTypeDeclaration("Annot2"); // adding a method for annotation2 annotation1.method(JMod.NONE, String.class, "value"); //adding a method which has an annotation as type to annotation1 annotation2.method(JMod.NONE, annotation1.array(), "value"); // add an annotation to the Interface JAnnotationArrayMember paramarray = interface1.annotate(annotation2).paramArray("value"); paramarray.annotate(annotation1).param("value", "a"); //paramarray.annotate(annotation1).param("value", "b"); //paramarray.annotate(annotation1).param("value", "c"); jmod.build(new SingleStreamCodeWriter(System.out)); }
public void generate(final OutputStream ouputStream) throws IOException { this.codeModel.build(new SingleStreamCodeWriter(ouputStream)); }
public void write(OutputStream outputstream) throws java.io.IOException { this.model.build(new SingleStreamCodeWriter(outputstream)); }