1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
|
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <map>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include "JCCEnv.h"
#if defined(_MSC_VER) || defined(__WIN32)
_DLL_EXPORT DWORD VM_ENV = 0;
#else
pthread_key_t JCCEnv::VM_ENV = (pthread_key_t) NULL;
#endif
#if defined(_MSC_VER) || defined(__WIN32)
static CRITICAL_SECTION *mutex = NULL;
class lock {
public:
lock() {
EnterCriticalSection(mutex);
}
virtual ~lock() {
LeaveCriticalSection(mutex);
}
};
#else
static pthread_mutex_t *mutex = NULL;
class lock {
public:
lock() {
pthread_mutex_lock(mutex);
}
virtual ~lock() {
pthread_mutex_unlock(mutex);
}
};
#endif
JCCEnv::JCCEnv(JavaVM *vm, JNIEnv *vm_env)
{
#if defined(_MSC_VER) || defined(__WIN32)
if (!mutex)
{
mutex = new CRITICAL_SECTION();
InitializeCriticalSection(mutex); // recursive by default
}
#else
if (!mutex)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
mutex = new pthread_mutex_t();
pthread_mutex_init(mutex, &attr);
}
#endif
if (vm)
set_vm(vm, vm_env);
else
this->vm = NULL;
}
void JCCEnv::set_vm(JavaVM *vm, JNIEnv *vm_env)
{
this->vm = vm;
set_vm_env(vm_env);
_sys = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/System"));
_obj = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Object"));
#ifdef _jcc_lib
_thr = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("org/apache/jcc/PythonException"));
#else
_thr = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/RuntimeException"));
#endif
_boo = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Boolean"));
_byt = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Byte"));
_cha = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Character"));
_dou = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Double"));
_flo = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Float"));
_int = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Integer"));
_lon = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Long"));
_sho = (jclass) vm_env->NewGlobalRef(vm_env->FindClass("java/lang/Short"));
_mids = new jmethodID[max_mid];
_mids[mid_sys_identityHashCode] =
vm_env->GetStaticMethodID(_sys, "identityHashCode",
"(Ljava/lang/Object;)I");
_mids[mid_sys_setProperty] =
vm_env->GetStaticMethodID(_sys, "setProperty",
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
_mids[mid_sys_getProperty] =
vm_env->GetStaticMethodID(_sys, "getProperty",
"(Ljava/lang/String;)Ljava/lang/String;");
_mids[mid_obj_toString] =
vm_env->GetMethodID(_obj, "toString",
"()Ljava/lang/String;");
_mids[mid_obj_hashCode] =
vm_env->GetMethodID(_obj, "hashCode",
"()I");
_mids[mid_obj_getClass] =
vm_env->GetMethodID(_obj, "getClass",
"()Ljava/lang/Class;");
jclass iterable = vm_env->FindClass("java/lang/Iterable");
if (iterable == NULL) /* JDK < 1.5 */
{
vm_env->ExceptionClear();
_mids[mid_iterator] = NULL;
_mids[mid_iterator_next] = NULL;
}
else
{
_mids[mid_iterator] =
vm_env->GetMethodID(iterable,
"iterator", "()Ljava/util/Iterator;");
_mids[mid_iterator_next] =
vm_env->GetMethodID(vm_env->FindClass("java/util/Iterator"),
"next", "()Ljava/lang/Object;");
}
_mids[mid_enumeration_nextElement] =
vm_env->GetMethodID(vm_env->FindClass("java/util/Enumeration"),
"nextElement", "()Ljava/lang/Object;");
_mids[mid_Boolean_booleanValue] =
vm_env->GetMethodID(_boo, "booleanValue", "()Z");
_mids[mid_Byte_byteValue] =
vm_env->GetMethodID(_byt, "byteValue", "()B");
_mids[mid_Character_charValue] =
vm_env->GetMethodID(_cha, "charValue", "()C");
_mids[mid_Double_doubleValue] =
vm_env->GetMethodID(_dou, "doubleValue", "()D");
_mids[mid_Float_floatValue] =
vm_env->GetMethodID(_flo, "floatValue", "()F");
_mids[mid_Integer_intValue] =
vm_env->GetMethodID(_int, "intValue", "()I");
_mids[mid_Long_longValue] =
vm_env->GetMethodID(_lon, "longValue", "()J");
_mids[mid_Short_shortValue] =
vm_env->GetMethodID(_sho, "shortValue", "()S");
_mids[mid_Boolean_init] =
vm_env->GetMethodID(_boo, "<init>", "(Z)V");
_mids[mid_Byte_init] =
vm_env->GetMethodID(_byt, "<init>", "(B)V");
_mids[mid_Character_init] =
vm_env->GetMethodID(_cha, "<init>", "(C)V");
_mids[mid_Double_init] =
vm_env->GetMethodID(_dou, "<init>", "(D)V");
_mids[mid_Float_init] =
vm_env->GetMethodID(_flo, "<init>", "(F)V");
_mids[mid_Integer_init] =
vm_env->GetMethodID(_int, "<init>", "(I)V");
_mids[mid_Long_init] =
vm_env->GetMethodID(_lon, "<init>", "(J)V");
_mids[mid_Short_init] =
vm_env->GetMethodID(_sho, "<init>", "(S)V");
}
int JCCEnv::attachCurrentThread(char *name, int asDaemon)
{
JNIEnv *jenv = NULL;
JavaVMAttachArgs attach = {
JNI_VERSION_1_4, name, NULL
};
int result;
if (asDaemon)
result = vm->AttachCurrentThreadAsDaemon((void **) &jenv, &attach);
else
result = vm->AttachCurrentThread((void **) &jenv, &attach);
set_vm_env(jenv);
return result;
}
#if defined(_MSC_VER) || defined(__WIN32)
void JCCEnv::set_vm_env(JNIEnv *vm_env)
{
if (!VM_ENV)
VM_ENV = TlsAlloc();
TlsSetValue(VM_ENV, (LPVOID) vm_env);
}
#else
void JCCEnv::set_vm_env(JNIEnv *vm_env)
{
if (!VM_ENV)
pthread_key_create(&VM_ENV, NULL);
pthread_setspecific(VM_ENV, (void *) vm_env);
}
#endif
jint JCCEnv::getJNIVersion() const
{
return get_vm_env()->GetVersion();
}
jstring JCCEnv::getJavaVersion() const
{
return (jstring)
callStaticObjectMethod(_sys, _mids[mid_sys_getProperty],
get_vm_env()->NewStringUTF("java.version"));
}
jobject JCCEnv::iterator(jobject obj) const
{
return callObjectMethod(obj, _mids[mid_iterator]);
}
jobject JCCEnv::iteratorNext(jobject obj) const
{
return callObjectMethod(obj, _mids[mid_iterator_next]);
}
jobject JCCEnv::enumerationNext(jobject obj) const
{
return callObjectMethod(obj, _mids[mid_enumeration_nextElement]);
}
jboolean JCCEnv::isInstanceOf(jobject obj, getclassfn initializeClass) const
{
return get_vm_env()->IsInstanceOf(obj, getClass(initializeClass));
}
jclass JCCEnv::findClass(const char *className) const
{
jclass cls = NULL;
if (vm)
{
JNIEnv *vm_env = get_vm_env();
if (vm_env)
{
cls = vm_env->FindClass(className);
if (cls == NULL)
reportException();
}
#ifdef PYTHON
else
{
PythonGIL gil;
PyErr_SetString(PyExc_RuntimeError, "attachCurrentThread() must be called first");
throw _EXC_PYTHON;
}
#else
else
throw _EXC_JAVA;
#endif
}
#ifdef PYTHON
else
{
PythonGIL gil;
PyErr_SetString(PyExc_RuntimeError, "initVM() must be called first");
throw _EXC_PYTHON;
}
#else
else
throw _EXC_JAVA;
#endif
reportException();
return cls;
}
void JCCEnv::registerNatives(jclass cls, JNINativeMethod *methods, int n) const
{
get_vm_env()->RegisterNatives(cls, methods, n);
}
jobject JCCEnv::newGlobalRef(jobject obj, int id)
{
if (obj)
{
if (id) /* zero when weak global ref is desired */
{
lock locked;
for (std::multimap<int, countedRef>::iterator iter = refs.find(id);
iter != refs.end();
iter++) {
if (iter->first != id)
break;
if (isSame(obj, iter->second.global))
{
/* If it's in the table but not the same reference,
* it must be a local reference and must be deleted.
*/
if (obj != iter->second.global)
get_vm_env()->DeleteLocalRef(obj);
iter->second.count += 1;
return iter->second.global;
}
}
JNIEnv *vm_env = get_vm_env();
countedRef ref;
ref.global = vm_env->NewGlobalRef(obj);
ref.count = 1;
refs.insert(std::pair<const int, countedRef>(id, ref));
vm_env->DeleteLocalRef(obj);
return ref.global;
}
else
return (jobject) get_vm_env()->NewWeakGlobalRef(obj);
}
return NULL;
}
jobject JCCEnv::deleteGlobalRef(jobject obj, int id)
{
if (obj)
{
if (id) /* zero when obj is weak global ref */
{
lock locked;
for (std::multimap<int, countedRef>::iterator iter = refs.find(id);
iter != refs.end();
iter++) {
if (iter->first != id)
break;
if (isSame(obj, iter->second.global))
{
if (iter->second.count == 1)
{
JNIEnv *vm_env = get_vm_env();
if (!vm_env)
{
/* Python's cyclic garbage collector may remove
* an object inside a thread that is not attached
* to the JVM. This makes sure the JVM doesn't
* segfault.
*/
attachCurrentThread(NULL, 0);
vm_env = get_vm_env();
}
vm_env->DeleteGlobalRef(iter->second.global);
refs.erase(iter);
}
else
iter->second.count -= 1;
return NULL;
}
}
printf("deleting non-existent ref: 0x%x\n", id);
}
else
get_vm_env()->DeleteWeakGlobalRef((jweak) obj);
}
return NULL;
}
jclass JCCEnv::getClass(getclassfn initializeClass) const
{
jclass cls = (*initializeClass)(true);
if (cls == NULL)
{
lock locked;
cls = (*initializeClass)(false);
}
return cls;
}
jobject JCCEnv::newObject(getclassfn initializeClass, jmethodID **mids,
int m, ...)
{
jclass cls = getClass(initializeClass);
JNIEnv *vm_env = get_vm_env();
jobject obj;
if (vm_env)
{
va_list ap;
va_start(ap, m);
obj = vm_env->NewObjectV(cls, (*mids)[m], ap);
va_end(ap);
}
#ifdef PYTHON
else
{
PythonGIL gil;
PyErr_SetString(PyExc_RuntimeError, "attachCurrentThread() must be called first");
throw _EXC_PYTHON;
}
#else
else
throw _EXC_JAVA;
#endif
reportException();
return obj;
}
jobjectArray JCCEnv::newObjectArray(jclass cls, int size)
{
jobjectArray array = get_vm_env()->NewObjectArray(size, cls, NULL);
reportException();
return array;
}
void JCCEnv::setObjectArrayElement(jobjectArray array, int n,
jobject obj) const
{
get_vm_env()->SetObjectArrayElement(array, n, obj);
reportException();
}
jobject JCCEnv::getObjectArrayElement(jobjectArray array, int n) const
{
jobject obj = get_vm_env()->GetObjectArrayElement(array, n);
reportException();
return obj;
}
int JCCEnv::getArrayLength(jarray array) const
{
int len = get_vm_env()->GetArrayLength(array);
reportException();
return len;
}
#ifdef PYTHON
jclass JCCEnv::getPythonExceptionClass() const
{
return _thr;
}
// returns true if Python exception instance was successfully restored
bool JCCEnv::restorePythonException(jthrowable throwable) const
{
#ifdef _jcc_lib // PythonException is only available in shared mode
jclass pycls = getPythonExceptionClass();
JNIEnv *vm_env = get_vm_env();
// Support through-layer exceptions by taking the active PythonException
// and making the enclosed exception visible to Python again.
if (vm_env->IsSameObject(vm_env->GetObjectClass(throwable), pycls))
{
jfieldID fid = vm_env->GetFieldID(pycls, "py_error_state", "J");
PyObject *state = (PyObject *) vm_env->GetLongField(throwable, fid);
if (state != NULL)
{
PyObject *type = PyTuple_GET_ITEM(state, 0);
PyObject *value = PyTuple_GET_ITEM(state, 1);
PyObject *tb = PyTuple_GET_ITEM(state, 2);
Py_INCREF(type);
if (value == Py_None)
value = NULL;
else
Py_INCREF(value);
if (tb == Py_None)
tb = NULL;
else
Py_INCREF(tb);
PyErr_Restore(type, value, tb);
return true;
}
}
#endif
return false;
}
#endif
void JCCEnv::reportException() const
{
JNIEnv *vm_env = get_vm_env();
jthrowable throwable = vm_env->ExceptionOccurred();
if (throwable)
{
if (!env->handlers)
vm_env->ExceptionDescribe();
#ifdef PYTHON
PythonGIL gil;
if (PyErr_Occurred())
{
/* _thr is PythonException ifdef _jcc_lib (shared mode)
* if not shared mode, _thr is RuntimeException
*/
jobject cls = (jobject) vm_env->GetObjectClass(throwable);
if (vm_env->IsSameObject(cls, _thr))
{
#ifndef _jcc_lib
/* PythonException class is not available without shared mode.
* Python exception information thus gets lost and exception
* is reported via plain Java RuntimeException.
*/
PyErr_Clear();
throw _EXC_JAVA;
#else
throw _EXC_PYTHON;
#endif
}
}
#endif
throw _EXC_JAVA;
}
}
#define DEFINE_CALL(jtype, Type) \
jtype JCCEnv::call##Type##Method(jobject obj, \
jmethodID mid, ...) const \
{ \
va_list ap; \
jtype result; \
\
va_start(ap, mid); \
result = get_vm_env()->Call##Type##MethodV(obj, mid, ap); \
va_end(ap); \
\
reportException(); \
\
return result; \
}
#define DEFINE_NONVIRTUAL_CALL(jtype, Type) \
jtype JCCEnv::callNonvirtual##Type##Method(jobject obj, jclass cls, \
jmethodID mid, ...) const \
{ \
va_list ap; \
jtype result; \
\
va_start(ap, mid); \
result = get_vm_env()->CallNonvirtual##Type##MethodV(obj, cls, \
mid, ap); \
va_end(ap); \
\
reportException(); \
\
return result; \
}
#define DEFINE_STATIC_CALL(jtype, Type) \
jtype JCCEnv::callStatic##Type##Method(jclass cls, \
jmethodID mid, ...) const \
{ \
va_list ap; \
jtype result; \
\
va_start(ap, mid); \
result = get_vm_env()->CallStatic##Type##MethodV(cls, mid, ap); \
va_end(ap); \
\
reportException(); \
\
return result; \
}
DEFINE_CALL(jobject, Object)
DEFINE_CALL(jboolean, Boolean)
DEFINE_CALL(jbyte, Byte)
DEFINE_CALL(jchar, Char)
DEFINE_CALL(jdouble, Double)
DEFINE_CALL(jfloat, Float)
DEFINE_CALL(jint, Int)
DEFINE_CALL(jlong, Long)
DEFINE_CALL(jshort, Short)
DEFINE_NONVIRTUAL_CALL(jobject, Object)
DEFINE_NONVIRTUAL_CALL(jboolean, Boolean)
DEFINE_NONVIRTUAL_CALL(jbyte, Byte)
DEFINE_NONVIRTUAL_CALL(jchar, Char)
DEFINE_NONVIRTUAL_CALL(jdouble, Double)
DEFINE_NONVIRTUAL_CALL(jfloat, Float)
DEFINE_NONVIRTUAL_CALL(jint, Int)
DEFINE_NONVIRTUAL_CALL(jlong, Long)
DEFINE_NONVIRTUAL_CALL(jshort, Short)
DEFINE_STATIC_CALL(jobject, Object)
DEFINE_STATIC_CALL(jboolean, Boolean)
DEFINE_STATIC_CALL(jbyte, Byte)
DEFINE_STATIC_CALL(jchar, Char)
DEFINE_STATIC_CALL(jdouble, Double)
DEFINE_STATIC_CALL(jfloat, Float)
DEFINE_STATIC_CALL(jint, Int)
DEFINE_STATIC_CALL(jlong, Long)
DEFINE_STATIC_CALL(jshort, Short)
void JCCEnv::callVoidMethod(jobject obj, jmethodID mid, ...) const
{
va_list ap;
va_start(ap, mid);
get_vm_env()->CallVoidMethodV(obj, mid, ap);
va_end(ap);
reportException();
}
void JCCEnv::callNonvirtualVoidMethod(jobject obj, jclass cls,
jmethodID mid, ...) const
{
va_list ap;
va_start(ap, mid);
get_vm_env()->CallNonvirtualVoidMethodV(obj, cls, mid, ap);
va_end(ap);
reportException();
}
void JCCEnv::callStaticVoidMethod(jclass cls, jmethodID mid, ...) const
{
va_list ap;
va_start(ap, mid);
get_vm_env()->CallStaticVoidMethodV(cls, mid, ap);
va_end(ap);
reportException();
}
jboolean JCCEnv::booleanValue(jobject obj) const
{
return get_vm_env()->CallBooleanMethod(obj, _mids[mid_Boolean_booleanValue]);
}
jbyte JCCEnv::byteValue(jobject obj) const
{
return get_vm_env()->CallByteMethod(obj, _mids[mid_Byte_byteValue]);
}
jchar JCCEnv::charValue(jobject obj) const
{
return get_vm_env()->CallCharMethod(obj, _mids[mid_Character_charValue]);
}
jdouble JCCEnv::doubleValue(jobject obj) const
{
return get_vm_env()->CallDoubleMethod(obj, _mids[mid_Double_doubleValue]);
}
jfloat JCCEnv::floatValue(jobject obj) const
{
return get_vm_env()->CallFloatMethod(obj, _mids[mid_Float_floatValue]);
}
jint JCCEnv::intValue(jobject obj) const
{
return get_vm_env()->CallIntMethod(obj, _mids[mid_Integer_intValue]);
}
jlong JCCEnv::longValue(jobject obj) const
{
return get_vm_env()->CallLongMethod(obj, _mids[mid_Long_longValue]);
}
jshort JCCEnv::shortValue(jobject obj) const
{
return get_vm_env()->CallShortMethod(obj, _mids[mid_Short_shortValue]);
}
jobject JCCEnv::boxBoolean(jboolean value) const
{
return get_vm_env()->NewObject(_boo, _mids[mid_Boolean_init], value);
}
jobject JCCEnv::boxByte(jbyte value) const
{
return get_vm_env()->NewObject(_byt, _mids[mid_Byte_init], value);
}
jobject JCCEnv::boxChar(jchar value) const
{
return get_vm_env()->NewObject(_cha, _mids[mid_Character_init], value);
}
jobject JCCEnv::boxDouble(jdouble value) const
{
return get_vm_env()->NewObject(_dou, _mids[mid_Double_init], value);
}
jobject JCCEnv::boxFloat(jfloat value) const
{
return get_vm_env()->NewObject(_flo, _mids[mid_Float_init], value);
}
jobject JCCEnv::boxInteger(jint value) const
{
return get_vm_env()->NewObject(_int, _mids[mid_Integer_init], value);
}
jobject JCCEnv::boxLong(jlong value) const
{
return get_vm_env()->NewObject(_lon, _mids[mid_Long_init], value);
}
jobject JCCEnv::boxShort(jshort value) const
{
return get_vm_env()->NewObject(_sho, _mids[mid_Short_init], value);
}
jmethodID JCCEnv::getMethodID(jclass cls, const char *name,
const char *signature) const
{
jmethodID id = get_vm_env()->GetMethodID(cls, name, signature);
reportException();
return id;
}
jfieldID JCCEnv::getFieldID(jclass cls, const char *name,
const char *signature) const
{
jfieldID id = get_vm_env()->GetFieldID(cls, name, signature);
reportException();
return id;
}
jmethodID JCCEnv::getStaticMethodID(jclass cls, const char *name,
const char *signature) const
{
jmethodID id = get_vm_env()->GetStaticMethodID(cls, name, signature);
reportException();
return id;
}
jobject JCCEnv::getStaticObjectField(jclass cls, const char *name,
const char *signature) const
{
JNIEnv *vm_env = get_vm_env();
jfieldID id = vm_env->GetStaticFieldID(cls, name, signature);
reportException();
return vm_env->GetStaticObjectField(cls, id);
}
#define DEFINE_GET_STATIC_FIELD(jtype, Type, signature) \
jtype JCCEnv::getStatic##Type##Field(jclass cls, \
const char *name) const \
{ \
JNIEnv *vm_env = get_vm_env(); \
jfieldID id = vm_env->GetStaticFieldID(cls, name, #signature); \
reportException(); \
return vm_env->GetStatic##Type##Field(cls, id); \
}
DEFINE_GET_STATIC_FIELD(jboolean, Boolean, Z)
DEFINE_GET_STATIC_FIELD(jbyte, Byte, B)
DEFINE_GET_STATIC_FIELD(jchar, Char, C)
DEFINE_GET_STATIC_FIELD(jdouble, Double, D)
DEFINE_GET_STATIC_FIELD(jfloat, Float, F)
DEFINE_GET_STATIC_FIELD(jint, Int, I)
DEFINE_GET_STATIC_FIELD(jlong, Long, J)
DEFINE_GET_STATIC_FIELD(jshort, Short, S)
#define DEFINE_GET_FIELD(jtype, Type) \
jtype JCCEnv::get##Type##Field(jobject obj, jfieldID id) const \
{ \
jtype value = get_vm_env()->Get##Type##Field(obj, id); \
reportException(); \
return value; \
}
DEFINE_GET_FIELD(jobject, Object)
DEFINE_GET_FIELD(jboolean, Boolean)
DEFINE_GET_FIELD(jbyte, Byte)
DEFINE_GET_FIELD(jchar, Char)
DEFINE_GET_FIELD(jdouble, Double)
DEFINE_GET_FIELD(jfloat, Float)
DEFINE_GET_FIELD(jint, Int)
DEFINE_GET_FIELD(jlong, Long)
DEFINE_GET_FIELD(jshort, Short)
#define DEFINE_SET_FIELD(jtype, Type) \
void JCCEnv::set##Type##Field(jobject obj, jfieldID id, \
jtype value) const \
{ \
get_vm_env()->Set##Type##Field(obj, id, value); \
reportException(); \
}
DEFINE_SET_FIELD(jobject, Object)
DEFINE_SET_FIELD(jboolean, Boolean)
DEFINE_SET_FIELD(jbyte, Byte)
DEFINE_SET_FIELD(jchar, Char)
DEFINE_SET_FIELD(jdouble, Double)
DEFINE_SET_FIELD(jfloat, Float)
DEFINE_SET_FIELD(jint, Int)
DEFINE_SET_FIELD(jlong, Long)
DEFINE_SET_FIELD(jshort, Short)
bool JCCEnv::setClassPath(const char *classPath)
{
JNIEnv *vm_env = get_vm_env();
jclass _ucl = (jclass) vm_env->FindClass("java/net/URLClassLoader");
jclass _fil = (jclass) vm_env->FindClass("java/io/File");
jmethodID mid = vm_env->GetStaticMethodID(_ucl, "getSystemClassLoader",
"()Ljava/lang/ClassLoader;");
jobject classLoader = vm_env->CallStaticObjectMethod(_ucl, mid);
if (!vm_env->IsInstanceOf(classLoader, _ucl))
return false;
jmethodID mf = vm_env->GetMethodID(_fil, "<init>", "(Ljava/lang/String;)V");
jmethodID mu = vm_env->GetMethodID(_fil, "toURL", "()Ljava/net/URL;");
jmethodID ma = vm_env->GetMethodID(_ucl, "addURL", "(Ljava/net/URL;)V");
#if defined(_MSC_VER) || defined(__WIN32)
const char *pathsep = ";";
char *path = _strdup(classPath);
#else
const char *pathsep = ":";
char *path = strdup(classPath);
#endif
for (char *cp = strtok(path, pathsep);
cp != NULL;
cp = strtok(NULL, pathsep)) {
jstring string = vm_env->NewStringUTF(cp);
jobject file = vm_env->NewObject(_fil, mf, string);
jobject url = vm_env->CallObjectMethod(file, mu);
vm_env->CallVoidMethod(classLoader, ma, url);
}
free(path);
return true;
}
char *JCCEnv::getClassPath()
{
JNIEnv *vm_env = get_vm_env();
jclass _ucl = (jclass) vm_env->FindClass("java/net/URLClassLoader");
jclass _url = (jclass) vm_env->FindClass("java/net/URL");
jmethodID mid = vm_env->GetStaticMethodID(_ucl, "getSystemClassLoader",
"()Ljava/lang/ClassLoader;");
jobject classLoader = vm_env->CallStaticObjectMethod(_ucl, mid);
jmethodID gu = vm_env->GetMethodID(_ucl, "getURLs", "()[Ljava/net/URL;");
jmethodID gp = vm_env->GetMethodID(_url, "getPath", "()Ljava/lang/String;");
#if defined(_MSC_VER) || defined(__WIN32)
const char *pathsep = ";";
#else
const char *pathsep = ":";
#endif
jobjectArray array = (jobjectArray)
vm_env->CallObjectMethod(classLoader, gu);
int count = array ? vm_env->GetArrayLength(array) : 0;
int first = 1, total = 0;
char *classpath = NULL;
for (int i = 0; i < count; i++) {
jobject url = vm_env->GetObjectArrayElement(array, i);
jstring path = (jstring) vm_env->CallObjectMethod(url, gp);
const char *chars = vm_env->GetStringUTFChars(path, NULL);
int size = vm_env->GetStringUTFLength(path);
total += size + 1;
if (classpath == NULL)
classpath = (char *) calloc(total, 1);
else
classpath = (char *) realloc(classpath, total);
if (classpath == NULL)
return NULL;
if (first)
first = 0;
else
strcat(classpath, pathsep);
strcat(classpath, chars);
}
return classpath;
}
jstring JCCEnv::fromUTF(const char *bytes) const
{
jstring str = get_vm_env()->NewStringUTF(bytes);
reportException();
return str;
}
char *JCCEnv::toUTF(jstring str) const
{
JNIEnv *vm_env = get_vm_env();
int len = vm_env->GetStringUTFLength(str);
char *bytes = new char[len + 1];
jboolean isCopy = 0;
const char *utf = vm_env->GetStringUTFChars(str, &isCopy);
if (!bytes)
return NULL;
memcpy(bytes, utf, len);
bytes[len] = '\0';
vm_env->ReleaseStringUTFChars(str, utf);
return bytes;
}
char *JCCEnv::toString(jobject obj) const
{
try {
return obj
? toUTF((jstring) callObjectMethod(obj, _mids[mid_obj_toString]))
: NULL;
} catch (int e) {
switch (e) {
case _EXC_PYTHON:
return NULL;
case _EXC_JAVA: {
JNIEnv *vm_env = get_vm_env();
vm_env->ExceptionDescribe();
vm_env->ExceptionClear();
return NULL;
}
default:
throw;
}
}
}
char *JCCEnv::getClassName(jobject obj) const
{
return obj
? toString(callObjectMethod(obj, _mids[mid_obj_getClass]))
: NULL;
}
#ifdef PYTHON
jstring JCCEnv::fromPyString(PyObject *object) const
{
if (object == Py_None)
return NULL;
if (PyUnicode_Check(object))
{
if (sizeof(Py_UNICODE) == sizeof(jchar))
{
jchar *buf = (jchar *) PyUnicode_AS_UNICODE(object);
jsize len = (jsize) PyUnicode_GET_SIZE(object);
return get_vm_env()->NewString(buf, len);
}
else
{
jsize len = PyUnicode_GET_SIZE(object);
Py_UNICODE *pchars = PyUnicode_AS_UNICODE(object);
jchar *jchars = new jchar[len];
jstring str;
for (int i = 0; i < len; i++)
jchars[i] = (jchar) pchars[i];
str = get_vm_env()->NewString(jchars, len);
delete[] jchars;
return str;
}
}
else if (PyString_Check(object))
return fromUTF(PyString_AS_STRING(object));
else
{
PyObject *tuple = Py_BuildValue("(sO)", "expected a string", object);
PyErr_SetObject(PyExc_TypeError, tuple);
Py_DECREF(tuple);
return NULL;
}
}
PyObject *JCCEnv::fromJString(jstring js, int delete_local_ref) const
{
if (!js)
Py_RETURN_NONE;
JNIEnv *vm_env = get_vm_env();
PyObject *string;
if (sizeof(Py_UNICODE) == sizeof(jchar))
{
jboolean isCopy;
const jchar *buf = vm_env->GetStringChars(js, &isCopy);
jsize len = vm_env->GetStringLength(js);
string = PyUnicode_FromUnicode((const Py_UNICODE *) buf, len);
vm_env->ReleaseStringChars(js, buf);
}
else
{
jsize len = vm_env->GetStringLength(js);
string = PyUnicode_FromUnicode(NULL, len);
if (string)
{
jboolean isCopy;
const jchar *jchars = vm_env->GetStringChars(js, &isCopy);
Py_UNICODE *pchars = PyUnicode_AS_UNICODE(string);
for (int i = 0; i < len; i++)
pchars[i] = (Py_UNICODE) jchars[i];
vm_env->ReleaseStringChars(js, jchars);
}
}
if (delete_local_ref)
vm_env->DeleteLocalRef((jobject) js);
return string;
}
/* may be called from finalizer thread which has no vm_env thread local */
void JCCEnv::finalizeObject(JNIEnv *jenv, PyObject *obj)
{
PythonGIL gil;
set_vm_env(jenv);
Py_DECREF(obj);
}
#endif /* PYTHON */
|