ASTPrinter { public static String print(Doc doc) { final StringBuilder builder = new StringBuilder(doc.pkg.name.isEmpty() ? "" : ("package " + doc.pkg.name + "\n\n")); if (!doc.imports.isEmpty()) { for (Imprt imp : doc.imports) { builder.append("import " + imp.name + "\n"); } builder.append("\n"); } for (DataType dataType : doc.dataTypes) { builder.append(print(dataType)); builder.append("\n"); } return builder.toString(); }  static String print(Doc doc); static String printComments(String indent, List<JavaComment> comments); static String print(final String indent, JavaComment comment); static String print(DataType dataType); static String print(Constructor constructor); static String print(Arg arg); static String printArgModifiers(List<ArgModifier> modifiers); static String print(Type type); static String print(RefType type); static String print(PrimitiveType type); static String print(ArgModifier modifier); static String print(final Literal literal); static String print(Expression expression); static String print(Annotation annotation); static String print(AnnotationElement value); static String print(AnnotationValue value); static String print(BinaryOperator op);  }
@Test public void testPrimitiveTypes() { assertEquals("boolean", print(_Primitive(_BooleanType()))); assertEquals("byte", print(_Primitive(_ByteType()))); assertEquals("char", print(_Primitive(_CharType()))); assertEquals("short", print(_Primitive(_ShortType()))); assertEquals("int", print(_Primitive(_IntType()))); assertEquals("long", print(_Primitive(_LongType()))); assertEquals("float", print(_Primitive(_FloatType()))); assertEquals("double", print(_Primitive(_DoubleType()))); }
@Test public void testClassTypes() { assertEquals("String", print(_Ref(_ClassType("String", Util.<RefType> list())))); assertEquals("List<String>", print(_Ref(_ClassType("List", list(_ClassType("String", Util.<RefType> list())))))); assertEquals("Map<String, List<List<String>>>", print(_Ref(_ClassType("Map", list(_ClassType("String", Util.<RefType> list()), _ClassType("List", list((_ClassType("List", list(_ClassType("String", Util.<RefType> list()))))))))))); }
@Test public void testArrayTypes() { assertEquals("boolean[]", print(_Ref(_ArrayType(_Primitive(_BooleanType()))))); assertEquals("String[][]", print(_Ref(_ArrayType(_Ref(_ArrayType(_Ref(_ClassType("String", Util.<RefType> list())))))))); }
@Test public void testArg() { assertEquals("boolean[] Foo", print(new Arg(Util.<ArgModifier>list(), _Ref(_ArrayType(_Primitive(_BooleanType()))), "Foo"))); assertEquals("final boolean[] Foo", print(new Arg(list(_Final()), _Ref(_ArrayType(_Primitive(_BooleanType()))), "Foo"))); assertEquals("final boolean[] Foo", print(new Arg(list(_Final(), _Final()), _Ref(_ArrayType(_Primitive(_BooleanType()))), "Foo"))); }
@Test public void testArgModifier() { assertEquals("final", print(_Final())); assertEquals("volatile", print(_Volatile())); assertEquals("transient", print(_Transient())); }
@Test public void testConstructors() { assertEquals("Foo", print(new Constructor(NO_COMMENTS, "Foo", Util.<Arg> list()))); assertEquals("Foo(boolean hello, int World)", print(new Constructor(NO_COMMENTS, "Foo", list(new Arg( Util.<ArgModifier>list(), _Primitive(_BooleanType()), "hello"), new Arg(Util.<ArgModifier>list(), _Primitive(_IntType()), "World"))))); }
@Test public void testDataTypes() { assertEquals("Foo =\n" + " Bar\n" + " | Baz", print(new DataType(NO_COMMENTS, NO_ANNOTATIONS, "Foo", Util.<String>list(), NO_EXTENDS, NO_IMPLEMENTS, list(new Constructor(NO_COMMENTS, "Bar", Util.<Arg> list()), new Constructor(NO_COMMENTS, "Baz", Util.<Arg> list()))))); assertEquals("Foo extends FooA implements FooB, FooC =\n" + " Bar\n" + " | Baz", print(new DataType(NO_COMMENTS, NO_ANNOTATIONS, "Foo", Util.<String>list(), _Some(_ClassType("FooA", NO_TYPE_ARGS)), list(_ClassType("FooB", NO_TYPE_ARGS), _ClassType("FooC", NO_TYPE_ARGS)), list(new Constructor(NO_COMMENTS, "Bar", Util.<Arg> list()), new Constructor(NO_COMMENTS, "Baz", Util.<Arg> list()))))); assertEquals("@foo\n@foo( @bar )\nFoo =\n" + " Bar\n" + " | Baz", print(new DataType(NO_COMMENTS, list(_Annotation("foo", Optional.<AnnotationElement>_None()), _Annotation("foo", _Some(_ElementValue(_AnnotationValueAnnotation(_Annotation("bar", Optional.<AnnotationElement>_None())))))), "Foo", Util.<String>list(), NO_EXTENDS, NO_IMPLEMENTS, list(new Constructor(NO_COMMENTS, "Bar", Util.<Arg> list()), new Constructor(NO_COMMENTS, "Baz", Util.<Arg> list()))))); }
@Test public void testDoc() { assertEquals("", print(new Doc("PrinterTest", EMPTY_PKG, NO_IMPORTS, Util.<DataType> list()))); assertEquals("package some.package\n\n", print(new Doc("PrinterTest", Pkg._Pkg(NO_COMMENTS, "some.package"), NO_IMPORTS, Util.<DataType> list()))); assertEquals("import number.one\nimport number.two\n\n", print(new Doc("PrinterTest", EMPTY_PKG, list(Imprt._Imprt(NO_COMMENTS, "number.one"), Imprt._Imprt(NO_COMMENTS,"number.two")), Util.<DataType> list()))); assertEquals("package some.package\n\nimport number.one\nimport number.two\n\n", print(new Doc( "PrinterTest", Pkg._Pkg(NO_COMMENTS, "some.package"), list(Imprt._Imprt(NO_COMMENTS, "number.one"), Imprt._Imprt(NO_COMMENTS,"number.two")), Util.<DataType> list()))); assertEquals("package some.package\n\nimport number.one\nimport number.two\n\nFoo =\n Bar\n", print(new Doc( "PrinterTest", Pkg._Pkg(NO_COMMENTS, "some.package"), list(Imprt._Imprt(NO_COMMENTS, "number.one"), Imprt._Imprt(NO_COMMENTS,"number.two")), list(new DataType(NO_COMMENTS, NO_ANNOTATIONS, "Foo", Util.<String>list(), NO_EXTENDS, NO_IMPLEMENTS, list(new Constructor(NO_COMMENTS, "Bar", Util.<Arg> list()))))))); }