Skip to content

Commit 49078e4

Browse files
committed
Voided "invoke" like methods in JBlock; #62
1 parent b7d1969 commit 49078e4

File tree

7 files changed

+74
-76
lines changed

7 files changed

+74
-76
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ A site with the links to the [API docs](http://phax.github.io/jcodemodel/) etc.
1414

1515
# News and noteworthy
1616

17-
* v3.0.4 - work in progress
17+
* v3.1.0 - work in progress
1818
* Added ` AbstractJType._new`()`
19+
* Change return types of special `JBlock` methods to `void` to avoid chaining (issue #62) - incompatible change
20+
* Added new `JExpr.invokeThis` and `JExpr.invokeSuper` static methods
1921
* v3.0.3 - 2018-06-12
2022
* Improved API access to inner classes (issue #60)
2123
* Changed order of emitted modifiers (`final static` -> `static final`)

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<version>1.10.5</version>
5050
</parent>
5151
<artifactId>jcodemodel</artifactId>
52-
<version>3.0.4-SNAPSHOT</version>
52+
<version>3.1.0-SNAPSHOT</version>
5353
<packaging>bundle</packaging>
5454
<name>jcodemodel</name>
5555
<description>Java code generation library</description>
@@ -137,7 +137,7 @@
137137
<dependency>
138138
<groupId>com.github.javaparser</groupId>
139139
<artifactId>javaparser-core</artifactId>
140-
<version>3.6.17</version>
140+
<version>3.6.18</version>
141141
<scope>test</scope>
142142
</dependency>
143143
</dependencies>

src/main/java/com/helger/jcodemodel/JBlock.java

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -380,23 +380,25 @@ public JBlock assignDivide (@Nonnull final IJAssignmentTarget aLhs, @Nonnull fin
380380
}
381381

382382
/**
383-
* Creates an invocation statement and adds it to this block.
383+
* Creates an invocation statement and adds it to this block.<br>
384+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
385+
* chaining, as this would not work (see #62)
384386
*
385387
* @param aExpr
386388
* {@link IJExpression} evaluating to the class or object upon which
387389
* the named method will be invoked
388390
* @param sMethod
389391
* Name of method to invoke
390-
* @return Newly generated {@link JInvocation}
391392
*/
392-
@Nonnull
393-
public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final String sMethod)
393+
public void invoke (@Nonnull final IJExpression aExpr, @Nonnull final String sMethod)
394394
{
395-
return invoke ((JCodeModel) null, aExpr, sMethod);
395+
invoke ((JCodeModel) null, aExpr, sMethod);
396396
}
397397

398398
/**
399-
* Creates an invocation statement and adds it to this block.
399+
* Creates an invocation statement and adds it to this block.<br>
400+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
401+
* chaining, as this would not work (see #62)
400402
*
401403
* @param aCM
402404
* CodeModel to use. May be <code>null</code>.
@@ -405,123 +407,117 @@ public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final Str
405407
* the named method will be invoked
406408
* @param sMethod
407409
* Name of method to invoke
408-
* @return Newly generated {@link JInvocation}
409-
* @since 3.0.5
410+
* @since 3.1.0
410411
*/
411-
@Nonnull
412-
public JInvocation invoke (@Nullable final JCodeModel aCM,
413-
@Nonnull final IJExpression aExpr,
414-
@Nonnull final String sMethod)
412+
public void invoke (@Nullable final JCodeModel aCM, @Nonnull final IJExpression aExpr, @Nonnull final String sMethod)
415413
{
416-
return internalInsert (new JInvocation (aCM, aExpr, sMethod));
414+
internalInsert (new JInvocation (aCM, aExpr, sMethod));
417415
}
418416

419417
/**
420-
* Creates an invocation statement and adds it to this block.
418+
* Creates an invocation statement and adds it to this block.<br>
419+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
420+
* chaining, as this would not work (see #62)
421421
*
422422
* @param sMethod
423423
* Name of method to invoke on this
424-
* @return Newly generated {@link JInvocation}
425424
*/
426-
@Nonnull
427-
public JInvocation invokeThis (@Nonnull final String sMethod)
425+
public void invokeThis (@Nonnull final String sMethod)
428426
{
429-
return invoke (JExpr._this (), sMethod);
427+
invoke (JExpr._this (), sMethod);
430428
}
431429

432430
/**
433431
* Explicitly call the super class constructor in this block. This method may
434-
* only be called as the first call inside a constructor block!
432+
* only be called as the first call inside a constructor block!<br>
433+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
434+
* chaining, as this would not work (see #62)
435435
*
436-
* @return Newly generated super {@link JInvocation}
437436
* @since 3.0.1
438437
*/
439-
@Nonnull
440-
public JInvocation invokeSuper ()
438+
public void invokeSuper ()
441439
{
442-
return internalInsert (JInvocation._super ());
440+
internalInsert (JInvocation._super ());
443441
}
444442

445443
/**
446-
* Creates an invocation statement and adds it to this block.
444+
* Creates an invocation statement and adds it to this block.<br>
445+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
446+
* chaining, as this would not work (see #62)
447447
*
448448
* @param aExpr
449449
* {@link IJExpression} evaluating to the class or object upon which
450450
* the method will be invoked
451451
* @param aMethod
452452
* {@link JMethod} to invoke
453-
* @return Newly generated {@link JInvocation}
454453
*/
455-
@Nonnull
456-
public JInvocation invoke (@Nonnull final IJExpression aExpr, @Nonnull final JMethod aMethod)
454+
public void invoke (@Nullable final IJExpression aExpr, @Nonnull final JMethod aMethod)
457455
{
458-
return internalInsert (new JInvocation (aMethod.owner (), aExpr, aMethod));
456+
internalInsert (new JInvocation (aMethod.owner (), aExpr, aMethod));
459457
}
460458

461459
/**
462-
* Creates an invocation statement and adds it to this block.
460+
* Creates an invocation statement and adds it to this block.<br>
461+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
462+
* chaining, as this would not work (see #62)
463463
*
464464
* @param aMethod
465465
* {@link JMethod} to invoke on this
466-
* @return Newly generated {@link JInvocation}
467466
*/
468-
@Nonnull
469-
public JInvocation invokeThis (@Nonnull final JMethod aMethod)
467+
public void invokeThis (@Nonnull final JMethod aMethod)
470468
{
471-
return invoke (JExpr._this (), aMethod);
469+
invoke (JExpr._this (), aMethod);
472470
}
473471

474472
/**
475-
* Creates a static invocation statement.
473+
* Creates a static invocation statement.<br>
474+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
475+
* chaining, as this would not work (see #62)
476476
*
477477
* @param aType
478478
* Type upon which the method should be invoked
479479
* @param sMethod
480480
* Name of method to invoke
481-
* @return Newly generated {@link JInvocation}
482481
*/
483-
@Nonnull
484-
public JInvocation staticInvoke (@Nonnull final AbstractJClass aType, @Nonnull final String sMethod)
482+
public void staticInvoke (@Nonnull final AbstractJClass aType, @Nonnull final String sMethod)
485483
{
486-
return internalInsert (new JInvocation (aType.owner (), aType, sMethod));
484+
internalInsert (new JInvocation (aType.owner (), aType, sMethod));
487485
}
488486

489487
/**
490-
* Creates an invocation statement and adds it to this block.
488+
* Creates an invocation statement and adds it to this block.<br>
489+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
490+
* chaining, as this would not work (see #62)
491491
*
492492
* @param sMethod
493493
* Name of method to invoke
494-
* @return Newly generated {@link JInvocation}
495494
*/
496-
@Nonnull
497-
public JInvocation invoke (@Nonnull final String sMethod)
495+
public void invoke (@Nonnull final String sMethod)
498496
{
499-
return internalInsert (new JInvocation ((JCodeModel) null, (IJExpression) null, sMethod));
497+
internalInsert (new JInvocation ((JCodeModel) null, (IJExpression) null, sMethod));
500498
}
501499

502500
/**
503-
* Creates an invocation statement and adds it to this block.
501+
* Creates an invocation statement and adds it to this block.<br>
502+
* Note: since 3.1.0 this method no longer returns the invocation, to avoid
503+
* chaining, as this would not work (see #62)
504504
*
505505
* @param aMethod
506506
* JMethod to invoke
507-
* @return Newly generated {@link JInvocation}
508507
*/
509-
@Nonnull
510-
public JInvocation invoke (@Nonnull final JMethod aMethod)
508+
public void invoke (@Nonnull final JMethod aMethod)
511509
{
512-
return internalInsert (new JInvocation (aMethod.owner (), (IJExpression) null, aMethod));
510+
internalInsert (new JInvocation (aMethod.owner (), (IJExpression) null, aMethod));
513511
}
514512

515-
@Nonnull
516-
public JInvocation _new (@Nonnull final AbstractJClass aClass)
513+
public void _new (@Nonnull final AbstractJClass aClass)
517514
{
518-
return internalInsert (new JInvocation (aClass));
515+
internalInsert (new JInvocation (aClass));
519516
}
520517

521-
@Nonnull
522-
public JInvocation _new (@Nonnull final AbstractJType aType)
518+
public void _new (@Nonnull final AbstractJType aType)
523519
{
524-
return internalInsert (new JInvocation (aType));
520+
internalInsert (new JInvocation (aType));
525521
}
526522

527523
/**

src/test/java/com/helger/jcodemodel/ForEachFuncTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void testBasic () throws Exception
7575
// printing out the variable
7676
final JFieldRef out1 = cm.ref (System.class).staticRef ("out");
7777
// JInvocation invocation =
78-
foreach.body ().invoke (out1, "println").arg ($count1);
78+
foreach.body ().add (JExpr.invoke (out1, "println").arg ($count1));
7979

8080
CodeModelTestsHelper.parseCodeModel (cm);
8181
}

src/test/java/com/helger/jcodemodel/JDefinedClassTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void testCallSuper () throws Exception
110110
final JDefinedClass c2 = cm._package ("myPackage")._class (0, "DerivedClass");
111111
c2._extends (c1);
112112
final JMethod con2 = c2.constructor (JMod.PUBLIC);
113-
con2.body ().invokeSuper ().arg ("Test");
113+
con2.body ().add (JExpr.invokeSuper ().arg ("Test"));
114114
CodeModelTestsHelper.parseCodeModel (cm);
115115
}
116116
}

src/test/java/com/helger/jcodemodel/JInvocationTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,22 @@ public void testWithGenerics () throws Exception
9090
m2.body ()._return ();
9191

9292
final JMethod minvoke = cls.method (JMod.PUBLIC, cm.VOID, "bar");
93-
minvoke.body ()._new (cls).narrow (Integer.class).arg (cm.INT.wrap (JExpr.lit (17)));
94-
minvoke.body ().invokeThis (m1).narrow (String.class).arg ("jippie");
95-
minvoke.body ().invoke (m1).arg ("jippie");
93+
minvoke.body ().add (JExpr._new (cls).narrow (Integer.class).arg (cm.INT.wrap (JExpr.lit (17))));
94+
minvoke.body ().add (JExpr.invokeThis (m1).narrow (String.class).arg ("jippie"));
95+
minvoke.body ().add (JExpr.invoke (m1).arg ("jippie"));
9696
minvoke.body ()
97-
.invokeThis (m2)
98-
.narrow (String.class)
99-
.narrow (cls)
100-
.narrow (cm.ref (List.class).narrow (Long.class))
101-
.arg ("jippie")
102-
.arg (JExpr._this ())
103-
.arg (cm.ref (ArrayList.class).narrow (Long.class)._new ());
97+
.add (JExpr.invokeThis (m2)
98+
.narrow (String.class)
99+
.narrow (cls)
100+
.narrow (cm.ref (List.class).narrow (Long.class))
101+
.arg ("jippie")
102+
.arg (JExpr._this ())
103+
.arg (cm.ref (ArrayList.class).narrow (Long.class)._new ()));
104104
minvoke.body ()
105-
.invoke (m2)
106-
.arg ("jippie")
107-
.arg (JExpr._this ())
108-
.arg (cm.ref (ArrayList.class).narrow (Long.class)._new ());
105+
.add (JExpr.invoke (m2)
106+
.arg ("jippie")
107+
.arg (JExpr._this ())
108+
.arg (cm.ref (ArrayList.class).narrow (Long.class)._new ()));
109109

110110
CodeModelTestsHelper.parseCodeModel (cm);
111111
}
@@ -123,8 +123,8 @@ public void testChainedInvoke () throws Exception
123123
m2.body ()._return (JExpr._this ());
124124

125125
final JMethod minvoke = cls.method (JMod.PUBLIC, cm.VOID, "bar");
126-
minvoke.body ().invoke (m1).invoke (m2);
126+
minvoke.body ().add (JExpr.invoke (m1).invoke (m2));
127127

128-
CodeModelTestsHelper.printCodeModel (cm);
128+
CodeModelTestsHelper.parseCodeModel (cm);
129129
}
130130
}

src/test/java/com/helger/jcodemodel/VarArgsFuncTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void testBasic () throws Exception
9999
assertNotNull (typearray);
100100

101101
// JInvocation invocation =
102-
forloop.body ().invoke (out, "println").arg (JExpr.direct ("param3[count]"));
102+
forloop.body ().add (JExpr.invoke (out, "println").arg (JExpr.direct ("param3[count]")));
103103

104104
final JMethod main = cls.method (JMod.PUBLIC | JMod.STATIC, cm.VOID, "main");
105105
main.param (stringArray, "args");

0 commit comments

Comments
 (0)