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);
}
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;
}
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());
}
}
}
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());
}
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());
}
Aggregations