@SuppressWarnings("unchecked")
  public static @Nullable <T extends PsiReference> T findReferenceOfClass(PsiReference ref, Class<T> clazz) {
    if (clazz.isInstance(ref)) return (T)ref;
    if (ref instanceof PsiMultiReference) {
      for (PsiReference reference : ((PsiMultiReference)ref).getReferences()) {
        if (clazz.isInstance(reference)) {
          return (T)reference;
        }
      }
    } 
    if (ref instanceof PsiDynaReference) {
      for (PsiReference reference : ((PsiDynaReference<?>)ref).getReferences()) {
        if (clazz.isInstance(reference)) {
          return (T)reference;
        }
      }
    }
    return null;
  }
@SuppressWarnings("unchecked")
  public static @Nullable <T extends PsiReference> T findReferenceOfClass(PsiReference ref, Class<T> clazz)