Allow Java code generation to use nested method invocations
See original GitHub issueI do not currently see a way to generate the following Java code:
Builder.newBuilder().setId("example-dependency");
I can produce the following multi-line version
Builder builder = Builder.newBuilder();
builder.setId("example-dependency");
but in reality the fluent call chain I am trying to generate is like 9 calls long and I don’t want to produce this multi-statement style code.
This limitation stems from the fact that JavaMethodInvocation
target can only be a String class name. I would like to add the ability to have its target optionally be another JavaMethodInvocation
.
Example code:
new JavaMethodInvocation(
new JavaMethodInvocation("com.example.Builder", "newBuilder")),
"setId", "example-dependeny"));
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Java Nested Methods | Edureka Community
Java program implements method inside method public class Hello { interface myInterface { //create a local interface with one abstract ...
Read more >Nested code generator invocation by @-expression.
Dynamic code generation is a technique that generates and executes fragments of executable code during the run-time of an program to reduce the...
Read more >Nested functions in Java - Stack Overflow
The () -> syntax works on any interface that defines exactly one method. So you can use it with Runnable but it doesn't...
Read more >Java Generated Code | Protocol Buffers - Google Developers
However, it will not create build/gen or build ; they must already exist. You can specify multiple .proto files in a single invocation;...
Read more >Chapter 8. Classes - Oracle Help Center
The body of a class declares members (fields and methods and nested ... The Java programming language allows threads to access shared variables...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for asking. I can’t really say without spending some time investigating. My plan was to open up the API a bit so that other forms of writer could be provided. So something where you’d define the type and still offer a way to add annotations on the fly via a customizer but where methods could be provided by another form.
We don’t want a hard dependency on Javapoet or any other library so that would work in the form of a strategy interface where we’d provide a JavaPoet implementation (and therefore an optional dependency on it).
From that perspective, I don’t think that the fact we have to support all languages is necessarily true. For languages where such third party library does not exist, we’d stick with the limitation of the current implementation (and/or we would improve it).
At the end of the day, custom instances that need to write “complex” java code in Java (or Kotlin) could simply create a
ProjectContributor
that does that using JavaPoet directly and not rely on any of the code writer API of this project.I started looking into this briefly @snicoll and Javapoet does seem promising. Its pretty slick. I am continuing to look into it - just giving a bit of status.