diff --git a/src/main/java/org/openrewrite/java/migrate/lang/UseVarKeyword.java b/src/main/java/org/openrewrite/java/migrate/lang/UseVarKeyword.java index ef22dce1ea..27537436de 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/UseVarKeyword.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/UseVarKeyword.java @@ -97,14 +97,17 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m Expression initializer = vd.getVariables().get(0).getInitializer(); boolean isDeclarationOnly = isNull(initializer); + if (isDeclarationOnly) return vd; + + initializer = initializer.unwrap(); boolean isNullAssigment = initializer instanceof J.Literal && isNull(((J.Literal) initializer).getValue()); boolean alreadyUseVar = typeExpression instanceof J.Identifier && "var".equals(((J.Identifier) typeExpression).getSimpleName()); - boolean isGeneric = typeExpression instanceof J.ParameterizedType; // todo implement generics! + boolean isGenericDefinition = typeExpression instanceof J.ParameterizedType ; + boolean isGenericInitializer = initializer instanceof J.NewClass && ((J.NewClass) initializer).getClazz() instanceof J.ParameterizedType; boolean useTernary = initializer instanceof J.Ternary; - if (alreadyUseVar || isDeclarationOnly || isNullAssigment || isGeneric || useTernary) return vd; + if (alreadyUseVar || isNullAssigment|| isGenericDefinition || isGenericInitializer|| useTernary) return vd; - J.VariableDeclarations result = transformToVar(vd); - return result; + return transformToVar(vd); } private boolean determineIfOutsideInitializer(Cursor cursor, boolean childWasBlock) { @@ -149,8 +152,11 @@ private J.VariableDeclarations transformToVar(@NotNull J.VariableDeclarations vd if (initializer instanceof J.Literal) { initializer = expandWithPrimitivTypeHint(vd, initializer); - } + } else if(initializer instanceof J.MethodInvocation && nonNull(((J.MethodInvocation) initializer).getTypeParameters())) { + initializer = initializer; + } else if(initializer instanceof J.NewClass && ((J.NewClass) initializer).getClazz() instanceof J.ParameterizedType) { + } return vd.withTemplate(template, vd.getCoordinates().replace(), simpleName, initializer); } diff --git a/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java b/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java index 47461754b0..fa4e02723d 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/UseVarKeywordTest.java @@ -59,7 +59,7 @@ void assignNothing() { java(""" package com.example.app; - class A { + class A { void m() { String str; } @@ -129,159 +129,187 @@ void m() { ) ); } - } - - @Nested - class Objects { - @Nested - class Applicable { - @Test - void inMethodBody() { - //language=java - rewriteRun( - version( - java(""" - package com.example.app; - - class A { - void m() { - Object o = new Object(); - } - } - """, """ + @Test + void withTernary() { + //language=java + rewriteRun( + version( + java(""" package com.example.app; class A { void m() { - var o = new Object(); + String o = true ? "isTrue" : "Test"; } } """), + 10 + ) + ); + } + + @Nested + class Generics { + @Test + void ifWelldefined() { + //language=java + rewriteRun( + version( + java(""" + package com.example.app; + + import java.util.ArrayList; + + class A { + void m() { + List strs = new ArrayList(); + } + } + """), 10 ) ); } @Test - @Disabled("Not yet implemented") - void withTernary() { + void forNoDiamondOperators() { //language=java rewriteRun( version( - java(""" - package com.example.app; - - class A { - void m() { - String o = true ? "isTrue" : "Test"; - } - } - """, """ - package com.example.app; - - class A { - void m() { - var o = true ? "isTrue" : "Test"; - } - } - """), + java( + """ + package com.example.app; + + import java.util.ArrayList; + import java.util.List; + + class A { + void m() { + List strs = new ArrayList(); + } + } + """,""" + package com.example.app; + + import java.util.ArrayList; + import java.util.List; + + class A { + void m() { + var strs = new ArrayList(); + } + } + """), 10 ) ); } @Test - void inStaticInitializer() { + void withFactoryMethods() { //language=java rewriteRun( version( - java(""" - package com.example.app; - - class A { - static { - Object o = new Object(); - } - } - """, """ - package com.example.app; - - class A { - static { - var o = new Object(); + java( + """ + package com.example.app; + + import java.util.List; + + class A { + void m() { + List strs = List.of("one", "two"); + } } - } - """), + """), 10 ) ); } @Test - void inInstanceInitializer() { + void forEmptyFactoryMethod() { //language=java rewriteRun( version( - java(""" - package com.example.app; - - class A { - { - Object o = new Object(); - } - } - """, """ - package com.example.app; - - class A { - { - var o = new Object(); - } - } - """), + java( + """ + package com.example.app; + + import java.util.List; + + class A { + void m() { + List strs = List.of(); + } + } + """), 10 ) ); } - } - @Nested - class NotApplicable { @Test - void asParameter() { + void withDiamondOperatorOnRaw() { //language=java rewriteRun( version( java(""" - package com.example.app; - - class A { - Object m(Object o) { - return o; - } - } - """), + package com.example.app; + + import java.util.ArrayList; + + class A { + void m() { + List strs = new ArrayList(); + } + } + """), 10 ) ); } @Test - void asField() { + void withDiamondOperator() { //language=java rewriteRun( version( java(""" - package com.example.app; - - class A { - Object o; - Object m() { - return o; - } - } - """), + package com.example.app; + + import java.util.ArrayList; + + class A { + void m() { + List strs = new ArrayList<>(); + } + } + """), + 10 + ) + ); + } + + @Test + void forEmptyDiamondOperators() { + //language=java + rewriteRun( + version( + java( + """ + package com.example.app; + + import java.util.ArrayList; + import java.util.List; + + class A { + void m() { + List strs = new ArrayList<>(); + } + } + """), 10 ) ); @@ -290,33 +318,29 @@ Object m() { } @Nested - @Disabled("Not yet implemented") - class Generics { + class Objects { + @Nested class Applicable { @Test - void ifWelldefined() { + void inMethodBody() { //language=java rewriteRun( version( java(""" package com.example.app; - import java.util.ArrayList; - class A { void m() { - List strs = new ArrayList(); + Object o = new Object(); } } """, """ package com.example.app; - import java.util.ArrayList; - class A { void m() { - var strs = new ArrayList(); + var o = new Object(); } } """), @@ -326,28 +350,25 @@ void m() { } @Test - void withDiamondOperator() { + @Disabled("this should be possible, but it needs very hard type inference") + void withTernary() { //language=java rewriteRun( version( java(""" package com.example.app; - import java.util.ArrayList; - class A { void m() { - List strs = new ArrayList<>(); + String o = true ? "isTrue" : "Test"; } } """, """ package com.example.app; - import java.util.ArrayList; - class A { void m() { - var strs = new ArrayList(); + var o = true ? "isTrue" : "Test"; } } """), @@ -356,30 +377,25 @@ void m() { ); } - // seem strange I know, but we do not "loose" anything and catching this would be really hard. @Test - void withDiamondOperatorOnRaw() { + void inStaticInitializer() { //language=java rewriteRun( version( java(""" package com.example.app; - import java.util.ArrayList; - class A { - void m() { - List strs = new ArrayList(); + static { + Object o = new Object(); } } """, """ package com.example.app; - import java.util.ArrayList; - class A { - void m() { - var strs = new ArrayList(); + static { + var o = new Object(); } } """), @@ -389,66 +405,27 @@ void m() { } @Test - void forNoDiamondOperators() { - //language=java - rewriteRun( - version( - java( - """ - package com.example.app; - - import java.util.ArrayList; - import java.util.List; - - class A { - void m() { - List strs = new ArrayList(); - } - } - """, """ - package com.example.app; - - import java.util.ArrayList; - import java.util.List; - - class A { - void m() { - var strs = new ArrayList(); - } - } - """), - 10 - ) - ); - } - - @Test - void withFactoryMethods() { + void inInstanceInitializer() { //language=java rewriteRun( version( - java( - """ - package com.example.app; - - import java.util.List; - - class A { - void m() { - List strs = List.of("one","two"); - } + java(""" + package com.example.app; + + class A { + { + Object o = new Object(); } - """, """ - package com.example.app; - - import java.util.List; - - class A { - void m() { - var strs = List.of("one","two"); - } + } + """, """ + package com.example.app; + + class A { + { + var o = new Object(); } - """), + } + """), 10 ) ); @@ -458,45 +435,39 @@ void m() { @Nested class NotApplicable { @Test - void forEmptyFactoryMethod() { + void asParameter() { //language=java rewriteRun( version( - java( - """ - package com.example.app; - - import java.util.List; - - class A { - void m() { - List strs = List.of(); - } - } - """), + java(""" + package com.example.app; + + class A { + Object m(Object o) { + return o; + } + } + """), 10 ) ); } @Test - void forEmptyDiamondOperators() { + void asField() { //language=java rewriteRun( version( - java( - """ - package com.example.app; - - import java.util.ArrayList; - import java.util.List; - - class A { - void m() { - List strs = new ArrayList<>(); - } - } - """), + java(""" + package com.example.app; + + class A { + Object o; + Object m() { + return o; + } + } + """), 10 ) );