openrewrite / openrewrite/rewrite
Allow optional target type to be specified for `JavaTemplate`
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 571
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
When creating a JavaTemplate it should be possible to optionally specify a target type (as defined in the JLS). This is for example necessary when the template expression is only a method reference or a lambda and would unlock further Refaster use cases as well as declaring some more templates as context-free (like this example).
This idea would be for the JavaTemplate.Builder to declare a new targetType() method. Whether the parameter's type should be a plain String, JavaType, or even have both overloads would have to be explored.
Note that the target type would have to be included in the cache key for the context-free template cache.
An example would be a template like () -> 3 which would not be usable as a context-free template unless a matching target type like Supplier<Integer> is provided. This target type would then allow the code generator to generate a variable declaration for the template, like Supplier<Integer> o = () -> 3;.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with JavaTemplate.Builder and the context-free template cache key in rewrite-java/src/main/java/org/openrewrite/java/internal/template/JavaTemplateParser.java, then review the linked SuccessFailureCallbackToBiConsumerVisitor example. Determine how the target type should be represented and included in cache identity. Done means a target type can support context-free lambda or method-reference templates such as () -> 3 without breaking existing templates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100