Skip to content

Commit d58fd75

Browse files
MBoegerstimtebeek
andauthored
Fix var migration of variables initialized by static methods (#609)
* add Test to reproduce issue 608 * (Hot) Fix issue 608 by disable migration of variables that are initialized by static methods * Minor polish * Use org.jspecify.annotations.Nullable --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 6dce1fb commit d58fd75

File tree

3 files changed

+106
-32
lines changed

3 files changed

+106
-32
lines changed

src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.migrate.lang.var;
1717

18+
import org.jspecify.annotations.Nullable;
1819
import org.openrewrite.Cursor;
1920
import org.openrewrite.java.tree.*;
2021

@@ -198,4 +199,30 @@ private static boolean isInsideInitializer(Cursor cursor, int nestedBlockLevel)
198199

199200
return isInsideInitializer(requireNonNull(cursor.getParent()), nestedBlockLevel);
200201
}
202+
203+
/**
204+
* Checks whether the initializer {@linkplain Expression} is a {@linkplain J.MethodInvocation} targeting a static method.
205+
*
206+
* @param initializer {@linkplain J.VariableDeclarations.NamedVariable#getInitializer()} value
207+
* @return true iff is initialized by static method
208+
*/
209+
public static boolean initializedByStaticMethod(@Nullable Expression initializer) {
210+
if (initializer == null) {
211+
return false;
212+
}
213+
initializer = initializer.unwrap();
214+
215+
if (!(initializer instanceof J.MethodInvocation)) {
216+
// no MethodInvocation -> false
217+
return false;
218+
}
219+
220+
J.MethodInvocation invocation = (J.MethodInvocation) initializer;
221+
if (invocation.getMethodType() == null) {
222+
// not a static method -> false
223+
return false;
224+
}
225+
226+
return invocation.getMethodType().hasFlags(Flag.Static);
227+
}
201228
}

src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
7575
boolean isPrimitive = DeclarationCheck.isPrimitive(vd);
7676
boolean usesGenerics = DeclarationCheck.useGenerics(vd);
7777
boolean usesTernary = DeclarationCheck.initializedByTernary(vd);
78-
boolean usesArrayInitializer = vd.getVariables().get(0).getInitializer() instanceof J.NewArray;
79-
if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer) {
78+
Expression initializer = vd.getVariables().get(0).getInitializer();
79+
boolean usesArrayInitializer = initializer instanceof J.NewArray;
80+
boolean initializedByStaticMethod = DeclarationCheck.initializedByStaticMethod(initializer);
81+
if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer || initializedByStaticMethod) {
8082
return vd;
8183
}
8284

src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,6 @@ public void defaults(RecipeSpec spec) {
3232
.allSources(s -> s.markers(javaVersion(10)));
3333
}
3434

35-
@Test
36-
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/550")
37-
void genericType() {
38-
rewriteRun(
39-
java(
40-
"""
41-
import java.io.Serializable;
42-
43-
abstract class Outer<T extends Serializable> {
44-
abstract T doIt();
45-
void trigger() {
46-
T x = doIt();
47-
}
48-
}
49-
""",
50-
"""
51-
import java.io.Serializable;
52-
53-
abstract class Outer<T extends Serializable> {
54-
abstract T doIt();
55-
void trigger() {
56-
var x = doIt();
57-
}
58-
}
59-
"""
60-
)
61-
);
62-
}
63-
6435
@Nested
6536
class Applicable {
6637
@DocumentExample
@@ -299,6 +270,7 @@ void m() {
299270
}
300271

301272
@Test
273+
@Disabled("in favor to https://github.com/openrewrite/rewrite-migrate-java/issues/608 we skip all static methods ATM")
302274
void staticMethods() {
303275
//language=java
304276
rewriteRun(
@@ -336,12 +308,85 @@ void m() {
336308
)
337309
);
338310
}
311+
312+
@Test
313+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/550")
314+
void genericType() {
315+
rewriteRun(
316+
//language=java
317+
java(
318+
"""
319+
import java.io.Serializable;
320+
321+
abstract class Outer<T extends Serializable> {
322+
abstract T doIt();
323+
void trigger() {
324+
T x = doIt();
325+
}
326+
}
327+
""",
328+
"""
329+
import java.io.Serializable;
330+
331+
abstract class Outer<T extends Serializable> {
332+
abstract T doIt();
333+
void trigger() {
334+
var x = doIt();
335+
}
336+
}
337+
"""
338+
)
339+
);
340+
}
339341
}
340342
}
341343

342344
@Nested
343345
class NotApplicable {
344346

347+
@Test
348+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/608")
349+
void genericTypeInStaticMethod() {
350+
// ATM the recipe skips all static method initialized variables
351+
rewriteRun(
352+
//language=java
353+
java(
354+
"""
355+
package example;
356+
357+
class Global {
358+
static <T> T cast(Object o) {
359+
return (T) o;
360+
}
361+
}
362+
class User {
363+
public String test() {
364+
Object o = "Hello";
365+
String string = Global.cast(o); // static method unchanged
366+
return string;
367+
}
368+
}
369+
""",
370+
"""
371+
package example;
372+
373+
class Global {
374+
static <T> T cast(Object o) {
375+
return (T) o;
376+
}
377+
}
378+
class User {
379+
public String test() {
380+
var o = "Hello";
381+
String string = Global.cast(o); // static method unchanged
382+
return string;
383+
}
384+
}
385+
"""
386+
)
387+
);
388+
}
389+
345390
@Test
346391
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/551")
347392
void arrayInitializer() {
@@ -350,7 +395,7 @@ void arrayInitializer() {
350395
java(
351396
"""
352397
package com.example.app;
353-
398+
354399
class A {
355400
void m() {
356401
String[] dictionary = {"aa", "b", "aba", "ba"};

0 commit comments

Comments
 (0)