Open robertvazan opened 1 year ago
Hey @robertvazan, Would like to contribute to this issue? Could you please assign this issue to me?
And just to confirm you are looking for an API which would call a generalized method and outputs the certain result based on the type parameter, right?
FWIW, I implemented something extremely similar to this for in-lining static final fields of a complex Object: https://github.com/Christopher-Chianelli/timefold-solver/blob/38c98a8b6311ac1ecc7fafccc35db748f7080db8/spring-integration/spring-boot-autoconfigure/src/main/java/ai/timefold/solver/spring/boot/autoconfigure/util/PojoInliner.java
In theory, it could be modified to work with any CodeBuilder
as a new formatting option, something like "$O" for object or "$P" for POJO.
What I needed the feature for was in-lining a calculated complex object (i.e. many different fields and nested classes).
The main issue for integrating are:
Managed to make it work with fragments: https://github.com/square/javapoet/pull/999
It still need fresh variable names to store objects; it currently uses a $$javapoet$
as a default prefix, and allows it to be changed using useNamePrefix
.
It would be neat to have CodeBlock API that recursively emits code to construct transparent values (records, arrays, collections, enums, primitives, strings, and null, perhaps others).
For now, I have a utility method that does this for the types I care about:
This could be implemented as a new parameter type (perhaps
$V
for value) that generalizes$L
and$S
. It would sort of serialize values as Java code the same way JSON serializes values as JavaScript code.