How to find annotations in package hierarchy?
Hello there
I'm writing an inspection that checks annotations of the package of a java class. What I basically do is I get the PsiPackage from PsiClass and then use the AnnotationUtil.
To get the PsiPackage from PsiClass I first get the PsiJavaFile with getParent() and get packageStatement and the package reference. Finally I resolve the reference to get the PsiPackage.
psiClass.getParent().getPackageStatement().getPackageReference().resolve()
This works fine for classes that are defined locally in a module. But for classes like java.lang.String this does not work, since it's package statement is of type ClsPackageStatementImpl and this class do not implement the method getPackageReference().
How can I check if it's possible to get the package reference without comparing classes like below?
if (csiClass.getParent().getClass().equals(PsiJavaFileImpl.class)) ...
Or is there a better way to browse packages of a psiClass?
Cheers
Stefan
Please sign in to leave a comment.
PsiJavaFile javaFile = (PsiJavaFile) psiClass.getContaningFile();
PsiPackage pkg = JavaPsiFacade.getInstance(project).findPackage(javaFile.getPackageName());
Dmitry, thank you for the quick answer!