From 49078e47f2415dd9552ba4ffbd27f17d8083381a Mon Sep 17 00:00:00 2001 From: Philip Helger Date: Wed, 22 Aug 2018 10:19:49 +0200 Subject: [PATCH] Voided "invoke" like methods in JBlock; #62 --- README.md | 4 +- pom.xml | 4 +- .../java/com/helger/jcodemodel/JBlock.java | 104 +++++++++--------- .../helger/jcodemodel/ForEachFuncTest.java | 2 +- .../helger/jcodemodel/JDefinedClassTest.java | 2 +- .../helger/jcodemodel/JInvocationTest.java | 32 +++--- .../helger/jcodemodel/VarArgsFuncTest.java | 2 +- 7 files changed, 74 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index d7d8f1dc..925fa23b 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,10 @@ A site with the links to the [API docs](http://phax.github.io/jcodemodel/) etc. # News and noteworthy -* v3.0.4 - work in progress +* v3.1.0 - work in progress * Added ` AbstractJType._new`()` + * Change return types of special `JBlock` methods to `void` to avoid chaining (issue #62) - incompatible change + * Added new `JExpr.invokeThis` and `JExpr.invokeSuper` static methods * v3.0.3 - 2018-06-12 * Improved API access to inner classes (issue #60) * Changed order of emitted modifiers (`final static` -> `static final`) diff --git a/pom.xml b/pom.xml index bd540f48..5053bdc9 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ 1.10.5 jcodemodel - 3.0.4-SNAPSHOT + 3.1.0-SNAPSHOT bundle jcodemodel Java code generation library @@ -137,7 +137,7 @@ com.github.javaparser javaparser-core - 3.6.17 + 3.6.18 test diff --git a/src/main/java/com/helger/jcodemodel/JBlock.java b/src/main/java/com/helger/jcodemodel/JBlock.java index 99fc8300..1746a02f 100644 --- a/src/main/java/com/helger/jcodemodel/JBlock.java +++ b/src/main/java/com/helger/jcodemodel/JBlock.java @@ -380,23 +380,25 @@ public JBlock assignDivide (@Nonnull final IJAssignmentTarget aLhs, @Nonnull fin } /** - * Creates an invocation statement and adds it to this block. + * Creates an invocation statement and adds it to this block.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param aExpr * {@link IJExpression} evaluating to the class or object upon which * the named method will be invoked * @param sMethod * Name of method to invoke - * @return Newly generated {@link JInvocation} */ - @Nonnull - public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final String sMethod) + public void invoke (@Nonnull final IJExpression aExpr, @Nonnull final String sMethod) { - return invoke ((JCodeModel) null, aExpr, sMethod); + invoke ((JCodeModel) null, aExpr, sMethod); } /** - * Creates an invocation statement and adds it to this block. + * Creates an invocation statement and adds it to this block.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param aCM * CodeModel to use. May be null. @@ -405,123 +407,117 @@ public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final Str * the named method will be invoked * @param sMethod * Name of method to invoke - * @return Newly generated {@link JInvocation} - * @since 3.0.5 + * @since 3.1.0 */ - @Nonnull - public JInvocation invoke (@Nullable final JCodeModel aCM, - @Nonnull final IJExpression aExpr, - @Nonnull final String sMethod) + public void invoke (@Nullable final JCodeModel aCM, @Nonnull final IJExpression aExpr, @Nonnull final String sMethod) { - return internalInsert (new JInvocation (aCM, aExpr, sMethod)); + internalInsert (new JInvocation (aCM, aExpr, sMethod)); } /** - * Creates an invocation statement and adds it to this block. + * Creates an invocation statement and adds it to this block.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param sMethod * Name of method to invoke on this - * @return Newly generated {@link JInvocation} */ - @Nonnull - public JInvocation invokeThis (@Nonnull final String sMethod) + public void invokeThis (@Nonnull final String sMethod) { - return invoke (JExpr._this (), sMethod); + invoke (JExpr._this (), sMethod); } /** * Explicitly call the super class constructor in this block. This method may - * only be called as the first call inside a constructor block! + * only be called as the first call inside a constructor block!
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * - * @return Newly generated super {@link JInvocation} * @since 3.0.1 */ - @Nonnull - public JInvocation invokeSuper () + public void invokeSuper () { - return internalInsert (JInvocation._super ()); + internalInsert (JInvocation._super ()); } /** - * Creates an invocation statement and adds it to this block. + * Creates an invocation statement and adds it to this block.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param aExpr * {@link IJExpression} evaluating to the class or object upon which * the method will be invoked * @param aMethod * {@link JMethod} to invoke - * @return Newly generated {@link JInvocation} */ - @Nonnull - public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final JMethod aMethod) + public void invoke (@Nullable final IJExpression aExpr, @Nonnull final JMethod aMethod) { - return internalInsert (new JInvocation (aMethod.owner (), aExpr, aMethod)); + internalInsert (new JInvocation (aMethod.owner (), aExpr, aMethod)); } /** - * Creates an invocation statement and adds it to this block. + * Creates an invocation statement and adds it to this block.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param aMethod * {@link JMethod} to invoke on this - * @return Newly generated {@link JInvocation} */ - @Nonnull - public JInvocation invokeThis (@Nonnull final JMethod aMethod) + public void invokeThis (@Nonnull final JMethod aMethod) { - return invoke (JExpr._this (), aMethod); + invoke (JExpr._this (), aMethod); } /** - * Creates a static invocation statement. + * Creates a static invocation statement.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param aType * Type upon which the method should be invoked * @param sMethod * Name of method to invoke - * @return Newly generated {@link JInvocation} */ - @Nonnull - public JInvocation staticInvoke (@Nonnull final AbstractJClass aType, @Nonnull final String sMethod) + public void staticInvoke (@Nonnull final AbstractJClass aType, @Nonnull final String sMethod) { - return internalInsert (new JInvocation (aType.owner (), aType, sMethod)); + internalInsert (new JInvocation (aType.owner (), aType, sMethod)); } /** - * Creates an invocation statement and adds it to this block. + * Creates an invocation statement and adds it to this block.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param sMethod * Name of method to invoke - * @return Newly generated {@link JInvocation} */ - @Nonnull - public JInvocation invoke (@Nonnull final String sMethod) + public void invoke (@Nonnull final String sMethod) { - return internalInsert (new JInvocation ((JCodeModel) null, (IJExpression) null, sMethod)); + internalInsert (new JInvocation ((JCodeModel) null, (IJExpression) null, sMethod)); } /** - * Creates an invocation statement and adds it to this block. + * Creates an invocation statement and adds it to this block.
+ * Note: since 3.1.0 this method no longer returns the invocation, to avoid + * chaining, as this would not work (see #62) * * @param aMethod * JMethod to invoke - * @return Newly generated {@link JInvocation} */ - @Nonnull - public JInvocation invoke (@Nonnull final JMethod aMethod) + public void invoke (@Nonnull final JMethod aMethod) { - return internalInsert (new JInvocation (aMethod.owner (), (IJExpression) null, aMethod)); + internalInsert (new JInvocation (aMethod.owner (), (IJExpression) null, aMethod)); } - @Nonnull - public JInvocation _new (@Nonnull final AbstractJClass aClass) + public void _new (@Nonnull final AbstractJClass aClass) { - return internalInsert (new JInvocation (aClass)); + internalInsert (new JInvocation (aClass)); } - @Nonnull - public JInvocation _new (@Nonnull final AbstractJType aType) + public void _new (@Nonnull final AbstractJType aType) { - return internalInsert (new JInvocation (aType)); + internalInsert (new JInvocation (aType)); } /** diff --git a/src/test/java/com/helger/jcodemodel/ForEachFuncTest.java b/src/test/java/com/helger/jcodemodel/ForEachFuncTest.java index 11fabcf5..4942ac02 100644 --- a/src/test/java/com/helger/jcodemodel/ForEachFuncTest.java +++ b/src/test/java/com/helger/jcodemodel/ForEachFuncTest.java @@ -75,7 +75,7 @@ public void testBasic () throws Exception // printing out the variable final JFieldRef out1 = cm.ref (System.class).staticRef ("out"); // JInvocation invocation = - foreach.body ().invoke (out1, "println").arg ($count1); + foreach.body ().add (JExpr.invoke (out1, "println").arg ($count1)); CodeModelTestsHelper.parseCodeModel (cm); } diff --git a/src/test/java/com/helger/jcodemodel/JDefinedClassTest.java b/src/test/java/com/helger/jcodemodel/JDefinedClassTest.java index 255ef8f2..79985e34 100644 --- a/src/test/java/com/helger/jcodemodel/JDefinedClassTest.java +++ b/src/test/java/com/helger/jcodemodel/JDefinedClassTest.java @@ -110,7 +110,7 @@ public void testCallSuper () throws Exception final JDefinedClass c2 = cm._package ("myPackage")._class (0, "DerivedClass"); c2._extends (c1); final JMethod con2 = c2.constructor (JMod.PUBLIC); - con2.body ().invokeSuper ().arg ("Test"); + con2.body ().add (JExpr.invokeSuper ().arg ("Test")); CodeModelTestsHelper.parseCodeModel (cm); } } diff --git a/src/test/java/com/helger/jcodemodel/JInvocationTest.java b/src/test/java/com/helger/jcodemodel/JInvocationTest.java index 8e347797..38ba07ab 100644 --- a/src/test/java/com/helger/jcodemodel/JInvocationTest.java +++ b/src/test/java/com/helger/jcodemodel/JInvocationTest.java @@ -90,22 +90,22 @@ public void testWithGenerics () throws Exception m2.body ()._return (); final JMethod minvoke = cls.method (JMod.PUBLIC, cm.VOID, "bar"); - minvoke.body ()._new (cls).narrow (Integer.class).arg (cm.INT.wrap (JExpr.lit (17))); - minvoke.body ().invokeThis (m1).narrow (String.class).arg ("jippie"); - minvoke.body ().invoke (m1).arg ("jippie"); + minvoke.body ().add (JExpr._new (cls).narrow (Integer.class).arg (cm.INT.wrap (JExpr.lit (17)))); + minvoke.body ().add (JExpr.invokeThis (m1).narrow (String.class).arg ("jippie")); + minvoke.body ().add (JExpr.invoke (m1).arg ("jippie")); minvoke.body () - .invokeThis (m2) - .narrow (String.class) - .narrow (cls) - .narrow (cm.ref (List.class).narrow (Long.class)) - .arg ("jippie") - .arg (JExpr._this ()) - .arg (cm.ref (ArrayList.class).narrow (Long.class)._new ()); + .add (JExpr.invokeThis (m2) + .narrow (String.class) + .narrow (cls) + .narrow (cm.ref (List.class).narrow (Long.class)) + .arg ("jippie") + .arg (JExpr._this ()) + .arg (cm.ref (ArrayList.class).narrow (Long.class)._new ())); minvoke.body () - .invoke (m2) - .arg ("jippie") - .arg (JExpr._this ()) - .arg (cm.ref (ArrayList.class).narrow (Long.class)._new ()); + .add (JExpr.invoke (m2) + .arg ("jippie") + .arg (JExpr._this ()) + .arg (cm.ref (ArrayList.class).narrow (Long.class)._new ())); CodeModelTestsHelper.parseCodeModel (cm); } @@ -123,8 +123,8 @@ public void testChainedInvoke () throws Exception m2.body ()._return (JExpr._this ()); final JMethod minvoke = cls.method (JMod.PUBLIC, cm.VOID, "bar"); - minvoke.body ().invoke (m1).invoke (m2); + minvoke.body ().add (JExpr.invoke (m1).invoke (m2)); - CodeModelTestsHelper.printCodeModel (cm); + CodeModelTestsHelper.parseCodeModel (cm); } } diff --git a/src/test/java/com/helger/jcodemodel/VarArgsFuncTest.java b/src/test/java/com/helger/jcodemodel/VarArgsFuncTest.java index f71bfd67..72df008b 100644 --- a/src/test/java/com/helger/jcodemodel/VarArgsFuncTest.java +++ b/src/test/java/com/helger/jcodemodel/VarArgsFuncTest.java @@ -99,7 +99,7 @@ public void testBasic () throws Exception assertNotNull (typearray); // JInvocation invocation = - forloop.body ().invoke (out, "println").arg (JExpr.direct ("param3[count]")); + forloop.body ().add (JExpr.invoke (out, "println").arg (JExpr.direct ("param3[count]"))); final JMethod main = cls.method (JMod.PUBLIC | JMod.STATIC, cm.VOID, "main"); main.param (stringArray, "args");