Is there an easy way to get class/field/method using specific annotations?

Answered

I have an annotation: @DeepLock(name = '')

I want to find all class/field/methods using it, and get the name value inside the annotation.

 

0
6 comments
@MyAnnotation
public class FatherController {
@MyAnnotation
public void speakWhat() {
System.out.println("Speak what");
}
}

Like the code above, I need to get class FatherController and method speakWhat()

0

Hi,

Check:

  • com.intellij.psi.search.searches.AnnotatedElementsSearch
  • com.intellij.psi.search.searches.AnnotatedMembersSearch
  • and related classes
0

You 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 needs
if (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:

Thanks for it

Karol Lewandowski

By the way, if my annotation is not in the project for plugin(only in the project using plugin),

how can I refer to this annotation?

0

Thank you very much!❤️❤️❤️
I've complety solved the problem!

0

Karol Lewandowski

I met another problem that the searching result lost some results:

I have two class(One is FatherController).

FatherController.java

@DeepLock(userName = "Jacky")
public class FatherController {
@DynamicLock
@DeepLock(userName = "Mick")
public void speakWhat() {
System.out.println("Speak what");
}

public void eat() {
System.out.println("Eat");
}
}

Another is TestController:

TestController.java

import java.util.List;

@DeepLock(userName = "Test user")
public class TestController extends FatherController{

@DeepLock(userName = "Test user")
void showRed() {
System.out.println("This function's background should be red");
}
@DeepLock(userName = "nihao")
void showGreen() {
System.out.println("This function's background should be green");
}

@Override
public void speakWhat() {
super.speakWhat();
System.out.println("After that, just do it");
}
public static void Main() {
System.out.println("hello");
}
}

However, I use the code below:

GlobalSearchScope scope = GlobalSearchScope.allScope(project);
// get DeepLock annotation
PsiClass deepLockClass =
JavaPsiFacade.getInstance(project).findClass("DeepLock", GlobalSearchScope.allScope(project));
if (deepLockClass == null || !deepLockClass.isAnnotationType()) {
return deepLockRegionList;
}
// get annotated Member
Query<PsiMember> psiMemberQuery = AnnotatedElementsSearch.searchPsiMembers(deepLockClass, scope);
List<PsiMember> memberList = new ArrayList<>(psiMemberQuery.findAll());
List<PsiClass> classList = new ArrayList<>(AnnotatedElementsSearch.searchPsiClasses(deepLockClass, scope).findAll());
System.out.println("Total deepLock number: " + memberList.size());
System.out.println("Then classes:" + classList.size());

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?

0

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)?

0

Please sign in to leave a comment.