openrewrite / openrewrite/rewrite-migrate-java

Apply var to method invocations

Open
#236 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

recipe
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

  1. the method has return type ´short´ or byte
  2. 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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.