-
Notifications
You must be signed in to change notification settings - Fork 101
Add scope to injected classes #248
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 scope to injected classes #248
Conversation
@jdelobel Following on from our earlier discussion, this PR should interest you. |
src/main/java/org/openrewrite/java/migrate/AddScopeToInjectedClass.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/AddScopeToInjectedClass.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/migrate/AddScopeToInjectedClassTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/migrate/AddScopeToInjectedClassTest.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/AddScopeToInjectedClass.java
Outdated
Show resolved
Hide resolved
Use `JavaIsoVisitor` instead of `TreeVisitor` Use `ScanningRecipe<Set<JavaType.FullyQualified>>` instead of `ScanningRecipe<Accumulator>` Pull `variableTypeRequiresScope` into scanner visitor
src/main/java/org/openrewrite/java/migrate/AnnotateTypesVisitor.java
Outdated
Show resolved
Hide resolved
Hi @timtebeek, thanks for those changes, they look good to me. |
Great! Then I'm only wondering if we should move these classes into the |
I didn't do that initially because I wasn't sure if the changes would only be useful for migrations from javax or if it could be used more generally but I think that's probably the best place for it, yes. |
Hi @timtebeek I've refactored the recipe to the javax space |
return false; | ||
} | ||
|
||
for (JavaType.FullyQualified annotation : memberVariable.getAnnotations()) { |
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.
Detail: You can instead declare an AnnotationMatcher
for javax.inject.Inject
. That would be slightly more OpenRewrite idiomatic.
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.
The idea behind storing the annotation to look for in a Collection was that the recipe might be parameterized in future, allowing to search for more than one annotation at once. However this isn't happening at the moment so it's probably best to code for the now
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.
Even then you could have a collection of AnnotationMatcher
s which you match in a loop.
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'm trying to implement this now but I'm struggling a bit because JavaType.Variable getAnnotations() method returns a collection of FullyQualified and when I apply the matcher to that it doesn't compile because it expects J.Annotation. Is there a way to construct a J.Annotation from a FullyQualified? Or will it mean entirely rewriting to use cd.getAnnotations()?
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.
AnnotationMatcher
has a method called matchesAnnotationOrMetaAnnotation(JavaType.FullyQualified)
. That is probably what you are looking for, no?
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.
done
src/main/java/org/openrewrite/java/migrate/javax/AddScopeToInjectedClass.java
Show resolved
Hide resolved
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit compilationUnit, ExecutionContext executionContext) { | ||
J.CompilationUnit cu = super.visitCompilationUnit(compilationUnit, executionContext); | ||
for (J.ClassDeclaration aClass : cu.getClasses()) { | ||
if (injectedTypes.contains(aClass.getType())) { |
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.
Please note that JavaType.Class
does not implement hashCode()
so you cannot really add these to a HashSet
. This could be problematic when the recipe scheduler executes a recipe against a large repository, because then it breaks up the sources into "chunks", which each have their own set of JavaType
objects (i.e. not referential equality).
For the same reason it isn't really a good idea to add JavaType
objects to the recipe's accumulator in the first place. You should instead get the type's fully qualified name using JavaType.FullyQualified#getFullyQualifiedName()
and add these strings to the set.
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.
done
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.
Thanks for your contribution. Let me know if you are done and I shall merge it :)
Yes, that's it, thanks
…On Mon, 10 Jul 2023 at 13:23, Joan Viladrosa ***@***.***> wrote:
***@***.**** approved this pull request.
Thanks for your contribution. Let me know if you are done and I shall
merge it :)
—
Reply to this email directly, view it on GitHub
<#248 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIZ6KS3FZPCVSIU3VQW5QE3XPPX4XANCNFSM6AAAAAAZYMHOQI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Thank you very much for your contribution! Merged it! |
What's changed?
A recipe to add scope to an injected class
What's your motivation?
when beans.xml is ignored (eg in Quarkus) or not present (eg from CDI 1.1 onwards), bean injection requires a scope-defining annotation to enable discovery of a bean
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
./gradlew licenseFormat