PsiClass (@NotNull PsiElement element) { if (!PsiUtil.isLanguageLevel5OrHigher(element)) return null; if (!(element instanceof PsiIdentifier) || !(element.getParent() instanceof PsiJavaCodeReferenceElement refExpr)) { return null; } if (PsiTreeUtil.getParentOfType(element, PsiErrorElement.class, PsiImportStatementBase.class) != null) return null; if (refExpr instanceof PsiMethodReferenceExpression) return null; final PsiElement gParent = refExpr.getParent(); if (gParent instanceof PsiMethodReferenceExpression) return null; if (!(gParent instanceof PsiJavaCodeReferenceElement parentRef)) return null; if (isParameterizedReference(parentRef)) return null; if (PsiUtilCore.getElementType(PsiTreeUtil.nextCodeLeaf(gParent)) == JavaTokenType.ARROW && !(gParent.getParent() instanceof PsiCaseLabelElementList)) { return null; } PsiElement resolved = refExpr.resolve(); if (!(resolved instanceof PsiClass psiClass)) { return null; } if (PsiUtil.isFromDefaultPackage(psiClass) || psiClass.hasModifierProperty(PsiModifier.PRIVATE) || psiClass.getQualifiedName() == null) return null; final PsiElement ggParent = gParent.getParent(); if (ggParent instanceof PsiMethodCallExpression) { final PsiMethodCallExpression call = (PsiMethodCallExpression)ggParent.copy(); final PsiElement qualifier = call.getMethodExpression().getQualifier(); if (qualifier == null) return null; qualifier.delete(); final PsiMethod method = call.resolveMethod(); if (method != null && method.getContainingClass() != psiClass) return null; } else { PsiElement refNameElement = parentRef.getReferenceNameElement(); if (refNameElement == null) return null; final PsiJavaCodeReferenceElement copy = JavaPsiFacade.getElementFactory(refNameElement.getProject()) .createReferenceFromText(refNameElement.getText(), refExpr); final PsiElement target = copy.resolve(); if (target != null) { PsiClass parentClass = PsiTreeUtil.getParentOfType(target, PsiClass.class); if (parentClass != psiClass) { if (parentClass == null || parentClass.isPhysical()) { return null; } // In preview mode we could resolve to real class instead of non-physical one String qualifiedName = parentClass.getQualifiedName(); if (qualifiedName == null || !qualifiedName.equals(psiClass.getQualifiedName())) { return null; } } } if (parentRef.resolve() instanceof PsiMember member && !member.hasModifierProperty(PsiModifier.STATIC)) return null; } if (refExpr.getContainingFile() instanceof PsiJavaFile javaFile && javaFile.getImportList() != null) { return psiClass; } return null; }