openrewrite / openrewrite/rewrite-migrate-java

UseVarForGenericMethodInvocations 'mvn rewrite:run' crash

Open
#698 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
156
Forks
130
Avg merge
20h 57m
Merged PRs (30d)
21

Description

What version of OpenRewrite are you using?

Latest 3.4.0

What is the smallest, simplest way to reproduce the problem?

    @Test
    public void useVarBug() {
        rewriteRun(
          spec -> spec.recipe(new UseVarForGenericMethodInvocations()),
          version(
            //language=java
            java(
              """
                import java.util.Optional;
                
                class A {
                    <T extends Number> void test(T element) {
                        Optional<T> o = Optional.of(element);
                    }
                }
                """,
              """
                import java.util.Optional;
                
                class A {
                    <T extends Number> void test(T element) {
                        var o = Optional.of(element);
                    }
                }
                """), 17));
    }

What did you expect to see?

Replace variable with var

What did you see instead?

It crashes.

What is the full stack trace of any errors you encountered?

org.openrewrite.internal.RecipeRunException: java.lang.IllegalArgumentException: Expected a template that would generate exactly one statement to replace one statement, but generated 2. Template:
var o = __P__.<java.util.Optional<T extends java.lang.Number>>/*__p1__*/p()
Substitutions:
Substitutions(code=var #{} = #{any()}, parameters=[o, Optional.of(element)], propertyPlaceholderHelper=org.openrewrite.internal.PropertyPlaceholderHelper@3ee0b4f7)
Statement:
Optional<T> o = Optional.of(element)

Are you interested in contributing a fix to OpenRewrite

I think there are 2 issues here.
In the JavaTemplate implementation:
var o = __P__.<java.util.Optional<T extends java.lang.Number>>/*__p1__*/p()
should be
var o = __P__.<java.util.Optional<T>>/*__p1__*/p()

and this would work by making the template in UseVarForGenericMethodInvocations context sensitive.

But maybe, for this recipe, it would be best to do the "transformation" by hand, instead of using JavaTemplate?

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

Locate the UseVarForGenericMethodInvocations recipe and the JavaTemplate implementation, then reproduce the crash with the Java test case shown in the issue. The work is done when the generic method invocation is converted to var without an exception and the expected source output is produced.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.