PsiClass (@NotNull PsiReferenceExpression ref, @NotNull PsiClass fieldOrMethodClass) { PsiElement[] children = ref.getChildren(); if (children.length > 1 && children[0] instanceof PsiExpression expr) { PsiType type = expr.getType(); if (type != null) { if (type instanceof PsiClassType classType) { return classType.resolve(); } } else { if (expr instanceof PsiReferenceExpression referenceExpression) { PsiElement refElement = referenceExpression.resolve(); if (refElement instanceof PsiClass psiClass) return psiClass; } } return null; } PsiManager manager = ref.getManager(); for(PsiElement parent = ref; parent != null; parent = parent.getParent()){ if (parent instanceof PsiClass psiClass && (manager.areElementsEquivalent(parent, fieldOrMethodClass) || psiClass.isInheritor(fieldOrMethodClass, true))){ return psiClass; } } return null; }