openrewrite / openrewrite/rewrite

Allow optional target type to be specified for `JavaTemplate`

Open
#4,599 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.