Obtaining .java files in source root

Hi 

Is there an API to obtain all all the .java files in the source directory of a module(preferably as a list of PsiClass) . Further, is there anyway for me to filter that class list based on a class level annotation present in those classes? (e.g: I want to filter the classes with annotation @Deprecated)

Thanks.

4 comments

Obtain Query from com.intellij.psi.search.searches.AllClassesSearch#search(com.intellij.psi.search.SearchScope, com.intellij.openapi.project.Project) and use com.intellij.util.Query#forEach to filter out @Deprecated classes.

 

SearchScope = com.intellij.psi.search.GlobalSearchScopesCore#directoryScope(com.intellij.psi.PsiDirectory, boolean)

0

Hi Yann,

Thank you for the response. What I did to obtain the class list was

final Collection<VirtualFile> moduleJavaFiles = FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, JavaFileType.INSTANCE,
GlobalSearchScope.moduleScope(module));
moduleJavaFiles.stream()
.forEach(vFile -> {
final PsiFile file = PsiManager.getInstance(project).findFile(vFile);

if (file instanceof PsiJavaFile) {
((PsiJavaFile) file).getPackageName();
}
});

Can you explain me how to use the Query class to filter out the annotations? I couldn't find an example in IDEA source code.

0

In com.intellij.util.Query#forEach you check com.intellij.psi.PsiDocCommentOwner#isDeprecated on given PsiClass and add it to results list/process it accordingly.

0

Hi Yann, 

Got it. Thank you 

0

Please sign in to leave a comment.