You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
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
The text was updated successfully, but these errors were encountered:
In Javapoet, these type of codes called "CodeBlock" also a class named CodeBlock is used to generate this type of calls.
First, you need to define your custom classes like MyTransactionBuilder and MyAmountBuilder by their package name.
Then, other calls can be added by a simple addStatement method. In this method, you can reference custom types that you defined before. For example, $T is a placeholder for typename.
For more detailed information, you should look at CodeBlock docs
The short answer to your question, the below code does the job 👍
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:
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
The text was updated successfully, but these errors were encountered: