Java 类com.sun.codemodel.writer.SingleStreamCodeWriter 实例源码

项目:kc-rice    文件:ImmutableJaxbGenerator.java   
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();

}
项目:alchemy-rest-client-generator    文件:ServiceStubGeneratorTest.java   
/**
 * 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());
}
项目:alchemy-rest-client-generator    文件:ServiceStubGeneratorTest.java   
/**
 * 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());

}
项目:jaob    文件:SmallJModelTest.java   
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));
}
项目:rice    文件:ImmutableJaxbGenerator.java   
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();

}
项目:kuali_rice    文件:ImmutableJaxbGenerator.java   
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();

}
项目:aml    文件:JavaWriter.java   
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();
    }
}
项目:springmvc-raml-plugin    文件:JsonSchema2PojoTest.java   
@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()));
      }
  }
项目:springmvc-raml-plugin    文件:AbstractRuleTestBase.java   
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();
}
项目:springmvc-raml-plugin    文件:RamlInterpreterTest.java   
private String serialiseModel(JCodeModel codeModel) {
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        jCodeModel.build(new SingleStreamCodeWriter(bos));
        return bos.toString();
    } catch (IOException e) {
        //do nothing
    }
    return "";
}
项目:jaob    文件:SmallJModelTest.java   
/**
 * 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));

}
项目:libraries    文件:BeanGenerator.java   
public void generate(final OutputStream ouputStream) throws IOException {
  this.codeModel.build(new SingleStreamCodeWriter(ouputStream));
}
项目:jsignalml    文件:JavaClassGen.java   
public void write(OutputStream outputstream)
    throws java.io.IOException
{
    this.model.build(new SingleStreamCodeWriter(outputstream));
}
项目:jsignalml    文件:JavaClassGen.java   
public void write(OutputStream outputstream)
    throws java.io.IOException
{
    this.model.build(new SingleStreamCodeWriter(outputstream));
}