Skip to content

Commit

Permalink
Voided "invoke" like methods in JBlock; #62
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 22, 2018
1 parent b7d1969 commit 49078e4
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 76 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<version>1.10.5</version>
</parent>
<artifactId>jcodemodel</artifactId>
<version>3.0.4-SNAPSHOT</version>
<version>3.1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>jcodemodel</name>
<description>Java code generation library</description>
Expand Down Expand Up @@ -137,7 +137,7 @@
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>3.6.17</version>
<version>3.6.18</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
104 changes: 50 additions & 54 deletions src/main/java/com/helger/jcodemodel/JBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
* 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.<br>
* 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 <code>null</code>.
Expand All @@ -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.<br>
* 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!<br>
* 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.<br>
* 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.<br>
* 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.<br>
* 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.<br>
* 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.<br>
* 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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/helger/jcodemodel/ForEachFuncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/helger/jcodemodel/JDefinedClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
32 changes: 16 additions & 16 deletions src/test/java/com/helger/jcodemodel/JInvocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/helger/jcodemodel/VarArgsFuncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 49078e4

Please sign in to comment.