GClass { public String toCode() { StringBuilderr sb = new StringBuilderr(); if (this.name.packageName != null) { sb.line("package {};", this.name.packageName); sb.line(); } if (this.isAnonymous) { sb.line("new {}() {", this.name.simpleNameWithGenerics); } else { if (this.imports.size() > 0) { for (String importClassName : this.imports) { sb.line("import {};", importClassName); } sb.line(); } for (String annotation : this.annotations) { sb.line(annotation); } sb.append(this.access.asPrefix()); if (this.isStaticInnerClass) { sb.append("static "); } if (this.isAbstract) { sb.append("abstract "); } if (this.isEnum) { sb.append("enum "); } else if (this.isInterface) { sb.append("interface "); } else { sb.append("class "); } sb.append(this.name.simpleNameWithGenerics); sb.append(" "); if (!this.isInterface) { if (this.baseClassName != null) { sb.append("extends {} ", this.stripAndImportPackageIfPossible(this.baseClassName)); } if (this.implementsInterfaces.size() > 0) { sb.append("implements {} ", Join.commaSpace(this.implementsInterfaces)); } } else { List<String> interfaces = Copy.list(); if (this.baseClassName != null) { interfaces.add(this.baseClassName); } interfaces.addAll(this.implementsInterfaces); if (!interfaces.isEmpty()) { sb.append("extends {} ", Join.commaSpace(interfaces)); } } sb.line("{"); } if (this.isEnum && this.enumValues.size() > 0) { boolean hasMore = this.fields.size() > 0 || this.methods.size() > 0 || this.constructors.size() > 0; this.lineIfNotAnonymousOrInner(sb); for (Iterator<String> i = this.enumValues.iterator(); i.hasNext();) { sb.append(1, i.next()); sb.append(i.hasNext() ? "," : hasMore ? ";" : ""); sb.line(); } } if (this.fields.size() > 0) { this.lineIfNotAnonymousOrInner(sb); for (GField field : this.fields) { sb.append(1, field.toCode()); } } if (this.staticInitializer.toString().length() > 0) { sb.line(); sb.line(1, "static {"); sb.append(2, this.staticInitializer.toString()); sb.lineIfNeeded(); sb.line(1, "}"); } for (GMethod constructor : this.constructors) { this.lineIfNotAnonymousOrInner(sb); sb.append(1, constructor.toCode()); } if (this.methods.size() > 0) { for (GMethod method : this.methods) { this.lineIfNotAnonymousOrInner(sb); sb.append(1, method.toCode()); } } if (this.innerClasses.size() > 0) { for (GClass gc : this.innerClasses) { sb.line(); sb.append(1, gc.toCode()); } } this.lineIfNotAnonymousOrInner(sb); sb.line("}"); return sb.toString(); } GClass(String fullClassName);  GClass(GDirectory directory, String fullClassName); GDirectory getDirectory(); GClass setEnum(); GClass setInterface(); GClass addEnumValue(String value, Object... args); GClass addGetterSetter(String type, String name); boolean isSameClass(String fullNameWithOrWithoutGenerics); GClass getInnerClass(String name, Object... args); GClass notStatic(); GMethod getConstructor(String... typeAndNames); GMethod getConstructor(Argument arg0, Argument... _args); GMethod getConstructor(List<Argument> _args); boolean hasMethod(String name); GMethod getMethod(String name, Object... args); GMethod getMethod(String name, Argument arg0, Argument... _args); GMethod getMethod(String name, List<Argument> _args); GField getField(String name, Object... args); String toCode(); GClass setAbstract(); GClass setPackagePrivate(); GClass setPrivate(); GClass setAccess(Access access); GClass setAnonymous(); GClass addImports(Class<?>... types); GClass addImports(String... importClassNames); String getBaseClassName(); String getBaseQualifiedClassName(); GClass baseClass(Class<?> type); GClass baseClassName(String baseClassName, Object... args); String stripAndImportPackageIfPossible(String fullClassName); List<GMethod> getConstructors(); String toString(); String getPackageName(); String getSimpleName(); String getFullName(); String getFileName(); GClass implementsInterface(Class<?> interfaceClass); GClass implementsInterface(String interfaceFullName, Object... args); GClass addAnnotation(String annotation, Object... args); GClass addEquals(); GClass addEquals(String... fieldNames); GClass addEquals(Collection<String> fieldNames); GClass addHashCode(); GClass addHashCode(String... fieldNames); GClass addHashCode(Collection<String> fieldNames); GClass addToString(); GClass addToString(String... fieldNames); GClass addToString(Collection<String> fieldNames); final StringBuilderr staticInitializer; }
@Test public void testEmptyClass() { GClass gc = new GClass("Foo"); Assert.assertEquals(Join.lines( "public class Foo {", "", "}", ""), gc.toCode()); }
@Test public void testEmptyClassWithPackage() { GClass gc = new GClass("foo.bar.Foo"); Assert.assertEquals(Join.lines( "package foo.bar;", "", "public class Foo {", "", "}", ""), gc.toCode()); }