88import com .intellij .openapi .module .Module ;
99import com .intellij .openapi .module .ModuleUtilCore ;
1010import com .intellij .psi .*;
11+ import com .intellij .psi .search .GlobalSearchScope ;
1112import org .jdom .Element ;
1213import org .jetbrains .annotations .NonNls ;
1314import org .jetbrains .annotations .NotNull ;
@@ -82,24 +83,27 @@ private void analyseReference(PsiJavaCodeReferenceElement reference) {
8283 optionalPackageName .ifPresentOrElse (
8384 packageName -> {
8485 if (isMonitored (inspectionOptions , packageName ) && isOtherModule (reference , psiClass )) {
85- if (!hasAnnotationInPackage (psiClass , inspectionOptions .getExportAnnotation ())) {
86- Module moduleForFile = ModuleUtilCore .findModuleForFile (psiClass .getContainingFile ());
87- holder .registerProblem (reference , String .format (
88- "'%s' is module private and not allowed to use outside its module '%s'" ,
86+ PsiPackage foundPackage = JavaPsiFacade .getInstance (holder .getProject ()).findPackage (packageName );
87+ if (foundPackage != null ) {
88+ if (!hasAnnotationInPackage (foundPackage , inspectionOptions .getExportAnnotation ())) {
89+ Module moduleForFile = ModuleUtilCore .findModuleForFile (psiClass .getContainingFile ());
90+ holder .registerProblem (reference , String .format (
91+ "'%s' is module private and not allowed to use outside its module '%s'" ,
92+ psiClass .getName (),
93+ moduleForFile != null ? moduleForFile .getName () : "<unknown>"
94+ )
95+ );
96+ }
97+ } else {
98+ LOGGER .warn (
99+ String .format (
100+ "PsiClass element with missing package => reference ignored; PsiClass type: %s, PsiClass name: %s, Text: %s" ,
101+ psiClass .getClass (),
89102 psiClass .getName (),
90- moduleForFile != null ? moduleForFile . getName () : "<unknown>"
103+ psiClass . getText ()
91104 )
92105 );
93106 }
94- } else {
95- LOGGER .warn (
96- String .format (
97- "PsiClass element with missing package => reference ignored; PsiClass type: %s, PsiClass name: %s, Text: %s" ,
98- psiClass .getClass (),
99- psiClass .getName (),
100- psiClass .getText ()
101- )
102- );
103107 }
104108 },
105109 () -> LOGGER .warn (
@@ -113,25 +117,19 @@ private void analyseReference(PsiJavaCodeReferenceElement reference) {
113117 }
114118 }
115119
116- private boolean hasAnnotationInPackage (PsiClass psiClass , String annotationToCheckFor ) {
120+ private boolean hasAnnotationInPackage (PsiPackage psiPackage , String annotationToCheckFor ) {
117121 var found = new AtomicBoolean (false );
118- PsiFile containingFile = psiClass .getContainingFile ();
119- if (containingFile == null ) {
120- LOGGER .warn (
121- String .format (
122- "PsiClass element with no PsiFile => reference ignored; PsiClass type: %s, PsiClass name: %s, Text: %s" ,
123- psiClass .getClass (),
124- psiClass .getName (),
125- psiClass .getText ()
126- )
127- );
128- return false ;
129- }
130- JavaPsiAnnotationUtil .processPackageAnnotations (containingFile , (annotation , superPackage ) -> {
131- if (annotationToCheckFor .equals (annotation .getQualifiedName ())) {
132- found .set (true );
122+ PsiFile [] files = psiPackage .getFiles (GlobalSearchScope .allScope (psiPackage .getProject ()));
123+ for (PsiFile file : files ) {
124+ JavaPsiAnnotationUtil .processPackageAnnotations (file , (annotation , superPackage ) -> {
125+ if (annotationToCheckFor .equals (annotation .getQualifiedName ())) {
126+ found .set (true );
127+ }
128+ }, false );
129+ if (found .get ()) {
130+ break ;
133131 }
134- }, false );
132+ }
135133 return found .get ();
136134 }
137135
0 commit comments