Search in sources :

Example 6 with DefaultJavaPrettyPrinter

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

the class PrinterTest method testListPrinter.

@Test
public void testListPrinter() {
    Launcher spoon = new Launcher();
    DefaultJavaPrettyPrinter pp = (DefaultJavaPrettyPrinter) spoon.createPrettyPrinter();
    PrinterHelper ph = new PrinterHelper(spoon.getEnvironment());
    TokenWriter tw = new DefaultTokenWriter(ph);
    pp.setPrinterTokenWriter(tw);
    ElementPrinterHelper elementPrinterHelper = pp.getElementPrinterHelper();
    String[] listString = new String[] { "un", "deux", "trois" };
    try (ListPrinter listPrinter = elementPrinterHelper.createListPrinter(true, "start", true, true, "next", true, true, "end")) {
        for (String s : listString) {
            listPrinter.printSeparatorIfAppropriate();
            tw.writeIdentifier(s);
        }
    }
    String expectedResult = " start un next deux next trois end";
    assertEquals(expectedResult, pp.toString());
}
Also used : ElementPrinterHelper(spoon.reflect.visitor.ElementPrinterHelper) DefaultTokenWriter(spoon.reflect.visitor.DefaultTokenWriter) TokenWriter(spoon.reflect.visitor.TokenWriter) Launcher(spoon.Launcher) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) DefaultTokenWriter(spoon.reflect.visitor.DefaultTokenWriter) ListPrinter(spoon.reflect.visitor.ListPrinter) ElementPrinterHelper(spoon.reflect.visitor.ElementPrinterHelper) PrinterHelper(spoon.reflect.visitor.PrinterHelper) Test(org.junit.Test)

Example 7 with DefaultJavaPrettyPrinter

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

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

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

the class GenericsTest method testBugCommonCollection.

@Test
public void testBugCommonCollection() throws Exception {
    try {
        CtClass<?> type = build("spoon.test.generics", "BugCollection");
        CtField<?> INSTANCE = type.getElements(new NamedElementFilter<>(CtField.class, "INSTANCE")).get(0);
        // assertTrue(INSTANCE.getDefaultExpression().getType().getActualTypeArguments().get(0)
        // instanceof CtAnnonTypeParameterReference);
        assertEquals("public static final spoon.test.generics.ACLass<?> INSTANCE = new spoon.test.generics.ACLass();", INSTANCE.toString());
        CtField<?> INSTANCE2 = type.getElements(new NamedElementFilter<>(CtField.class, "INSTANCE2")).get(0);
        INSTANCE2.setAnnotations(new ArrayList<CtAnnotation<?>>());
        assertEquals("public static final spoon.test.generics.ACLass<?> INSTANCE2 = new spoon.test.generics.ACLass();", INSTANCE2.toString());
        CtClass<?> ComparableComparator = type.getPackage().getElements(new NamedElementFilter<>(CtClass.class, "ComparableComparator")).get(0);
        assertTrue(ComparableComparator.toString().startsWith("class ComparableComparator<E extends java.lang.Comparable<? super E>>"));
        CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
        CtTypeReference<?> ref = x.getType();
        DefaultJavaPrettyPrinter pp = new DefaultJavaPrettyPrinter(new StandardEnvironment());
        // qualifed name
        assertEquals("java.util.Map$Entry", ref.getQualifiedName());
        // toString uses DefaultJavaPrettyPrinter
        assertEquals("java.util.Map.Entry", ref.toString());
        // now visitCtTypeReference
        assertEquals(java.util.Map.class, ref.getDeclaringType().getActualClass());
        pp.visitCtTypeReference(ref);
        assertEquals("java.util.Map.Entry", pp.getResult().toString());
        CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
        assertEquals("java.util.Map.Entry<?, ?> y;", y.toString());
        CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
        assertEquals("java.util.Map.Entry<java.lang.String, java.lang.Integer> z;", z.toString());
        // now as local variables
        CtLocalVariable<?> lx = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "lx")).get(0);
        assertEquals("java.util.Map.Entry lx", lx.toString());
        CtLocalVariable<?> ly = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "ly")).get(0);
        assertEquals("java.util.Map.Entry<?, ?> ly", ly.toString());
        CtLocalVariable<?> lz = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "lz")).get(0);
        assertEquals("java.util.Map.Entry<java.lang.String, java.lang.Integer> lz", lz.toString());
        CtLocalVariable<?> it = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "it")).get(0);
        assertEquals("java.util.Iterator<java.util.Map.Entry<?, ?>> it", it.toString());
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) StandardEnvironment(spoon.support.StandardEnvironment) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 10 with DefaultJavaPrettyPrinter

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

the class CompilationUnitWrapper method getContents.

@Override
public char[] getContents() {
    DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(type.getFactory().getEnvironment());
    List<CtType<?>> types = new ArrayList<>();
    types.add(type);
    printer.calculate(type.getPosition().getCompilationUnit(), types);
    String result = printer.getResult();
    char[] content = result.toCharArray();
    return content;
}
Also used : CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter)

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