@@ -26,9 +26,13 @@ namespace jni
2626 static std::atomic_bool isVm (false );
2727 static JavaVM* javaVm = nullptr ;
2828
29+ static bool getEnv (JavaVM *vm, JNIEnv **env) {
30+ return vm->GetEnv ((void **)env, JNI_VERSION_1_2) == JNI_OK;
31+ }
32+
2933 static bool isAttached (JavaVM *vm) {
3034 JNIEnv *env = nullptr ;
31- return vm-> GetEnv (( void **) &env, JNI_VERSION_1_2) == JNI_OK ;
35+ return getEnv (vm, &env) ;
3236 }
3337 /* *
3438 Maintains the lifecycle of a JNIEnv.
@@ -63,7 +67,7 @@ namespace jni
6367 if (vm == nullptr )
6468 throw InitializationException (" JNI not initialized" );
6569
66- if (!isAttached (vm))
70+ if (!getEnv (vm, &_env ))
6771 {
6872#ifdef __ANDROID__
6973 if (vm->AttachCurrentThread (&_env, nullptr ) != 0 )
@@ -368,20 +372,20 @@ namespace jni
368372 return _handle == nullptr || env ()->IsSameObject (_handle, nullptr );
369373 }
370374
371- template <> void Object::callMethod (method_t method, internal::value_t * args) const
375+ void Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< void > const & ) const
372376 {
373377 env ()->CallVoidMethodA (_handle, method, (jvalue*) args);
374378 handleJavaExceptions ();
375379 }
376380
377- template <> bool Object::callMethod (method_t method, internal::value_t * args) const
381+ bool Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< bool > const & ) const
378382 {
379383 auto result = env ()->CallBooleanMethodA (_handle, method, (jvalue*) args);
380384 handleJavaExceptions ();
381385 return result != 0 ;
382386 }
383387
384- template <> bool Object::get (field_t field) const
388+ bool Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< bool > const & ) const
385389 {
386390 return env ()->GetBooleanField (_handle, field) != 0 ;
387391 }
@@ -391,122 +395,122 @@ namespace jni
391395 env ()->SetBooleanField (_handle, field, value);
392396 }
393397
394- template <> byte_t Object::callMethod (method_t method, internal::value_t * args) const
398+ byte_t Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< byte_t > const & ) const
395399 {
396400 auto result = env ()->CallByteMethodA (_handle, method, (jvalue*) args);
397401 handleJavaExceptions ();
398402 return result;
399403 }
400404
401- template <> wchar_t Object::callMethod (method_t method, internal::value_t * args) const
405+ wchar_t Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< wchar_t > const & ) const
402406 {
403407 auto result = env ()->CallCharMethodA (_handle, method, (jvalue*) args);
404408 handleJavaExceptions ();
405409 return result;
406410 }
407411
408- template <> short Object::callMethod (method_t method, internal::value_t * args) const
412+ short Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< short > const & ) const
409413 {
410414 auto result = env ()->CallShortMethodA (_handle, method, (jvalue*) args);
411415 handleJavaExceptions ();
412416 return result;
413417 }
414418
415- template <> int Object::callMethod (method_t method, internal::value_t * args) const
419+ int Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< int > const & ) const
416420 {
417421 auto result = env ()->CallIntMethodA (_handle, method, (jvalue*) args);
418422 handleJavaExceptions ();
419423 return result;
420424 }
421425
422- template <> long long Object::callMethod (method_t method, internal::value_t * args) const
426+ long long Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< long long > const & ) const
423427 {
424428 auto result = env ()->CallLongMethodA (_handle, method, (jvalue*) args);
425429 handleJavaExceptions ();
426430 return result;
427431 }
428432
429- template <> float Object::callMethod (method_t method, internal::value_t * args) const
433+ float Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< float > const & ) const
430434 {
431435 auto result = env ()->CallFloatMethodA (_handle, method, (jvalue*) args);
432436 handleJavaExceptions ();
433437 return result;
434438 }
435439
436- template <> double Object::callMethod (method_t method, internal::value_t * args) const
440+ double Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper< double > const & ) const
437441 {
438442 auto result = env ()->CallDoubleMethodA (_handle, method, (jvalue*) args);
439443 handleJavaExceptions ();
440444 return result;
441445 }
442446
443- template <> std::string Object::callMethod (method_t method, internal::value_t * args) const
447+ std::string Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper<std::string> const & ) const
444448 {
445449 auto result = env ()->CallObjectMethodA (_handle, method, (jvalue*) args);
446450 handleJavaExceptions ();
447451 return toString (result);
448452 }
449453
450- template <> std::wstring Object::callMethod (method_t method, internal::value_t * args) const
454+ std::wstring Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper<std::wstring> const & ) const
451455 {
452456 auto result = env ()->CallObjectMethodA (_handle, method, (jvalue*) args);
453457 handleJavaExceptions ();
454458 return toWString (result);
455459 }
456460
457- template <> jni::Object Object::callMethod (method_t method, internal::value_t * args) const
461+ jni::Object Object::callMethod (method_t method, internal::value_t * args, internal::ReturnTypeWrapper<jni::Object> const & ) const
458462 {
459463 auto result = env ()->CallObjectMethodA (_handle, method, (jvalue*) args);
460464 handleJavaExceptions ();
461465 return Object (result, DeleteLocalInput);
462466 }
463467
464- template <> byte_t Object::get (field_t field) const
468+ byte_t Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< byte_t > const & ) const
465469 {
466470 return env ()->GetByteField (_handle, field);
467471 }
468472
469- template <> wchar_t Object::get (field_t field) const
473+ wchar_t Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< wchar_t > const & ) const
470474 {
471475 return env ()->GetCharField (_handle, field);
472476 }
473477
474- template <> short Object::get (field_t field) const
478+ short Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< short > const & ) const
475479 {
476480 return env ()->GetShortField (_handle, field);
477481 }
478482
479- template <> int Object::get (field_t field) const
483+ int Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< int > const & ) const
480484 {
481485 return env ()->GetIntField (_handle, field);
482486 }
483487
484- template <> long long Object::get (field_t field) const
488+ long long Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< long long > const & ) const
485489 {
486490 return env ()->GetLongField (_handle, field);
487491 }
488492
489- template <> float Object::get (field_t field) const
493+ float Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< float > const & ) const
490494 {
491495 return env ()->GetFloatField (_handle, field);
492496 }
493497
494- template <> double Object::get (field_t field) const
498+ double Object::getFieldValue (field_t field, internal::ReturnTypeWrapper< double > const & ) const
495499 {
496500 return env ()->GetDoubleField (_handle, field);
497501 }
498502
499- template <> std::string Object::get (field_t field) const
503+ std::string Object::getFieldValue (field_t field, internal::ReturnTypeWrapper<std::string> const & ) const
500504 {
501505 return toString (env ()->GetObjectField (_handle, field));
502506 }
503507
504- template <> std::wstring Object::get (field_t field) const
508+ std::wstring Object::getFieldValue (field_t field, internal::ReturnTypeWrapper<std::wstring> const & ) const
505509 {
506510 return toWString (env ()->GetObjectField (_handle, field));
507511 }
508512
509- template <> Object Object::get (field_t field) const
513+ Object Object::getFieldValue (field_t field, internal::ReturnTypeWrapper<Object> const & ) const
510514 {
511515 return Object (env ()->GetObjectField (_handle, field), DeleteLocalInput);
512516 }
@@ -1475,7 +1479,6 @@ namespace jni
14751479 }
14761480
14771481#else
1478- fprintf (stderr, " opening %s\n " , path.c_str ());
14791482
14801483 void * lib = ::dlopen (path.c_str (), RTLD_NOW | RTLD_GLOBAL);
14811484
0 commit comments