-
Notifications
You must be signed in to change notification settings - Fork 258
Description
Given a TypeScript implementation:
export interface IResource {
readonly stack: Stack;
}
The Java generated code will include a Jsii$Proxy
class that Implements all (including inherited) members obtained from .jsii
:
final static class Jsii$Proxy extends software.amazon.jsii.JsiiObject implements software.amazon.awscdk.core.IResource {
public @org.jetbrains.annotations.NotNull software.amazon.awscdk.core.Stack getStack() {
return this.jsiiGet("stack", software.amazon.awscdk.core.Stack.class);
}
}
In the above example, the IResource
interface includes a single property Stack
which translates to an implementation of a get method - getStack()
, satisfying the interface requirement.
A common pattern in the AWS CDK is to create an interface and an abstract class which implement the interface, in the IResource
example we will have the abstract class Resource
which implements IResource
:
export abstract class Resource implements IResource {
...
}
Every class that wishes to implement IResource
will extends Resource
.
We can use the same logic when generating Jsii$Proxy
class, instead of implementing all of the memebers required by the interface, Jsii$Proxy
can extend the base class:
final static class Jsii$Proxy extends software.amazon.jsii.JsiiObject implements software.amazon.awscdk.core.IResource extends software.amazon.awscdk.core.Resource {
public @org.jetbrains.annotations.NotNull software.amazon.awscdk.core.Stack getStack() {
return this.jsiiGet("stack", software.amazon.awscdk.core.Stack.class);
}
}
Why should we do it?
First it will reduce the amount of generated code.
It will also prevent a specific situation in which Jsii$Proxy
class that was generated from a .jsii
file of an old AWS CDK version might break when compiled against java class files that were generated with a newer version of the AWS CDK.
To make it clearer lets look at an example, assume the above Jsii$Proxy
was created using a .jsii
file generated for AWS CDK version 1.59.0
. In version 1.60.0
of the AWS CDK we add a property to IResource
- env
and we also add it to Resource
. Since the Jsii$Proxy
class was created with no knowledge of the newly added env
property, if we try to compile it with Java class files generated by version 1.60.0
we will get a compilation error as it no longer implements IResource
. We can avoid this by having Jsii$Proxy
extending Resource
.