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

Add recipe to replace Annotation #4217

Merged
merged 2 commits into from
May 31, 2024
Merged

Add recipe to replace Annotation #4217

merged 2 commits into from
May 31, 2024

Conversation

MBoegers
Copy link
Contributor

@MBoegers MBoegers commented May 28, 2024

What's changed?

Add a Recipe which replaces an Annotation with a JavaTemplate.

What's your motivation?

While writing a cross Framework Migration from TestNG to JUnit Jupiter I implemented the replacement of annotations repeatedly.
This could become handy for other cross Framework Migrations as well.

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

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

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

@timtebeek timtebeek added the recipe Requested Recipe label May 28, 2024
String annotationPatternToReplace;
@Option(displayName = "Annotation template to insert",
description = "An annotation template to add instead of original one, will be parsed with `JavaTemplate`.",
example = "@NonNull")
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 it would make sense with an example that includes a parameter, to make it clear that this is supported. Additionally, since a parameter could require a reference to a Java class, I think it would make sense for the JavaTemplate here to be required to be fully context-free and thus not rely on any imports being registered with the JavaTemplate. As a result I imagine the annotationFQN parameter can be dropped (didn't look at the code). Then we can apply the ShortenFullyQualifiedNames visitor to get imports added.

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 tried executing ShortenFullyQualifiedNames with doAfterVisit(new ShortenFullyQualifiedTypeReferences().getVisitor()); earlier, but this didn't make the expected changes. I'll try again and post a gist if not working.

Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC the class declares a static utility method. We should find some uses of it in rewrite-static-analysis.

Copy link
Contributor Author

@MBoegers MBoegers May 31, 2024

Choose a reason for hiding this comment

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

With ShortenFullyQualifiedTypeReferences#modifyOnly the recipe looks better now, thanks. But now the tests are failing because ShortenFullyQualifiedTypeReferences handles annotations unexpected.

It seems to only shorten Annotations of the pattern ´packagename.@annotation´ (see ShortenFullyQualifiedTypeReferencesTest#typeFullyQualifiedAnnotatedField and ShortenFullyQualifiedTypeReferencesTest#annotatedFieldAccess).
In the ShortenFullyQualifiedTypeReferences fails if the annotation has the pattern @packagename.TypeName
According to the Java Language Spec 9.7.1 Annotations in FQN format have the pattern @packagename.TypeName.
I think there might be a bug in ShortenFullyQualifiedTypeReferences or am I missing something?

See https://gist.github.com/MBoegers/9c805b9caa35144466d9f380ddbb40bd for a reproducing test.

Copy link
Contributor

Choose a reason for hiding this comment

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

Quite possible that we have a bug there. I will try to investigate next week.

…ypeReferences and in consequence remove not needed parameters
@sambsnyd sambsnyd merged commit 5083d9c into openrewrite:main May 31, 2024
1 check passed
@sambsnyd
Copy link
Member

Thanks @MBoegers !

@sambsnyd
Copy link
Member

sambsnyd commented Jun 1, 2024

I've cleaned up the formatting of the tests, enabled them, and ensured that the relevant parser classpath can configured.
4831b1f

@MBoegers
Copy link
Contributor Author

MBoegers commented Jun 1, 2024

I open an issue for the unexpected behaviour of ShortenFullyQualifiedTypeNames 4222

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Requested Recipe
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants