auto dynamic_ref_cast()

in cxx/fbjni/detail/References-inl.h [517:542]


auto dynamic_ref_cast(const RefType& ref) ->
enable_if_t<IsPlainJniReference<JniType<T>>(), decltype(static_ref_cast<T>(ref))>
{
  if (!ref) {
    return decltype(static_ref_cast<T>(ref))();
  }

  static alias_ref<jclass> target_class = findClassStatic(jtype_traits<T>::kBaseName.c_str());
  if (!target_class) {
    throwNewJavaException("java/lang/ClassCastException",
                          "Could not find class %s.",
                          jtype_traits<T>::kBaseName.c_str());

  }

  local_ref<jclass> source_class = ref->getClass();

  if (!target_class->isAssignableFrom(source_class)) {
    throwNewJavaException("java/lang/ClassCastException",
                          "Tried to cast from %s to %s.",
                          source_class->toString().c_str(),
                          jtype_traits<T>::kBaseName.c_str());
  }

  return static_ref_cast<T>(ref);
}