Search in sources :

Example 6 with PrettyPrinter

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);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 7 with PrettyPrinter

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;"));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) Test(org.junit.Test)

Example 8 with PrettyPrinter

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;"));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) Test(org.junit.Test)

Example 9 with PrettyPrinter

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());
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ByteArrayInputStream(java.io.ByteArrayInputStream) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) Environment(spoon.compiler.Environment) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter)

Example 10 with PrettyPrinter

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();
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher)

Aggregations

PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 Launcher (spoon.Launcher)18 Test (org.junit.Test)17 CtType (spoon.reflect.declaration.CtType)17 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)17 ArrayList (java.util.ArrayList)16 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 StringTokenizer (java.util.StringTokenizer)2 CtClass (spoon.reflect.declaration.CtClass)2 CtPackage (spoon.reflect.declaration.CtPackage)2 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)1 Environment (spoon.compiler.Environment)1 MainTest (spoon.test.main.MainTest)1