Skip to content

Commit cb7e11f

Browse files
dagnirmergify[bot]
authored andcommitted
feat(java): Indicate if method param is required (#762)
This commit enhances javadoc generation for Java classes so that required method/constructor parameters are indicated in their description in the javadoc. This makes it easy for customers to see which paramaters must be provided when interacting with libraries from Java. Fixes #365
1 parent a398d3a commit cb7e11f

File tree

75 files changed

+296
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+296
-76
lines changed

packages/jsii-pacmak/lib/targets/java.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,8 @@ class JavaGenerator extends Generator {
896896
this.code.line();
897897
this.code.line('/**');
898898
this.code.line(` * Sets the value of ${prop.propName}`);
899-
if (prop.docs && prop.docs.summary) {
900-
this.code.line(` * @param ${prop.fieldName} ${prop.docs.summary}`);
901-
} else {
902-
this.code.line(` * @param ${prop.fieldName} the value to be set`);
903-
}
899+
const summary = (prop.docs && prop.docs.summary) || "the value to be set";
900+
this.code.line(` * ${paramJavadoc(prop.fieldName, prop.nullable, summary)}`);
904901
this.code.line(` * @return {@code this}`);
905902
if (prop.docs && prop.docs.deprecated) {
906903
this.code.line(` * @deprecated ${prop.docs.deprecated}`);
@@ -1194,9 +1191,8 @@ class JavaGenerator extends Generator {
11941191
const method = doc as spec.Method;
11951192
if (method.parameters) {
11961193
for (const param of method.parameters) {
1197-
if (param.docs && param.docs.summary) {
1198-
tagLines.push(`@param ${param.name} ${param.docs.summary}`);
1199-
}
1194+
const summary = (param.docs && param.docs.summary) || undefined;
1195+
tagLines.push(paramJavadoc(param.name, param.optional, summary));
12001196
}
12011197
}
12021198
}
@@ -1543,3 +1539,18 @@ function isNullable(optionalValue: spec.OptionalValue | undefined): boolean {
15431539
|| (spec.isPrimitiveTypeReference(optionalValue.type)
15441540
&& optionalValue.type.primitive === spec.PrimitiveType.Any);
15451541
}
1542+
1543+
function paramJavadoc(name: string, optional?: boolean, summary?: string): string {
1544+
const parts = ['@param', name];
1545+
if (summary) { parts.push(endWithPeriod(summary)); }
1546+
if (!optional) { parts.push('This parameter is required.'); }
1547+
1548+
return parts.join(' ');
1549+
}
1550+
1551+
function endWithPeriod(s: string): string {
1552+
if (!s.endsWith('.')) {
1553+
return s + '.';
1554+
}
1555+
return s;
1556+
}

packages/jsii-pacmak/test/expected.jsii-calc-base/java/src/main/java/software/amazon/jsii/tests/calculator/base/BaseProps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static final class Builder {
2020

2121
/**
2222
* Sets the value of Bar
23-
* @param bar the value to be set
23+
* @param bar the value to be set. This parameter is required.
2424
* @return {@code this}
2525
*/
2626
public Builder bar(java.lang.String bar) {
@@ -30,7 +30,7 @@ public Builder bar(java.lang.String bar) {
3030

3131
/**
3232
* Sets the value of Foo
33-
* @param foo the value to be set
33+
* @param foo the value to be set. This parameter is required.
3434
* @return {@code this}
3535
*/
3636
public Builder foo(software.amazon.jsii.tests.calculator.baseofbase.Very foo) {

packages/jsii-pacmak/test/expected.jsii-calc-lib/java/src/main/java/software/amazon/jsii/tests/calculator/lib/MyFirstStruct.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static final class Builder {
4848

4949
/**
5050
* Sets the value of Anumber
51-
* @param anumber An awesome number value.
51+
* @param anumber An awesome number value. This parameter is required.
5252
* @return {@code this}
5353
*/
5454
@Deprecated
@@ -60,7 +60,7 @@ public Builder anumber(java.lang.Number anumber) {
6060

6161
/**
6262
* Sets the value of Astring
63-
* @param astring A string value.
63+
* @param astring A string value. This parameter is required.
6464
* @return {@code this}
6565
*/
6666
@Deprecated
@@ -72,7 +72,7 @@ public Builder astring(java.lang.String astring) {
7272

7373
/**
7474
* Sets the value of FirstOptional
75-
* @param firstOptional the value to be set
75+
* @param firstOptional the value to be set.
7676
* @return {@code this}
7777
*/
7878
@Deprecated

packages/jsii-pacmak/test/expected.jsii-calc-lib/java/src/main/java/software/amazon/jsii/tests/calculator/lib/Number.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected Number(final software.amazon.jsii.JsiiObject.InitializationMode initia
2020
/**
2121
* Creates a Number object.
2222
*
23-
* @param value The number.
23+
* @param value The number. This parameter is required.
2424
*/
2525
@Deprecated
2626
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Deprecated)

packages/jsii-pacmak/test/expected.jsii-calc-lib/java/src/main/java/software/amazon/jsii/tests/calculator/lib/StructWithOnlyOptionals.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static final class Builder {
4747

4848
/**
4949
* Sets the value of Optional1
50-
* @param optional1 The first optional!
50+
* @param optional1 The first optional!.
5151
* @return {@code this}
5252
*/
5353
@Deprecated
@@ -59,7 +59,7 @@ public Builder optional1(java.lang.String optional1) {
5959

6060
/**
6161
* Sets the value of Optional2
62-
* @param optional2 the value to be set
62+
* @param optional2 the value to be set.
6363
* @return {@code this}
6464
*/
6565
@Deprecated
@@ -71,7 +71,7 @@ public Builder optional2(java.lang.Number optional2) {
7171

7272
/**
7373
* Sets the value of Optional3
74-
* @param optional3 the value to be set
74+
* @param optional3 the value to be set.
7575
* @return {@code this}
7676
*/
7777
@Deprecated

packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AbstractClass.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ protected AbstractClass() {
2323

2424
/**
2525
* EXPERIMENTAL
26+
*
27+
* @param name This parameter is required.
2628
*/
2729
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
2830
public abstract java.lang.String abstractMethod(final java.lang.String name);
@@ -73,6 +75,8 @@ public java.lang.String getAbstractProperty() {
7375

7476
/**
7577
* EXPERIMENTAL
78+
*
79+
* @param name This parameter is required.
7680
*/
7781
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
7882
@Override

packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/Add.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ protected Add(final software.amazon.jsii.JsiiObject.InitializationMode initializ
2323
*
2424
* EXPERIMENTAL
2525
*
26-
* @param lhs Left-hand side operand.
27-
* @param rhs Right-hand side operand.
26+
* @param lhs Left-hand side operand. This parameter is required.
27+
* @param rhs Right-hand side operand. This parameter is required.
2828
*/
2929
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
3030
public Add(final software.amazon.jsii.tests.calculator.lib.Value lhs, final software.amazon.jsii.tests.calculator.lib.Value rhs) {

packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AllTypes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public AllTypes() {
2828

2929
/**
3030
* EXPERIMENTAL
31+
*
32+
* @param inp This parameter is required.
3133
*/
3234
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
3335
public void anyIn(final java.lang.Object inp) {
@@ -44,6 +46,8 @@ public java.lang.Object anyOut() {
4446

4547
/**
4648
* EXPERIMENTAL
49+
*
50+
* @param value This parameter is required.
4751
*/
4852
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
4953
public software.amazon.jsii.tests.calculator.StringEnum enumMethod(final software.amazon.jsii.tests.calculator.StringEnum value) {

packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AllowedMethodNames.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public AllowedMethodNames() {
2323

2424
/**
2525
* EXPERIMENTAL
26+
*
27+
* @param _p1 This parameter is required.
28+
* @param _p2 This parameter is required.
2629
*/
2730
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
2831
public void getBar(final java.lang.String _p1, final java.lang.Number _p2) {
@@ -33,6 +36,8 @@ public void getBar(final java.lang.String _p1, final java.lang.Number _p2) {
3336
* getXxx() is not allowed (see negatives), but getXxx(a, ...) is okay.
3437
*
3538
* EXPERIMENTAL
39+
*
40+
* @param withParam This parameter is required.
3641
*/
3742
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
3843
public java.lang.String getFoo(final java.lang.String withParam) {
@@ -41,6 +46,10 @@ public java.lang.String getFoo(final java.lang.String withParam) {
4146

4247
/**
4348
* EXPERIMENTAL
49+
*
50+
* @param _x This parameter is required.
51+
* @param _y This parameter is required.
52+
* @param _z This parameter is required.
4453
*/
4554
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
4655
public void setBar(final java.lang.String _x, final java.lang.Number _y, final java.lang.Boolean _z) {
@@ -51,6 +60,9 @@ public void setBar(final java.lang.String _x, final java.lang.Number _y, final j
5160
* setFoo(x) is not allowed (see negatives), but setXxx(a, b, ...) is okay.
5261
*
5362
* EXPERIMENTAL
63+
*
64+
* @param _x This parameter is required.
65+
* @param _y This parameter is required.
5466
*/
5567
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
5668
public void setFoo(final java.lang.String _x, final java.lang.Number _y) {

packages/jsii-pacmak/test/expected.jsii-calc/java/src/main/java/software/amazon/jsii/tests/calculator/AsyncVirtualMethods.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public java.lang.Number dontOverrideMe() {
6363

6464
/**
6565
* EXPERIMENTAL
66+
*
67+
* @param mult This parameter is required.
6668
*/
6769
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
6870
public java.lang.Number overrideMe(final java.lang.Number mult) {

0 commit comments

Comments
 (0)