public static PsiExpression getEffectiveQualifier(@NotNull PsiReferenceExpression ref, @NotNull PsiMember member) {
    PsiMember containingMember = PsiTreeUtil.getParentOfType(ref, PsiMethod.class, PsiClassInitializer.class, PsiField.class);
    if (!member.hasModifierProperty(PsiModifier.STATIC) && isStaticMember(containingMember)) {
      return null;
    }
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(ref.getProject());
    PsiClass memberClass = member.getContainingClass();
    if (memberClass != null) {
      if (member.hasModifierProperty(PsiModifier.STATIC)) {
        if (memberClass.getName() == null || memberClass instanceof PsiImplicitClass) return null;
        return factory.createReferenceExpression(memberClass);
      }
      PsiClass containingClass = PsiUtil.getContainingClass(ref);
      if (containingClass == null) {
        containingClass = PsiTreeUtil.getContextOfType(ref, PsiClass.class);
      }
      if (!InheritanceUtil.isInheritorOrSelf(containingClass, memberClass, true)) {
        do {
          if (!member.hasModifierProperty(PsiModifier.STATIC) && isStaticMember(containingClass)) return null;
          containingClass = PsiUtil.getContainingClass(containingClass);
        }
        while (containingClass != null && !InheritanceUtil.isInheritorOrSelf(containingClass, memberClass, true));
        if (containingClass != null) {
          String thisQualifier = containingClass.getQualifiedName();
          if (thisQualifier == null) {
            if (PsiUtil.isLocalClass(containingClass)) {
              thisQualifier = containingClass.getName();
            }
            else {
              // Cannot qualify anonymous class
              return null;
            }
          }
          return factory.createExpressionFromText(thisQualifier + "." + PsiKeyword.THIS, ref);
        }
      }
    }
    return factory.createExpressionFromText(PsiKeyword.THIS, ref);
  }
public static PsiExpression getEffectiveQualifier(@NotNull PsiReferenceExpression ref, @NotNull PsiMember member)