From 73c7da6c1eaf44e356de1a4bb2355d938d718ae0 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Mon, 28 Oct 2019 13:10:31 -0700 Subject: [PATCH] Support getMethod without ::javaobject or local_ref in return (#19) Summary: Treat `getMethod` as equivalent to `getMethod()>`, which is equivalent to `getMethod`. Update all call sites. Some were calling getMethod>, which is equivalent. Pull Request resolved: https://github.com/facebookincubator/fbjni/pull/19 Test Plan: CI Reviewed By: cjhopman Differential Revision: D18121237 Pulled By: dreiss fbshipit-source-id: 2f0b6fc1909599256cdc708e721c9d877492d54d --- first-party/fbjni/cxx/fbjni/ByteBuffer.cpp | 2 +- first-party/fbjni/cxx/fbjni/Context.h | 4 ++-- first-party/fbjni/cxx/fbjni/detail/Meta-inl.h | 18 ++++++++++++------ first-party/fbjni/test/jni/doc_tests.cpp | 2 +- first-party/fbjni/test/jni/iterator_tests.cpp | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/first-party/fbjni/cxx/fbjni/ByteBuffer.cpp b/first-party/fbjni/cxx/fbjni/ByteBuffer.cpp index 41888552fc3..1fa9bab8499 100644 --- a/first-party/fbjni/cxx/fbjni/ByteBuffer.cpp +++ b/first-party/fbjni/cxx/fbjni/ByteBuffer.cpp @@ -85,7 +85,7 @@ local_ref JByteBuffer::wrapBytes(uint8_t* data, size_t size) { local_ref JByteBuffer::allocateDirect(jint size) { static auto cls = JByteBuffer::javaClassStatic(); - static auto meth = cls->getStaticMethod("allocateDirect"); + static auto meth = cls->getStaticMethod("allocateDirect"); return meth(cls, size); } diff --git a/first-party/fbjni/cxx/fbjni/Context.h b/first-party/fbjni/cxx/fbjni/Context.h index 39d47bdf659..7e3ba18eb9a 100644 --- a/first-party/fbjni/cxx/fbjni/Context.h +++ b/first-party/fbjni/cxx/fbjni/Context.h @@ -28,12 +28,12 @@ class AContext : public JavaClass { // Define a method that calls into the represented Java class local_ref getCacheDir() { - static const auto method = getClass()->getMethod("getCacheDir"); + static const auto method = getClass()->getMethod("getCacheDir"); return method(self()); } local_ref getFilesDir() { - static const auto method = getClass()->getMethod("getFilesDir"); + static const auto method = getClass()->getMethod("getFilesDir"); return method(self()); } }; diff --git a/first-party/fbjni/cxx/fbjni/detail/Meta-inl.h b/first-party/fbjni/cxx/fbjni/detail/Meta-inl.h index f2b3d517dbb..a501d6c9093 100644 --- a/first-party/fbjni/cxx/fbjni/detail/Meta-inl.h +++ b/first-party/fbjni/cxx/fbjni/detail/Meta-inl.h @@ -111,7 +111,9 @@ class JMethod : public JMethodBase { public: // TODO: static_assert is jobject-derived or local_ref jobject using JniRet = typename detail::Convert::type>::jniType; - static_assert(IsPlainJniReference(), "JniRet must be a JNI reference"); + static_assert( + IsPlainJniReference() || detail::IsJavaClassType(), + "Return type must be a JNI reference or JavaClass type."); using JMethodBase::JMethodBase; JMethod() noexcept {}; JMethod(const JMethod& other) noexcept = default; @@ -130,7 +132,7 @@ inline auto JMethod::operator()(alias_ref self, Args... arg getId(), detail::callToJni(detail::Convert::type>::toCall(args))...); FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return adopt_local(static_cast(result)); + return adopt_local(static_cast>(result)); } template @@ -173,7 +175,9 @@ class JStaticMethod : public JMethodBase { public: using JniRet = typename detail::Convert::type>::jniType; - static_assert(IsPlainJniReference(), "T* must be a JNI reference"); + static_assert( + IsPlainJniReference() || detail::IsJavaClassType(), + "Return type must be a JNI reference or JavaClass type."); using JMethodBase::JMethodBase; JStaticMethod() noexcept {}; JStaticMethod(const JStaticMethod& other) noexcept = default; @@ -186,7 +190,7 @@ class JStaticMethod : public JMethodBase { getId(), detail::callToJni(detail::Convert::type>::toCall(args))...); FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return adopt_local(static_cast(result)); + return adopt_local(static_cast>(result)); } friend class JClass; @@ -235,7 +239,9 @@ template class JNonvirtualMethod : public JMethodBase { public: using JniRet = typename detail::Convert::type>::jniType; - static_assert(IsPlainJniReference(), "T* must be a JNI reference"); + static_assert( + IsPlainJniReference() || detail::IsJavaClassType(), + "Return type must be a JNI reference or JavaClass type."); using JMethodBase::JMethodBase; JNonvirtualMethod() noexcept {}; JNonvirtualMethod(const JNonvirtualMethod& other) noexcept = default; @@ -249,7 +255,7 @@ class JNonvirtualMethod : public JMethodBase { getId(), detail::callToJni(detail::Convert::type>::toCall(args))...); FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); - return adopt_local(static_cast(result)); + return adopt_local(static_cast>(result)); } friend class JClass; diff --git a/first-party/fbjni/test/jni/doc_tests.cpp b/first-party/fbjni/test/jni/doc_tests.cpp index caa52df2bea..fe7697e6faa 100644 --- a/first-party/fbjni/test/jni/doc_tests.cpp +++ b/first-party/fbjni/test/jni/doc_tests.cpp @@ -176,7 +176,7 @@ struct DocTests : JavaClass { // Convert JString to std::string. std::string result = s1->toStdString(); // Java methods can receive and return JString ... - static const auto doubler_java = clazz->getStaticMethod(JString)>("doubler"); + static const auto doubler_java = clazz->getStaticMethod("doubler"); result += doubler_java(clazz, *s1)->toStdString(); // and also std::string (converted from real UTF-8). static const auto doubler_std = clazz->getStaticMethod("doubler"); diff --git a/first-party/fbjni/test/jni/iterator_tests.cpp b/first-party/fbjni/test/jni/iterator_tests.cpp index 82d124d0bac..4d39561f485 100644 --- a/first-party/fbjni/test/jni/iterator_tests.cpp +++ b/first-party/fbjni/test/jni/iterator_tests.cpp @@ -62,7 +62,7 @@ jboolean nativeTestListIterator( EXPECT(vs1 == vs3); static auto iteratorMethod = - JIterable::javaClassStatic()->getMethod::javaobject()>("iterator"); + JIterable::javaClassStatic()->getMethod()>("iterator"); auto iter = iteratorMethod(jlist); EXPECT(std::equal(iter->begin(), iter->end(), jlist->begin()));