diff --git a/src/main/java/org/fulib/classmodel/FMethod.java b/src/main/java/org/fulib/classmodel/FMethod.java index d24c2c64..df1cc875 100644 --- a/src/main/java/org/fulib/classmodel/FMethod.java +++ b/src/main/java/org/fulib/classmodel/FMethod.java @@ -125,7 +125,7 @@ public String getDeclaration() // no fulib if (this.annotations != null && !this.annotations.isEmpty()) { builder.append(this.annotations); - builder.append("\n "); + builder.append('\n'); } builder.append(this.modifiers); diff --git a/src/main/resources/org/fulib/templates/method.stg b/src/main/resources/org/fulib/templates/method.stg index 4ce2bcdf..70fc2900 100644 --- a/src/main/resources/org/fulib/templates/method.stg +++ b/src/main/resources/org/fulib/templates/method.stg @@ -3,9 +3,7 @@ method: >> method(method, body) ::= << - - - () + { } diff --git a/src/test/java/org/fulib/generator/FMethodTest.java b/src/test/java/org/fulib/generator/FMethodTest.java index 1be8b003..d023c484 100644 --- a/src/test/java/org/fulib/generator/FMethodTest.java +++ b/src/test/java/org/fulib/generator/FMethodTest.java @@ -5,16 +5,15 @@ import org.fulib.builder.ClassModelManager; import org.fulib.classmodel.ClassModel; import org.fulib.classmodel.Clazz; -import org.fulib.classmodel.FMethod; import org.junit.jupiter.api.Test; import java.io.File; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; +import java.util.Arrays; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; public class FMethodTest @@ -29,73 +28,41 @@ public void testFMethods() throws Exception Tools.removeDirAndFiles(targetFolder); - ClassModelManager mm = new ClassModelManager(); + final ClassModelManager mm = new ClassModelManager(); mm.setMainJavaDir(sourceFolder); mm.setPackageName(packageName); - Clazz party = mm.haveClass("Party"); - // mm.haveAttribute(party, "name", "String"); + final Clazz party = mm.haveClass("Party"); - FMethod method = mm.haveMethod(party, "public void hello()", " System.out.println(\"World!\");\n"); + mm.haveMethod(party, "@Deprecated public int theAnswer()", "return 42;"); + mm.haveMethod(party, "public int theAnswer(int question)", "return question * 2;"); + mm.haveMethod(party, "public import(java.util.List) generic(T... args)", + "return import(java.util.Arrays).asList(args);"); - ClassModel model = mm.getClassModel(); + final ClassModel model = mm.getClassModel(); Fulib.generator().generate(model); - int returnCode = Tools.javac(outFolder, model.getPackageSrcFolder()); + final int returnCode = Tools.javac(outFolder, model.getPackageSrcFolder()); assertThat("compiler return code: ", returnCode, is(0)); - // does removal work? - party.withoutMethods(method); - - Fulib.generator().generate(model); - - returnCode = Tools.javac(outFolder, model.getPackageSrcFolder()); - assertThat("compiler return code: ", returnCode, is(0)); - - method = mm.haveMethod(party, "public int theAnswer()", " return 42;\n"); - - Fulib.generator().generate(model); - - returnCode = Tools.javac(outFolder, model.getPackageSrcFolder()); - assertThat("compiler return code: ", returnCode, is(0)); - - // add a parameter - method.getParams().put("question", "int"); - method.setMethodBody(" return question * 2;\n"); - - Fulib.generator().generate(model); - - returnCode = Tools.javac(outFolder, model.getPackageSrcFolder()); - assertThat("compiler return code: ", returnCode, is(0)); - - mm.haveMethod(party, "@import(org.junit.jupiter.api.Test) public void testQuestion()", - "import(static org.hamcrest.MatcherAssert.assertThat)(theAnswer(21), import(static org.hamcrest.CoreMatchers.equalTo)(42));"); - - Fulib.generator().generate(model); - - mm.haveMethod(party, "public int theAnswer()", " return 42;\n"); - - // TODO why 3 times? - Fulib.generator().generate(model); - Fulib.generator().generate(model); - Fulib.generator().generate(model); - - returnCode = Tools.javac(outFolder, model.getPackageSrcFolder()); - assertThat("compiler return code: ", returnCode, is(0)); - - try (final URLClassLoader classLoader = URLClassLoader - .newInstance(new URL[] { new File(outFolder).toURI().toURL() })) + try (final URLClassLoader classLoader = URLClassLoader.newInstance( + new URL[] { new File(outFolder).toURI().toURL() })) { final Class partyClass = Class.forName(model.getPackageName() + ".Party", true, classLoader); - final Method answerMethod = partyClass.getMethod("theAnswer", int.class); - final Method secondMethod = partyClass.getMethod("testQuestion"); - final Object theParty = partyClass.newInstance(); - final Object answer = answerMethod.invoke(theParty, 23); - assertThat(answer, equalTo(46)); + final Method answerMethod = partyClass.getMethod("theAnswer"); + assertThat(answerMethod.getAnnotation(Deprecated.class), notNullValue()); + assertThat(answerMethod.invoke(theParty), equalTo(42)); + + final Method answerMethod2 = partyClass.getMethod("theAnswer", int.class); + assertThat(answerMethod2.invoke(theParty, 23), equalTo(46)); + + final Method genericMethod = partyClass.getMethod("generic", Number[].class); + assertThat(genericMethod.isVarArgs(), is(true)); + assertThat(genericMethod.getTypeParameters()[0].getBounds()[0], is(Number.class)); - secondMethod.invoke(theParty); + assertThat(genericMethod.invoke(theParty, (Object) new Number[] { 1, 2, 3 }), equalTo(Arrays.asList(1, 2, 3))); } } }