From f7f27347b91f5dae9541468516011565aaa3adec Mon Sep 17 00:00:00 2001 From: David Reiss Date: Sat, 12 Oct 2019 21:45:06 -0700 Subject: [PATCH] Support getMethod without ::javaobject or local_ref in return Summary: Treat getMethod as equivalent to getMethod>, which is equivalent to getMethod. Update all call sites. Some were calling getMethod>, which is equivalent. Test Plan: CI --- cxx/fbjni/ByteBuffer.cpp | 2 +- cxx/fbjni/Context.h | 4 ++-- cxx/fbjni/detail/Meta-inl.h | 18 ++++++++++++------ test/jni/doc_tests.cpp | 2 +- test/jni/iterator_tests.cpp | 2 +- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cxx/fbjni/ByteBuffer.cpp b/cxx/fbjni/ByteBuffer.cpp index 4188855..1fa9bab 100644 --- a/cxx/fbjni/ByteBuffer.cpp +++ b/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/cxx/fbjni/Context.h b/cxx/fbjni/Context.h index 39d47bd..7e3ba18 100644 --- a/cxx/fbjni/Context.h +++ b/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/cxx/fbjni/detail/Meta-inl.h b/cxx/fbjni/detail/Meta-inl.h index f2b3d51..a501d6c 100644 --- a/cxx/fbjni/detail/Meta-inl.h +++ b/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/test/jni/doc_tests.cpp b/test/jni/doc_tests.cpp index 7f12ff9..4a8ec54 100644 --- a/test/jni/doc_tests.cpp +++ b/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/test/jni/iterator_tests.cpp b/test/jni/iterator_tests.cpp index ca6e51e..fc1e156 100644 --- a/test/jni/iterator_tests.cpp +++ b/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()));