GMethod { public String toCode() { StringBuilderr sb = new StringBuilderr(); for (String annotation : this.annotations) { sb.line(annotation); } boolean isStaticBlock = this.isStatic && this.constructorFor != null; if (!isStaticBlock) { sb.append(this.access.asPrefix()); } if (this.isStatic) { sb.append("static "); } if (this.isAbstract) { sb.append("abstract "); } if (this.typeParameters != null) { sb.append("<{}> ", this.typeParameters); } if (this.constructorFor != null) { if (!isStaticBlock) { sb.append(this.constructorFor); } } else { sb.append("{} {}", this.returnClassName, this.getName()); } if (!isStaticBlock) { sb.append("({})", Join.commaSpace(this.arguments)); } if (this.exceptions.size() > 0) { sb.append(" throws "); List<String> exceptionTypes = new ArrayList<String>(); for (String exception : this.exceptions) { exceptionTypes.add(this.gclass.stripAndImportPackageIfPossible(exception)); } sb.append(Join.commaSpace(exceptionTypes)); } if (this.gclass.isInterface || this.isAbstract) { sb.line(";"); } else { sb.line(" {"); sb.append(1, this.body.toString()); sb.lineIfNeeded(); sb.line(0, "}"); } return sb.toString(); } GMethod(GClass gclass, String methodName); GMethod returnType(Class<?> type); GMethod returnType(String returnClassName, Object... args); GMethod argument(String typeClassName, String name); GMethod arguments(String... typeAndNames); GMethod arguments(List<Argument> args); GMethod constructorFor(String shortClassName); void setBody(String body); String toCode(); String getName(); GMethod setPrivate(); GMethod setProtected(); GMethod setAccess(Access access); GMethod setStatic(); GMethod setAbstract(); boolean hasArguments(); boolean hasSameArguments(String... typeAndNames); boolean hasSameArguments(List<Argument> other); GMethod addAnnotation(String annotation, Object... args); GMethod addOverride(); GMethod addThrows(String exception); GMethod typeParameters(String typeParameters); GMethod assignFields(); final StringBuilderr body; }
@Test public void testOverloadedMethods() { GClass gc = new GClass("Foo"); gc.getMethod("method(String one)"); gc.getMethod("method(Integer two)"); Assert.assertEquals(Join.lines( "public class Foo {", "", " public void method(String one) {", " }", "", " public void method(Integer two) {", " }", "", "}", ""), gc.toCode()); }
@Test public void testOverloadedMethodsWithArguments() { GClass gc = new GClass("Foo"); gc.getMethod("method", Argument.arg("String", "one")); gc.getMethod("method", Argument.arg("Integer", "two")); Assert.assertEquals(Join.lines( "public class Foo {", "", " public void method(String one) {", " }", "", " public void method(Integer two) {", " }", "", "}", ""), gc.toCode()); }
@Test public void testAutoImportDirectly() { GClass gc = new GClass("foo.Foo"); gc.addImports("foo.Zaz<java.lang.Integer>"); gc.addImports("foo.Set<java.util.Set<java.lang.Integer>>"); Assert.assertEquals(Join.lines( "package foo;", "", "public class Foo {", "", "}", ""), gc.toCode()); }
@Test public void testIndentDoesNotAddExtraWhiteSpace() { GClass gc = new GClass("foo.bar.Foo"); GMethod hello = gc.getMethod("hello"); hello.body.line("int i = 1;"); hello.body.line(""); hello.body.line("return i;"); Assert.assertEquals( Join.lines(new Object[] { "package foo.bar;", "", "public class Foo {", "", " public void hello() {", " int i = 1;", "", " return i;", " }", "", "}", "" }), gc.toCode()); }
@Test public void testOverloadedWithNoArguments() { GClass gc = new GClass("foo.bar.Foo"); gc.getMethod("hello()"); Assert.assertEquals( Join.lines(new Object[] { "package foo.bar;", "", "public class Foo {", "", " public void hello() {", " }", "", "}", "" }), gc.toCode()); }