#850·javapoet

Generate Builder Code

Author: 38leinaDCreated Aug 11, 2021Updated Nov 1, 2023

Hi, This is more a question than an issue. I am trying to understand JavaPoet. I am able to understand how to generate simple code fragements, but what i would like to do is generated nested code structures based on builder objects. So, i would like to generate code like this:

new MyTransactionBuilder()
  .amount(MyAmountBuilder()
    .value(100)
    .currency("EUR")
    .build())
  .build()

I am having a hard time to understand how i can generate code that includes methods-calls which again need to have a code fragment as the methods parameter. I.e. amount method call needs a code fragment again. If someone could give me a push in the right direction, that would be great. Feels, like I am missing some basic understanding somewhere.

Thanks, Daniel