question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow Java code generation to use nested method invocations

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
snicollcommented, Mar 30, 2020

what were you envisioning?

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.

1reaction
onobccommented, Mar 23, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found