File tree Expand file tree Collapse file tree
main/java/org/openrewrite/java/migrate/lang/var
test/java/org/openrewrite/java/migrate/lang/var Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -74,7 +74,9 @@ private static boolean hasDimensionsAfterName(J.VariableDeclarations vd) {
7474 * @return true if single variable definition with initialization and without var
7575 */
7676 private static boolean isSingleVariableDefinition (J .VariableDeclarations vd ) {
77- TypeTree typeExpression = vd .getTypeExpression ();
77+ if (!replaceableTypeExpression (vd .getTypeExpression ())) {
78+ return false ;
79+ }
7880
7981 boolean definesSingleVariable = vd .getVariables ().size () == 1 ;
8082 boolean isPureAssigment = JavaType .Primitive .Null == vd .getType ();
@@ -90,8 +92,22 @@ private static boolean isSingleVariableDefinition(J.VariableDeclarations vd) {
9092
9193 initializer = initializer .unwrap ();
9294 boolean isNullAssigment = J .Literal .isLiteralValue (initializer , null );
93- boolean alreadyUseVar = typeExpression instanceof J .Identifier && "var" .equals (((J .Identifier ) typeExpression ).getSimpleName ());
94- return !isNullAssigment && !alreadyUseVar ;
95+ return !isNullAssigment ;
96+ }
97+
98+ /**
99+ * Groovy parses {@code def x = ...} into an empty {@link J.Identifier} and prints {@code def}
100+ * from a marker, so writing {@code var} there splices it onto the name ({@code def varx = ...}).
101+ */
102+ private static boolean replaceableTypeExpression (@ Nullable TypeTree typeExpression ) {
103+ if (typeExpression == null ) {
104+ return false ;
105+ }
106+ if (typeExpression instanceof J .Identifier ) {
107+ String simpleName = ((J .Identifier ) typeExpression ).getSimpleName ();
108+ return !simpleName .isEmpty () && !"var" .equals (simpleName );
109+ }
110+ return true ;
95111 }
96112
97113 /**
Original file line number Diff line number Diff line change 2020import org .openrewrite .test .RecipeSpec ;
2121import org .openrewrite .test .RewriteTest ;
2222
23+ import static org .openrewrite .groovy .Assertions .groovy ;
2324import static org .openrewrite .java .Assertions .java ;
2425import static org .openrewrite .java .Assertions .javaVersion ;
2526
@@ -267,4 +268,19 @@ ArrayList<String> getList() {
267268 )
268269 );
269270 }
271+
272+ @ Test
273+ void doNotSpliceVarIntoGroovyDef () {
274+ rewriteRun (
275+ //language=groovy
276+ groovy (
277+ """
278+ def stripVmExtension(File dir) {
279+ def newFile = new File(dir, "child")
280+ assert newFile != null
281+ }
282+ """
283+ )
284+ );
285+ }
270286}
Original file line number Diff line number Diff line change 2121import org .openrewrite .Issue ;
2222import org .openrewrite .test .RecipeSpec ;
2323
24+ import static org .openrewrite .groovy .Assertions .groovy ;
2425import static org .openrewrite .java .Assertions .java ;
2526import static org .openrewrite .java .Assertions .javaVersion ;
2627
@@ -33,6 +34,22 @@ public void defaults(RecipeSpec spec) {
3334
3435 @ Nested
3536 class NotApplicable {
37+ @ Test
38+ void forGroovyDef () {
39+ //language=groovy
40+ rewriteRun (
41+ groovy (
42+ """
43+ class A {
44+ void m() {
45+ def count = 1
46+ }
47+ }
48+ """
49+ )
50+ );
51+ }
52+
3653 @ Test
3754 void forShort () {
3855 //language=java
You can’t perform that action at this time.
0 commit comments