Skip to content

Commit c0be3e5

Browse files
committed
Remove meaningless matchOverrides on static-method MethodMatchers
`matchOverrides=true` only changes how a `MethodMatcher` treats overrides of a method on subtypes. Static methods cannot be overridden, so the flag is a no-op on matchers that target them — it is misleading, and it can defeat precondition optimizations that key on the declaring type. Drop it from the static-method matchers in: - MigrateCollections{Singleton,Unmodifiable}{Set,List,Map} (`Collections.*`, `Arrays.asList`) - ChangeDefaultKeyStore (`KeyStore.getDefaultType`) - FindLocaleDateTimeFormats (`DateFormat`/`DateTimeFormatter` factory methods) Add a static-import regression test confirming the singleton recipe still fires when the declaring type is never referenced directly.
1 parent a897423 commit c0be3e5

8 files changed

Lines changed: 43 additions & 13 deletions

src/main/java/org/openrewrite/java/migrate/ChangeDefaultKeyStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import static org.openrewrite.Tree.randomId;
3333

3434
public class ChangeDefaultKeyStore extends Recipe {
35-
private static final MethodMatcher KEYSTORE_METHOD_REF = new MethodMatcher("java.security.KeyStore getDefaultType()", true);
35+
private static final MethodMatcher KEYSTORE_METHOD_REF = new MethodMatcher("java.security.KeyStore getDefaultType()");
3636

3737
@Getter
3838
final String displayName = "Return String `jks` when `KeyStore.getDefaultType()` is called";

src/main/java/org/openrewrite/java/migrate/search/FindLocaleDateTimeFormats.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ public class FindLocaleDateTimeFormats extends Recipe {
4949

5050
// DateFormat factory methods that return locale-sensitive formatters
5151
private static final MethodMatcher DATE_FORMAT_GET_TIME_INSTANCE =
52-
new MethodMatcher("java.text.DateFormat getTimeInstance(..)", true);
52+
new MethodMatcher("java.text.DateFormat getTimeInstance(..)");
5353
private static final MethodMatcher DATE_FORMAT_GET_DATE_TIME_INSTANCE =
54-
new MethodMatcher("java.text.DateFormat getDateTimeInstance(..)", true);
54+
new MethodMatcher("java.text.DateFormat getDateTimeInstance(..)");
5555
private static final MethodMatcher DATE_FORMAT_GET_INSTANCE =
56-
new MethodMatcher("java.text.DateFormat getInstance(..)", true);
56+
new MethodMatcher("java.text.DateFormat getInstance(..)");
5757

5858
// DateTimeFormatter factory methods that return locale-sensitive formatters
5959
private static final MethodMatcher DTF_OF_LOCALIZED_TIME =
60-
new MethodMatcher("java.time.format.DateTimeFormatter ofLocalizedTime(..)", true);
60+
new MethodMatcher("java.time.format.DateTimeFormatter ofLocalizedTime(..)");
6161
private static final MethodMatcher DTF_OF_LOCALIZED_DATE_TIME =
62-
new MethodMatcher("java.time.format.DateTimeFormatter ofLocalizedDateTime(..)", true);
62+
new MethodMatcher("java.time.format.DateTimeFormatter ofLocalizedDateTime(..)");
6363

6464
private static final List<MethodMatcher> ALL_MATCHERS = Arrays.asList(
6565
DATE_FORMAT_GET_TIME_INSTANCE,

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import static java.util.Collections.emptyList;
3333

3434
public class MigrateCollectionsSingletonList extends Recipe {
35-
private static final MethodMatcher SINGLETON_LIST = new MethodMatcher("java.util.Collections singletonList(..)", true);
35+
private static final MethodMatcher SINGLETON_LIST = new MethodMatcher("java.util.Collections singletonList(..)");
3636

3737
@Getter
3838
final String displayName = "Prefer `List.of(..)`";

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.util.StringJoiner;
3535

3636
public class MigrateCollectionsSingletonMap extends Recipe {
37-
private static final MethodMatcher SINGLETON_MAP = new MethodMatcher("java.util.Collections singletonMap(..)", true);
37+
private static final MethodMatcher SINGLETON_MAP = new MethodMatcher("java.util.Collections singletonMap(..)");
3838

3939
@Getter
4040
final String displayName = "Prefer `Map.of(..)`";

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;
3131

3232
public class MigrateCollectionsSingletonSet extends Recipe {
33-
private static final MethodMatcher SINGLETON_SET = new MethodMatcher("java.util.Collections singleton(..)", true);
33+
private static final MethodMatcher SINGLETON_SET = new MethodMatcher("java.util.Collections singleton(..)");
3434

3535
@Getter
3636
final String displayName = "Prefer `Set.of(..)`";

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsUnmodifiableList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;
3131

3232
public class MigrateCollectionsUnmodifiableList extends Recipe {
33-
private static final MethodMatcher UNMODIFIABLE_LIST = new MethodMatcher("java.util.Collections unmodifiableList(java.util.List)", true);
34-
private static final MethodMatcher ARRAYS_AS_LIST = new MethodMatcher("java.util.Arrays asList(..)", true);
33+
private static final MethodMatcher UNMODIFIABLE_LIST = new MethodMatcher("java.util.Collections unmodifiableList(java.util.List)");
34+
private static final MethodMatcher ARRAYS_AS_LIST = new MethodMatcher("java.util.Arrays asList(..)");
3535

3636
@Getter
3737
final String displayName = "Prefer `List.of(..)`";

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsUnmodifiableSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import java.util.StringJoiner;
3535

3636
public class MigrateCollectionsUnmodifiableSet extends Recipe {
37-
private static final MethodMatcher UNMODIFIABLE_SET = new MethodMatcher("java.util.Collections unmodifiableSet(java.util.Set)", true);
38-
private static final MethodMatcher ARRAYS_AS_LIST = new MethodMatcher("java.util.Arrays asList(..)", true);
37+
private static final MethodMatcher UNMODIFIABLE_SET = new MethodMatcher("java.util.Collections unmodifiableSet(java.util.Set)");
38+
private static final MethodMatcher ARRAYS_AS_LIST = new MethodMatcher("java.util.Arrays asList(..)");
3939

4040
@Getter
4141
final String displayName = "Prefer `Set.of(..)`";

src/test/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonSetTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,36 @@ class Test {
8888
);
8989
}
9090

91+
@Test
92+
void staticImport() {
93+
// The recipe must still fire when `singleton` is statically imported and the declaring
94+
// type `java.util.Collections` is never referenced directly.
95+
//language=java
96+
rewriteRun(
97+
version(
98+
java(
99+
"""
100+
import java.util.Set;
101+
102+
import static java.util.Collections.singleton;
103+
104+
class Test {
105+
Set<String> set = singleton("Hello");
106+
}
107+
""",
108+
"""
109+
import java.util.Set;
110+
111+
class Test {
112+
Set<String> set = Set.of("Hello");
113+
}
114+
"""
115+
),
116+
9
117+
)
118+
);
119+
}
120+
91121
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/571")
92122
@Test
93123
void shouldNotConvertLiteralNull() {

0 commit comments

Comments
 (0)