Skip to content

Commit

Permalink
fix(java): support abstract return types (#224)
Browse files Browse the repository at this point in the history
Generate a $proxy class for all abstract classes
and use them when the return type is abstract,
similar to interface proxies.

This change also adds "abstract: true" to all
interface members, so the proxy generator can
treat interfaces and classes polymorphically.

Added a compliance test "returnAbstract" which
verifies this behavior.

Fixes #220
Related to #223 (.NET)
Related to aws/aws-cdk#680 (require jsii update)
  • Loading branch information
Elad Ben-Israel committed Sep 13, 2018
1 parent 72927bd commit 3257223
Show file tree
Hide file tree
Showing 69 changed files with 7,834 additions and 6,804 deletions.
2,642 changes: 1,321 additions & 1,321 deletions package-lock.json

Large diffs are not rendered by default.

1,282 changes: 641 additions & 641 deletions packages/codemaker/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/jsii-build-tools/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/jsii-calc-base-of-base/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"name": "VeryBaseProps",
"properties": [
{
"abstract": true,
"name": "foo",
"type": {
"fqn": "@scope/jsii-calc-base-of-base.Very"
Expand All @@ -67,5 +68,5 @@
}
},
"version": "0.7.4",
"fingerprint": "xM+yEz06NDR7n4G5n4VDoITs275dSqMhFDXgKOErl/M="
"fingerprint": "uxjBWk0nRDSHNR2FL01kRusKUoxa53smUFsizIJYBSs="
}
3 changes: 2 additions & 1 deletion packages/jsii-calc-base/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"name": "BaseProps",
"properties": [
{
"abstract": true,
"name": "bar",
"type": {
"primitive": "string"
Expand All @@ -100,5 +101,5 @@
}
},
"version": "0.7.4",
"fingerprint": "/t6/DercTjOI1TLjj3MowD7pZkpm70sJQdeKwQMpa9M="
"fingerprint": "6NsGM++itCYmctZJdOp8kPJ3CfBnajQ78wNpY+EwZIQ="
}
9 changes: 8 additions & 1 deletion packages/jsii-calc-lib/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"kind": "interface",
"methods": [
{
"abstract": true,
"docs": {
"comment": "Say hello!"
},
Expand All @@ -123,6 +124,7 @@
"name": "MyFirstStruct",
"properties": [
{
"abstract": true,
"docs": {
"comment": "An awesome number value"
},
Expand All @@ -132,6 +134,7 @@
}
},
{
"abstract": true,
"docs": {
"comment": "A string value"
},
Expand All @@ -141,6 +144,7 @@
}
},
{
"abstract": true,
"name": "firstOptional",
"type": {
"collection": {
Expand Down Expand Up @@ -250,6 +254,7 @@
"name": "StructWithOnlyOptionals",
"properties": [
{
"abstract": true,
"docs": {
"comment": "The first optional!"
},
Expand All @@ -260,13 +265,15 @@
}
},
{
"abstract": true,
"name": "optional2",
"type": {
"optional": true,
"primitive": "number"
}
},
{
"abstract": true,
"name": "optional3",
"type": {
"optional": true,
Expand Down Expand Up @@ -317,5 +324,5 @@
}
},
"version": "0.7.4",
"fingerprint": "dGLkY7rPKJjBMLQCOCj5AXR3Q1+cZcoiazcIhDUV++8="
"fingerprint": "PD1bhVhBjZ0dNrD8K4FK6RT+W5RhpkTEJDml/OiM7Ws="
}
52 changes: 52 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,4 +813,56 @@ export namespace InterfaceInNamespaceIncludesClasses {
*/
export interface InterfaceWithOptionalMethodArguments {
hello(arg1: string, arg2?: number): void
}

/**
* awslabs/jsii#220
* Abstract return type
*/

export interface InterfaceImplementedByAbstractClass {
readonly propFromInterface: string;
}

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

export abstract class AbstractClass extends AbstractClassBase implements InterfaceImplementedByAbstractClass {
public nonAbstractMethod() {
return 42;
}

public abstract abstractMethod(name: string): string;

public get propFromInterface() {
return 'propFromInterfaceValue';
}
}

class ConcreteClass extends AbstractClass {
public abstractMethod(name: string) {
return `Hello, ${name}!!`;
}

public get abstractProperty() {
return 'Hello, dude!';
}
}


export class AbstractClassReturner {
public giveMeAbstract(): AbstractClass {
return new ConcreteClass();
}

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

public get returnAbstractFromProperty(): AbstractClassBase {
return {
abstractProperty: 'hello-abstract-property'
}
}
}
Loading

0 comments on commit 3257223

Please sign in to comment.