Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

UseVarForGenericMethodInvocations 'mvn rewrite:run' crash

Aperta
#698 1 commento 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
java
Ambito
devtools

Direzione di ricerca

Individua la recipe UseVarForGenericMethodInvocations e l’implementazione di JavaTemplate, quindi riproduci il crash con il caso di test Java mostrato nell’issue. Il lavoro è completato quando l’invocazione del metodo generico viene convertita in var senza eccezioni e viene prodotto l’output del codice sorgente previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

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?

Lingua principale
Java
Stelle
156
Fork
129
Merge medio
1g 12h
PR unite (30g)
15

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openrewrite/rewrite-migrate-java

Tutte le issue di openrewrite/rewrite-migrate-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.