Example usage for javax.tools JavaFileObject getCharContent

List of usage examples for javax.tools JavaFileObject getCharContent

Introduction

In this page you can find the example usage for javax.tools JavaFileObject getCharContent.

Prototype

CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException;

Source Link

Document

Returns the character content of this file object, if available.

Usage

From source file:com.webcohesion.enunciate.modules.java_json_client.JavaJSONClientModule.java

protected void copyServerSideType(File sourceDir, TypeElement type) throws IOException {
    SourcePosition source = this.context.getProcessingEnvironment().findSourcePosition(type);
    JavaFileObject sourceFile = source.getSourceFile();
    File destFile = getServerSideDestFile(sourceDir, sourceFile, type);
    FileWriter writer = new FileWriter(destFile);
    debug("Writing server-side java type to %s.", destFile);
    writer.write(sourceFile.getCharContent(false).toString());
    writer.flush();/*from  w ww . j av  a2 s . com*/
    writer.close();
}

From source file:org.cloudfoundry.tools.compiler.CloudFoundryJavaCompilerTest.java

@Test
public void shouldCompileVirtualFile() throws Exception {
    JavaFileObject sourceFile = mock(JavaFileObject.class);
    given(sourceFile.getKind()).willReturn(Kind.SOURCE);
    given(sourceFile.getName()).willReturn("Example.java");
    given(sourceFile.getCharContent(anyBoolean())).willReturn(getExampleJavaContent());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    JavaFileObject classFile = mock(JavaFileObject.class);
    given(classFile.openOutputStream()).willReturn(outputStream);

    JavaFileManager fileManager = mock(JavaFileManager.class);
    given(fileManager.getJavaFileForOutput(Matchers.any(Location.class), anyString(), eq(Kind.CLASS),
            eq(sourceFile))).willReturn(classFile);
    Iterable<? extends JavaFileObject> compilationUnits = Collections.singleton(sourceFile);
    CompilationTask task = this.javaCompiler.getTask(null, fileManager, null, standardCompilerOptions(), null,
            compilationUnits);//from   w  w  w.j  ava2  s. c  o  m
    assertThat(task.call(), is(Boolean.TRUE));
    assertThat(outputStream.toByteArray().length, is(greaterThan(0)));
}