From b430245d621b14cb3d05047beaa176ea2946e396 Mon Sep 17 00:00:00 2001 From: lingenj Date: Mon, 10 Aug 2026 11:23:55 +0200 Subject: [PATCH 1/4] Fix UseVarForObject with generic method return types --- .../migrate/lang/var/DeclarationCheck.java | 50 +++++++++-- .../migrate/lang/var/UseVarForObject.java | 3 +- .../lang/var/UseVarForObjectsTest.java | 88 ++++++++++++++++++- 3 files changed, 133 insertions(+), 8 deletions(-) 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 6450259c39..57faa7acec 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 @@ -24,6 +24,7 @@ import org.openrewrite.marker.Markers; import java.util.List; +import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.UnaryOperator; @@ -216,23 +217,60 @@ private static boolean isInsideInitializer(Cursor cursor, int nestedBlockLevel) * @return true iff is initialized by static method */ public static boolean initializedByStaticMethod(@Nullable Expression initializer) { - if (initializer == null) { + J.MethodInvocation invocation = getMethodInvocation(initializer); + if (invocation == null || invocation.getMethodType() == null) { return false; } + return invocation.getMethodType().hasFlags(Flag.Static); + } + + public static boolean initializedByUnresolvableGenericMethod(@Nullable Expression initializer) { + J.MethodInvocation invocation = getMethodInvocation(initializer); + return invocation != null && isUnresolvableGenericMethod(invocation); + } + + private J.@Nullable MethodInvocation getMethodInvocation(@Nullable Expression initializer) { + if (initializer == null) { + return null; + } initializer = initializer.unwrap(); if (!(initializer instanceof J.MethodInvocation)) { - // no MethodInvocation -> false + return null; + } + + return (J.MethodInvocation) initializer; + } + + private static boolean isUnresolvableGenericMethod(J.MethodInvocation mi) { + JavaType.Method mt = mi.getMethodType(); + if (mt == null || mi.getTypeParameters() != null) { + return false; + } + JavaType.FullyQualified clazz = mt.getDeclaringType(); + Optional maybeDeclaredMethod = TypeUtils.findDeclaredMethod(clazz, mt.getName(), mt.getParameterTypes()); + if (!maybeDeclaredMethod.isPresent()) { return false; } - J.MethodInvocation invocation = (J.MethodInvocation) initializer; - if (invocation.getMethodType() == null) { - // not a static method -> false + JavaType returnType = maybeDeclaredMethod.get().getReturnType(); + JavaType.GenericTypeVariable genericReturnType; + if (returnType instanceof JavaType.GenericTypeVariable) { + genericReturnType = (JavaType.GenericTypeVariable) returnType; + } else if (returnType instanceof JavaType.Array && ((JavaType.Array) returnType).getElemType() instanceof JavaType.GenericTypeVariable) { + genericReturnType = (JavaType.GenericTypeVariable) ((JavaType.Array) returnType).getElemType(); + } else { return false; } - return invocation.getMethodType().hasFlags(Flag.Static); + for (JavaType classParam : clazz.getTypeParameters()) { + if (classParam instanceof JavaType.GenericTypeVariable && + ((JavaType.GenericTypeVariable) classParam).getName().equals(genericReturnType.getName())) { + return false; + } + } + + return true; } /** diff --git a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java index 087dfe3501..286bb1421c 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java @@ -62,7 +62,8 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v Expression initializer = vd.getVariables().get(0).getInitializer(); boolean usesArrayInitializer = initializer instanceof J.NewArray; boolean initializedByStaticMethod = DeclarationCheck.initializedByStaticMethod(initializer); - if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer || initializedByStaticMethod) { + boolean initializedByUnresolvableGenericMethod = DeclarationCheck.initializedByUnresolvableGenericMethod(initializer); + if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer || initializedByStaticMethod || initializedByUnresolvableGenericMethod) { return vd; } diff --git a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java index 49d0f1d65b..ca58674505 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java @@ -232,6 +232,39 @@ void m() { ); } + @Test + void typeToken() { + //language=java + rewriteRun( + version( + java(""" + package com.example.app; + + class A { + T typeToken(Class clazz) { + return null; + } + void m() { + String s = typeToken(String.class); + } + } + """, """ + package com.example.app; + + class A { + T typeToken(Class clazz) { + return null; + } + void m() { + var s = typeToken(String.class); + } + } + """), + 10 + ) + ); + } + @Test void subType() { //language=java @@ -317,7 +350,7 @@ void m() { @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/550") @Test - void genericType() { + void classLevelGenericType() { rewriteRun( //language=java java( @@ -344,12 +377,65 @@ void trigger() { ) ); } + + @Test + void genericMethodWithTypeParameters() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + class A { + T method() { + return null; + } + void m() { + String strs = new A().method(); + } + } + """,""" + package com.example.app; + + class A { + T method() { + return null; + } + void m() { + var strs = new A().method(); + } + } + """ + ) + ); + } } } @Nested class NotApplicable { + @Test + void genericMethod() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + class A { + S[] method() { + return null; + } + void m() { + String[] strs = method(); + } + } + """ + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/608") @Test void genericTypeInStaticMethod() { From 1f16395ba126b67eec0fefd3717b7b0ced524bf5 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 10 Aug 2026 12:39:30 +0200 Subject: [PATCH 2/4] Detect method-level type variables through the method declaration Determine whether the return type variable is declared by the method itself via `getDeclaredFormalTypeNames()`, rather than by comparing its name against the declaring class' type parameters. This also covers a method type variable shadowing a class type variable of the same name. Unwrap array return types repeatedly, so that multi dimensional arrays such as ` T[][] method()` are detected as well. Only resolve the method declaration when the cheaper checks have not already rejected the declaration, as the lookup walks the supertype and interface hierarchy. --- .../migrate/lang/var/DeclarationCheck.java | 40 +++--- .../migrate/lang/var/UseVarForObject.java | 4 +- .../lang/var/UseVarForObjectsTest.java | 129 +++++++++++++++++- 3 files changed, 152 insertions(+), 21 deletions(-) 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 57faa7acec..1cc5bae308 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 @@ -224,12 +224,21 @@ public static boolean initializedByStaticMethod(@Nullable Expression initializer return invocation.getMethodType().hasFlags(Flag.Static); } + /** + * Checks whether the initializer {@linkplain Expression} is a {@linkplain J.MethodInvocation} of a generic method + * whose type parameter can only be inferred from the assignment target, as in {@code T getArgument(int index)}. + * Replacing the declared type with {@code var} drops that target type, so the type parameter is inferred as + * {@linkplain Object} instead of the declared type. + * + * @param initializer {@linkplain J.VariableDeclarations.NamedVariable#getInitializer()} value + * @return true iff is initialized by a generic method whose return type cannot be resolved without the declared type + */ public static boolean initializedByUnresolvableGenericMethod(@Nullable Expression initializer) { J.MethodInvocation invocation = getMethodInvocation(initializer); return invocation != null && isUnresolvableGenericMethod(invocation); } - private J.@Nullable MethodInvocation getMethodInvocation(@Nullable Expression initializer) { + private static J.@Nullable MethodInvocation getMethodInvocation(@Nullable Expression initializer) { if (initializer == null) { return null; } @@ -247,30 +256,25 @@ private static boolean isUnresolvableGenericMethod(J.MethodInvocation mi) { if (mt == null || mi.getTypeParameters() != null) { return false; } - JavaType.FullyQualified clazz = mt.getDeclaringType(); - Optional maybeDeclaredMethod = TypeUtils.findDeclaredMethod(clazz, mt.getName(), mt.getParameterTypes()); + // The invocation's return type is already resolved to the declared type, so the declaration is consulted instead. + // Methods mentioning their own type parameters in their signature fail this lookup, as the invocation's parameter + // types are resolved while the declaration's are not; those are also the methods javac infers from arguments alone. + Optional maybeDeclaredMethod = TypeUtils.findDeclaredMethod(mt.getDeclaringType(), mt.getName(), mt.getParameterTypes()); if (!maybeDeclaredMethod.isPresent()) { return false; } - JavaType returnType = maybeDeclaredMethod.get().getReturnType(); - JavaType.GenericTypeVariable genericReturnType; - if (returnType instanceof JavaType.GenericTypeVariable) { - genericReturnType = (JavaType.GenericTypeVariable) returnType; - } else if (returnType instanceof JavaType.Array && ((JavaType.Array) returnType).getElemType() instanceof JavaType.GenericTypeVariable) { - genericReturnType = (JavaType.GenericTypeVariable) ((JavaType.Array) returnType).getElemType(); - } else { - return false; + JavaType.Method declaredMethod = maybeDeclaredMethod.get(); + JavaType returnType = declaredMethod.getReturnType(); + while (returnType instanceof JavaType.Array) { + returnType = ((JavaType.Array) returnType).getElemType(); } - - for (JavaType classParam : clazz.getTypeParameters()) { - if (classParam instanceof JavaType.GenericTypeVariable && - ((JavaType.GenericTypeVariable) classParam).getName().equals(genericReturnType.getName())) { - return false; - } + if (!(returnType instanceof JavaType.GenericTypeVariable)) { + return false; } - return true; + // Type parameters declared by the class are resolved through the receiver, not the assignment target + return declaredMethod.getDeclaredFormalTypeNames().contains(((JavaType.GenericTypeVariable) returnType).getName()); } /** diff --git a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java index 286bb1421c..f1d8355906 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java @@ -62,8 +62,8 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v Expression initializer = vd.getVariables().get(0).getInitializer(); boolean usesArrayInitializer = initializer instanceof J.NewArray; boolean initializedByStaticMethod = DeclarationCheck.initializedByStaticMethod(initializer); - boolean initializedByUnresolvableGenericMethod = DeclarationCheck.initializedByUnresolvableGenericMethod(initializer); - if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer || initializedByStaticMethod || initializedByUnresolvableGenericMethod) { + if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer || initializedByStaticMethod || + DeclarationCheck.initializedByUnresolvableGenericMethod(initializer)) { return vd; } diff --git a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java index ca58674505..585a86470a 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java @@ -232,8 +232,10 @@ void m() { ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") @Test void typeToken() { + // `T` is inferred from the `Class` argument rather than from the declared type, so `var` is safe //language=java rewriteRun( version( @@ -378,6 +380,7 @@ void trigger() { ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") @Test void genericMethodWithTypeParameters() { //language=java @@ -394,7 +397,8 @@ void m() { String strs = new A().method(); } } - """,""" + """, + """ package com.example.app; class A { @@ -409,14 +413,93 @@ void m() { ) ); } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodInferredFromArgument() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + class A { + T identity(T t) { + return t; + } + void m() { + String s = identity("x"); + } + } + """, + """ + package com.example.app; + + class A { + T identity(T t) { + return t; + } + void m() { + var s = identity("x"); + } + } + """ + ) + ); + } } } @Nested class NotApplicable { + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") @Test void genericMethod() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + class A { + T method() { + return null; + } + void m() { + String s = method(); + } + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodDeclaredOnInterface() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + interface Invocation { + T getArgument(int index); + } + class A { + void m(Invocation invocation) { + String s = invocation.getArgument(0); + } + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodReturningArray() { //language=java rewriteRun( java( @@ -436,6 +519,50 @@ void m() { ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodReturningMultiDimensionalArray() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + class A { + T[][] method() { + return null; + } + void m() { + String[][] strs = method(); + } + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodShadowingClassTypeParameter() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + class A { + T method() { + return null; + } + void m() { + String s = method(); + } + } + """ + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/608") @Test void genericTypeInStaticMethod() { From 9878daf0e7a261038b4c99231c86affc14ba276c Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 10 Aug 2026 13:07:52 +0200 Subject: [PATCH 3/4] Resolve the declared method without relying on signature matching `TypeUtils.findDeclaredMethod` cannot match a signature whose declared parameters mention a type variable, as the invocation's parameter types are resolved while the declaration's are not. Generic methods declared on a generic type, such as `interface Cache { T get(K key); }`, were therefore never found and slipped past the check. Look up candidate declarations by name and arity across the declaring type, its supertypes and its interfaces instead, and treat a method as unresolvable when one of its own type parameters occurs in the declared return type but in none of the declared parameter types. Occurrence checking recurses through arrays, type arguments and generic bounds. --- .../migrate/lang/var/DeclarationCheck.java | 80 ++++++++++++---- .../lang/var/UseVarForObjectsTest.java | 92 ++++++++++++++++++- 2 files changed, 154 insertions(+), 18 deletions(-) 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 1cc5bae308..24f616deba 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 @@ -23,12 +23,14 @@ import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; +import java.util.IdentityHashMap; import java.util.List; -import java.util.Optional; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.UnaryOperator; import static java.util.Collections.emptyList; +import static java.util.Collections.newSetFromMap; import static java.util.Collections.singleton; import static java.util.Objects.requireNonNull; import static org.openrewrite.Tree.randomId; @@ -226,12 +228,10 @@ public static boolean initializedByStaticMethod(@Nullable Expression initializer /** * Checks whether the initializer {@linkplain Expression} is a {@linkplain J.MethodInvocation} of a generic method - * whose type parameter can only be inferred from the assignment target, as in {@code T getArgument(int index)}. - * Replacing the declared type with {@code var} drops that target type, so the type parameter is inferred as - * {@linkplain Object} instead of the declared type. + * whose type parameter is only inferable from the declared type, as in {@code T getArgument(int index)}. * * @param initializer {@linkplain J.VariableDeclarations.NamedVariable#getInitializer()} value - * @return true iff is initialized by a generic method whose return type cannot be resolved without the declared type + * @return true iff is initialized by a generic method that needs the declared type to infer its return type */ public static boolean initializedByUnresolvableGenericMethod(@Nullable Expression initializer) { J.MethodInvocation invocation = getMethodInvocation(initializer); @@ -256,25 +256,71 @@ private static boolean isUnresolvableGenericMethod(J.MethodInvocation mi) { if (mt == null || mi.getTypeParameters() != null) { return false; } - // The invocation's return type is already resolved to the declared type, so the declaration is consulted instead. - // Methods mentioning their own type parameters in their signature fail this lookup, as the invocation's parameter - // types are resolved while the declaration's are not; those are also the methods javac infers from arguments alone. - Optional maybeDeclaredMethod = TypeUtils.findDeclaredMethod(mt.getDeclaringType(), mt.getName(), mt.getParameterTypes()); - if (!maybeDeclaredMethod.isPresent()) { + // The invocation's types are already resolved against the declared type, so the declaration is consulted instead + return declaresUninferableReturnType(mt.getDeclaringType(), mt.getName(), mt.getParameterTypes().size(), newIdentitySet()); + } + + private static boolean declaresUninferableReturnType(JavaType.@Nullable FullyQualified clazz, String name, int arity, Set seen) { + if (clazz == null || !seen.add(clazz)) { return false; } + for (JavaType.Method method : clazz.getMethods()) { + if (name.equals(method.getName()) && method.getParameterTypes().size() == arity && returnsUninferableTypeParameter(method)) { + return true; + } + } + if (declaresUninferableReturnType(clazz.getSupertype(), name, arity, seen)) { + return true; + } + for (JavaType.FullyQualified anInterface : clazz.getInterfaces()) { + if (declaresUninferableReturnType(anInterface, name, arity, seen)) { + return true; + } + } + return false; + } - JavaType.Method declaredMethod = maybeDeclaredMethod.get(); - JavaType returnType = declaredMethod.getReturnType(); - while (returnType instanceof JavaType.Array) { - returnType = ((JavaType.Array) returnType).getElemType(); + private static boolean returnsUninferableTypeParameter(JavaType.Method method) { + for (String typeParameterName : method.getDeclaredFormalTypeNames()) { + if (mentions(method.getReturnType(), typeParameterName, newIdentitySet()) && + !mentionsAny(method.getParameterTypes(), typeParameterName, newIdentitySet())) { + return true; + } } - if (!(returnType instanceof JavaType.GenericTypeVariable)) { + return false; + } + + private static boolean mentions(JavaType type, String typeParameterName, Set seen) { + if (!seen.add(type)) { return false; } + if (type instanceof JavaType.GenericTypeVariable) { + JavaType.GenericTypeVariable generic = (JavaType.GenericTypeVariable) type; + if (typeParameterName.equals(generic.getName())) { + return true; + } + return mentionsAny(generic.getBounds(), typeParameterName, seen); + } + if (type instanceof JavaType.Array) { + return mentions(((JavaType.Array) type).getElemType(), typeParameterName, seen); + } + if (type instanceof JavaType.Parameterized) { + return mentionsAny(((JavaType.Parameterized) type).getTypeParameters(), typeParameterName, seen); + } + return false; + } + + private static boolean mentionsAny(List types, String typeParameterName, Set seen) { + for (JavaType type : types) { + if (mentions(type, typeParameterName, seen)) { + return true; + } + } + return false; + } - // Type parameters declared by the class are resolved through the receiver, not the assignment target - return declaredMethod.getDeclaredFormalTypeNames().contains(((JavaType.GenericTypeVariable) returnType).getName()); + private static Set newIdentitySet() { + return newSetFromMap(new IdentityHashMap<>()); } /** diff --git a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java index 585a86470a..92d0b42b44 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/var/UseVarForObjectsTest.java @@ -235,7 +235,7 @@ void m() { @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") @Test void typeToken() { - // `T` is inferred from the `Class` argument rather than from the declared type, so `var` is safe + // `T` is inferred from the `Class` argument, not from the declared type //language=java rewriteRun( version( @@ -447,6 +447,44 @@ void m() { ) ); } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodInferredFromWildcardBoundedArgument() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + import java.util.function.Supplier; + + class A { + T orElse(Supplier supplier) { + return supplier.get(); + } + void m(Supplier supplier) { + String s = orElse(supplier); + } + } + """, + """ + package com.example.app; + + import java.util.function.Supplier; + + class A { + T orElse(Supplier supplier) { + return supplier.get(); + } + void m(Supplier supplier) { + var s = orElse(supplier); + } + } + """ + ) + ); + } } } @@ -497,6 +535,58 @@ void m(Invocation invocation) { ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodDeclaredOnGenericInterface() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + import java.util.List; + + interface Cache { + T get(K key); + T getAll(List keys); + } + class A { + void m(Cache cache, List keys) { + String s = cache.get("k"); + String t = cache.getAll(keys); + } + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") + @Test + void genericMethodInheritedFromSuperclass() { + //language=java + rewriteRun( + java( + """ + package com.example.app; + + class Base { + T get(int index) { + return null; + } + } + class Sub extends Base { + } + class A { + void m(Sub sub) { + String s = sub.get(0); + } + } + """ + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/pull/1187") @Test void genericMethodReturningArray() { From fadb2cd69aee4e858a8e0e248fd13a586af786fa Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 10 Aug 2026 13:53:38 +0200 Subject: [PATCH 4/4] Short circuit the applicability checks Inline the conditions into their `if` statements, such that the cheaper checks can reject a declaration before the more expensive ones run. --- .../lang/var/UseVarForGenericMethodInvocations.java | 11 ++++------- .../java/migrate/lang/var/UseVarForObject.java | 11 +++++------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForGenericMethodInvocations.java b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForGenericMethodInvocations.java index ef96dfc84c..5a38870a1d 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForGenericMethodInvocations.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForGenericMethodInvocations.java @@ -58,10 +58,9 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v } // Recipe specific - boolean isPrimitive = DeclarationCheck.isPrimitive(vd); - boolean usesNoGenerics = !DeclarationCheck.useGenerics(vd); - boolean usesTernary = DeclarationCheck.initializedByTernary(vd); - if (isPrimitive || usesTernary || usesNoGenerics) { + if (DeclarationCheck.isPrimitive(vd) || + DeclarationCheck.initializedByTernary(vd) || + !DeclarationCheck.useGenerics(vd)) { return vd; } @@ -73,9 +72,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v J.MethodInvocation invocation = (J.MethodInvocation) originalInitializer.unwrap(); // If no type parameters and no arguments are present, we assume the type is too hard to determine - boolean hasNoTypeParams = invocation.getTypeParameters() == null; - boolean argumentsEmpty = allArgumentsEmpty(invocation); - if (hasNoTypeParams && argumentsEmpty) { + if (invocation.getTypeParameters() == null && allArgumentsEmpty(invocation)) { return vd; } diff --git a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java index f1d8355906..4692183686 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/var/UseVarForObject.java @@ -56,13 +56,12 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v return vd; } - boolean isPrimitive = DeclarationCheck.isPrimitive(vd); - boolean usesGenerics = DeclarationCheck.useGenerics(vd); - boolean usesTernary = DeclarationCheck.initializedByTernary(vd); Expression initializer = vd.getVariables().get(0).getInitializer(); - boolean usesArrayInitializer = initializer instanceof J.NewArray; - boolean initializedByStaticMethod = DeclarationCheck.initializedByStaticMethod(initializer); - if (isPrimitive || usesGenerics || usesTernary || usesArrayInitializer || initializedByStaticMethod || + if (DeclarationCheck.isPrimitive(vd) || + DeclarationCheck.useGenerics(vd) || + DeclarationCheck.initializedByTernary(vd) || + initializer instanceof J.NewArray || + DeclarationCheck.initializedByStaticMethod(initializer) || DeclarationCheck.initializedByUnresolvableGenericMethod(initializer)) { return vd; }