-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dotnet): abstract classes should have proxy implementations (#241)
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
Showing
53 changed files
with
1,022 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
...t-generator/src/Amazon.JSII.Generator.UnitTests/Class/AbstractClassProxyGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.