Skip to content

Commit

Permalink
feat(jsii): Erase un-exported base classes instead of prohibiting tho…
Browse files Browse the repository at this point in the history
…se (#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
  • Loading branch information
RomainMuller committed Apr 3, 2019
1 parent 9cfd867 commit d006f5c
Show file tree
Hide file tree
Showing 20 changed files with 534 additions and 31 deletions.
25 changes: 25 additions & 0 deletions packages/jsii-calc/lib/erasures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Un-exported base classes are erased
// https://github.com/awslabs/jsii/issues/417
//
class JSII417PrivateRoot {
public readonly hasRoot = true;
}
export class JSII417PublicBaseOfBase extends JSII417PrivateRoot {
public static makeInstance(): JSII417PublicBaseOfBase {
return new JSII417PrivateBase("TEST");
}
public foo() { return; }
}
class JSII417PrivateBase extends JSII417PublicBaseOfBase {
constructor(protected readonly property: string) {
super();
}
public bar() { return; }
}
export class JSII417Derived extends JSII417PrivateBase {
public bar() {
return super.bar();
}
public baz() { return; }
}
1 change: 1 addition & 0 deletions packages/jsii-calc/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './calculator';
export * from './compliance';
export * from './documented';
export * from './erasures';
70 changes: 69 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,74 @@
}
]
},
"jsii-calc.JSII417Derived": {
"assembly": "jsii-calc",
"base": {
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
},
"fqn": "jsii-calc.JSII417Derived",
"initializer": {
"initializer": true,
"parameters": [
{
"name": "property",
"type": {
"primitive": "string"
}
}
]
},
"kind": "class",
"methods": [
{
"name": "bar"
},
{
"name": "baz"
}
],
"name": "JSII417Derived",
"properties": [
{
"immutable": true,
"name": "property",
"protected": true,
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.JSII417PublicBaseOfBase": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.JSII417PublicBaseOfBase",
"initializer": {
"initializer": true
},
"kind": "class",
"methods": [
{
"name": "makeInstance",
"returns": {
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
},
"static": true
},
{
"name": "foo"
}
],
"name": "JSII417PublicBaseOfBase",
"properties": [
{
"immutable": true,
"name": "hasRoot",
"type": {
"primitive": "boolean"
}
}
]
},
"jsii-calc.JSObjectLiteralForInterface": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.JSObjectLiteralForInterface",
Expand Down Expand Up @@ -4732,5 +4800,5 @@
}
},
"version": "0.8.2",
"fingerprint": "RIbNFrgvpaT1nTpWaVDxO0fWs/kZcVmMscsDGU/GXwE="
"fingerprint": "5kMI3UzT9N2sovIndhAmSefEqtrUStnpb0hmWwfLjOs="
}
5 changes: 5 additions & 0 deletions packages/jsii-kernel/test/test.kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,11 @@ defineTest('struct: non-empty object deserializes properly', async (test, sandbo
test.equal('foo', field.value);
});

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

// =================================================================================================

const testNames: { [name: string]: boolean } = { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,74 @@
}
]
},
"jsii-calc.JSII417Derived": {
"assembly": "jsii-calc",
"base": {
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
},
"fqn": "jsii-calc.JSII417Derived",
"initializer": {
"initializer": true,
"parameters": [
{
"name": "property",
"type": {
"primitive": "string"
}
}
]
},
"kind": "class",
"methods": [
{
"name": "bar"
},
{
"name": "baz"
}
],
"name": "JSII417Derived",
"properties": [
{
"immutable": true,
"name": "property",
"protected": true,
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.JSII417PublicBaseOfBase": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.JSII417PublicBaseOfBase",
"initializer": {
"initializer": true
},
"kind": "class",
"methods": [
{
"name": "makeInstance",
"returns": {
"fqn": "jsii-calc.JSII417PublicBaseOfBase"
},
"static": true
},
{
"name": "foo"
}
],
"name": "JSII417PublicBaseOfBase",
"properties": [
{
"immutable": true,
"name": "hasRoot",
"type": {
"primitive": "boolean"
}
}
]
},
"jsii-calc.JSObjectLiteralForInterface": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.JSObjectLiteralForInterface",
Expand Down Expand Up @@ -4732,5 +4800,5 @@
}
},
"version": "0.8.2",
"fingerprint": "RIbNFrgvpaT1nTpWaVDxO0fWs/kZcVmMscsDGU/GXwE="
"fingerprint": "5kMI3UzT9N2sovIndhAmSefEqtrUStnpb0hmWwfLjOs="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Amazon.JSII.Runtime.Deputy;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(typeof(JSII417Derived), "jsii-calc.JSII417Derived", "[{\"name\":\"property\",\"type\":{\"primitive\":\"string\"}}]")]
public class JSII417Derived : JSII417PublicBaseOfBase
{
public JSII417Derived(string property): base(new DeputyProps(new object[]{property}))
{
}

protected JSII417Derived(ByRefValue reference): base(reference)
{
}

protected JSII417Derived(DeputyProps props): base(props)
{
}

[JsiiProperty("property", "{\"primitive\":\"string\"}")]
protected virtual string Property
{
get => GetInstanceProperty<string>();
}

[JsiiMethod("bar", null, "[]")]
public virtual void Bar()
{
InvokeInstanceVoidMethod(new object[]{});
}

[JsiiMethod("baz", null, "[]")]
public virtual void Baz()
{
InvokeInstanceVoidMethod(new object[]{});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Amazon.JSII.Runtime.Deputy;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(typeof(JSII417PublicBaseOfBase), "jsii-calc.JSII417PublicBaseOfBase", "[]")]
public class JSII417PublicBaseOfBase : DeputyBase
{
public JSII417PublicBaseOfBase(): base(new DeputyProps(new object[]{}))
{
}

protected JSII417PublicBaseOfBase(ByRefValue reference): base(reference)
{
}

protected JSII417PublicBaseOfBase(DeputyProps props): base(props)
{
}

[JsiiProperty("hasRoot", "{\"primitive\":\"boolean\"}")]
public virtual bool HasRoot
{
get => GetInstanceProperty<bool>();
}

[JsiiMethod("makeInstance", "{\"fqn\":\"jsii-calc.JSII417PublicBaseOfBase\"}", "[]")]
public static JSII417PublicBaseOfBase MakeInstance()
{
return InvokeStaticMethod<JSII417PublicBaseOfBase>(typeof(JSII417PublicBaseOfBase), new object[]{});
}

[JsiiMethod("foo", null, "[]")]
public virtual void Foo()
{
InvokeInstanceVoidMethod(new object[]{});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ protected Class<?> resolveClass(final String fqn) throws ClassNotFoundException
case "jsii-calc.InterfaceInNamespaceIncludesClasses.Foo": return software.amazon.jsii.tests.calculator.InterfaceInNamespaceIncludesClasses.Foo.class;
case "jsii-calc.InterfaceInNamespaceIncludesClasses.Hello": return software.amazon.jsii.tests.calculator.InterfaceInNamespaceIncludesClasses.Hello.class;
case "jsii-calc.InterfaceInNamespaceOnlyInterface.Hello": return software.amazon.jsii.tests.calculator.InterfaceInNamespaceOnlyInterface.Hello.class;
case "jsii-calc.JSII417Derived": return software.amazon.jsii.tests.calculator.JSII417Derived.class;
case "jsii-calc.JSII417PublicBaseOfBase": return software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase.class;
case "jsii-calc.JSObjectLiteralForInterface": return software.amazon.jsii.tests.calculator.JSObjectLiteralForInterface.class;
case "jsii-calc.JSObjectLiteralToNative": return software.amazon.jsii.tests.calculator.JSObjectLiteralToNative.class;
case "jsii-calc.JSObjectLiteralToNativeClass": return software.amazon.jsii.tests.calculator.JSObjectLiteralToNativeClass.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package software.amazon.jsii.tests.calculator;

@javax.annotation.Generated(value = "jsii-pacmak")
@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.JSII417Derived")
public class JSII417Derived extends software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase {
protected JSII417Derived(final software.amazon.jsii.JsiiObject.InitializationMode mode) {
super(mode);
}
public JSII417Derived(final java.lang.String property) {
super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii);
software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this, java.util.stream.Stream.of(java.util.Objects.requireNonNull(property, "property is required")).toArray());
}

public void bar() {
this.jsiiCall("bar", Void.class);
}

public void baz() {
this.jsiiCall("baz", Void.class);
}

protected java.lang.String getProperty() {
return this.jsiiGet("property", java.lang.String.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package software.amazon.jsii.tests.calculator;

@javax.annotation.Generated(value = "jsii-pacmak")
@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.JSII417PublicBaseOfBase")
public class JSII417PublicBaseOfBase extends software.amazon.jsii.JsiiObject {
protected JSII417PublicBaseOfBase(final software.amazon.jsii.JsiiObject.InitializationMode mode) {
super(mode);
}
public JSII417PublicBaseOfBase() {
super(software.amazon.jsii.JsiiObject.InitializationMode.Jsii);
software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this);
}

public static software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase makeInstance() {
return software.amazon.jsii.JsiiObject.jsiiStaticCall(software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase.class, "makeInstance", software.amazon.jsii.tests.calculator.JSII417PublicBaseOfBase.class);
}

public void foo() {
this.jsiiCall("foo", Void.class);
}

public java.lang.Boolean getHasRoot() {
return this.jsiiGet("hasRoot", java.lang.Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,43 @@ class Hello(jsii.compat.TypedDict):
foo: jsii.Number


class JSII417PublicBaseOfBase(metaclass=jsii.JSIIMeta, jsii_type="jsii-calc.JSII417PublicBaseOfBase"):
def __init__(self) -> None:
jsii.create(JSII417PublicBaseOfBase, self, [])

@jsii.member(jsii_name="makeInstance")
@classmethod
def make_instance(cls) -> "JSII417PublicBaseOfBase":
return jsii.sinvoke(cls, "makeInstance", [])

@jsii.member(jsii_name="foo")
def foo(self) -> None:
return jsii.invoke(self, "foo", [])

@property
@jsii.member(jsii_name="hasRoot")
def has_root(self) -> bool:
return jsii.get(self, "hasRoot")


class JSII417Derived(JSII417PublicBaseOfBase, metaclass=jsii.JSIIMeta, jsii_type="jsii-calc.JSII417Derived"):
def __init__(self, property: str) -> None:
jsii.create(JSII417Derived, self, [property])

@jsii.member(jsii_name="bar")
def bar(self) -> None:
return jsii.invoke(self, "bar", [])

@jsii.member(jsii_name="baz")
def baz(self) -> None:
return jsii.invoke(self, "baz", [])

@property
@jsii.member(jsii_name="property")
def _property(self) -> str:
return jsii.get(self, "property")


class JSObjectLiteralForInterface(metaclass=jsii.JSIIMeta, jsii_type="jsii-calc.JSObjectLiteralForInterface"):
def __init__(self) -> None:
jsii.create(JSObjectLiteralForInterface, self, [])
Expand Down Expand Up @@ -2454,6 +2491,6 @@ def parts(self, value: typing.List[scope.jsii_calc_lib.Value]):
return jsii.set(self, "parts", value)


__all__ = ["AbstractClass", "AbstractClassBase", "AbstractClassReturner", "Add", "AllTypes", "AllTypesEnum", "AllowedMethodNames", "AsyncVirtualMethods", "AugmentableClass", "BinaryOperation", "Calculator", "CalculatorProps", "ClassThatImplementsTheInternalInterface", "ClassThatImplementsThePrivateInterface", "ClassWithMutableObjectLiteralProperty", "ClassWithPrivateConstructorAndAutomaticProperties", "ConstructorPassesThisOut", "Constructors", "ConsumersOfThisCrazyTypeSystem", "DefaultedConstructorArgument", "DerivedClassHasNoProperties", "DerivedStruct", "DoNotOverridePrivates", "DoNotRecognizeAnyAsOptional", "DocumentedClass", "DontComplainAboutVariadicAfterOptional", "DoubleTrouble", "EraseUndefinedHashValues", "EraseUndefinedHashValuesOptions", "ExportedBaseClass", "ExtendsInternalInterface", "ExtendsPrivateInterface", "GiveMeStructs", "Greetee", "GreetingAugmenter", "IAnotherPublicInterface", "IFriendlier", "IFriendlyRandomGenerator", "IInterfaceImplementedByAbstractClass", "IInterfaceThatShouldNotBeADataType", "IInterfaceWithInternal", "IInterfaceWithMethods", "IInterfaceWithOptionalMethodArguments", "IInterfaceWithProperties", "IInterfaceWithPropertiesExtension", "IMutableObjectLiteral", "INonInternalInterface", "IPrivatelyImplemented", "IPublicInterface", "IPublicInterface2", "IRandomNumberGenerator", "IReturnsNumber", "ImplementInternalInterface", "ImplementsInterfaceWithInternal", "ImplementsInterfaceWithInternalSubclass", "ImplementsPrivateInterface", "ImplictBaseOfBase", "InbetweenClass", "InterfaceInNamespaceIncludesClasses", "InterfaceInNamespaceOnlyInterface", "JSObjectLiteralForInterface", "JSObjectLiteralToNative", "JSObjectLiteralToNativeClass", "JavaReservedWords", "JsiiAgent", "LoadBalancedFargateServiceProps", "Multiply", "Negate", "NodeStandardLibrary", "NullShouldBeTreatedAsUndefined", "NullShouldBeTreatedAsUndefinedData", "NumberGenerator", "ObjectRefsInCollections", "Old", "OptionalConstructorArgument", "OptionalStruct", "OptionalStructConsumer", "OverrideReturnsObject", "PartiallyInitializedThisConsumer", "Polymorphism", "Power", "PublicClass", "PythonReservedWords", "ReferenceEnumFromScopedPackage", "ReturnsPrivateImplementationOfInterface", "RuntimeTypeChecking", "SingleInstanceTwoTypes", "Statics", "StringEnum", "StripInternal", "Sum", "SyncVirtualMethods", "Thrower", "UnaryOperation", "UnionProperties", "UseBundledDependency", "UseCalcBase", "UsesInterfaceWithProperties", "VariadicMethod", "VirtualMethodPlayground", "__jsii_assembly__", "composition"]
__all__ = ["AbstractClass", "AbstractClassBase", "AbstractClassReturner", "Add", "AllTypes", "AllTypesEnum", "AllowedMethodNames", "AsyncVirtualMethods", "AugmentableClass", "BinaryOperation", "Calculator", "CalculatorProps", "ClassThatImplementsTheInternalInterface", "ClassThatImplementsThePrivateInterface", "ClassWithMutableObjectLiteralProperty", "ClassWithPrivateConstructorAndAutomaticProperties", "ConstructorPassesThisOut", "Constructors", "ConsumersOfThisCrazyTypeSystem", "DefaultedConstructorArgument", "DerivedClassHasNoProperties", "DerivedStruct", "DoNotOverridePrivates", "DoNotRecognizeAnyAsOptional", "DocumentedClass", "DontComplainAboutVariadicAfterOptional", "DoubleTrouble", "EraseUndefinedHashValues", "EraseUndefinedHashValuesOptions", "ExportedBaseClass", "ExtendsInternalInterface", "ExtendsPrivateInterface", "GiveMeStructs", "Greetee", "GreetingAugmenter", "IAnotherPublicInterface", "IFriendlier", "IFriendlyRandomGenerator", "IInterfaceImplementedByAbstractClass", "IInterfaceThatShouldNotBeADataType", "IInterfaceWithInternal", "IInterfaceWithMethods", "IInterfaceWithOptionalMethodArguments", "IInterfaceWithProperties", "IInterfaceWithPropertiesExtension", "IMutableObjectLiteral", "INonInternalInterface", "IPrivatelyImplemented", "IPublicInterface", "IPublicInterface2", "IRandomNumberGenerator", "IReturnsNumber", "ImplementInternalInterface", "ImplementsInterfaceWithInternal", "ImplementsInterfaceWithInternalSubclass", "ImplementsPrivateInterface", "ImplictBaseOfBase", "InbetweenClass", "InterfaceInNamespaceIncludesClasses", "InterfaceInNamespaceOnlyInterface", "JSII417Derived", "JSII417PublicBaseOfBase", "JSObjectLiteralForInterface", "JSObjectLiteralToNative", "JSObjectLiteralToNativeClass", "JavaReservedWords", "JsiiAgent", "LoadBalancedFargateServiceProps", "Multiply", "Negate", "NodeStandardLibrary", "NullShouldBeTreatedAsUndefined", "NullShouldBeTreatedAsUndefinedData", "NumberGenerator", "ObjectRefsInCollections", "Old", "OptionalConstructorArgument", "OptionalStruct", "OptionalStructConsumer", "OverrideReturnsObject", "PartiallyInitializedThisConsumer", "Polymorphism", "Power", "PublicClass", "PythonReservedWords", "ReferenceEnumFromScopedPackage", "ReturnsPrivateImplementationOfInterface", "RuntimeTypeChecking", "SingleInstanceTwoTypes", "Statics", "StringEnum", "StripInternal", "Sum", "SyncVirtualMethods", "Thrower", "UnaryOperation", "UnionProperties", "UseBundledDependency", "UseCalcBase", "UsesInterfaceWithProperties", "VariadicMethod", "VirtualMethodPlayground", "__jsii_assembly__", "composition"]

publication.publish()

0 comments on commit d006f5c

Please sign in to comment.