jsii icon indicating copy to clipboard operation
jsii copied to clipboard

Java Transpiled Builder classes to have a method or way to copy the instance

Open bdoyle0182 opened this issue 4 months ago • 0 comments

Describe the feature

When transpiling from typescript interfaces to java, Jsii creates builder classes for the interface to. We have places in our library where we want to take in an existing pre-built instance of the interface and be able to copy the existing state and make some overrides. Right now it seems the only way is to manually copy all of the existing stateful members of the class into a new builder.

     JsiiClassProps newProps = new JsiiClassBuilder()
          // call all setting methods to copy state like so .myJsiiInterfaceMember(props.getMyJsiiInterfaceMember())
     .build();```
     
versus something like this where the builder can just take the state of the existing inteface object as a common pattern w/ a constructor param

```JsiiClassProps props;
     JsiiClassProps newProps = new JsiiClassBuilder(props)
          .myJsiiInterfaceMemberToOverride(props.getMyJsiiInterfaceMemberToOverride())
     .build();```
     
whereas in typescript this can be easily accomplished already w/ the spread operator on instantiation

### Use Case

See description, we regularly need to perform some overrides on an existing base level instance, having to override everything using all of the base level instance member getters is tedious, verbose, and error prone.

### Proposed Solution

Have an additional constructor on the builder that takes in an existing instance of the built type T class and creates the initial state of the builder

```JsiiClassProps props;
     JsiiClassProps newProps = new JsiiClassBuilder(props)
          .myJsiiInterfaceMemberToOverride(props.getMyJsiiInterfaceMemberToOverride())
     .build();``` 

### Other Information

_No response_

### Acknowledgements

- [ ] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

2.162.1

### Environment details (OS name and version, etc.)

MacOS

bdoyle0182 avatar Oct 22 '24 16:10 bdoyle0182