Skip to content

Commit 57a4b01

Browse files
authored
Merge pull request #37 from rpavlik/methods-returning-array
Methods returning array
2 parents e6c4158 + 5381e61 commit 57a4b01

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

jnipp.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,13 @@ namespace jni
465465
return Object(result, DeleteLocalInput);
466466
}
467467

468+
jarray Object::callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<jarray> const&) const
469+
{
470+
auto result = env()->CallObjectMethodA(_handle, method, (jvalue*) args);
471+
handleJavaExceptions();
472+
return (jarray)result;
473+
}
474+
468475
byte_t Object::getFieldValue(field_t field, internal::ReturnTypeWrapper<byte_t> const&) const
469476
{
470477
return env()->GetByteField(_handle, field);

jnipp.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ namespace jni
7373

7474
// Foward Declarations
7575
class Object;
76+
template <class TElement> class Array;
7677

7778
/**
7879
This namespace is for messy implementation details only. It is not a part
@@ -103,6 +104,9 @@ namespace jni
103104
std::string valueSig(const Object* obj);
104105
inline std::string valueSig(const Object* const* obj) { return valueSig(obj ? *obj : nullptr); }
105106

107+
template <class TArg>
108+
inline std::string valueSig(const Array<TArg>*) { return "[" + valueSig((TArg*) nullptr); }
109+
106110
template <int n, class TArg>
107111
inline std::string valueSig(const TArg(*arg)[n]) { return valueSig((const TArg* const*)arg); }
108112

@@ -452,7 +456,10 @@ namespace jni
452456
std::string callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<std::string> const&) const;
453457
std::wstring callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<std::wstring> const&) const;
454458
jni::Object callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<jni::Object> const&) const;
459+
jarray callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<jarray> const&) const;
455460

461+
template<typename T>
462+
jni::Array<T> callMethod(method_t method, internal::value_t* args, internal::ReturnTypeWrapper<jni::Array<T>> const&) const;
456463

457464
void getFieldValue(field_t field, internal::ReturnTypeWrapper<void> const&) const;
458465
bool getFieldValue(field_t field, internal::ReturnTypeWrapper<bool> const&) const;
@@ -1025,6 +1032,17 @@ namespace jni
10251032
InitializationException(const char* msg) : Exception(msg) {}
10261033
};
10271034

1035+
/*
1036+
Call method returning array: implementation
1037+
*/
1038+
template <typename T>
1039+
inline jni::Array<T> Object::callMethod(method_t method, internal::value_t* args,
1040+
internal::ReturnTypeWrapper<jni::Array<T>> const&) const
1041+
{
1042+
jarray result = callMethod(method, args, internal::ReturnTypeWrapper<jarray>{});
1043+
return jni::Array<T>(result, DeleteLocalInput);
1044+
}
1045+
10281046
/*
10291047
Array Implementation
10301048
*/

tests/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,21 @@ TEST(Object_call_byNameWithArgs)
304304
ASSERT(str2.call<wchar_t>("charAt", 1) == L'e');
305305
}
306306

307+
TEST(Object_call_returningArray) {
308+
jni::Object str = jni::Class("java/lang/String").newInstance("Testing");
309+
310+
{
311+
auto getBytes =
312+
jni::Class("java/lang/String").getMethod("getBytes", "()[B");
313+
auto bytes = str.call<jni::Array<jni::byte_t>>(getBytes);
314+
ASSERT(bytes.getLength() == 7);
315+
}
316+
{
317+
auto bytes = str.call<jni::Array<jni::byte_t>>("getBytes");
318+
ASSERT(bytes.getLength() == 7);
319+
}
320+
}
321+
307322
TEST(Object_makeLocalReference)
308323
{
309324
jni::Object str = jni::Class("java/lang/String").newInstance("Testing");
@@ -593,6 +608,7 @@ int main()
593608
RUN_TEST(Object_call_byName);
594609
RUN_TEST(Object_call_withArgs);
595610
RUN_TEST(Object_call_byNameWithArgs);
611+
RUN_TEST(Object_call_returningArray);
596612
RUN_TEST(Object_makeLocalReference);
597613

598614
// jni::Enum Tests

0 commit comments

Comments
 (0)