PsiClass (final PsiExpression expression) { PsiClass enclosingClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class); if (enclosingClass == null) return null; final Pair<PsiMember, PsiClass> pair = getMemberAndClassReferencedByThis(expression); if (pair != null) return pair.getSecond(); if (expression instanceof PsiThisExpression) { final PsiJavaCodeReferenceElement thisQualifier = ((PsiThisExpression)expression).getQualifier(); if (thisQualifier == null) { return enclosingClass; } else { return (PsiClass)thisQualifier.resolve(); } } return null; }