-
Notifications
You must be signed in to change notification settings - Fork 359
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
Conversation
…annotation matcher and JavaTemplate
rewrite-java/src/main/java/org/openrewrite/java/ReplaceAnnotation.java
Outdated
Show resolved
Hide resolved
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
rewrite-java/src/main/java/org/openrewrite/java/ReplaceAnnotation.java
Outdated
Show resolved
Hide resolved
rewrite-java/src/main/java/org/openrewrite/java/ReplaceAnnotation.java
Outdated
Show resolved
Hide resolved
…ypeReferences and in consequence remove not needed parameters
Thanks @MBoegers ! |
I've cleaned up the formatting of the tests, enabled them, and ensured that the relevant parser classpath can configured. |
I open an issue for the unexpected behaviour of |
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