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