Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ChangeType and ChangePackage to work with SourceFileWithReference #4648

Merged
merged 24 commits into from
Nov 13, 2024

Conversation

Laurens-W
Copy link
Contributor

What's changed?

Added implementation for SourceFileWithTypeReference in ChangeType and ChangePackage recipes

Anything in particular you'd like reviewers to focus on?

Test coverage (edge cases)

Anyone you would like to review specifically?

@knutwannheden @timtebeek

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@Laurens-W Laurens-W self-assigned this Nov 5, 2024
@Laurens-W Laurens-W added enhancement New feature or request recipe Requested Recipe labels Nov 5, 2024
Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good already!

I added some more review comments. Most of them probably apply to both recipes.

Copy link
Contributor

@knutwannheden knutwannheden Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should consider renaming this to something more generic like SymbolReference or just Reference and then have a "kind" property, which would probably be an enum like Kind with constants for Type and Namespace (or Package) for now.

That "name" property will probably also have to be complemented by a structured "symbol" property at some point (so that the caller can query a method reference for name, declaring type, and parameters). But this can wait, I think.

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but I also feel like this is a creep of the scope, could we log it somewhere as future improvement?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. It would just be good if we could get the API finalized before people try to start using it.

boolean recursive = Boolean.TRUE.equals(ChangePackage.this.recursive);
String recursivePackageNamePrefix = oldPackageName + ".";
for (TypeReference ref : cu.getTypeReferences().getTypeReferences()) {
if (ref.getName().equals(oldPackageName) || recursive && ref.getName().startsWith(recursivePackageNamePrefix)) {
Copy link
Contributor

@knutwannheden knutwannheden Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally I think we should only be using the matches() and renameTo() methods instead of the getName() method, but coming up with a good API for those two methods feels pretty difficult, so maybe this will have to wait. I was thinking something along the lines of Java's Matcher for regex:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example I keep thinking about here is when we have META-INF/services files as we have them for the TypeReference.Provider interface. In this case the name of a nested type looks like foo.Bar$Baz. Now ChangeType can't possibly know that it needs to call renameTo("foo.Bar$Baz") and not renameTo("foo.Bar.Baz"). The latter is probably correct for some other config files. Currently, the ChangeType visitor would have to make sure to always pass in names like "foo.Bar$Baz" and then the TypeReference implementation would have to know if it should apply a heuristic (although Bar$Baz could even be the name of the top-level class, but that would be highly unlikely). The question is just what should the API look like to cater for this?

Comment on lines +41 to +43
public Collection<Reference> findMatches(Reference.Matcher matcher, Reference.Kind kind) {
return findMatchesInternal(matcher, kind);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might as well simplify the API by removing this method. As the caller supplies the matcher that matcher can filter on the kind.

Suggested change
public Collection<Reference> findMatches(Reference.Matcher matcher, Reference.Kind kind) {
return findMatchesInternal(matcher, kind);
}

Instead, we might want to provide something like a containsMatches() returning a boolean. But this isn't high priority.

@Laurens-W Laurens-W changed the title Update ChangeType and ChangePackage to work with SourceFileWithTypeReference Update ChangeType and ChangePackage to work with SourceFileWithReference Nov 13, 2024
@Laurens-W Laurens-W marked this pull request as ready for review November 13, 2024 15:12
Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have missed some, but I added a comment regarding the preVisit() methods. I suggest you take a look at TreeVisitor#visit() so you understand how this mechanism works. When all you want to do is extract some information from the top-level node (or also some other node) using the preVisit() method, you should call stopAfterPreVisit() before returning.

@knutwannheden
Copy link
Contributor

If we can go through all visitors and make sure preVisit() is implemented as appropriate, I think we should merge the PR.

@Laurens-W Laurens-W merged commit b9a6fbb into main Nov 13, 2024
2 checks passed
@Laurens-W Laurens-W deleted the implements-xml-typrefs-in-changetype branch November 13, 2024 17:15
MBoegers pushed a commit to MBoegers/rewrite that referenced this pull request Dec 18, 2024
…eference` (openrewrite#4648)

* Apply SourceFileWithTypeReference to ChangePackage and ChangeType

* Revert indent changes

* Renaming and applying `supportsRename`

* Apply bot feedback

* Apply bot feedback

* Apply review feedback

* First batch of improvements

* Another batch of improvements

* Another batch of improvements

* Another batch of improvements

* Another batch of improvements

* Another batch of improvements

* Another batch of improvements

* Another batch of improvements

* Do not use streams to filter matches

* Another batch of improvements

* Fix tests

* Another batch of improvements

* Revert primitive

* Update `preVisit`'s

* Update `preVisit`'s

* Update `preVisit`

* Update `preVisit`

---------

Co-authored-by: Tim te Beek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request recipe Requested Recipe
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants