use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class PrinterTest method testJDTBatchCompilerCanBeBuild.
@Test
public void testJDTBatchCompilerCanBeBuild() {
Launcher spoon = new Launcher();
PrettyPrinter printer = spoon.createPrettyPrinter();
spoon.getEnvironment().setAutoImports(false);
String output = "./target/spoon-jdtbatchcompiler/";
spoon.addInputResource("./src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java");
spoon.setSourceOutputDirectory(output);
spoon.run();
CtType element = spoon.getFactory().Class().getAll().get(0);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
printer.calculate(element.getPosition().getCompilationUnit(), toPrint);
String result = printer.getResult();
// assertTrue("The result should contain direct this accessor for field: "+result, !result.contains("Rule.Phoneme.this.phonemeText"));
canBeBuilt(output, 7);
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class PackageTest method testRenamePackageAndPrettyPrintNoclasspath.
@Test
public void testRenamePackageAndPrettyPrintNoclasspath() throws Exception {
final Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/resources/noclasspath/app/Test.java");
spoon.getEnvironment().setNoClasspath(true);
spoon.buildModel();
CtPackage ctPackage = spoon.getModel().getElements(new NamedElementFilter<CtPackage>(CtPackage.class, "app")).get(0);
ctPackage.setSimpleName("otherName");
CtClass foo = spoon.getModel().getElements(new NamedElementFilter<CtClass>(CtClass.class, "Test")).get(0);
assertEquals("otherName.Test", foo.getQualifiedName());
PrettyPrinter prettyPrinter = new DefaultJavaPrettyPrinter(spoon.getEnvironment());
prettyPrinter.calculate(spoon.getFactory().CompilationUnit().getOrCreate("./src/test/resources/noclasspath/app/Test.java"), Collections.singletonList(foo));
String result = prettyPrinter.getResult();
assertTrue(result.contains("package otherName;"));
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class PackageTest method testRenamePackageAndPrettyPrint.
@Test
public void testRenamePackageAndPrettyPrint() throws Exception {
final Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
spoon.buildModel();
CtPackage ctPackage = spoon.getModel().getElements(new NamedElementFilter<CtPackage>(CtPackage.class, "spoon")).get(0);
ctPackage.setSimpleName("otherName");
CtClass foo = spoon.getModel().getElements(new NamedElementFilter<CtClass>(CtClass.class, "Foo")).get(0);
assertEquals("otherName.test.pkg.testclasses.Foo", foo.getQualifiedName());
PrettyPrinter prettyPrinter = new DefaultJavaPrettyPrinter(spoon.getEnvironment());
prettyPrinter.calculate(spoon.getFactory().CompilationUnit().getOrCreate("./src/test/java/spoon/test/pkg/testclasses/Foo.java"), Collections.singletonList(foo));
String result = prettyPrinter.getResult();
assertTrue(result.contains("package otherName.test.pkg.testclasses;"));
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class JDTBasedSpoonCompiler method getCompilationUnitInputStream.
protected InputStream getCompilationUnitInputStream(String path) {
Environment env = factory.getEnvironment();
spoon.reflect.cu.CompilationUnit cu = factory.CompilationUnit().getMap().get(path);
List<CtType<?>> toBePrinted = cu.getDeclaredTypes();
PrettyPrinter printer = new DefaultJavaPrettyPrinter(env);
printer.calculate(cu, toBePrinted);
return new ByteArrayInputStream(printer.getResult().toString().getBytes());
}
use of spoon.reflect.visitor.PrettyPrinter in project spoon by INRIA.
the class AccessFullyQualifiedFieldTest method buildResourceAndReturnResult.
private String buildResourceAndReturnResult(String pathResource, String output) {
Launcher spoon = new Launcher();
// spoon.setArgs(new String[]{"--with-imports"});
spoon.addInputResource(pathResource);
spoon.setSourceOutputDirectory(output);
spoon.run();
PrettyPrinter prettyPrinter = spoon.createPrettyPrinter();
CtType element = spoon.getFactory().Class().getAll().get(0);
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);
prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
return prettyPrinter.getResult();
}
Aggregations