@Nullable
 public static PsiType getQualifierType(PsiMethodReferenceExpression expression) {
   PsiType qualifierType = null;
   final PsiTypeElement typeElement = expression.getQualifierType();
   if (typeElement != null) {
     qualifierType = typeElement.getType();
   } else {
     final PsiElement qualifier = expression.getQualifier();
     if (qualifier instanceof PsiExpression) {
       qualifierType = ((PsiExpression) qualifier).getType();
     }
   }
   if (qualifierType == null) {
     final QualifierResolveResult qualifierResolveResult = getQualifierResolveResult(expression);
     final PsiClass containingClass = qualifierResolveResult.getContainingClass();
     if (containingClass == null) {
       return null;
     }
     qualifierType =
         JavaPsiFacade.getElementFactory(expression.getProject()).createType(containingClass);
   }
   return qualifierType;
 }
 @NotNull
 public static QualifierResolveResult getQualifierResolveResult(
     @NotNull PsiMethodReferenceExpression methodReferenceExpression) {
   PsiClass containingClass = null;
   PsiSubstitutor substitutor = PsiSubstitutor.EMPTY;
   final PsiExpression expression = methodReferenceExpression.getQualifierExpression();
   if (expression != null) {
     final PsiType expressionType = getExpandedType(expression.getType(), expression);
     PsiClassType.ClassResolveResult result = PsiUtil.resolveGenericsClassInType(expressionType);
     containingClass = result.getElement();
     if (containingClass != null) {
       substitutor = result.getSubstitutor();
     }
     if (containingClass == null && expression instanceof PsiReferenceExpression) {
       final JavaResolveResult resolveResult =
           ((PsiReferenceExpression) expression).advancedResolve(false);
       final PsiElement resolve = resolveResult.getElement();
       if (resolve instanceof PsiClass) {
         containingClass = (PsiClass) resolve;
         substitutor = resolveResult.getSubstitutor();
         return new QualifierResolveResult(containingClass, substitutor, true);
       }
     }
   } else {
     final PsiTypeElement typeElement = methodReferenceExpression.getQualifierType();
     if (typeElement != null) {
       PsiType type = getExpandedType(typeElement.getType(), typeElement);
       PsiClassType.ClassResolveResult result = PsiUtil.resolveGenericsClassInType(type);
       containingClass = result.getElement();
       if (containingClass != null) {
         return new QualifierResolveResult(containingClass, result.getSubstitutor(), true);
       }
     }
   }
   return new QualifierResolveResult(containingClass, substitutor, false);
 }