From da2a719f933c7393f5de31311cfb12fa4728de55 Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Tue, 12 Nov 2024 18:05:04 +0100 Subject: [PATCH] Another batch of improvements --- .../openrewrite/SourceFileWithReferences.java | 10 +++---- .../openrewrite/TypeReferenceProvider.java | 27 ------------------- .../trait/reference/Reference.java | 2 +- .../org/openrewrite/java/ChangePackage.java | 6 ++--- .../java/org/openrewrite/java/ChangeType.java | 4 +-- .../openrewrite/java/search/FindTypes.java | 8 +++--- .../org/openrewrite/java/search/UsesType.java | 4 +-- ...java => SpringTypeOrPackageReference.java} | 13 +++++---- .../java/org/openrewrite/xml/tree/Xml.java | 18 ++++++------- .../org/openrewrite/xml/XmlParserTest.java | 8 +++--- ... => SpringTypeOrPackageReferenceTest.java} | 4 +-- 11 files changed, 38 insertions(+), 66 deletions(-) delete mode 100644 rewrite-core/src/main/java/org/openrewrite/TypeReferenceProvider.java rename rewrite-xml/src/main/java/org/openrewrite/xml/trait/{SpringTypeReference.java => SpringTypeOrPackageReference.java} (89%) rename rewrite-xml/src/test/java/org/openrewrite/xml/trait/{SpringTypeReferenceTest.java => SpringTypeOrPackageReferenceTest.java} (95%) diff --git a/rewrite-core/src/main/java/org/openrewrite/SourceFileWithReferences.java b/rewrite-core/src/main/java/org/openrewrite/SourceFileWithReferences.java index 87bd0e36ca7..984563d548e 100644 --- a/rewrite-core/src/main/java/org/openrewrite/SourceFileWithReferences.java +++ b/rewrite-core/src/main/java/org/openrewrite/SourceFileWithReferences.java @@ -26,11 +26,11 @@ @Incubating(since = "8.39.0") public interface SourceFileWithReferences extends SourceFile { - TypeReferences getTypeReferences(); + References getReferences(); @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @Getter - class TypeReferences { + class References { private final SourceFile sourceFile; private final Set references; @@ -48,15 +48,15 @@ public Collection findMatches(Reference.Matcher matcher, Reference.Ki return findMatches(matcher).stream().filter(ref -> ref.getKind().equals(kind)).collect(Collectors.toList()); } - public static TypeReferences build(SourceFile sourceFile) { + public static References build(SourceFile sourceFile) { Set references = new HashSet<>(); ServiceLoader loader = ServiceLoader.load(Reference.Provider.class); loader.forEach(provider -> { if (provider.isAcceptable(sourceFile)) { - references.addAll(provider.getTypeReferences(sourceFile)); + references.addAll(provider.getReferences(sourceFile)); } }); - return new TypeReferences(sourceFile, references); + return new References(sourceFile, references); } } } diff --git a/rewrite-core/src/main/java/org/openrewrite/TypeReferenceProvider.java b/rewrite-core/src/main/java/org/openrewrite/TypeReferenceProvider.java deleted file mode 100644 index db2967ecff4..00000000000 --- a/rewrite-core/src/main/java/org/openrewrite/TypeReferenceProvider.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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; - -import org.openrewrite.trait.reference.Reference; - -import java.util.Set; - -public interface TypeReferenceProvider { - - Set getTypeReferences(SourceFile sourceFile); - - boolean isAcceptable(SourceFile sourceFile); -} diff --git a/rewrite-core/src/main/java/org/openrewrite/trait/reference/Reference.java b/rewrite-core/src/main/java/org/openrewrite/trait/reference/Reference.java index a3e7e472522..6aca3c728d0 100644 --- a/rewrite-core/src/main/java/org/openrewrite/trait/reference/Reference.java +++ b/rewrite-core/src/main/java/org/openrewrite/trait/reference/Reference.java @@ -46,7 +46,7 @@ default TreeVisitor rename(Renamer renamer, String repl interface Provider { - Set getTypeReferences(SourceFile sourceFile); + Set getReferences(SourceFile sourceFile); boolean isAcceptable(SourceFile sourceFile); } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangePackage.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangePackage.java index a1577b8f88f..dbc303b5c51 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangePackage.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangePackage.java @@ -120,7 +120,7 @@ public TreeVisitor getVisitor() { SourceFileWithReferences cu = (SourceFileWithReferences) tree; boolean recursive = Boolean.TRUE.equals(ChangePackage.this.recursive); String recursivePackageNamePrefix = oldPackageName + "."; - for (Reference ref : cu.getTypeReferences().getReferences()) { + for (Reference ref : cu.getReferences().getReferences()) { if (ref.getKind().equals(Reference.Kind.PACKAGE) && ref.getValue().equals(oldPackageName) || recursive && ref.getValue().startsWith(recursivePackageNamePrefix)) { return SearchResult.found(cu); } @@ -143,10 +143,10 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { return new JavaChangePackageVisitor().visit(tree, ctx, requireNonNull(getCursor().getParent())); } else if (tree instanceof SourceFileWithReferences) { SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree; - SourceFileWithReferences.TypeReferences typeReferences = sourceFile.getTypeReferences(); + SourceFileWithReferences.References references = sourceFile.getReferences(); boolean recursive = Boolean.TRUE.equals(ChangePackage.this.recursive); Reference.MatcherMutator matcherMutator = new PackageMatcher(oldPackageName, recursive); - Set matches = new HashSet<>(typeReferences.findMatches(matcherMutator, Reference.Kind.PACKAGE)); + Set matches = new HashSet<>(references.findMatches(matcherMutator, Reference.Kind.PACKAGE)); return new ReferenceChangePackageVisitor(matches, matcherMutator, newPackageName).visit(tree, ctx, requireNonNull(getCursor().getParent())); } return tree; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java index 3b58bf201fe..8582e223231 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java @@ -110,9 +110,9 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { return new JavaChangeTypeVisitor(oldFullyQualifiedTypeName, newFullyQualifiedTypeName, ignoreDefinition).visit(tree, ctx, requireNonNull(getCursor().getParent())); } else if (tree instanceof SourceFileWithReferences) { SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree; - SourceFileWithReferences.TypeReferences typeReferences = sourceFile.getTypeReferences(); + SourceFileWithReferences.References references = sourceFile.getReferences(); Reference.MatcherMutator matcherMutator = new TypeMatcher(oldFullyQualifiedTypeName); - Set matches = new HashSet<>(typeReferences.findMatches(matcherMutator, Reference.Kind.TYPE)); + Set matches = new HashSet<>(references.findMatches(matcherMutator, Reference.Kind.TYPE)); return new ReferenceChangeTypeVisitor(matches, matcherMutator, newFullyQualifiedTypeName).visit(tree, ctx, requireNonNull(getCursor().getParent())); } return tree; diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java b/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java index b6b4c859fc4..e28a4fa649c 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/FindTypes.java @@ -78,10 +78,10 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { return new JavaSourceFileVisitor(fullyQualifiedType).visit(tree, ctx); } else if (tree instanceof SourceFileWithReferences) { SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree; - SourceFileWithReferences.TypeReferences typeReferences = sourceFile.getTypeReferences(); + SourceFileWithReferences.References references = sourceFile.getReferences(); TypeMatcher matcher = new TypeMatcher(fullyQualifiedTypeName); - Set matches = typeReferences.findMatches(matcher, Reference.Kind.TYPE).stream().map(Trait::getTree).collect(Collectors.toSet()); - return new TypeReferenceVisitor(matches).visit(tree, ctx); + Set matches = references.findMatches(matcher, Reference.Kind.TYPE).stream().map(Trait::getTree).collect(Collectors.toSet()); + return new ReferenceVisitor(matches).visit(tree, ctx); } return tree; } @@ -150,7 +150,7 @@ private static boolean typeMatches(boolean checkAssignability, Pattern pattern, @Value @EqualsAndHashCode(callSuper = false) - private static class TypeReferenceVisitor extends TreeVisitor { + private static class ReferenceVisitor extends TreeVisitor { Set matches; @Override diff --git a/rewrite-java/src/main/java/org/openrewrite/java/search/UsesType.java b/rewrite-java/src/main/java/org/openrewrite/java/search/UsesType.java index 5c7ce478217..4afd2b287e7 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/search/UsesType.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/search/UsesType.java @@ -121,9 +121,9 @@ public boolean isAcceptable(SourceFile sourceFile, P p) { } } else if (tree instanceof SourceFileWithReferences) { SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree; - SourceFileWithReferences.TypeReferences typeReferences = sourceFile.getTypeReferences(); + SourceFileWithReferences.References references = sourceFile.getReferences(); TypeMatcher matcher = typeMatcher != null ? typeMatcher : new TypeMatcher(fullyQualifiedType); - for (Reference ignored : typeReferences.findMatches(matcher, Reference.Kind.TYPE)) { + for (Reference ignored : references.findMatches(matcher, Reference.Kind.TYPE)) { return SearchResult.found(sourceFile); } } diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringTypeReference.java b/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringTypeOrPackageReference.java similarity index 89% rename from rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringTypeReference.java rename to rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringTypeOrPackageReference.java index b2af3f7f9ee..51d2ad21379 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringTypeReference.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/trait/SpringTypeOrPackageReference.java @@ -18,7 +18,6 @@ import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.*; -import org.openrewrite.internal.StringUtils; import org.openrewrite.trait.SimpleTraitMatcher; import org.openrewrite.trait.reference.Reference; import org.openrewrite.xml.XPathMatcher; @@ -29,7 +28,7 @@ import java.util.regex.Pattern; @Value -class SpringTypeReference implements Reference { +class SpringTypeOrPackageReference implements Reference { Cursor cursor; Kind kind; @@ -62,21 +61,21 @@ public boolean supportsRename() { return true; } - static class Matcher extends SimpleTraitMatcher { + static class Matcher extends SimpleTraitMatcher { private final Pattern typeReference = Pattern.compile("\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(?:\\.\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)*"); private final XPathMatcher classXPath = new XPathMatcher("//@class"); private final XPathMatcher typeXPath = new XPathMatcher("//@type"); private final XPathMatcher tags = new XPathMatcher("//value"); @Override - protected @Nullable SpringTypeReference test(Cursor cursor) { + protected @Nullable SpringTypeOrPackageReference test(Cursor cursor) { Object value = cursor.getValue(); if (value instanceof Xml.Attribute) { Xml.Attribute attrib = (Xml.Attribute) value; if (classXPath.matches(cursor) || typeXPath.matches(cursor)) { String stringVal = attrib.getValueAsString(); if (typeReference.matcher(stringVal).matches()) { - return new SpringTypeReference(cursor, determineKind(stringVal)); + return new SpringTypeOrPackageReference(cursor, determineKind(stringVal)); } } } else if (value instanceof Xml.Tag) { @@ -84,7 +83,7 @@ static class Matcher extends SimpleTraitMatcher { if (tags.matches(cursor)) { String stringVal = tag.getValue().get(); if (tag.getValue().isPresent() && typeReference.matcher(stringVal).matches()) { - return new SpringTypeReference(cursor, determineKind(stringVal)); + return new SpringTypeOrPackageReference(cursor, determineKind(stringVal)); } } } @@ -100,7 +99,7 @@ Kind determineKind(String value) { public static class Provider implements Reference.Provider { @Override - public Set getTypeReferences(SourceFile sourceFile) { + public Set getReferences(SourceFile sourceFile) { Set references = new HashSet<>(); new Matcher().asVisitor(reference -> { references.add(reference); diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/tree/Xml.java b/rewrite-xml/src/main/java/org/openrewrite/xml/tree/Xml.java index 5dda6d005ce..1fabf24388c 100755 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/tree/Xml.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/tree/Xml.java @@ -169,20 +169,20 @@ public T service(Class service) { @Nullable @NonFinal - transient SoftReference typeReferences; + transient SoftReference references; @Transient @Override - public TypeReferences getTypeReferences() { - TypeReferences cache; - if (this.typeReferences == null) { - cache = TypeReferences.build(this); - this.typeReferences = new SoftReference<>(cache); + public References getReferences() { + References cache; + if (this.references == null) { + cache = References.build(this); + this.references = new SoftReference<>(cache); } else { - cache = this.typeReferences.get(); + cache = this.references.get(); if (cache == null || cache.getSourceFile() != this) { - cache = TypeReferences.build(this); - this.typeReferences = new SoftReference<>(cache); + cache = References.build(this); + this.references = new SoftReference<>(cache); } } return cache; diff --git a/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java b/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java index 346b8aa00b3..56bb6d0eda8 100755 --- a/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java +++ b/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java @@ -121,7 +121,7 @@ void parseXmlDocument() { } @Test - void javaTypeReferenceDocument() { + void javaReferenceDocument() { rewriteRun( xml( """ @@ -145,9 +145,9 @@ void javaTypeReferenceDocument() { """, spec -> spec.afterRecipe(doc -> { - assertThat(doc.getTypeReferences().getReferences().stream().anyMatch(typeRef -> typeRef.getValue().equals("java.lang.String"))); - assertThat(doc.getTypeReferences().getReferences().stream().anyMatch(typeRef -> typeRef.getKind().equals(Reference.Kind.TYPE))); - assertThat(doc.getTypeReferences().getReferences().stream().anyMatch(typeRef -> typeRef.getKind().equals(Reference.Kind.PACKAGE))); + assertThat(doc.getReferences().getReferences().stream().anyMatch(typeRef -> typeRef.getValue().equals("java.lang.String"))); + assertThat(doc.getReferences().getReferences().stream().anyMatch(typeRef -> typeRef.getKind().equals(Reference.Kind.TYPE))); + assertThat(doc.getReferences().getReferences().stream().anyMatch(typeRef -> typeRef.getKind().equals(Reference.Kind.PACKAGE))); }) ) ); diff --git a/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringTypeReferenceTest.java b/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringTypeOrPackageReferenceTest.java similarity index 95% rename from rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringTypeReferenceTest.java rename to rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringTypeOrPackageReferenceTest.java index 60d72c26008..599c3aa1744 100644 --- a/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringTypeReferenceTest.java +++ b/rewrite-xml/src/test/java/org/openrewrite/xml/trait/SpringTypeOrPackageReferenceTest.java @@ -23,11 +23,11 @@ import static org.openrewrite.xml.Assertions.xml; -class SpringTypeReferenceTest implements RewriteTest { +class SpringTypeOrPackageReferenceTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe(RewriteTest.toRecipe(() -> new SpringTypeReference.Matcher() + spec.recipe(RewriteTest.toRecipe(() -> new SpringTypeOrPackageReference.Matcher() .asVisitor(springJavaTypeReference -> SearchResult.found(springJavaTypeReference.getTree(), springJavaTypeReference.getValue())))); }