From 742c3512274a8132a2cf9fe303dabb5e82f3bfd2 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 6 Mar 2024 16:55:53 +0100 Subject: [PATCH] Improve `UsesField` --- .../java/search/UsesFieldTest.java | 90 +++++++++++++++++++ .../openrewrite/java/search/UsesField.java | 10 +-- 2 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 rewrite-java-test/src/test/java/org/openrewrite/java/search/UsesFieldTest.java diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/search/UsesFieldTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/search/UsesFieldTest.java new file mode 100644 index 00000000000..e919569704f --- /dev/null +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/search/UsesFieldTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.search; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.test.RewriteTest.toRecipe; + +class UsesFieldTest implements RewriteTest { + + @Test + void staticFieldAccess() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new UsesField<>("java.util.Collections", "EMPTY_LIST"))), + java( + """ + import java.util.Collections; + + class T { + Object o = Collections.EMPTY_LIST; + } + """, + """ + /*~~>*/import java.util.Collections; + + class T { + Object o = Collections.EMPTY_LIST; + } + """ + ) + ); + } + + @Test + void staticImport() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new UsesField<>("java.util.Collections", "EMPTY_LIST"))), + java( + """ + import static java.util.Collections.EMPTY_LIST; + + class T { + Object o = EMPTY_LIST; + } + """, + """ + /*~~>*/import static java.util.Collections.EMPTY_LIST; + + class T { + Object o = EMPTY_LIST; + } + """ + ) + ); + } + + @Test + void noImportStaticField() { + rewriteRun( + spec -> spec.recipe(toRecipe(() -> new UsesField<>("java.util.Collections", "EMPTY_LIST"))), + java( + """ + class T { + Object o = java.util.Collections.EMPTY_LIST; + } + """, + """ + /*~~>*/class T { + Object o = java.util.Collections.EMPTY_LIST; + } + """ + ) + ); + } +} diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/UsesField.java b/rewrite-java/src/main/java/org/openrewrite/java/search/UsesField.java index 9cc53dad760..6b1ce3c08d4 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/UsesField.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/UsesField.java @@ -26,8 +26,6 @@ import org.openrewrite.java.tree.JavaType; import org.openrewrite.marker.SearchResult; -import static java.util.Objects.requireNonNull; - @RequiredArgsConstructor public class UsesField

extends JavaIsoVisitor

{ private final String owner; @@ -36,10 +34,12 @@ public class UsesField

extends JavaIsoVisitor

{ @Override public J visit(@Nullable Tree tree, P p) { if (tree instanceof JavaSourceFile) { - JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree); + JavaSourceFile cu = (JavaSourceFile) tree; + boolean isGlob = field.contains("*") || field.contains("?"); + TypeMatcher typeMatcher = null; for (JavaType.Variable variable : cu.getTypesInUse().getVariables()) { - if (StringUtils.matchesGlob(variable.getName(), field) && - new TypeMatcher(owner, true).matches(variable.getOwner())) { + if ((variable.getName().equals(field) || isGlob && StringUtils.matchesGlob(variable.getName(), field)) && + (typeMatcher = typeMatcher == null ? new TypeMatcher(owner, true) : typeMatcher).matches(variable.getOwner())) { return SearchResult.found(cu); } }