Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(java): invalid code when multi-inheriting optional properties #2591

Merged
merged 4 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/@scope/jsii-calc-lib/lib/duplicate-inherited-prop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This is half of the contraption, the rest is in jsii-calc
//
// @see https://github.com/aws/jsii/issues/2256

/**
* This struct is intentionally private. Any type that implements it will get
* a copy of it's properties hoisted in by jsii.
*/
interface InternalDiamondTip {
readonly hoistedTop?: string;
}

export interface DiamondLeft extends InternalDiamondTip {
readonly left?: number;
}

export interface DiamondRight extends InternalDiamondTip {
readonly right?: boolean;
}
1 change: 1 addition & 0 deletions packages/@scope/jsii-calc-lib/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,4 @@ export interface IThreeLevelsInterface extends base.IBaseInterface {
}

export * as submodule from './submodule';
export * from './duplicate-inherited-prop';
98 changes: 97 additions & 1 deletion packages/@scope/jsii-calc-lib/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,102 @@
}
},
"types": {
"@scope/jsii-calc-lib.DiamondLeft": {
"assembly": "@scope/jsii-calc-lib",
"datatype": true,
"docs": {
"stability": "deprecated"
},
"fqn": "@scope/jsii-calc-lib.DiamondLeft",
"kind": "interface",
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 13
},
"name": "DiamondLeft",
"properties": [
{
"abstract": true,
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 10
},
"name": "hoistedTop",
"optional": true,
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 14
},
"name": "left",
"optional": true,
"type": {
"primitive": "number"
}
}
]
},
"@scope/jsii-calc-lib.DiamondRight": {
"assembly": "@scope/jsii-calc-lib",
"datatype": true,
"docs": {
"stability": "deprecated"
},
"fqn": "@scope/jsii-calc-lib.DiamondRight",
"kind": "interface",
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 17
},
"name": "DiamondRight",
"properties": [
{
"abstract": true,
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 10
},
"name": "hoistedTop",
"optional": true,
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 18
},
"name": "right",
"optional": true,
"type": {
"primitive": "boolean"
}
}
]
},
"@scope/jsii-calc-lib.EnumFromScopedModule": {
"assembly": "@scope/jsii-calc-lib",
"docs": {
Expand Down Expand Up @@ -776,5 +872,5 @@
}
},
"version": "0.0.0",
"fingerprint": "7STgxc8/b+pajng4R4fEptbcOF2aQJBiQkO4bMAWpI4="
"fingerprint": "YlmxPtOusVTgKe2YUzZDpB4UnmnU3zZ0UUdXwSDcgOo="
}
9 changes: 9 additions & 0 deletions packages/jsii-calc/lib/duplicate-inherited-prop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DiamondLeft, DiamondRight } from '@scope/jsii-calc-lib';

// This is half of the contraption, the rest is in @scope/jsii-calc-lib.
//
// @see https://github.com/aws/jsii/issues/2256

export interface DiamondBottom extends DiamondLeft, DiamondRight {
readonly bottom?: Date;
}
1 change: 1 addition & 0 deletions packages/jsii-calc/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './calculator';
export * from './compliance';
export * from './date';
export * from './documented';
export * from './duplicate-inherited-prop';
export * from './erasures';
export * from './nested-class';
export * from './stability';
Expand Down
40 changes: 38 additions & 2 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
"jsii-calc.submodule": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 10
"line": 11
},
"readme": {
"markdown": "Read you, read me\n=================\n\nThis is the readme of the `jsii-calc.submodule` module.\n"
Expand Down Expand Up @@ -3821,6 +3821,42 @@
}
]
},
"jsii-calc.DiamondBottom": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.DiamondBottom",
"interfaces": [
"@scope/jsii-calc-lib.DiamondLeft",
"@scope/jsii-calc-lib.DiamondRight"
],
"kind": "interface",
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 7
},
"name": "DiamondBottom",
"properties": [
{
"abstract": true,
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/duplicate-inherited-prop.ts",
"line": 8
},
"name": "bottom",
"optional": true,
"type": {
"primitive": "date"
}
}
]
},
"jsii-calc.DiamondInheritanceBaseLevelStruct": {
"assembly": "jsii-calc",
"datatype": true,
Expand Down Expand Up @@ -14441,5 +14477,5 @@
}
},
"version": "3.20.120",
"fingerprint": "DELAKIDgUwCEVXhZflQBdllcDRnscledlYW0UXrjzbM="
"fingerprint": "DPUvV1cBxUeRNl5GK1DVzpU7/802/Hb8znpxKRttRUk="
}
63 changes: 63 additions & 0 deletions packages/jsii-pacmak/lib/targets/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,8 @@ class JavaGenerator extends Generator {
}

protected onEndInterface(ifc: spec.InterfaceType) {
this.emitMultiplyInheritedOptionalProperties(ifc);

if (ifc.datatype) {
this.emitDataType(ifc);
} else {
Expand Down Expand Up @@ -883,6 +885,9 @@ class JavaGenerator extends Generator {
this.addJavaDocs(prop);
this.emitStabilityAnnotations(prop);
if (prop.optional) {
if (prop.overrides) {
this.code.line('@Override');
}
this.code.openBlock(`default ${getterType} get${propName}()`);
this.code.line('return null;');
this.code.closeBlock();
Expand All @@ -896,6 +901,9 @@ class JavaGenerator extends Generator {
this.code.line();
this.addJavaDocs(prop);
if (prop.optional) {
if (prop.overrides) {
this.code.line('@Override');
}
this.code.line('@software.amazon.jsii.Optional');
this.code.openBlock(
`default void set${propName}(final ${type} value)`,
Expand All @@ -911,6 +919,61 @@ class JavaGenerator extends Generator {
}
}

/**
* Emits a local default implementation for optional properties inherited from
* multiple distinct parent types. This remvoes the default method dispatch
* ambiguity that would otherwise exist.
*
* @param ifc the interface to be processed.

*
* @see https://github.com/aws/jsii/issues/2256
*/
private emitMultiplyInheritedOptionalProperties(ifc: spec.InterfaceType) {
if (ifc.interfaces == null || ifc.interfaces.length <= 1) {
// Nothing to do if we don't have parent interfaces, or if we have exactly one
return;
}
const inheritedOptionalProps = ifc.interfaces
.map(allOptionalProps.bind(this))
// Calculate how many direct parents brought a given optional property
.reduce((histogram, entry) => {
for (const [name, spec] of Object.entries(entry)) {
histogram[name] = histogram[name] ?? { spec, count: 0 };
histogram[name].count += 1;
}
return histogram;
}, {} as Record<string, { readonly spec: spec.Property; count: number }>);

const localProps = new Set(ifc.properties?.map((prop) => prop.name) ?? []);
for (const { spec, count } of Object.values(inheritedOptionalProps)) {
if (count < 2 || localProps.has(spec.name)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the latter half of this check doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoids re-declaring a property that was already overridden locally (that would be illegal).

continue;
}
this.onInterfaceProperty(ifc, spec);
}

function allOptionalProps(this: JavaGenerator, fqn: string) {
const type = this.findType(fqn) as spec.InterfaceType;
const result: Record<string, spec.Property> = {};
for (const prop of type.properties ?? []) {
// Adding artifical "overrides" here for code-gen quality's sake.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why artificial?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, the property as we fetch it is not overridden... We actually generate an override, but per the jsii type model, it's only inherited.

result[prop.name] = { ...prop, overrides: type.fqn };
}
// Include optional properties of all super interfaces in the result
for (const base of type.interfaces ?? []) {
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
for (const [name, prop] of Object.entries(
allOptionalProps.call(this, base),
)) {
if (!(name in result)) {
result[name] = prop;
}
}
}
return result;
}
}

private emitPackageInfo(mod: spec.Assembly) {
if (!mod.docs) {
return;
Expand Down
Loading