in packages/jsii-pacmak/lib/targets/java.ts [2290:2351]
private emitBuilderSetter(
prop: JavaProp,
builderName: string,
parentType: spec.InterfaceType,
) {
for (const type of prop.javaTypes) {
this.code.line();
this.code.line('/**');
this.code.line(
` * Sets the value of {@link ${parentType.name}#${getterFor(
prop.fieldName,
)}}`,
);
const summary = prop.docs?.summary ?? 'the value to be set';
this.code.line(
` * ${paramJavadoc(prop.fieldName, prop.nullable, summary)}`,
);
if (prop.docs?.remarks != null) {
const indent = ' '.repeat(7 + prop.fieldName.length);
const remarks = myMarkDownToJavaDoc(
this.convertSamplesInMarkdown(prop.docs.remarks, {
api: 'member',
fqn: prop.definingType.fqn,
memberName: prop.jsiiName,
}),
).trimRight();
for (const line of remarks.split('\n')) {
this.code.line(` * ${indent} ${line}`);
}
}
this.code.line(' * @return {@code this}');
if (prop.docs?.deprecated) {
this.code.line(` * @deprecated ${prop.docs.deprecated}`);
}
this.code.line(' */');
this.emitStabilityAnnotations(prop.spec);
// We add an explicit cast if both types are generic but they are not identical (one is covariant, the other isn't)
const explicitCast =
type.includes('<') &&
prop.fieldJavaType.includes('<') &&
type !== prop.fieldJavaType
? `(${prop.fieldJavaType})`
: '';
if (explicitCast !== '') {
// We'll be doing a safe, but unchecked cast, so suppress that warning
this.code.line('@SuppressWarnings("unchecked")');
}
this.code.openBlock(
`public ${builderName} ${prop.fieldName}(${type} ${prop.fieldName})`,
);
this.code.line(
`this.${prop.fieldName} = ${explicitCast}${prop.fieldName};`,
);
this.code.line('return this;');
this.code.closeBlock();
}
function getterFor(fieldName: string): string {
const [first, ...rest] = fieldName;
return `get${first.toUpperCase()}${rest.join('')}`;
}
}