Skip to content

Commit

Permalink
fix(dotnet): abstract classes should have proxy implementations (#241)
Browse files Browse the repository at this point in the history
Abstract classes have can implementations that are not shared thorugh jsii. When trying to instantiate one for these types as a return value, the runtime crashes.
This change creates proxy class types for abstract classes. These types can be instantiated, and have implementations for any methods/properties marked as abstract.

Fixes #223

Add the ReturnsAbstract test to the complaince suite. This test ensures that proxy implementations are returned from methods that return a abstract class or interface.

* Add .DS_Store to .gitignore
* Rename JsiiInterfaceProxyAttribute to JsiiTypeProxy
* Proxy implementations are sealed
* Removed redundant checks for proxy resolution in runtime.
  • Loading branch information
costleya authored and Elad Ben-Israel committed Sep 22, 2018
1 parent 04cb6bb commit 828a26f
Show file tree
Hide file tree
Showing 53 changed files with 1,022 additions and 287 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,6 @@ jspm_packages/
# that NuGet uses for external packages. We don't want to ignore the
# lerna packages.
!/packages/*

#MacOS metadata
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using Amazon.JSII.Generator.Class;
using Amazon.JSII.JsonModel.Spec;
using Xunit;

namespace Amazon.JSII.Generator.UnitTests.Class
{
public class AbstractClassProxyGeneratorTests : GeneratorTestBase
{
private const string Prefix = nameof(Generator) + "." + nameof(AbstractClassProxyGenerator) + ".";

private string Render(ClassType classType)
{
Symbols.MapTypeToPackage("myFqn", classType.Assembly);
Symbols.MapNamespace(classType.QualifiedNamespace, "MyNamespace");
Symbols.MapTypeName("myFqn", "MyClass", TypeKind.Class);

var generator = new AbstractClassProxyGenerator(classType.Assembly, classType, Symbols, Namespaces);

var syntaxTree = generator.CreateSyntaxTree();
return syntaxTree.ToString();
}

[Fact(DisplayName = Prefix + nameof(IncludesAttribute))]
public void IncludesAttribute()
{
var classType = new ClassType
(
"myFqn",
"myPackage",
"myClass",
true,
initializer: new Method(true, false, false)
);

var actual = Render(classType);
var expected =
@"namespace MyNamespace
{
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesDocs))]
public void IncludesDocs()
{
// Docs are not currently generated as part of the C# code.
ClassType classType = new ClassType
(
fullyQualifiedName: "myFqn",
assembly: "myPackage",
name: "myClass",
isAbstract: true,
initializer: new Method(true, false, false),
docs: new Docs {{"foo", "bar"}}
);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
{
/// <remarks>foo: bar</remarks>
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesProperties))]
public void IncludesProperties()
{
ClassType classType = new ClassType
(
fullyQualifiedName: "myFqn",
assembly: "myPackage",
name: "myClass",
isAbstract: true,
initializer: new Method(true, false, false),
properties: new[]
{
new Property
(
name: "myProp",
type: new TypeReference("myPropTypeFqn"),
isImmutable: false,
isAbstract: true,
isProtected: false
),
new Property
(
name: "notMyProp",
type: new TypeReference("myPropTypeFqn"),
isImmutable: false,
isAbstract: false,
isProtected: false
)
}
);

Symbols.MapTypeName("myPropTypeFqn", "MyPropType", TypeKind.Class);
Symbols.MapPropertyName("myFqn", "myProp", "MyProp");

string actual = Render(classType);
string expected =
@"namespace MyNamespace
{
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}
[JsiiProperty(""myProp"", ""{\""fqn\"":\""myPropTypeFqn\""}"")]
public override MyPropType MyProp
{
get => GetInstanceProperty<MyPropType>();
set => SetInstanceProperty(value);
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}

[Fact(DisplayName = Prefix + nameof(IncludesMethods))]
public void IncludesMethods()
{
ClassType classType = new ClassType
(
fullyQualifiedName: "myFqn",
assembly: "myPackage",
name: "myClass",
isAbstract: true,
initializer: new Method(true, false, false),
methods: new[]
{
new Method
(
false,
false,
true,
name: "myMethod"
),
new Method
(
false,
false,
false,
name: "notMyMethod"
)
}
);

Symbols.MapMethodName("myFqn", "myMethod", "MyMethod");

string actual = Render(classType);
string expected =
@"namespace MyNamespace
{
[JsiiTypeProxy(typeof(MyClass), ""myFqn"")]
internal sealed class MyClassProxy : MyClass
{
private MyClassProxy(ByRefValue reference): base(reference)
{
}
[JsiiMethod(""myMethod"", null, ""[]"")]
public override void MyMethod()
{
InvokeInstanceVoidMethod(new object[]{});
}
}
}";
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Amazon.JSII.Generator.Class;
using Amazon.JSII.Generator.Interface;
using Amazon.JSII.JsonModel.Spec;
using Microsoft.CodeAnalysis;
using Xunit;
using TypeKind = Amazon.JSII.JsonModel.Spec.TypeKind;

namespace Amazon.JSII.Generator.UnitTests.Class
{
public class ClassGeneratorTests : GeneratorTestBase
{
const string Prefix = nameof(Generator) + "." + nameof(InterfaceGenerator) + ".";
const string Prefix = nameof(Generator) + "." + nameof(ClassGenerator) + ".";

string Render(ClassType classType)
private string Render(ClassType classType)
{
Symbols.MapTypeToPackage("myFqn", classType.Assembly);
Symbols.MapNamespace(classType.QualifiedNamespace, "MyNamespace");
Symbols.MapTypeName("myFqn", "MyClass", JsonModel.Spec.TypeKind.Class);
Symbols.MapTypeName("myFqn", "MyClass", TypeKind.Class);

ClassGenerator generator = new ClassGenerator(classType.Assembly, classType, Symbols, Namespaces);

Expand All @@ -36,7 +36,7 @@ public void IncludesAttribute()

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase
Expand Down Expand Up @@ -71,7 +71,7 @@ public void IncludesAbstractKeyword()

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public abstract class MyClass : DeputyBase
Expand Down Expand Up @@ -103,12 +103,12 @@ public void IncludesDocs()
name: "myClass",
isAbstract: false,
initializer: new Method(true, false, false),
docs: new Docs { { "foo", "bar" } }
docs: new Docs {{"foo", "bar"}}
);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
/// <remarks>foo: bar</remarks>
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
Expand Down Expand Up @@ -144,7 +144,7 @@ public void IncludesProperties()
{
new Property
(
name: "myProp",
name: "myProp",
type: new TypeReference("myPropTypeFqn"),
isImmutable: false,
isAbstract: false,
Expand All @@ -153,12 +153,12 @@ public void IncludesProperties()
}
);

Symbols.MapTypeName("myPropTypeFqn", "MyPropType", JsonModel.Spec.TypeKind.Class);
Symbols.MapTypeName("myPropTypeFqn", "MyPropType", TypeKind.Class);
Symbols.MapPropertyName("myFqn", "myProp", "MyProp");

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase
Expand Down Expand Up @@ -212,7 +212,7 @@ public void IncludesMethods()

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase
Expand Down Expand Up @@ -252,11 +252,11 @@ public void IncludesBase()
@base: new TypeReference("myBaseTypeFqn")
);

Symbols.MapTypeName("myBaseTypeFqn", "MyBaseType", JsonModel.Spec.TypeKind.Class);
Symbols.MapTypeName("myBaseTypeFqn", "MyBaseType", TypeKind.Class);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : MyBaseType
Expand Down Expand Up @@ -294,12 +294,12 @@ public void IncludesInterfaces()
}
);

Symbols.MapTypeName("myInterfaceFqn1", "MyInterface1", JsonModel.Spec.TypeKind.Interface);
Symbols.MapTypeName("myInterfaceFqn2", "MyInterface2", JsonModel.Spec.TypeKind.Interface);
Symbols.MapTypeName("myInterfaceFqn1", "MyInterface1", TypeKind.Interface);
Symbols.MapTypeName("myInterfaceFqn2", "MyInterface2", TypeKind.Interface);

string actual = Render(classType);
string expected =
@"namespace MyNamespace
@"namespace MyNamespace
{
[JsiiClass(typeof(MyClass), ""myFqn"", ""[]"")]
public class MyClass : DeputyBase, IMyInterface1, IMyInterface2
Expand All @@ -320,4 +320,4 @@ protected MyClass(DeputyProps props): base(props)
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
}
}
}
}
Loading

0 comments on commit 828a26f

Please sign in to comment.