From 3753398b4bb6e96b6724c0e12d4a1de479e4de1f Mon Sep 17 00:00:00 2001 From: David Reiss Date: Mon, 28 Oct 2019 13:10:31 -0700 Subject: [PATCH] Support {static,dynamic}_ref_cast without ::javaobject (#17) Summary: Treat `static_ref_cast` as equivalent to `static_ref_cast>`, which is equivalent to `static_ref_cast`. As a side-effect, this also allows `JMap`, since the only thing blocking that before was a cast. Pull Request resolved: https://github.com/facebookincubator/fbjni/pull/17 Test Plan: CI Reviewed By: cjhopman Differential Revision: D18121225 Pulled By: dreiss fbshipit-source-id: 81d0d56967dcb8d907d3a3cec4a52e110743853c --- first-party/fbjni/cxx/fbjni/JThread.h | 4 ++-- first-party/fbjni/cxx/fbjni/detail/Iterator-inl.h | 2 +- .../fbjni/cxx/fbjni/detail/References-inl.h | 14 +++++++------- first-party/fbjni/cxx/fbjni/detail/References.h | 8 ++++---- first-party/fbjni/test/jni/doc_tests.cpp | 6 +++--- first-party/fbjni/test/jni/iterator_tests.cpp | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/first-party/fbjni/cxx/fbjni/JThread.h b/first-party/fbjni/cxx/fbjni/JThread.h index 190c892dce9..075fabccdc5 100644 --- a/first-party/fbjni/cxx/fbjni/JThread.h +++ b/first-party/fbjni/cxx/fbjni/JThread.h @@ -38,12 +38,12 @@ class JThread : public JavaClass { static local_ref create(std::function&& runnable) { auto jrunnable = JNativeRunnable::newObjectCxxArgs(std::move(runnable)); - return newInstance(static_ref_cast(jrunnable)); + return newInstance(static_ref_cast(jrunnable)); } static local_ref create(std::function&& runnable, std::string&& name) { auto jrunnable = JNativeRunnable::newObjectCxxArgs(std::move(runnable)); - return newInstance(static_ref_cast(jrunnable), make_jstring(std::move(name))); + return newInstance(static_ref_cast(jrunnable), make_jstring(std::move(name))); } static local_ref getCurrent() { diff --git a/first-party/fbjni/cxx/fbjni/detail/Iterator-inl.h b/first-party/fbjni/cxx/fbjni/detail/Iterator-inl.h index 1f441c5eb84..c1b98562b4c 100644 --- a/first-party/fbjni/cxx/fbjni/detail/Iterator-inl.h +++ b/first-party/fbjni/cxx/fbjni/detail/Iterator-inl.h @@ -42,7 +42,7 @@ struct IteratorHelper : public JavaClass> { value_type next() { static auto elementField = JavaBase_::javaClassStatic()->template getField("mElement"); - return dynamic_ref_cast>(JavaBase_::getFieldValue(elementField)); + return dynamic_ref_cast(JavaBase_::getFieldValue(elementField)); } static void reset(value_type& v) { diff --git a/first-party/fbjni/cxx/fbjni/detail/References-inl.h b/first-party/fbjni/cxx/fbjni/detail/References-inl.h index f07a2211087..0250d1b531f 100644 --- a/first-party/fbjni/cxx/fbjni/detail/References-inl.h +++ b/first-party/fbjni/cxx/fbjni/detail/References-inl.h @@ -491,32 +491,32 @@ inline void swap(alias_ref& a, alias_ref& b) noexcept { // template argument. I'm not sure whether that would make the code // more maintainable (DRY), or less (too clever/confusing.). template -enable_if_t(), local_ref> +enable_if_t>(), local_ref> static_ref_cast(const local_ref& ref) noexcept { - T p = static_cast(ref.get()); + JniType p = static_cast>(ref.get()); return make_local(p); } template -enable_if_t(), global_ref> +enable_if_t>(), global_ref> static_ref_cast(const global_ref& ref) noexcept { - T p = static_cast(ref.get()); + JniType p = static_cast>(ref.get()); return make_global(p); } template -enable_if_t(), alias_ref> +enable_if_t>(), alias_ref> static_ref_cast(const alias_ref& ref) noexcept { - T p = static_cast(ref.get()); + JniType p = static_cast>(ref.get()); return wrap_alias(p); } template auto dynamic_ref_cast(const RefType& ref) -> -enable_if_t(), decltype(static_ref_cast(ref))> +enable_if_t>(), decltype(static_ref_cast(ref))> { if (!ref) { return decltype(static_ref_cast(ref))(); diff --git a/first-party/fbjni/cxx/fbjni/detail/References.h b/first-party/fbjni/cxx/fbjni/detail/References.h index eaac4c4dd58..bbc9ba82769 100644 --- a/first-party/fbjni/cxx/fbjni/detail/References.h +++ b/first-party/fbjni/cxx/fbjni/detail/References.h @@ -598,20 +598,20 @@ class JniLocalScope { }; template -enable_if_t(), local_ref> +enable_if_t>(), local_ref> static_ref_cast(const local_ref& ref) noexcept; template -enable_if_t(), global_ref> +enable_if_t>(), global_ref> static_ref_cast(const global_ref& ref) noexcept; template -enable_if_t(), alias_ref> +enable_if_t>(), alias_ref> static_ref_cast(const alias_ref& ref) noexcept; template auto dynamic_ref_cast(const RefType& ref) -> -enable_if_t(), decltype(static_ref_cast(ref))> ; +enable_if_t>(), decltype(static_ref_cast(ref))> ; }} diff --git a/first-party/fbjni/test/jni/doc_tests.cpp b/first-party/fbjni/test/jni/doc_tests.cpp index 7f12ff9fdbb..aac961f75fb 100644 --- a/first-party/fbjni/test/jni/doc_tests.cpp +++ b/first-party/fbjni/test/jni/doc_tests.cpp @@ -260,11 +260,11 @@ struct DocTests : JavaClass { // Just like raw pointers, upcasting is implicit. alias_ref obj = base; // static_ref_cast is like C++ static_cast. No runtime checking is done. - alias_ref derived_1 = static_ref_cast(base); + alias_ref derived_1 = static_ref_cast(base); // dynamic_ref_cast is like C++ dynamic_cast. // It will check that the runtime Java type is actually derived from the target type. try { - alias_ref derived_2 = dynamic_ref_cast(base); + alias_ref derived_2 = dynamic_ref_cast(base); (void)derived_2; } catch (const JniException& exn) { // Throws ClassCastException if the cast fails. @@ -346,7 +346,7 @@ struct DocTests : JavaClass { alias_ref clazz, // Note that generic types are *not* checked against Java declarations. alias_ref> values, - alias_ref> names) { + alias_ref> names) { int sum = 0; std::string ret; // Iterator and Iterable support C++ iteration. diff --git a/first-party/fbjni/test/jni/iterator_tests.cpp b/first-party/fbjni/test/jni/iterator_tests.cpp index ca6e51e66e3..82d124d0bac 100644 --- a/first-party/fbjni/test/jni/iterator_tests.cpp +++ b/first-party/fbjni/test/jni/iterator_tests.cpp @@ -76,7 +76,7 @@ jboolean nativeTestListIterator( jboolean nativeTestMapIterator( alias_ref, - alias_ref> jmap) { + alias_ref> jmap) { EXPECT(jmap); EXPECT(jmap->size() == 3); @@ -129,7 +129,7 @@ jboolean nativeTestIterateWrongType( jboolean nativeTestIterateNullKey( alias_ref, - alias_ref> jmap) { + alias_ref> jmap) { EXPECT(jmap); EXPECT(jmap->size() == 3);