Skip to content

Commit

Permalink
Improve UsesField
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden authored and U808771 committed Mar 14, 2024
1 parent f1a466c commit 742c351
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<P> extends JavaIsoVisitor<P> {
private final String owner;
Expand All @@ -36,10 +34,12 @@ public class UsesField<P> extends JavaIsoVisitor<P> {
@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);
}
}
Expand Down

0 comments on commit 742c351

Please sign in to comment.