-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate Builder Code #850
Comments
In Javapoet, these type of codes called "CodeBlock" also a class named First, you need to define your custom classes like Then, other calls can be added by a simple For more detailed information, you should look at CodeBlock docs The short answer to your question, the below code does the job 👍 ClassName myTransactionBuilderClazz = ClassName.get("com.yourpackage", "MyTransactionBuilder");
ClassName myAmountBuilderClazz = ClassName.get("com.yourpackage", "MyAmountBuilder");
CodeBlock block = CodeBlock.builder()
.addStatement("return new $T()", myTransactionBuilderClazz)
.indent()
.addStatement(".amount(new $T().value(100).currency(\"EUR\").build())", myAmountBuilderClazz)
.addStatement(".build()")
.unindent()
.build(); |
Sorry, it's not working because addStatement appends |
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: