openrewrite / openrewrite/rewrite-migrate-java
Apply var to method invocations
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 156
- Forks
- 130
- Avg merge
- 20h 57m
- Merged PRs (30d)
- 21
Description
What problem are you trying to solve?
While developing basic var usage with #217 java reveals that local variable type inference is a big field, so we decided to split.
One open area is applying var to variable declarations initialized by method invocations.
What precondition(s) should be checked before applying this recipe?
varis not applicable if
- the method has return type ´short´ or
byte - the method's return type does not match the type definition in the left-hand side
Describe the situation before applying the recipe
class A {
String getHello() {
return "Hello Rewrite";
}
void simple(String bar) {
String msg = getHello(); // (1)
System.out.println(msg);
}
void generic(String bar) {
List<String> msgs = List.of("Hello", "Rewrite"); // (2)
System.out.println(msgs);
}
}
Describe the situation after applying the recipe
class A {
String getHello() {
return "Hello Rewrite";
}
void simple(String bar) {
var msg = getHello(); // (1)
System.out.println(msg);
}
void generic(String bar) {
var msgs = List.of("Hello", "Rewrite"); // (2)
System.out.println(msgs);
}
}
Have you considered any alternatives or workarounds?
An alternative would be to no support var in combination with methods
Any additional context
It may be a good idea to implement this as an independent recipe, this would increase separation of concern and make configuration easier.
Are you interested in contributing this recipe to OpenRewrite?
Yes, but need additional support understanding generics.
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 by reviewing the existing basic var work referenced by #217 and its recipe tests, then trace how Java method return types and generic invocations are represented. Use the before-and-after examples as the acceptance cases, including the stated exclusions for short, byte, and mismatched return types. The issue does not name implementation files or test paths, so those must be located first.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100