Search in sources :

Example 1 with JavaOutputProcessor

use of spoon.support.JavaOutputProcessor in project dspot by STAMP-project.

the class DSpotUtils method printCtTypeToGivenDirectory.

public static void printCtTypeToGivenDirectory(CtType<?> type, File directory) {
    Factory factory = type.getFactory();
    Environment env = factory.getEnvironment();
    env.setAutoImports(true);
    env.setCommentEnabled(withComment);
    JavaOutputProcessor processor = new JavaOutputProcessor(new DefaultJavaPrettyPrinter(env));
    processor.setFactory(factory);
    processor.setOutputDirectory(directory);
    processor.createJavaFile(type);
    env.setAutoImports(false);
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Factory(spoon.reflect.factory.Factory) Environment(spoon.compiler.Environment) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) JavaOutputProcessor(spoon.support.JavaOutputProcessor)

Example 2 with JavaOutputProcessor

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

the class Launcher method createOutputWriter.

public JavaOutputProcessor createOutputWriter() {
    JavaOutputProcessor outputProcessor = new JavaOutputProcessor(createPrettyPrinter());
    outputProcessor.setFactory(this.getFactory());
    return outputProcessor;
}
Also used : JavaOutputProcessor(spoon.support.JavaOutputProcessor)

Example 3 with JavaOutputProcessor

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

the class PackageTest method testAddAnnotationToPackage.

@Test
public void testAddAnnotationToPackage() throws Exception {
    // contract: Created package-info should used imports in auto-import
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
    spoon.getEnvironment().setAutoImports(true);
    File outputDir = new File("./target/spoon-packageinfo");
    spoon.getEnvironment().setSourceOutputDirectory(outputDir);
    spoon.buildModel();
    CtAnnotationType annotationType = (CtAnnotationType) spoon.getFactory().Annotation().get(GlobalAnnotation.class);
    CtAnnotation annotation = spoon.getFactory().Core().createAnnotation();
    annotation.setAnnotationType(annotationType.getReference());
    CtPackage ctPackage = spoon.getFactory().Package().get("spoon.test.pkg.testclasses");
    ctPackage.addAnnotation(annotation);
    JavaOutputProcessor outputProcessor = spoon.createOutputWriter();
    outputProcessor.process(ctPackage);
    File packageInfo = new File(outputDir, "spoon/test/pkg/testclasses/package-info.java");
    assertTrue(packageInfo.exists());
    canBeBuilt(packageInfo, 8);
    List<String> lines = Files.readAllLines(packageInfo.toPath());
    assertFalse(lines.isEmpty());
    for (String s : lines) {
        if (s.trim().startsWith("import")) {
            assertEquals("import spoon.test.annotation.testclasses.GlobalAnnotation;", s.trim());
        }
        if (s.trim().startsWith("@")) {
            assertEquals("@GlobalAnnotation", s.trim());
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) Launcher(spoon.Launcher) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Test(org.junit.Test)

Example 4 with JavaOutputProcessor

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

the class APITest method testOverrideOutputWriter.

@Test
public void testOverrideOutputWriter() throws Exception {
    // this test that we can correctly set the Java output processor
    final List<Object> l = new ArrayList<Object>();
    Launcher spoon = new Launcher() {

        @Override
        public JavaOutputProcessor createOutputWriter() {
            return new JavaOutputProcessor() {

                @Override
                public void process(CtNamedElement e) {
                    l.add(e);
                }

                @Override
                public void init() {
                // we do nothing
                }
            };
        }
    };
    spoon.run(new String[] { "-i", "src/test/resources/spoon/test/api/", // we shouldn't write anything anyway
    "-o", // we shouldn't write anything anyway
    "fancy/fake/apitest" });
    Assert.assertEquals(3, l.size());
}
Also used : ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CtNamedElement(spoon.reflect.declaration.CtNamedElement) JavaOutputProcessor(spoon.support.JavaOutputProcessor) Test(org.junit.Test)

Example 5 with JavaOutputProcessor

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

the class LauncherTest method testInitEnvironmentDefault.

@Test
public void testInitEnvironmentDefault() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[0]);
    launcher.processArguments();
    final Environment environment = launcher.getEnvironment();
    // specify the default values
    Assert.assertFalse(environment.isAutoImports());
    Assert.assertFalse(environment.isUsingTabulations());
    Assert.assertFalse(environment.isPreserveLineNumbers());
    assertEquals(4, environment.getTabulationSize());
    Assert.assertTrue(environment.isCopyResources());
    JavaOutputProcessor processor = (JavaOutputProcessor) environment.getDefaultFileGenerator();
    Assert.assertTrue(processor.getPrinter() instanceof DefaultJavaPrettyPrinter);
    // now assertions on the model builder
    final SpoonModelBuilder builder = launcher.getModelBuilder();
    assertEquals(new File("spooned").getCanonicalFile(), builder.getSourceOutputDirectory());
    assertEquals(0, builder.getInputSources().size());
    assertEquals("UTF-8", environment.getEncoding().displayName());
}
Also used : Environment(spoon.compiler.Environment) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) File(java.io.File) 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