Is there an easy way to get class/field/method using specific annotations?
已回答
I have an annotation: @DeepLock(name = '')
I want to find all class/field/methods using it, and get the name value inside the annotation.
请先登录再写评论。
Like the code above, I need to get class FatherController and method speakWhat()
Hi,
Check:
com.intellij.psi.search.searches.AnnotatedElementsSearchcom.intellij.psi.search.searches.AnnotatedMembersSearchYou should find the annotation PsiClass and pass it to the search method. Example:
PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass("com.example.MyAnnotation", scope) // scope: e.g. GlobalSearchScope.projectScope() or another, depending on your needsif (psiClass != null) {
AnnotatedElementsSearch.searchPsiMembers(psiClass, scopeForFindingAnnotatedMembers).forEach(...)
}
You don't need this annotation to be a part of your project. It should be accessible in the project using the plugin.
Edit: Please do not delete posts after sending them 😉
Deleted message:
Thank you very much!❤️❤️❤️
I've complety solved the problem!
Karol Lewandowski
I met another problem that the searching result lost some results:
I have two class(One is FatherController).
FatherController.java
Another is TestController:
TestController.java
However, I use the code below:
The output is missing some results, where the annotation for TestController is totally missed.
Total deepLock number: 2
Then classes:1
Plus: I copy a same class (rename TestController to SonController). While TestController cannot be found, SonController can. How?
Hi,
It's unclear what the reason is.
Is the class that is not found in the project scope? Maybe indexes are broken/not up-to-date (try to clear them with File | Invalidate Caches... and see if the class is found after IDE restart)?