diff --git a/src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java b/src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java index 886b5da046..698122a948 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java @@ -74,7 +74,9 @@ private static boolean hasDimensionsAfterName(J.VariableDeclarations vd) { * @return true if single variable definition with initialization and without var */ private static boolean isSingleVariableDefinition(J.VariableDeclarations vd) { - TypeTree typeExpression = vd.getTypeExpression(); + if (!replaceableTypeExpression(vd.getTypeExpression())) { + return false; + } boolean definesSingleVariable = vd.getVariables().size() == 1; boolean isPureAssigment = JavaType.Primitive.Null == vd.getType(); @@ -90,8 +92,22 @@ private static boolean isSingleVariableDefinition(J.VariableDeclarations vd) { initializer = initializer.unwrap(); boolean isNullAssigment = J.Literal.isLiteralValue(initializer, null); - boolean alreadyUseVar = typeExpression instanceof J.Identifier && "var".equals(((J.Identifier) typeExpression).getSimpleName()); - return !isNullAssigment && !alreadyUseVar; + return !isNullAssigment; + } + + /** + * Groovy parses {@code def x = ...} into an empty {@link J.Identifier} and prints {@code def} + * from a marker, so writing {@code var} there splices it onto the name ({@code def varx = ...}). + */ + private static boolean replaceableTypeExpression(@Nullable TypeTree typeExpression) { + if (typeExpression == null) { + return false; + } + if (typeExpression instanceof J.Identifier) { + String simpleName = ((J.Identifier) typeExpression).getSimpleName(); + return !simpleName.isEmpty() && !"var".equals(simpleName); + } + return true; } /** diff --git a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForConstructorsTest.java b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForConstructorsTest.java index 05e2e87fbb..c148338420 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForConstructorsTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForConstructorsTest.java @@ -20,6 +20,7 @@ import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.java.Assertions.java; import static org.openrewrite.java.Assertions.javaVersion; @@ -267,4 +268,19 @@ ArrayList getList() { ) ); } + + @Test + void doNotSpliceVarIntoGroovyDef() { + rewriteRun( + //language=groovy + groovy( + """ + def stripVmExtension(File dir) { + def newFile = new File(dir, "child") + assert newFile != null + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForPrimitiveTest.java b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForPrimitiveTest.java index a5ad22f060..cead8dd9f7 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForPrimitiveTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForPrimitiveTest.java @@ -21,6 +21,7 @@ import org.openrewrite.Issue; import org.openrewrite.test.RecipeSpec; +import static org.openrewrite.groovy.Assertions.groovy; import static org.openrewrite.java.Assertions.java; import static org.openrewrite.java.Assertions.javaVersion; @@ -33,6 +34,22 @@ public void defaults(RecipeSpec spec) { @Nested class NotApplicable { + @Test + void forGroovyDef() { + //language=groovy + rewriteRun( + groovy( + """ + class A { + void m() { + def count = 1 + } + } + """ + ) + ); + } + @Test void forShort() { //language=java