Search in sources :

Example 6 with JavaOutputProcessor

use of spoon.support.JavaOutputProcessor in project spoon by INRIA.

the class DefaultPrettyPrinterTest method printClassCreatedWithSpoon.

@Test
public void printClassCreatedWithSpoon() throws Exception {
    /* test that spoon is able to print a class that it created without setting manually the output (default configuration) */
    final String nl = System.getProperty("line.separator");
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setSourceOutputDirectory(File.createTempFile("foo", "").getParentFile());
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtClass<Object> ctClass = factory.Class().create("foo.Bar");
    JavaOutputProcessor jop = launcher.createOutputWriter();
    jop.setFactory(factory);
    // JavaOutputProcessor is able to create the file even if we do not set the cu manually
    jop.createJavaFile(ctClass);
    String pathname = System.getProperty("java.io.tmpdir") + "/foo/Bar.java";
    File javaFile = new File(pathname);
    assertTrue(javaFile.exists());
    assertEquals("package foo;" + nl + nl + nl + "class Bar {}" + nl + nl, IOUtils.toString(new FileInputStream(javaFile), "UTF-8"));
}
Also used : Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) File(java.io.File) FileInputStream(java.io.FileInputStream) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Test(org.junit.Test)

Aggregations

JavaOutputProcessor (spoon.support.JavaOutputProcessor)6 Test (org.junit.Test)4 File (java.io.File)3 Launcher (spoon.Launcher)3 Environment (spoon.compiler.Environment)2 Factory (spoon.reflect.factory.Factory)2 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)2 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 LoggerFactory (org.slf4j.LoggerFactory)1 CtAnnotation (spoon.reflect.declaration.CtAnnotation)1 CtAnnotationType (spoon.reflect.declaration.CtAnnotationType)1 CtNamedElement (spoon.reflect.declaration.CtNamedElement)1 CtPackage (spoon.reflect.declaration.CtPackage)1 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)1