Skip to content

Commit

Permalink
Another batch of improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Nov 12, 2024
1 parent 56221b8 commit da2a719
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reference> references;

Expand All @@ -48,15 +48,15 @@ public Collection<Reference> 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<Reference> references = new HashSet<>();
ServiceLoader<Reference.Provider> 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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ default TreeVisitor<Tree, ExecutionContext> rename(Renamer renamer, String repl

interface Provider {

Set<Reference> getTypeReferences(SourceFile sourceFile);
Set<Reference> getReferences(SourceFile sourceFile);

boolean isAcceptable(SourceFile sourceFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public TreeVisitor<?, ExecutionContext> 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);
}
Expand All @@ -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<Reference> matches = new HashSet<>(typeReferences.findMatches(matcherMutator, Reference.Kind.PACKAGE));
Set<Reference> matches = new HashSet<>(references.findMatches(matcherMutator, Reference.Kind.PACKAGE));
return new ReferenceChangePackageVisitor(matches, matcherMutator, newPackageName).visit(tree, ctx, requireNonNull(getCursor().getParent()));
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reference> matches = new HashSet<>(typeReferences.findMatches(matcherMutator, Reference.Kind.TYPE));
Set<Reference> matches = new HashSet<>(references.findMatches(matcherMutator, Reference.Kind.TYPE));
return new ReferenceChangeTypeVisitor(matches, matcherMutator, newFullyQualifiedTypeName).visit(tree, ctx, requireNonNull(getCursor().getParent()));
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tree> matches = typeReferences.findMatches(matcher, Reference.Kind.TYPE).stream().map(Trait::getTree).collect(Collectors.toSet());
return new TypeReferenceVisitor(matches).visit(tree, ctx);
Set<Tree> matches = references.findMatches(matcher, Reference.Kind.TYPE).stream().map(Trait::getTree).collect(Collectors.toSet());
return new ReferenceVisitor(matches).visit(tree, ctx);
}
return tree;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ private static boolean typeMatches(boolean checkAssignability, Pattern pattern,

@Value
@EqualsAndHashCode(callSuper = false)
private static class TypeReferenceVisitor extends TreeVisitor<Tree, ExecutionContext> {
private static class ReferenceVisitor extends TreeVisitor<Tree, ExecutionContext> {
Set<Tree> matches;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +28,7 @@
import java.util.regex.Pattern;

@Value
class SpringTypeReference implements Reference {
class SpringTypeOrPackageReference implements Reference {
Cursor cursor;
Kind kind;

Expand Down Expand Up @@ -62,29 +61,29 @@ public boolean supportsRename() {
return true;
}

static class Matcher extends SimpleTraitMatcher<SpringTypeReference> {
static class Matcher extends SimpleTraitMatcher<SpringTypeOrPackageReference> {
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) {
Xml.Tag tag = (Xml.Tag) value;
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));
}
}
}
Expand All @@ -100,7 +99,7 @@ Kind determineKind(String value) {
public static class Provider implements Reference.Provider {

@Override
public Set<Reference> getTypeReferences(SourceFile sourceFile) {
public Set<Reference> getReferences(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
new Matcher().asVisitor(reference -> {
references.add(reference);
Expand Down
18 changes: 9 additions & 9 deletions rewrite-xml/src/main/java/org/openrewrite/xml/tree/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,20 @@ public <S, T extends S> T service(Class<S> service) {

@Nullable
@NonFinal
transient SoftReference<TypeReferences> typeReferences;
transient SoftReference<References> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void parseXmlDocument() {
}

@Test
void javaTypeReferenceDocument() {
void javaReferenceDocument() {
rewriteRun(
xml(
"""
Expand All @@ -145,9 +145,9 @@ void javaTypeReferenceDocument() {
</beans>
""",
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)));
})
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))));
}

Expand Down

0 comments on commit da2a719

Please sign in to comment.