Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support getMethod without ::javaobject or local_ref in return #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cxx/fbjni/ByteBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ local_ref<JByteBuffer> JByteBuffer::wrapBytes(uint8_t* data, size_t size) {

local_ref<JByteBuffer> JByteBuffer::allocateDirect(jint size) {
static auto cls = JByteBuffer::javaClassStatic();
static auto meth = cls->getStaticMethod<JByteBuffer::javaobject(int)>("allocateDirect");
static auto meth = cls->getStaticMethod<JByteBuffer(int)>("allocateDirect");
return meth(cls, size);
}

Expand Down
4 changes: 2 additions & 2 deletions cxx/fbjni/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ class AContext : public JavaClass<AContext> {

// Define a method that calls into the represented Java class
local_ref<JFile> getCacheDir() {
static const auto method = getClass()->getMethod<JFile::javaobject()>("getCacheDir");
static const auto method = getClass()->getMethod<JFile()>("getCacheDir");
return method(self());
}

local_ref<JFile> getFilesDir() {
static const auto method = getClass()->getMethod<JFile::javaobject()>("getFilesDir");
static const auto method = getClass()->getMethod<JFile()>("getFilesDir");
return method(self());
}
};
Expand Down
18 changes: 12 additions & 6 deletions cxx/fbjni/detail/Meta-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ class JMethod<R(Args...)> : public JMethodBase {
public:
// TODO: static_assert is jobject-derived or local_ref jobject
using JniRet = typename detail::Convert<typename std::decay<R>::type>::jniType;
static_assert(IsPlainJniReference<JniRet>(), "JniRet must be a JNI reference");
static_assert(
IsPlainJniReference<JniRet>() || detail::IsJavaClassType<JniRet>(),
"Return type must be a JNI reference or JavaClass type.");
using JMethodBase::JMethodBase;
JMethod() noexcept {};
JMethod(const JMethod& other) noexcept = default;
Expand All @@ -130,7 +132,7 @@ inline auto JMethod<R(Args...)>::operator()(alias_ref<jobject> self, Args... arg
getId(),
detail::callToJni(detail::Convert<typename std::decay<Args>::type>::toCall(args))...);
FACEBOOK_JNI_THROW_PENDING_EXCEPTION();
return adopt_local(static_cast<JniRet>(result));
return adopt_local(static_cast<JniType<JniRet>>(result));
}

template<typename... Args>
Expand Down Expand Up @@ -173,7 +175,9 @@ class JStaticMethod<R(Args...)> : public JMethodBase {

public:
using JniRet = typename detail::Convert<typename std::decay<R>::type>::jniType;
static_assert(IsPlainJniReference<JniRet>(), "T* must be a JNI reference");
static_assert(
IsPlainJniReference<JniRet>() || detail::IsJavaClassType<JniRet>(),
"Return type must be a JNI reference or JavaClass type.");
using JMethodBase::JMethodBase;
JStaticMethod() noexcept {};
JStaticMethod(const JStaticMethod& other) noexcept = default;
Expand All @@ -186,7 +190,7 @@ class JStaticMethod<R(Args...)> : public JMethodBase {
getId(),
detail::callToJni(detail::Convert<typename std::decay<Args>::type>::toCall(args))...);
FACEBOOK_JNI_THROW_PENDING_EXCEPTION();
return adopt_local(static_cast<JniRet>(result));
return adopt_local(static_cast<JniType<JniRet>>(result));
}

friend class JClass;
Expand Down Expand Up @@ -235,7 +239,9 @@ template<typename R, typename... Args>
class JNonvirtualMethod<R(Args...)> : public JMethodBase {
public:
using JniRet = typename detail::Convert<typename std::decay<R>::type>::jniType;
static_assert(IsPlainJniReference<JniRet>(), "T* must be a JNI reference");
static_assert(
IsPlainJniReference<JniRet>() || detail::IsJavaClassType<JniRet>(),
"Return type must be a JNI reference or JavaClass type.");
using JMethodBase::JMethodBase;
JNonvirtualMethod() noexcept {};
JNonvirtualMethod(const JNonvirtualMethod& other) noexcept = default;
Expand All @@ -249,7 +255,7 @@ class JNonvirtualMethod<R(Args...)> : public JMethodBase {
getId(),
detail::callToJni(detail::Convert<typename std::decay<Args>::type>::toCall(args))...);
FACEBOOK_JNI_THROW_PENDING_EXCEPTION();
return adopt_local(static_cast<JniRet>(result));
return adopt_local(static_cast<JniType<JniRet>>(result));
}

friend class JClass;
Expand Down
2 changes: 1 addition & 1 deletion test/jni/doc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct DocTests : JavaClass<DocTests> {
// Convert JString to std::string.
std::string result = s1->toStdString();
// Java methods can receive and return JString ...
static const auto doubler_java = clazz->getStaticMethod<local_ref<JString>(JString)>("doubler");
static const auto doubler_java = clazz->getStaticMethod<JString(JString)>("doubler");
result += doubler_java(clazz, *s1)->toStdString();
// and also std::string (converted from real UTF-8).
static const auto doubler_std = clazz->getStaticMethod<std::string(std::string)>("doubler");
Expand Down
2 changes: 1 addition & 1 deletion test/jni/iterator_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jboolean nativeTestListIterator(
EXPECT(vs1 == vs3);

static auto iteratorMethod =
JIterable<jstring>::javaClassStatic()->getMethod<JIterator<jstring>::javaobject()>("iterator");
JIterable<jstring>::javaClassStatic()->getMethod<JIterator<jstring>()>("iterator");
auto iter = iteratorMethod(jlist);

EXPECT(std::equal(iter->begin(), iter->end(), jlist->begin()));
Expand Down