Skip to content

Commit d006f5c

Browse files
authored
feat(jsii): Erase un-exported base classes instead of prohibiting those (#425)
When an exported class extends an un-exported class, merge the declarations of the un-exported base into the exported one, and replace the base class relationship with the base's own base (recursively until an exported base is found, or no further base class is declared - in which case the exported class will not have a base class at all). Declarations from the erased base class(es) will be ignored if there is another declaration with the same name and type in the exported type (or another erased base class that is closer to the exported type in the inheritance chain). Fixes #417
1 parent 9cfd867 commit d006f5c

File tree

20 files changed

+534
-31
lines changed

20 files changed

+534
-31
lines changed

packages/jsii-calc/lib/erasures.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Un-exported base classes are erased
3+
// https://github.com/awslabs/jsii/issues/417
4+
//
5+
class JSII417PrivateRoot {
6+
public readonly hasRoot = true;
7+
}
8+
export class JSII417PublicBaseOfBase extends JSII417PrivateRoot {
9+
public static makeInstance(): JSII417PublicBaseOfBase {
10+
return new JSII417PrivateBase("TEST");
11+
}
12+
public foo() { return; }
13+
}
14+
class JSII417PrivateBase extends JSII417PublicBaseOfBase {
15+
constructor(protected readonly property: string) {
16+
super();
17+
}
18+
public bar() { return; }
19+
}
20+
export class JSII417Derived extends JSII417PrivateBase {
21+
public bar() {
22+
return super.bar();
23+
}
24+
public baz() { return; }
25+
}

packages/jsii-calc/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './calculator';
22
export * from './compliance';
33
export * from './documented';
4+
export * from './erasures';

packages/jsii-calc/test/assembly.jsii

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,74 @@
25582558
}
25592559
]
25602560
},
2561+
"jsii-calc.JSII417Derived": {
2562+
"assembly": "jsii-calc",
2563+
"base": {
2564+
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
2565+
},
2566+
"fqn": "jsii-calc.JSII417Derived",
2567+
"initializer": {
2568+
"initializer": true,
2569+
"parameters": [
2570+
{
2571+
"name": "property",
2572+
"type": {
2573+
"primitive": "string"
2574+
}
2575+
}
2576+
]
2577+
},
2578+
"kind": "class",
2579+
"methods": [
2580+
{
2581+
"name": "bar"
2582+
},
2583+
{
2584+
"name": "baz"
2585+
}
2586+
],
2587+
"name": "JSII417Derived",
2588+
"properties": [
2589+
{
2590+
"immutable": true,
2591+
"name": "property",
2592+
"protected": true,
2593+
"type": {
2594+
"primitive": "string"
2595+
}
2596+
}
2597+
]
2598+
},
2599+
"jsii-calc.JSII417PublicBaseOfBase": {
2600+
"assembly": "jsii-calc",
2601+
"fqn": "jsii-calc.JSII417PublicBaseOfBase",
2602+
"initializer": {
2603+
"initializer": true
2604+
},
2605+
"kind": "class",
2606+
"methods": [
2607+
{
2608+
"name": "makeInstance",
2609+
"returns": {
2610+
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
2611+
},
2612+
"static": true
2613+
},
2614+
{
2615+
"name": "foo"
2616+
}
2617+
],
2618+
"name": "JSII417PublicBaseOfBase",
2619+
"properties": [
2620+
{
2621+
"immutable": true,
2622+
"name": "hasRoot",
2623+
"type": {
2624+
"primitive": "boolean"
2625+
}
2626+
}
2627+
]
2628+
},
25612629
"jsii-calc.JSObjectLiteralForInterface": {
25622630
"assembly": "jsii-calc",
25632631
"fqn": "jsii-calc.JSObjectLiteralForInterface",
@@ -4732,5 +4800,5 @@
47324800
}
47334801
},
47344802
"version": "0.8.2",
4735-
"fingerprint": "RIbNFrgvpaT1nTpWaVDxO0fWs/kZcVmMscsDGU/GXwE="
4803+
"fingerprint": "5kMI3UzT9N2sovIndhAmSefEqtrUStnpb0hmWwfLjOs="
47364804
}

packages/jsii-kernel/test/test.kernel.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,11 @@ defineTest('struct: non-empty object deserializes properly', async (test, sandbo
11261126
test.equal('foo', field.value);
11271127
});
11281128

1129+
defineTest('erased base: can receive an instance of private type', async (test, sandbox) => {
1130+
const objref = sandbox.sinvoke({ fqn: 'jsii-calc.JSII417PublicBaseOfBase', method: 'makeInstance' });
1131+
test.deepEqual(objref.result, { [api.TOKEN_REF]: 'jsii-calc.JSII417PublicBaseOfBase@10000' });
1132+
});
1133+
11291134
// =================================================================================================
11301135

11311136
const testNames: { [name: string]: boolean } = { };

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,74 @@
25582558
}
25592559
]
25602560
},
2561+
"jsii-calc.JSII417Derived": {
2562+
"assembly": "jsii-calc",
2563+
"base": {
2564+
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
2565+
},
2566+
"fqn": "jsii-calc.JSII417Derived",
2567+
"initializer": {
2568+
"initializer": true,
2569+
"parameters": [
2570+
{
2571+
"name": "property",
2572+
"type": {
2573+
"primitive": "string"
2574+
}
2575+
}
2576+
]
2577+
},
2578+
"kind": "class",
2579+
"methods": [
2580+
{
2581+
"name": "bar"
2582+
},
2583+
{
2584+
"name": "baz"
2585+
}
2586+
],
2587+
"name": "JSII417Derived",
2588+
"properties": [
2589+
{
2590+
"immutable": true,
2591+
"name": "property",
2592+
"protected": true,
2593+
"type": {
2594+
"primitive": "string"
2595+
}
2596+
}
2597+
]
2598+
},
2599+
"jsii-calc.JSII417PublicBaseOfBase": {
2600+
"assembly": "jsii-calc",
2601+
"fqn": "jsii-calc.JSII417PublicBaseOfBase",
2602+
"initializer": {
2603+
"initializer": true
2604+
},
2605+
"kind": "class",
2606+
"methods": [
2607+
{
2608+
"name": "makeInstance",
2609+
"returns": {
2610+
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
2611+
},
2612+
"static": true
2613+
},
2614+
{
2615+
"name": "foo"
2616+
}
2617+
],
2618+
"name": "JSII417PublicBaseOfBase",
2619+
"properties": [
2620+
{
2621+
"immutable": true,
2622+
"name": "hasRoot",
2623+
"type": {
2624+
"primitive": "boolean"
2625+
}
2626+
}
2627+
]
2628+
},
25612629
"jsii-calc.JSObjectLiteralForInterface": {
25622630
"assembly": "jsii-calc",
25632631
"fqn": "jsii-calc.JSObjectLiteralForInterface",
@@ -4732,5 +4800,5 @@
47324800
}
47334801
},
47344802
"version": "0.8.2",
4735-
"fingerprint": "RIbNFrgvpaT1nTpWaVDxO0fWs/kZcVmMscsDGU/GXwE="
4803+
"fingerprint": "5kMI3UzT9N2sovIndhAmSefEqtrUStnpb0hmWwfLjOs="
47364804
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Amazon.JSII.Runtime.Deputy;
2+
3+
namespace Amazon.JSII.Tests.CalculatorNamespace
4+
{
5+
[JsiiClass(typeof(JSII417Derived), "jsii-calc.JSII417Derived", "[{\"name\":\"property\",\"type\":{\"primitive\":\"string\"}}]")]
6+
public class JSII417Derived : JSII417PublicBaseOfBase
7+
{
8+
public JSII417Derived(string property): base(new DeputyProps(new object[]{property}))
9+
{
10+
}
11+
12+
protected JSII417Derived(ByRefValue reference): base(reference)
13+
{
14+
}
15+
16+
protected JSII417Derived(DeputyProps props): base(props)
17+
{
18+
}
19+
20+
[JsiiProperty("property", "{\"primitive\":\"string\"}")]
21+
protected virtual string Property
22+
{
23+
get => GetInstanceProperty<string>();
24+
}
25+
26+
[JsiiMethod("bar", null, "[]")]
27+
public virtual void Bar()
28+
{
29+
InvokeInstanceVoidMethod(new object[]{});
30+
}
31+
32+
[JsiiMethod("baz", null, "[]")]
33+
public virtual void Baz()
34+
{
35+
InvokeInstanceVoidMethod(new object[]{});
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Amazon.JSII.Runtime.Deputy;
2+
3+
namespace Amazon.JSII.Tests.CalculatorNamespace
4+
{
5+
[JsiiClass(typeof(JSII417PublicBaseOfBase), "jsii-calc.JSII417PublicBaseOfBase", "[]")]
6+
public class JSII417PublicBaseOfBase : DeputyBase
7+
{
8+
public JSII417PublicBaseOfBase(): base(new DeputyProps(new object[]{}))
9+
{
10+
}
11+
12+
protected JSII417PublicBaseOfBase(ByRefValue reference): base(reference)
13+
{
14+
}
15+
16+
protected JSII417PublicBaseOfBase(DeputyProps props): base(props)
17+
{
18+
}
19+
20+
[JsiiProperty("hasRoot", "{\"primitive\":\"boolean\"}")]
21+
public virtual bool HasRoot
22+
{
23+
get => GetInstanceProperty<bool>();
24+
}
25+
26+
[JsiiMethod("makeInstance", "{\"fqn\":\"jsii-calc.JSII417PublicBaseOfBase\"}", "[]")]
27+
public static JSII417PublicBaseOfBase MakeInstance()
28+
{
29+
return InvokeStaticMethod<JSII417PublicBaseOfBase>(typeof(JSII417PublicBaseOfBase), new object[]{});
30+
}
31+
32+
[JsiiMethod("foo", null, "[]")]
33+
public virtual void Foo()
34+
{
35+
InvokeInstanceVoidMethod(new object[]{});
36+
}
37+
}
38+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ protected Class<?> resolveClass(final String fqn) throws ClassNotFoundException
8080
case "jsii-calc.InterfaceInNamespaceIncludesClasses.Foo": return software.amazon.jsii.tests.calculator.InterfaceInNamespaceIncludesClasses.Foo.class;
8181
case "jsii-calc.InterfaceInNamespaceIncludesClasses.Hello": return software.amazon.jsii.tests.calculator.InterfaceInNamespaceIncludesClasses.Hello.class;
8282
case "jsii-calc.InterfaceInNamespaceOnlyInterface.Hello": return software.amazon.jsii.tests.calculator.InterfaceInNamespaceOnlyInterface.Hello.class;
83+
case "jsii-calc.JSII417Derived": return software.amazon.jsii.tests.calculator.JSII417Derived.class;
84+
case "jsii-calc.JSII417PublicBaseOfBase": return software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase.class;
8385
case "jsii-calc.JSObjectLiteralForInterface": return software.amazon.jsii.tests.calculator.JSObjectLiteralForInterface.class;
8486
case "jsii-calc.JSObjectLiteralToNative": return software.amazon.jsii.tests.calculator.JSObjectLiteralToNative.class;
8587
case "jsii-calc.JSObjectLiteralToNativeClass": return software.amazon.jsii.tests.calculator.JSObjectLiteralToNativeClass.class;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package software.amazon.jsii.tests.calculator;
2+
3+
@javax.annotation.Generated(value = "jsii-pacmak")
4+
@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.JSII417Derived")
5+
public class JSII417Derived extends software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase {
6+
protected JSII417Derived(final software.amazon.jsii.JsiiObject.InitializationMode mode) {
7+
super(mode);
8+
}
9+
public JSII417Derived(final java.lang.String property) {
10+
super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii);
11+
software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this, java.util.stream.Stream.of(java.util.Objects.requireNonNull(property, "property is required")).toArray());
12+
}
13+
14+
public void bar() {
15+
this.jsiiCall("bar", Void.class);
16+
}
17+
18+
public void baz() {
19+
this.jsiiCall("baz", Void.class);
20+
}
21+
22+
protected java.lang.String getProperty() {
23+
return this.jsiiGet("property", java.lang.String.class);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package software.amazon.jsii.tests.calculator;
2+
3+
@javax.annotation.Generated(value = "jsii-pacmak")
4+
@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.JSII417PublicBaseOfBase")
5+
public class JSII417PublicBaseOfBase extends software.amazon.jsii.JsiiObject {
6+
protected JSII417PublicBaseOfBase(final software.amazon.jsii.JsiiObject.InitializationMode mode) {
7+
super(mode);
8+
}
9+
public JSII417PublicBaseOfBase() {
10+
super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii);
11+
software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this);
12+
}
13+
14+
public static software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase makeInstance() {
15+
return software.amazon.jsii.JsiiObject.jsiiStaticCall(software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase.class, "makeInstance", software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase.class);
16+
}
17+
18+
public void foo() {
19+
this.jsiiCall("foo", Void.class);
20+
}
21+
22+
public java.lang.Boolean getHasRoot() {
23+
return this.jsiiGet("hasRoot", java.lang.Boolean.class);
24+
}
25+
}

0 commit comments

Comments
 (0)