Search in sources :

Example 1 with DefaultJavaPrettyPrinter

use of spoon.reflect.visitor.DefaultJavaPrettyPrinter 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 DefaultJavaPrettyPrinter

use of spoon.reflect.visitor.DefaultJavaPrettyPrinter in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessWithoutAnyImport.

@Test
public void testFieldAccessWithoutAnyImport() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/Kuu.java");
    launcher.addInputResource("./src/test/java/spoon/test/fieldaccesses/testclasses/Mole.java");
    launcher.run();
    final CtType<Kuu> aType = launcher.getFactory().Type().get(Kuu.class);
    final DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(aType.getFactory().getEnvironment());
    assertEquals(0, printer.computeImports(aType).size());
    assertEquals("spoon.test.fieldaccesses.testclasses.Mole.Delicious delicious", aType.getMethodsByName("m").get(0).getParameters().get(0).toString());
}
Also used : Launcher(spoon.Launcher) Kuu(spoon.test.fieldaccesses.testclasses.Kuu) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) Test(org.junit.Test)

Example 3 with DefaultJavaPrettyPrinter

use of spoon.reflect.visitor.DefaultJavaPrettyPrinter in project spoon by INRIA.

the class CtReferenceImpl method toString.

@Override
public String toString() {
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(getFactory().getEnvironment());
    printer.scan(this);
    return printer.toString();
}
Also used : DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter)

Example 4 with DefaultJavaPrettyPrinter

use of spoon.reflect.visitor.DefaultJavaPrettyPrinter in project spoon by INRIA.

the class DefaultPrettyPrinterTest method importsFromMultipleTypesSupported.

@Test
public void importsFromMultipleTypesSupported() {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/prettyprinter/testclasses/A.java");
    launcher.run();
    Environment env = launcher.getEnvironment();
    env.setAutoImports(true);
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(env);
    printer.calculate(null, Arrays.asList(launcher.getFactory().Class().get("spoon.test.prettyprinter.testclasses.A"), launcher.getFactory().Class().get("spoon.test.prettyprinter.testclasses.B")));
    assertTrue(printer.getResult().contains("import java.util.ArrayList;"));
}
Also used : Launcher(spoon.Launcher) Environment(spoon.compiler.Environment) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) Test(org.junit.Test)

Example 5 with DefaultJavaPrettyPrinter

use of spoon.reflect.visitor.DefaultJavaPrettyPrinter in project spoon by INRIA.

the class LinesTest method testPrettyPrinterWithLines.

@Test
public void testPrettyPrinterWithLines() throws Exception {
    for (CtType<?> t : factory.Type().getAll()) {
        if (t.isTopLevel()) {
            // System.out.println("calculate " + t.getSimpleName());
            DefaultJavaPrettyPrinter pp = new DefaultJavaPrettyPrinter(factory.getEnvironment());
            pp.calculate(t.getPosition().getCompilationUnit(), t.getPosition().getCompilationUnit().getDeclaredTypes());
        // System.out.println(pp.getResult().toString());
        }
    }
    assertEquals(0, factory.getEnvironment().getWarningCount());
    assertEquals(0, factory.getEnvironment().getErrorCount());
    // contract: in line preserve mode, toString is not prefixed or suffixed by newlines
    String meth = factory.Type().get("spoon.test.prettyprinter.Validation").getMethodsByName("isIdentifier").get(0).toString();
    // the added linebreaks due to line preservation are removed
    assertFalse(Pattern.compile("^\\s", Pattern.DOTALL).asPredicate().test(meth.toString()));
}
Also used : DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) Test(org.junit.Test)

Aggregations

DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)16 Test (org.junit.Test)11 Launcher (spoon.Launcher)6 Environment (spoon.compiler.Environment)4 CtClass (spoon.reflect.declaration.CtClass)3 CtType (spoon.reflect.declaration.CtType)3 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 ArrayList (java.util.ArrayList)2 CtImport (spoon.reflect.declaration.CtImport)2 CtPackage (spoon.reflect.declaration.CtPackage)2 Factory (spoon.reflect.factory.Factory)2 DefaultTokenWriter (spoon.reflect.visitor.DefaultTokenWriter)2 ElementPrinterHelper (spoon.reflect.visitor.ElementPrinterHelper)2 PrinterHelper (spoon.reflect.visitor.PrinterHelper)2 TokenWriter (spoon.reflect.visitor.TokenWriter)2 JavaOutputProcessor (spoon.support.JavaOutputProcessor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 INameEnvironment (org.eclipse.jdt.internal.compiler.env.INameEnvironment)1