PsiPackage (@NotNull PsiJavaFile file, @NotNull String packageName) { PsiElement[] refs = file.getOnDemandImports(false, true); for (PsiElement ref : refs) { if (ref instanceof PsiPackage psiPackage && psiPackage.getQualifiedName().equals(packageName)) { return psiPackage; } } return null; }