Skip to content

Commit

Permalink
fix(jsii): Prohibit illegal uses of structs (aka data types) (#418)
Browse files Browse the repository at this point in the history
Expected failure cases:
- [X] An interface extending another one which is _not_ a struct, _cannot_ be a struct.
- [x] A class may not implement a struct interface
- [x] A regular interface may not implement a struct

Fixes #287
  • Loading branch information
RomainMuller authored Apr 2, 2019
1 parent a8ee954 commit 8ff9137
Show file tree
Hide file tree
Showing 27 changed files with 275 additions and 254 deletions.
6 changes: 3 additions & 3 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,15 +1056,15 @@ export interface IInterfaceWithOptionalMethodArguments {
* Abstract return type
*/

export interface InterfaceImplementedByAbstractClass {
export interface IInterfaceImplementedByAbstractClass {
readonly propFromInterface: string;
}

export abstract class AbstractClassBase {
public abstract readonly abstractProperty: string;
}

export abstract class AbstractClass extends AbstractClassBase implements InterfaceImplementedByAbstractClass {
export abstract class AbstractClass extends AbstractClassBase implements IInterfaceImplementedByAbstractClass {
public nonAbstractMethod() {
return 42;
}
Expand All @@ -1091,7 +1091,7 @@ export class AbstractClassReturner {
return new ConcreteClass();
}

public giveMeInterface(): InterfaceImplementedByAbstractClass {
public giveMeInterface(): IInterfaceImplementedByAbstractClass {
return new ConcreteClass();
}

Expand Down
47 changes: 23 additions & 24 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
},
"interfaces": [
{
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass"
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass"
}
],
"kind": "class",
Expand Down Expand Up @@ -263,7 +263,7 @@
"immutable": true,
"name": "propFromInterface",
"overrides": {
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass"
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass"
},
"type": {
"primitive": "string"
Expand Down Expand Up @@ -308,7 +308,7 @@
{
"name": "giveMeInterface",
"returns": {
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass"
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass"
}
}
],
Expand Down Expand Up @@ -2032,6 +2032,25 @@
"kind": "interface",
"name": "IFriendlyRandomGenerator"
},
"jsii-calc.IInterfaceImplementedByAbstractClass": {
"assembly": "jsii-calc",
"docs": {
"comment": "awslabs/jsii#220\nAbstract return type"
},
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass",
"kind": "interface",
"name": "IInterfaceImplementedByAbstractClass",
"properties": [
{
"abstract": true,
"immutable": true,
"name": "propFromInterface",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.IInterfaceThatShouldNotBeADataType": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -2409,26 +2428,6 @@
],
"name": "InbetweenClass"
},
"jsii-calc.InterfaceImplementedByAbstractClass": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"comment": "awslabs/jsii#220\nAbstract return type"
},
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass",
"kind": "interface",
"name": "InterfaceImplementedByAbstractClass",
"properties": [
{
"abstract": true,
"immutable": true,
"name": "propFromInterface",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.InterfaceInNamespaceIncludesClasses.Foo": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.InterfaceInNamespaceIncludesClasses.Foo",
Expand Down Expand Up @@ -4633,5 +4632,5 @@
}
},
"version": "0.8.2",
"fingerprint": "QQVEfUkkaxXMbXiD6wDVqdim8HdLW5L8CElwn+WdzUA="
"fingerprint": "CSV1TF9zK+8oZfXWIov5XOKrTmFIpov07DaTV3k1IfA="
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static software.amazon.jsii.JsiiVersion.JSII_RUNTIME_VERSION;

public final class JsiiVersionTest {
Expand All @@ -27,4 +29,10 @@ public void incompatibleVersions_2() {
public void incompatibleVersions_3() {
JsiiRuntime.assertVersionCompatible("0.7.0+abcd", "1.2.0+abcd");
}

@Test
public void versionIsDefined() {
assertNotNull(JSII_RUNTIME_VERSION);
assertNotEquals("", JSII_RUNTIME_VERSION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import software.amazon.jsii.tests.calculator.IPublicInterface;
import software.amazon.jsii.tests.calculator.IRandomNumberGenerator;
import software.amazon.jsii.tests.calculator.InbetweenClass;
import software.amazon.jsii.tests.calculator.InterfaceImplementedByAbstractClass;
import software.amazon.jsii.tests.calculator.IInterfaceImplementedByAbstractClass;
import software.amazon.jsii.tests.calculator.JSObjectLiteralForInterface;
import software.amazon.jsii.tests.calculator.JSObjectLiteralToNative;
import software.amazon.jsii.tests.calculator.JSObjectLiteralToNativeClass;
Expand Down Expand Up @@ -868,7 +868,7 @@ public void returnAbstract() {
assertEquals("propFromInterfaceValue", obj2.getPropFromInterface());
assertEquals(42, obj2.nonAbstractMethod());

InterfaceImplementedByAbstractClass iface = obj.giveMeInterface();
IInterfaceImplementedByAbstractClass iface = obj.giveMeInterface();
assertEquals("propFromInterfaceValue", iface.getPropFromInterface());

assertEquals("hello-abstract-property", obj.getReturnAbstractFromProperty().getAbstractProperty());
Expand All @@ -877,6 +877,7 @@ public void returnAbstract() {
@Test
public void doNotOverridePrivates_method_public() {
DoNotOverridePrivates obj = new DoNotOverridePrivates() {
@SuppressWarnings("unused")
public String privateMethod() {
return "privateMethod-Override";
}
Expand All @@ -888,6 +889,7 @@ public String privateMethod() {
@Test
public void doNotOverridePrivates_method_private() {
DoNotOverridePrivates obj = new DoNotOverridePrivates() {
@SuppressWarnings("unused")
private String privateMethod() {
return "privateMethod-Override";
}
Expand All @@ -899,6 +901,7 @@ private String privateMethod() {
@Test
public void doNotOverridePrivates_property_by_name_private() {
DoNotOverridePrivates obj = new DoNotOverridePrivates() {
@SuppressWarnings("unused")
private String privateProperty() {
return "privateProperty-Override";
}
Expand All @@ -910,6 +913,7 @@ private String privateProperty() {
@Test
public void doNotOverridePrivates_property_by_name_public() {
DoNotOverridePrivates obj = new DoNotOverridePrivates() {
@SuppressWarnings("unused")
public String privateProperty() {
return "privateProperty-Override";
}
Expand All @@ -921,9 +925,11 @@ public String privateProperty() {
@Test
public void doNotOverridePrivates_property_getter_public() {
DoNotOverridePrivates obj = new DoNotOverridePrivates() {
@SuppressWarnings("unused")
public String getPrivateProperty() {
return "privateProperty-Override";
}
@SuppressWarnings("unused")
public void setPrivateProperty(String value) {
throw new RuntimeException("Boom");
}
Expand All @@ -939,9 +945,11 @@ public void setPrivateProperty(String value) {
@Test
public void doNotOverridePrivates_property_getter_private() {
DoNotOverridePrivates obj = new DoNotOverridePrivates() {
@SuppressWarnings("unused")
private String getPrivateProperty() {
return "privateProperty-Override";
}
@SuppressWarnings("unused")
public void setPrivateProperty(String value) {
throw new RuntimeException("Boom");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
},
"interfaces": [
{
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass"
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass"
}
],
"kind": "class",
Expand Down Expand Up @@ -263,7 +263,7 @@
"immutable": true,
"name": "propFromInterface",
"overrides": {
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass"
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass"
},
"type": {
"primitive": "string"
Expand Down Expand Up @@ -308,7 +308,7 @@
{
"name": "giveMeInterface",
"returns": {
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass"
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass"
}
}
],
Expand Down Expand Up @@ -2032,6 +2032,25 @@
"kind": "interface",
"name": "IFriendlyRandomGenerator"
},
"jsii-calc.IInterfaceImplementedByAbstractClass": {
"assembly": "jsii-calc",
"docs": {
"comment": "awslabs/jsii#220\nAbstract return type"
},
"fqn": "jsii-calc.IInterfaceImplementedByAbstractClass",
"kind": "interface",
"name": "IInterfaceImplementedByAbstractClass",
"properties": [
{
"abstract": true,
"immutable": true,
"name": "propFromInterface",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.IInterfaceThatShouldNotBeADataType": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -2409,26 +2428,6 @@
],
"name": "InbetweenClass"
},
"jsii-calc.InterfaceImplementedByAbstractClass": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"comment": "awslabs/jsii#220\nAbstract return type"
},
"fqn": "jsii-calc.InterfaceImplementedByAbstractClass",
"kind": "interface",
"name": "InterfaceImplementedByAbstractClass",
"properties": [
{
"abstract": true,
"immutable": true,
"name": "propFromInterface",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.InterfaceInNamespaceIncludesClasses.Foo": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.InterfaceInNamespaceIncludesClasses.Foo",
Expand Down Expand Up @@ -4633,5 +4632,5 @@
}
},
"version": "0.8.2",
"fingerprint": "QQVEfUkkaxXMbXiD6wDVqdim8HdLW5L8CElwn+WdzUA="
"fingerprint": "CSV1TF9zK+8oZfXWIov5XOKrTmFIpov07DaTV3k1IfA="
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Amazon.JSII.Tests.CalculatorNamespace
{
[JsiiClass(typeof(AbstractClass), "jsii-calc.AbstractClass", "[]")]
public abstract class AbstractClass : AbstractClassBase, IInterfaceImplementedByAbstractClass
public abstract class AbstractClass : AbstractClassBase, IIInterfaceImplementedByAbstractClass
{
protected AbstractClass(): base(new DeputyProps(new object[]{}))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public virtual AbstractClass GiveMeAbstract()
return InvokeInstanceMethod<AbstractClass>(new object[]{});
}

[JsiiMethod("giveMeInterface", "{\"fqn\":\"jsii-calc.InterfaceImplementedByAbstractClass\"}", "[]")]
public virtual IInterfaceImplementedByAbstractClass GiveMeInterface()
[JsiiMethod("giveMeInterface", "{\"fqn\":\"jsii-calc.IInterfaceImplementedByAbstractClass\"}", "[]")]
public virtual IIInterfaceImplementedByAbstractClass GiveMeInterface()
{
return InvokeInstanceMethod<IInterfaceImplementedByAbstractClass>(new object[]{});
return InvokeInstanceMethod<IIInterfaceImplementedByAbstractClass>(new object[]{});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Amazon.JSII.Tests.CalculatorNamespace
/// awslabs/jsii#220
/// Abstract return type
/// </summary>
[JsiiInterface(typeof(IInterfaceImplementedByAbstractClass), "jsii-calc.InterfaceImplementedByAbstractClass")]
public interface IInterfaceImplementedByAbstractClass
[JsiiInterface(typeof(IIInterfaceImplementedByAbstractClass), "jsii-calc.IInterfaceImplementedByAbstractClass")]
public interface IIInterfaceImplementedByAbstractClass
{
[JsiiProperty("propFromInterface", "{\"primitive\":\"string\"}")]
string PropFromInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace Amazon.JSII.Tests.CalculatorNamespace
/// awslabs/jsii#220
/// Abstract return type
/// </summary>
[JsiiTypeProxy(typeof(IInterfaceImplementedByAbstractClass), "jsii-calc.InterfaceImplementedByAbstractClass")]
internal sealed class InterfaceImplementedByAbstractClassProxy : DeputyBase, IInterfaceImplementedByAbstractClass
[JsiiTypeProxy(typeof(IIInterfaceImplementedByAbstractClass), "jsii-calc.IInterfaceImplementedByAbstractClass")]
internal sealed class IInterfaceImplementedByAbstractClassProxy : DeputyBase, IIInterfaceImplementedByAbstractClass
{
private InterfaceImplementedByAbstractClassProxy(ByRefValue reference): base(reference)
private IInterfaceImplementedByAbstractClassProxy(ByRefValue reference): base(reference)
{
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected Class<?> resolveClass(final String fqn) throws ClassNotFoundException
case "jsii-calc.IAnotherPublicInterface": return software.amazon.jsii.tests.calculator.IAnotherPublicInterface.class;
case "jsii-calc.IFriendlier": return software.amazon.jsii.tests.calculator.IFriendlier.class;
case "jsii-calc.IFriendlyRandomGenerator": return software.amazon.jsii.tests.calculator.IFriendlyRandomGenerator.class;
case "jsii-calc.IInterfaceImplementedByAbstractClass": return software.amazon.jsii.tests.calculator.IInterfaceImplementedByAbstractClass.class;
case "jsii-calc.IInterfaceThatShouldNotBeADataType": return software.amazon.jsii.tests.calculator.IInterfaceThatShouldNotBeADataType.class;
case "jsii-calc.IInterfaceWithInternal": return software.amazon.jsii.tests.calculator.IInterfaceWithInternal.class;
case "jsii-calc.IInterfaceWithMethods": return software.amazon.jsii.tests.calculator.IInterfaceWithMethods.class;
Expand All @@ -74,7 +75,6 @@ protected Class<?> resolveClass(final String fqn) throws ClassNotFoundException
case "jsii-calc.ImplementsPrivateInterface": return software.amazon.jsii.tests.calculator.ImplementsPrivateInterface.class;
case "jsii-calc.ImplictBaseOfBase": return software.amazon.jsii.tests.calculator.ImplictBaseOfBase.class;
case "jsii-calc.InbetweenClass": return software.amazon.jsii.tests.calculator.InbetweenClass.class;
case "jsii-calc.InterfaceImplementedByAbstractClass": return software.amazon.jsii.tests.calculator.InterfaceImplementedByAbstractClass.class;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@javax.annotation.Generated(value = "jsii-pacmak")
@software.amazon.jsii.Jsii(module = software.amazon.jsii.tests.calculator.$Module.class, fqn = "jsii-calc.AbstractClass")
public abstract class AbstractClass extends software.amazon.jsii.tests.calculator.AbstractClassBase implements software.amazon.jsii.tests.calculator.InterfaceImplementedByAbstractClass {
public abstract class AbstractClass extends software.amazon.jsii.tests.calculator.AbstractClassBase implements software.amazon.jsii.tests.calculator.IInterfaceImplementedByAbstractClass {
protected AbstractClass(final software.amazon.jsii.JsiiObject.InitializationMode mode) {
super(mode);
}
Expand Down
Loading

0 comments on commit 8ff9137

Please sign in to comment.