31 #if defined (HAVE_CONFIG_H)
35 #if defined (HAVE_WINDOWS_H)
68 #if defined (HAVE_JAVA)
72 #if defined (HAVE_JAVA)
76 #if defined (OCTAVE_USE_WINDOWS_API)
77 # define LIBJVM_FILE_NAME "jvm.dll"
78 #elif defined (__APPLE__)
79 # define LIBJVM_FILE_NAME "libjvm.dylib"
80 # define JAVA_HOME_CMD "/usr/libexec/java_home"
82 # define LIBJVM_FILE_NAME "libjvm.so"
85 #define TO_JOBJECT(obj) reinterpret_cast<jobject> (obj)
86 #define TO_JCLASS(obj) reinterpret_cast<jclass> (obj)
88 #define TO_JNIENV(env) reinterpret_cast<JNIEnv *> (env)
101 java_local_ref (JNIEnv *
env)
102 : m_jobj (
nullptr), m_detached (
false), m_env (
env) { }
104 java_local_ref (JNIEnv *
env, T obj)
105 : m_jobj (obj), m_detached (
false), m_env (
env) { }
107 ~java_local_ref (
void) {
release (); }
109 T& operator = (T obj)
119 operator bool ()
const {
return (m_jobj != 0); }
120 operator T () {
return m_jobj; }
122 void detach (
void) { m_detached =
true; }
132 java_local_ref (
void)
133 : m_jobj (0), m_detached (
false), m_env (0)
138 if (m_env && m_jobj && ! m_detached)
139 m_env->DeleteLocalRef (m_jobj);
156 jstring_to_string (JNIEnv *jni_env, jstring s);
159 jstring_to_string (JNIEnv *jni_env, jobject obj);
162 box (JNIEnv *jni_env,
void *jobj,
void *jcls_arg =
nullptr);
165 box_more (JNIEnv *jni_env,
void *jobj_arg,
void *jcls_arg =
nullptr);
177 JNIEXPORT jboolean JNICALL
181 JNIEXPORT
void JNICALL
184 JNIEXPORT
void JNICALL
187 JNIEXPORT
void JNICALL
190 JNIEXPORT jboolean JNICALL
197 static JavaVM *jvm =
nullptr;
204 static bool jvm_attached =
false;
210 static octave::dynamic_library jvm_lib;
212 static std::map<int, octave_value> listener_map;
213 static std::map<int, octave_value> octave_ref_map;
214 static int octave_java_refcount = 0;
220 static long octave_thread_ID = -1;
234 m_vm_args.version = JNI_VERSION_1_6;
235 m_vm_args.nOptions = 0;
236 m_vm_args.options =
nullptr;
237 m_vm_args.ignoreUnrecognized =
false;
245 JavaVMInitArgs * to_args ()
251 void add (
const std::string& opt)
253 m_java_opts.push_back (opt);
256 void read_java_opts (
const std::string& filename)
260 if (! js.bad () && ! js.fail ())
264 while (! js.eof () && ! js.fail ())
266 std::getline (js,
line);
268 if (
line.find (
'-') == 0)
269 m_java_opts.push_back (
line);
271 warning (
"invalid JVM option, skipping: %s",
line.c_str ());
280 if (m_vm_args.options !=
nullptr)
282 for (
int i = 0; i < m_vm_args.nOptions; i++)
283 delete [] m_vm_args.options[i].optionString;
285 delete [] m_vm_args.options;
287 m_vm_args.options =
nullptr;
288 m_vm_args.nOptions = 0;
296 if (m_java_opts.size () > 0)
300 m_vm_args.nOptions = m_java_opts.size ();
301 m_vm_args.options =
new JavaVMOption [m_vm_args.nOptions];
303 for (
const auto& opt : m_java_opts)
307 m_vm_args.options[index++].optionString =
strsave (opt.c_str ());
310 m_java_opts.clear ();
316 JavaVMInitArgs m_vm_args;
318 std::list<std::string> m_java_opts;
321 OCTAVE_END_NAMESPACE(
octave)
345 static std::string java_dir;
347 if (java_dir.empty ())
349 java_dir = octave::sys::env::getenv (
"OCTAVE_JAVA_DIR");
351 if (java_dir.empty ())
374 read_classpath_txt (
const std::string& filepath)
376 std::string classpath;
380 if (! fs.bad () && ! fs.fail ())
384 while (! fs.eof () && ! fs.fail ())
386 std::getline (fs,
line);
387 if (
line.length () > 0 &&
line[0] !=
'#' &&
line[0] !=
'%')
390 classpath.append (octave::directory_path::path_sep_str ());
393 int last =
line.find_last_not_of (
" \t\f\v\r\n");
422 initial_class_path ()
424 std::string java_dir = initial_java_dir ();
426 std::string retval = java_dir;
429 if (! retval.empty ())
433 std::string jar_file = java_dir + sep +
"octave.jar";
435 octave::sys::file_stat jar_exists (jar_file);
448 std::string cwd = octave::sys::env::get_current_directory ();
449 std::string home_dir = octave::sys::env::get_home_directory ();
453 std::vector<std::string> cp_list = {
"javaclasspath.txt",
457 for (std::string filename : cp_list)
459 std::string cp_file = filename;
460 octave::sys::file_stat cp_exists;
464 cp_exists = octave::sys::file_stat (cp_file);
468 std::string classpath = read_classpath_txt (cp_file);
469 retval.append (classpath);
476 cp_file =
'~' + sep + filename;
478 cp_exists = octave::sys::file_stat (cp_file);
482 std::string classpath = read_classpath_txt (cp_file);
483 retval.append (classpath);
491 cp_file = java_dir + sep + filename;
492 cp_exists = octave::sys::file_stat (cp_file);
496 std::string classpath = read_classpath_txt (cp_file);
497 retval.append (classpath);
503 error (
"octave.jar does not exist: %s", jar_file.c_str ());
506 error (
"initial java dir is empty");
512 get_jvm_lib_path_in_subdir (std::string java_home_path)
516 #if defined (OCTAVE_USE_WINDOWS_API)
517 const std::array<const std::string, 2> subdirs = {
"bin/client",
"bin/server"};
519 const std::array<const std::string, 8> subdirs =
521 "jre/lib/server",
"jre/lib",
"lib/client",
"lib/server",
522 "jre/lib/amd64/client",
"jre/lib/amd64/server",
523 "jre/lib/i386/client",
"jre/lib/i386/server"
527 for (std::size_t i = 0; i < subdirs.size (); i++)
529 std::string candidate = java_home_path +
"/" + subdirs[i]
531 if (octave::sys::file_stat (candidate))
537 #if defined (OCTAVE_USE_WINDOWS_API)
542 get_regkey_value (HKEY h_rootkey,
const std::string subkey,
544 OCTAVE_END_NAMESPACE(
octave)
547 get_jvm_lib_path_from_registry ()
550 std::string key, jversion, value;
553 key = R
"(software\javasoft\jdk)";
555 jversion = octave::sys::env::getenv ("JAVA_VERSION");
556 bool maybe_version_15_or_newer =
true;
559 if (jversion.empty ())
561 value =
"CurrentVersion";
562 retval = octave::get_regkey_value (HKEY_LOCAL_MACHINE, key, value,
565 if (retval != ERROR_SUCCESS)
568 maybe_version_15_or_newer =
false;
569 key = R
"(software\javasoft\jre)";
570 retval = octave::get_regkey_value (HKEY_LOCAL_MACHINE, key, value,
573 if (retval != ERROR_SUCCESS)
576 key = R
"(software\javasoft\java runtime environment)";
577 retval = octave::get_regkey_value (HKEY_LOCAL_MACHINE, key,
582 if (retval != ERROR_SUCCESS)
583 error (
"unable to find Java Runtime Environment: %s::%s",
584 key.c_str (), value.c_str ());
586 jversion = regval.
xstring_value (
"initialize_jvm: registry value "
587 R
"("%s" at "%s" must be a string)",
588 value.c_str (), key.c_str ());
591 std::string jvm_lib_path;
592 if (maybe_version_15_or_newer)
595 key = key +
'\\' + jversion;
597 retval = octave::get_regkey_value (HKEY_LOCAL_MACHINE, key, value,
600 if (retval != ERROR_SUCCESS)
601 error (
"unable to find Java Runtime Environment: %s::%s",
602 key.c_str (), value.c_str ());
605 = regval.
xstring_value (R
"(initialize_jvm: registry value "%s" at )"
606 R"("%s" must be a string)",
607 value.c_str (), key.c_str ())
608 + R"(\bin\server\jvm.dll)";
610 if (! jvm_lib_path.empty ())
616 key = R
"(software\javasoft\jre\)" + jversion;
617 value = "RuntimeLib";
618 retval = octave::get_regkey_value (HKEY_LOCAL_MACHINE, key, value,
621 if (retval != ERROR_SUCCESS)
624 key = R
"(software\javasoft\java runtime environment\)" + jversion;
625 retval = octave::get_regkey_value (HKEY_LOCAL_MACHINE, key, value,
629 if (retval != ERROR_SUCCESS)
630 error (
"unable to find Java Runtime Environment: %s::%s",
631 key.c_str (), value.c_str ());
634 = regval.
xstring_value (R
"(initialize_jvm: registry value "%s" at )"
635 R"("%s" must be a string)",
636 value.c_str (), key.c_str ());
638 if (jvm_lib_path.empty ())
639 error (
"unable to find Java Runtime Environment: %s::%s",
640 key.c_str (), value.c_str ());
679 const char *static_locale = setlocale (LC_ALL,
nullptr);
682 locale = std::string (static_locale);
684 octave::dynamic_library lib (
"");
685 std::string jvm_lib_path;
689 (lib.search (
"JNI_CreateJavaVM"));
691 (lib.search (
"JNI_GetCreatedJavaVMs"));
693 if (create_vm && get_vm)
694 jvm_lib_path =
"linked in or loaded libraries";
698 std::string java_home_env = octave::sys::env::getenv (
"JAVA_HOME");
699 if (! java_home_env.empty ())
701 jvm_lib_path = get_jvm_lib_path_in_subdir (java_home_env);
705 if (jvm_lib_path.empty ())
709 # if defined (__APPLE__)
711 if (jvm_lib_path.empty ())
713 octave::sys::file_stat libexec_java_home_exists (JAVA_HOME_CMD);
714 if (libexec_java_home_exists)
719 std::string java_home_cmd = std::string (JAVA_HOME_CMD)
720 +
" --failfast --version 1.6+ 2>/dev/null";
722 octave::process_execution_result rslt
725 if (rslt.exit_status () == 0)
727 std::string output = rslt.stdout_output ();
728 std::string found_path = output.substr (0, output.length() - 1);
729 std::string jvm_lib_found = get_jvm_lib_path_in_subdir (found_path);
730 if (!jvm_lib_found.empty ())
731 jvm_lib_path = jvm_lib_found;
737 if (jvm_lib_path.empty ())
739 #if defined (OCTAVE_USE_WINDOWS_API)
740 jvm_lib_path = get_jvm_lib_path_from_registry ();
747 lib = octave::dynamic_library (jvm_lib_path);
750 error (
"unable to load Java Runtime Environment from %s",
751 jvm_lib_path.c_str ());
754 (lib.search (
"JNI_CreateJavaVM"));
756 (lib.search (
"JNI_GetCreatedJavaVMs"));
759 error (
"unable to find JNI_CreateJavaVM in %s", jvm_lib_path.c_str ());
762 error (
"unable to find JNI_GetCreatedJavaVMs in %s",
763 jvm_lib_path.c_str ());
769 if (get_vm (&jvm, 1, &nVMs) == 0 && nVMs > 0)
773 switch (jvm->GetEnv (
reinterpret_cast<void **
> (¤t_env),
778 JavaVMAttachArgs m_vm_args;
779 m_vm_args.version = JNI_VERSION_1_6;
780 m_vm_args.name =
const_cast<char *
> (
"octave");
781 m_vm_args.group =
nullptr;
782 if (jvm->AttachCurrentThread (
reinterpret_cast<void **
> (¤t_env),
784 error (
"JVM internal error, unable to attach octave to existing JVM");
788 error (
"JVM internal error, the required JNI version is not supported");
801 octave::JVMArgs m_vm_args;
804 m_vm_args.add (
"-Djava.class.path=" + initial_class_path ());
805 #if defined (HAVE_BROKEN_PTHREAD_STACKSIZE)
806 m_vm_args.add (
"-Djdk.lang.processReaperUseDefaultStackSize=true");
808 m_vm_args.add (
"-Xrs");
811 m_vm_args.read_java_opts (initial_java_dir () +
815 if (create_vm (&jvm, ¤t_env, m_vm_args.to_args ()) != JNI_OK)
816 error (
"unable to start Java VM in %s", jvm_lib_path.c_str ());
821 setlocale (LC_ALL, locale.c_str ());
839 jvm->DetachCurrentThread ();
841 jvm->DestroyJavaVM ();
844 jvm_attached =
false;
856 jstring_to_string (JNIEnv *jni_env, jstring s)
862 const char *cstr = jni_env->GetStringUTFChars (s,
nullptr);
864 jni_env->ReleaseStringUTFChars (s, cstr);
871 jstring_to_string (JNIEnv *jni_env, jobject obj)
877 jclass_ref cls (jni_env, jni_env->FindClass (
"java/lang/String"));
880 if (jni_env->IsInstanceOf (obj, cls))
881 retval = jstring_to_string (jni_env,
882 reinterpret_cast<jstring
> (obj));
895 static inline JNIEnv *
898 JNIEnv *
env =
nullptr;
901 jvm->GetEnv (
reinterpret_cast<void **
> (&
env), JNI_VERSION_1_6);
911 #if defined (HAVE_JAVA)
913 JNIEnv *current_env = thread_jni_env ();
915 if (current_env && m_java_object)
917 jclass_ref cls (current_env, current_env->FindClass (
"java/lang/String"));
918 return current_env->IsInstanceOf (
TO_JOBJECT (m_java_object), cls);
936 #if defined (HAVE_JAVA)
938 JNIEnv *current_env = thread_jni_env ();
940 std::string cls_cpp = cls_name;
941 std::replace (cls_cpp.begin (), cls_cpp.end (),
'.',
'/');
943 if (current_env && m_java_object)
945 jclass_ref cls (current_env, current_env->FindClass (cls_cpp.c_str ()));
946 if (current_env->ExceptionCheck ())
947 current_env->ExceptionClear ();
949 return current_env->IsInstanceOf (
TO_JOBJECT (m_java_object), cls);
955 octave_unused_parameter (cls_name);
965 #if defined (HAVE_JAVA)
968 check_exception (JNIEnv *jni_env)
977 jni_env->ExceptionDescribe ();
979 jni_env->ExceptionClear ();
981 jclass_ref jcls (jni_env, jni_env->GetObjectClass (ex));
982 jmethodID mID = jni_env->GetMethodID (jcls,
"toString",
983 "()Ljava/lang/String;");
985 reinterpret_cast<jstring
> (jni_env->CallObjectMethod (ex,
987 std::string msg = jstring_to_string (jni_env, js);
989 error (
"[java] %s", msg.c_str ());
998 find_octave_class (JNIEnv *jni_env,
const char *name)
1000 static std::string class_loader;
1001 static jclass uiClass =
nullptr;
1003 jclass jcls = jni_env->FindClass (name);
1005 if (jcls ==
nullptr)
1007 jni_env->ExceptionClear ();
1011 if (class_loader.empty ())
1014 jni_env->FindClass (
"java/lang/System"));
1015 jmethodID mID = jni_env->GetStaticMethodID
1018 "(Ljava/lang/String;)Ljava/lang/String;");
1020 jni_env->NewStringUTF (
"octave.class.loader"));
1021 js =
reinterpret_cast<jstring
> (jni_env->CallStaticObjectMethod
1022 (syscls, mID, jstring (js)));
1023 class_loader = jstring_to_string (jni_env, jstring (js));
1024 std::replace (class_loader.begin (), class_loader.end (),
1029 jni_env->FindClass (class_loader.c_str ()));
1033 jni_env->ExceptionClear ();
1036 std::replace (class_loader.begin (), class_loader.end (),
1039 jni_env->FindClass (
"org/openide/util/Lookup"));
1040 jmethodID mID = jni_env->GetStaticMethodID
1041 (jcls2,
"getDefault",
"()Lorg/openide/util/Lookup;");
1043 jni_env->CallStaticObjectMethod (jcls2, mID));
1044 mID = jni_env->GetMethodID (jcls2,
"lookup",
1045 "(Ljava/lang/Class;)Ljava/lang/Object;");
1047 jni_env->FindClass (
"java/lang/ClassLoader"));
1049 jni_env->CallObjectMethod
1050 (lObj, mID, jclass (cLoaderCls)));
1051 mID = jni_env->GetMethodID (cLoaderCls,
"loadClass",
1052 "(Ljava/lang/String;)Ljava/lang/Class;");
1054 jni_env->NewStringUTF (class_loader.c_str ()));
1055 uicls =
reinterpret_cast<jclass
>
1056 (jni_env->CallObjectMethod (cLoader, mID, jstring (js)));
1060 uiClass =
reinterpret_cast<jclass
>
1061 (jni_env->NewGlobalRef (jclass (uicls)));
1066 jmethodID mID = jni_env->GetStaticMethodID
1067 (uiClass,
"findClass",
"(Ljava/lang/String;)Ljava/lang/Class;");
1068 jstring_ref js (jni_env, jni_env->NewStringUTF (name));
1069 jcls =
reinterpret_cast<jclass
>
1070 (jni_env->CallStaticObjectMethod (uiClass, mID, jstring (js)));
1078 compute_array_dimensions (JNIEnv *jni_env, jobject obj)
1081 jclass_ref jcls (jni_env, jni_env->GetObjectClass (obj));
1082 jclass_ref ccls (jni_env, jni_env->GetObjectClass (jcls));
1083 jmethodID isArray_ID = jni_env->GetMethodID (ccls,
"isArray",
"()Z");
1084 jmethodID getComponentType_ID = jni_env->GetMethodID (ccls,
1086 "()Ljava/lang/Class;");
1092 while (jcls && jni_env->CallBooleanMethod (jcls, isArray_ID))
1094 int len = (jobj ? jni_env->GetArrayLength (jobj) : 0);
1095 if (idx >= dv.ndims ())
1098 jcls =
reinterpret_cast<jclass
>
1099 (jni_env->CallObjectMethod (jcls, getComponentType_ID));
1101 ?
reinterpret_cast<jobjectArray
> (jni_env->GetObjectArrayElement (jobj,
1115 jclass_ref ocls (jni_env, jni_env->FindClass (
"[I"));
1116 jobjectArray retval = jni_env->NewObjectArray (idx.
length (), ocls,
nullptr);
1120 for (
int i = 0; i < idx.
length (); i++)
1125 jintArray_ref i_array (jni_env, jni_env->NewIntArray (v.length ()));
1126 jint *buf = jni_env->GetIntArrayElements (i_array,
nullptr);
1130 for (
int k = 0; k < v.length (); k++)
1134 jni_env->ReleaseIntArrayElements (i_array, buf, 0);
1135 jni_env->SetObjectArrayElement (retval, i, i_array);
1137 check_exception (jni_env);
1139 catch (octave::index_exception& ie)
1142 ie.set_pos_if_unset (idx.
length (), i + 1);
1150 get_array_elements (JNIEnv *jni_env, jobject jobj,
1155 jobject_ref java_idx (jni_env, make_java_index (jni_env, idx));
1158 find_octave_class (jni_env,
1159 "org/octave/ClassHelper"));
1160 jmethodID mID = jni_env
1161 ->GetStaticMethodID (helperClass,
"arraySubsref",
1162 "(Ljava/lang/Object;[[I)Ljava/lang/Object;");
1163 resObj = jni_env->CallStaticObjectMethod
1164 (helperClass, mID, jobj, jobject (java_idx));
1167 retval = box (jni_env, resObj);
1169 retval = check_exception (jni_env);
1177 set_array_elements (JNIEnv *jni_env, jobject jobj,
1185 jobject_ref java_idx (jni_env, make_java_index (jni_env, idx));
1187 if (unbox (jni_env, rhs, rhsObj, rhsCls))
1190 find_octave_class (jni_env,
1191 "org/octave/ClassHelper"));
1192 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"arraySubsasgn",
1193 "(Ljava/lang/Object;[[ILjava/lang/Object;)" "Ljava/lang/Object;");
1194 resObj = jni_env->CallStaticObjectMethod
1195 (helperClass, mID, jobj, jobject (java_idx), jobject (rhsObj));
1199 retval = box (jni_env, resObj);
1201 retval = check_exception (jni_env);
1209 get_invoke_list (JNIEnv *jni_env,
void *jobj_arg)
1213 std::list<std::string> name_list;
1217 jclass_ref cls (jni_env, jni_env->GetObjectClass (jobj));
1218 jclass_ref ccls (jni_env, jni_env->GetObjectClass (cls));
1219 jmethodID getMethods_ID = jni_env->GetMethodID
1220 (ccls,
"getMethods",
"()[Ljava/lang/reflect/Method;");
1221 jmethodID getFields_ID = jni_env->GetMethodID
1222 (ccls,
"getFields",
"()[Ljava/lang/reflect/Field;");
1224 reinterpret_cast<jobjectArray
>
1225 (jni_env->CallObjectMethod (cls, getMethods_ID)));
1227 reinterpret_cast<jobjectArray
>
1228 (jni_env->CallObjectMethod (cls, getFields_ID)));
1229 int mLen = jni_env->GetArrayLength (mList);
1230 int fLen = jni_env->GetArrayLength (fList);
1232 jni_env->FindClass (
"java/lang/reflect/Method"));
1234 jni_env->FindClass (
"java/lang/reflect/Field"));
1235 jmethodID m_getName_ID = jni_env->GetMethodID (mCls,
"getName",
1236 "()Ljava/lang/String;");
1237 jmethodID f_getName_ID = jni_env->GetMethodID (fCls,
"getName",
1238 "()Ljava/lang/String;");
1240 for (
int i = 0; i < mLen; i++)
1242 jobject_ref meth (jni_env, jni_env->GetObjectArrayElement (mList, i));
1243 jstring_ref methName (jni_env,
reinterpret_cast<jstring
>
1244 (jni_env->CallObjectMethod (meth,
1246 name_list.push_back (jstring_to_string (jni_env, methName));
1249 for (
int i = 0; i < fLen; i++)
1252 jni_env->GetObjectArrayElement (fList, i));
1254 reinterpret_cast<jstring
>
1255 (jni_env->CallObjectMethod
1256 (field, f_getName_ID)));
1257 name_list.push_back (jstring_to_string (jni_env, fieldName));
1265 return v.sort (
true);
1269 convert_to_string (JNIEnv *jni_env, jobject m_java_object,
bool force,
1274 if (jni_env && m_java_object)
1276 jclass_ref cls (jni_env, jni_env->FindClass (
"java/lang/String"));
1278 if (jni_env->IsInstanceOf (m_java_object, cls))
1279 retval =
octave_value (jstring_to_string (jni_env, m_java_object),
1283 cls = jni_env->FindClass (
"[Ljava/lang/String;");
1285 if (jni_env->IsInstanceOf (m_java_object, cls))
1287 jobjectArray array =
reinterpret_cast<jobjectArray
> (m_java_object);
1288 int len = jni_env->GetArrayLength (array);
1291 for (
int i = 0; i <
len; i++)
1294 reinterpret_cast<jstring
>
1295 (jni_env->GetObjectArrayElement (array, i)));
1298 c(i) =
octave_value (jstring_to_string (jni_env, js), type);
1300 c(i) = check_exception (jni_env);
1307 cls = jni_env->FindClass (
"java/lang/Object");
1308 jmethodID mID = jni_env->GetMethodID (cls,
"toString",
1309 "()Ljava/lang/String;");
1311 reinterpret_cast<jstring
>
1312 (jni_env->CallObjectMethod (m_java_object,
1316 retval =
octave_value (jstring_to_string (jni_env, js), type);
1318 retval = check_exception (jni_env);
1322 error (
"unable to convert Java object to string");
1330 #define TO_JAVA(obj) dynamic_cast<octave_java *> ((obj).internal_rep ())
1344 is_auto_convertible_number (JNIEnv *jni_env, jobject jobj)
1347 cls = jni_env->FindClass (
"java/lang/Double");
1348 if (jni_env->IsInstanceOf (jobj, cls))
1350 cls = jni_env->FindClass (
"java/lang/Float");
1351 if (jni_env->IsInstanceOf (jobj, cls))
1353 cls = jni_env->FindClass (
"java/lang/Byte");
1354 if (jni_env->IsInstanceOf (jobj, cls))
1356 cls = jni_env->FindClass (
"java/lang/Short");
1357 if (jni_env->IsInstanceOf (jobj, cls))
1359 cls = jni_env->FindClass (
"java/lang/Integer");
1360 if (jni_env->IsInstanceOf (jobj, cls))
1362 cls = jni_env->FindClass (
"java/lang/Long");
1363 if (jni_env->IsInstanceOf (jobj, cls))
1390 box (JNIEnv *jni_env,
void *jobj_arg,
void *jcls_arg)
1407 cls = jni_env->FindClass (
"java/lang/Number");
1408 if (jni_env->IsInstanceOf (jobj, cls)
1409 && is_auto_convertible_number (jni_env, jobj))
1411 jmethodID
m = jni_env->GetMethodID (cls,
"doubleValue",
"()D");
1412 retval = jni_env->CallDoubleMethod (jobj,
m);
1416 cls = jni_env->FindClass (
"java/lang/Boolean");
1417 if (jni_env->IsInstanceOf (jobj, cls))
1419 jmethodID
m = jni_env->GetMethodID (cls,
"booleanValue",
"()Z");
1420 retval = (jni_env->CallBooleanMethod (jobj,
m) ? true :
false);
1424 cls = jni_env->FindClass (
"java/lang/String");
1425 if (jni_env->IsInstanceOf (jobj, cls))
1427 retval = jstring_to_string (jni_env, jobj);
1431 cls = jni_env->FindClass (
"java/lang/Character");
1432 if (jni_env->IsInstanceOf (jobj, cls))
1434 jmethodID
m = jni_env->GetMethodID (cls,
"charValue",
"()C");
1435 retval = jni_env->CallCharMethod (jobj,
m);
1440 #define BOX_PRIMITIVE_ARRAY(JAVA_TYPE, JAVA_ID, JAVA_TYPE_CAP, OCTAVE_ID) \
1441 cls = jni_env->FindClass (JAVA_ID); \
1442 if (jni_env->IsInstanceOf (jobj, cls)) \
1444 const JAVA_TYPE ## Array jarr = reinterpret_cast<JAVA_TYPE ## Array> (jobj); \
1445 const jsize len = jni_env->GetArrayLength (jarr); \
1446 OCTAVE_ID ## NDArray d (dim_vector (len, 1)); \
1447 JAVA_TYPE *buffer = reinterpret_cast<JAVA_TYPE *> (d.fortran_vec ()); \
1448 jni_env->Get ## JAVA_TYPE_CAP ## ArrayRegion (jarr, 0, len, buffer); \
1462 #undef BOX_PRIMITIVE_ARRAY
1466 cls = find_octave_class (jni_env,
"org/octave/Matrix");
1468 if (jni_env->IsInstanceOf (jobj, cls))
1470 jmethodID mID = jni_env->GetMethodID (cls,
"getDims",
"()[I");
1472 reinterpret_cast<jintArray
>
1473 (jni_env->CallObjectMethod (jobj, mID)));
1474 jint *iv_data = jni_env->GetIntArrayElements (jintArray (iv),
1477 dims.
resize (jni_env->GetArrayLength (jintArray (iv)));
1479 for (
int i = 0; i < dims.
ndims (); i++)
1480 dims(i) = iv_data[i];
1482 jni_env->ReleaseIntArrayElements (jintArray (iv), iv_data, 0);
1483 mID = jni_env->GetMethodID (cls,
"getClassName",
1484 "()Ljava/lang/String;");
1486 reinterpret_cast<jstring
>
1487 (jni_env->CallObjectMethod (jobj, mID)));
1489 std::string s = jstring_to_string (jni_env, js);
1494 mID = jni_env->GetMethodID (cls,
"toDouble",
"()[D");
1496 reinterpret_cast<jdoubleArray
>
1497 (jni_env->CallObjectMethod (jobj,
1499 jni_env->GetDoubleArrayRegion (dv, 0,
m.numel (),
1504 else if (s ==
"byte")
1509 mID = jni_env->GetMethodID (cls,
"toByte",
"()[B");
1511 reinterpret_cast<jbyteArray
>
1512 (jni_env->CallObjectMethod (jobj,
1514 jni_env->GetByteArrayRegion (dv, 0,
m.numel (),
1515 reinterpret_cast<jbyte *
>
1516 (
m.fortran_vec ()));
1523 mID = jni_env->GetMethodID (cls,
"toByte",
"()[B");
1525 reinterpret_cast<jbyteArray
>
1526 (jni_env->CallObjectMethod (jobj,
1528 jni_env->GetByteArrayRegion (dv, 0,
m.numel (),
1529 reinterpret_cast<jbyte *
>
1530 (
m.fortran_vec ()));
1535 else if (s ==
"integer")
1540 mID = jni_env->GetMethodID (cls,
"toInt",
"()[I");
1542 reinterpret_cast<jintArray
>
1543 (jni_env->CallObjectMethod (jobj,
1545 jni_env->GetIntArrayRegion (dv, 0,
m.numel (),
1546 reinterpret_cast<jint *
>
1547 (
m.fortran_vec ()));
1554 mID = jni_env->GetMethodID (cls,
"toInt",
"()[I");
1556 reinterpret_cast<jintArray
>
1557 (jni_env->CallObjectMethod (jobj,
1559 jni_env->GetIntArrayRegion (dv, 0,
m.numel (),
1560 reinterpret_cast<jint *
>
1561 (
m.fortran_vec ()));
1569 cls = find_octave_class (jni_env,
"org/octave/OctaveReference");
1570 if (jni_env->IsInstanceOf (jobj, cls))
1572 jmethodID mID = jni_env->GetMethodID (cls,
"getID",
"()I");
1573 int ID = jni_env->CallIntMethod (jobj, mID);
1574 auto it = octave_ref_map.find (ID);
1576 if (it != octave_ref_map.end ())
1577 retval = it->second;
1590 box_more (JNIEnv *jni_env,
void *jobj_arg,
void *jcls_arg)
1605 cls = jni_env->FindClass (
"[D");
1607 if (jni_env->IsInstanceOf (jobj, cls))
1609 jdoubleArray jarr =
reinterpret_cast<jdoubleArray
> (jobj);
1610 int len = jni_env->GetArrayLength (jarr);
1615 jni_env->GetDoubleArrayRegion (jarr, 0,
len,
1626 cls = jni_env->FindClass (
"[[D");
1628 if (jni_env->IsInstanceOf (jobj, cls))
1630 jobjectArray jarr =
reinterpret_cast<jobjectArray
> (jobj);
1631 int rows = jni_env->GetArrayLength (jarr);
1638 for (
int r = 0;
r < rows;
r++)
1641 reinterpret_cast<jdoubleArray
>
1642 (jni_env->GetObjectArrayElement
1647 cols = jni_env->GetArrayLength (row);
1648 m.resize (cols, rows);
1650 jni_env->GetDoubleArrayRegion
1651 (row, 0, cols,
m.fortran_vec () +
r * cols);
1653 retval =
m.transpose ();
1662 cls = jni_env->FindClass (
"[Ljava/lang/String;");
1664 if (jni_env->IsInstanceOf (jobj, cls))
1666 jobjectArray jarr =
reinterpret_cast<jobjectArray
> (jobj);
1667 int len = jni_env->GetArrayLength (jarr);
1670 for (
int i = 0; i <
len; i++)
1673 reinterpret_cast<jstring
>
1674 (jni_env->GetObjectArrayElement (jarr, i)));
1675 m(i) = jstring_to_string (jni_env, js);
1702 jcls = jni_env->GetObjectClass (jobj);
1708 jobj = jni_env->NewStringUTF (s.c_str ());
1709 jcls = jni_env->GetObjectClass (jobj);
1716 jclass_ref scls (jni_env, jni_env->FindClass (
"java/lang/String"));
1717 jobjectArray array = jni_env->NewObjectArray (
n, scls,
nullptr);
1722 (str_arr(i).c_str ()));
1723 jni_env->SetObjectArrayElement (array, i, jstr);
1727 jcls = jni_env->GetObjectClass (jobj);
1734 #define UNBOX_PRIMITIVE_ARRAY(METHOD_T, OCTAVE_T, JAVA_T, JAVA_T_CAP) \
1737 const OCTAVE_T ## NDArray v = val.METHOD_T ## array_value (); \
1738 JAVA_T ## Array jarr = jni_env->New ## JAVA_T_CAP ## Array (v.numel ()); \
1739 const JAVA_T *jv = reinterpret_cast<const JAVA_T *> (v.data ()); \
1740 jni_env->Set ## JAVA_T_CAP ## ArrayRegion (jarr, 0, v.numel (), jv); \
1741 jobj = reinterpret_cast<jobject> (jarr); \
1742 jcls = jni_env->GetObjectClass (jobj); \
1772 #undef UNBOX_PRIMITIVE_ARRAY
1779 #define UNBOX_PRIMITIVE_SCALAR(OCTAVE_T, METHOD_T, JAVA_T, JAVA_CON) \
1782 const OCTAVE_T ov = val.METHOD_T ## _value (); \
1783 jclass_ref dcls (jni_env, jni_env->FindClass (JAVA_T)); \
1784 const jfieldID fid = jni_env->GetStaticFieldID (dcls, "TYPE", "Ljava/lang/Class;"); \
1785 const jmethodID mid = jni_env->GetMethodID (dcls, "<init>", JAVA_CON); \
1786 jcls = reinterpret_cast<jclass> (jni_env->GetStaticObjectField (dcls, fid)); \
1787 jobj = jni_env->NewObject (dcls, mid, ov); \
1814 #undef UNBOX_PRIMITIVE_SCALAR
1828 jdoubleArray dv = jni_env->NewDoubleArray (
m.numel ());
1829 jni_env->SetDoubleArrayRegion (dv, 0,
m.numel (),
m.fortran_vec ());
1831 jcls = jni_env->GetObjectClass (jobj);
1837 jclass_ref mcls (jni_env, find_octave_class (jni_env,
1838 "org/octave/Matrix"));
1841 jint *iv_data = jni_env->GetIntArrayElements (jintArray (iv),
nullptr);
1843 for (
int i = 0; i < dims.
ndims (); i++)
1844 iv_data[i] = dims(i);
1846 jni_env->ReleaseIntArrayElements (jintArray (iv), iv_data, 0);
1852 jni_env->SetDoubleArrayRegion (jdoubleArray (dv), 0,
m.numel (),
1854 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([D[I)V");
1855 jobj = jni_env->NewObject (jclass (mcls), mID, jdoubleArray (dv),
1857 jcls = jni_env->GetObjectClass (jobj);
1863 jni_env->SetByteArrayRegion (jbyteArray (bv), 0,
m.numel (),
1864 reinterpret_cast<jbyte *
>
1865 (
m.fortran_vec ()));
1866 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([B[I)V");
1867 jobj = jni_env->NewObject
1868 (jclass (mcls), mID, jbyteArray (bv), jintArray (iv));
1869 jcls = jni_env->GetObjectClass (jobj);
1875 jni_env->SetByteArrayRegion (jbyteArray (bv), 0,
m.numel (),
1876 reinterpret_cast<jbyte *
>
1877 (
m.fortran_vec ()));
1878 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([B[I)V");
1879 jobj = jni_env->NewObject
1880 (jclass (mcls), mID, jbyteArray (bv), jintArray (iv));
1881 jcls = jni_env->GetObjectClass (jobj);
1887 jni_env->SetIntArrayRegion (jintArray (v), 0,
m.numel (),
1888 reinterpret_cast<jint *
>
1889 (
m.fortran_vec ()));
1890 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([I[I)V");
1891 jobj = jni_env->NewObject
1892 (jclass (mcls), mID, jintArray (v), jintArray (iv));
1893 jcls = jni_env->GetObjectClass (jobj);
1897 error (
"cannot convert matrix of type '%s'",
1903 jclass rcls = find_octave_class (jni_env,
"org/octave/OctaveReference");
1904 jmethodID mID = jni_env->GetMethodID (rcls,
"<init>",
"(I)V");
1905 int ID = octave_java_refcount++;
1907 jobj = jni_env->NewObject (rcls, mID, ID);
1909 octave_ref_map[ID] = val;
1921 jclass_ref ocls (jni_env, jni_env->FindClass (
"java/lang/Object"));
1922 jclass_ref ccls (jni_env, jni_env->FindClass (
"java/lang/Class"));
1925 jobjs = jni_env->NewObjectArray (args.
length (), ocls,
nullptr);
1928 jclss = jni_env->NewObjectArray (args.
length (), ccls,
nullptr);
1930 for (
int i = 0; i < args.
length (); i++)
1935 found = unbox (jni_env, args(i), jobj, jcls);
1939 jni_env->SetObjectArrayElement (jobjs, i, jobj);
1940 jni_env->SetObjectArrayElement (jclss, i, jcls);
1954 get_current_thread_ID (JNIEnv *jni_env)
1959 jclass_ref cls (jni_env, jni_env->FindClass (
"java/lang/Thread"));
1960 jmethodID mID = jni_env->GetStaticMethodID (cls,
"currentThread",
1961 "()Ljava/lang/Thread;");
1962 jobject_ref jthread (jni_env, jni_env->CallStaticObjectMethod (cls, mID));
1967 jclass_ref jth_cls (jni_env, jni_env->GetObjectClass (jthread));
1968 mID = jni_env->GetMethodID (jth_cls,
"getId",
"()J");
1969 long result = jni_env->CallLongMethod (jthread, mID);
1984 JNIEnv *current_env = thread_jni_env ();
1989 jclass_ref cls (current_env, find_octave_class (current_env,
1990 "org/octave/Octave"));
1991 jmethodID mID = current_env->GetStaticMethodID
1992 (cls,
"checkPendingAction",
"()V");
1993 current_env->CallStaticVoidMethod (cls, mID);
2017 JNIEnv *current_env = thread_jni_env ();
2019 octave::command_editor::add_event_hook (java_event_hook);
2021 octave_thread_ID = get_current_thread_ID (current_env);
2023 catch (
const std::string msg)
2025 error (
"%s", msg.c_str ());
2032 JNIEXPORT jboolean JNICALL
2034 jobjectArray argin, jobjectArray argout)
2036 std::string fname = jstring_to_string (
env, fcnName);
2038 int nargout =
env->GetArrayLength (argout);
2039 int nargin =
env->GetArrayLength (argin);
2043 for (
int i = 0; i < nargin; i++)
2044 varargin(i) = box (
env,
env->GetObjectArrayElement (argin, i),
nullptr);
2052 return unbox (
env, varargout, out_objs, out_clss);
2055 JNIEXPORT
void JNICALL
2058 octave_ref_map.erase (ID);
2061 JNIEXPORT
void JNICALL
2065 auto it = octave_ref_map.find (ID);
2067 if (it != octave_ref_map.end ())
2070 int len =
env->GetArrayLength (args);
2073 for (
int i = 0; i <
len; i++)
2076 oct_args(i) = box (
env, jobj,
nullptr);
2084 interp.feval (fcn, oct_args);
2088 && val.
cell_value()(0).is_function_handle ())
2093 for (
int i=1; i<c.
numel (); i++)
2094 oct_args(
len+i-1) = c(i);
2096 interp.feval (fcn, oct_args);
2099 error (
"trying to invoke non-invocable object");
2103 JNIEXPORT
void JNICALL
2108 std::string s = jstring_to_string (
env, cmd);
2110 interp.eval_string (s,
false, pstatus, 0);
2113 JNIEXPORT jboolean JNICALL
2116 return (get_current_thread_ID (
env) != octave_thread_ID);
2126 #if ! defined (HAVE_JAVA)
2136 #if defined (HAVE_JAVA)
2142 octave_unused_parameter (jobj);
2143 octave_unused_parameter (jcls);
2150 int octave_java::s_t_id (-1);
2152 const std::string octave_java::s_t_name (
"octave_java");
2157 #if defined (HAVE_JAVA)
2159 s_t_id = ti.register_type (octave_java::s_t_name,
"<unknown>",
2164 octave_unused_parameter (ti);
2172 #if defined (HAVE_JAVA)
2174 JNIEnv *current_env = thread_jni_env ();
2176 if (current_env && m_java_object)
2177 return compute_array_dimensions (current_env,
TO_JOBJECT (m_java_object));
2193 const std::list<octave_value_list>& idx,
int nargout)
2195 #if defined (HAVE_JAVA)
2200 JNIEnv *current_env = thread_jni_env ();
2205 if (type.length () > 1 && type[1] ==
'(')
2209 ovl(0) = (idx.front ())(0);
2211 auto it = idx.begin ();
2221 ovl(1) = (idx.front ())(0);
2228 retval = get_array_elements
2233 error (
"subsref: Java object cannot be indexed with %c", type[0]);
2237 if (idx.size () > 1 && type.length () > 1)
2238 retval = retval(0).next_subsref (nargout, type, idx, skip);
2244 octave_unused_parameter (type);
2245 octave_unused_parameter (idx);
2246 octave_unused_parameter (nargout);
2258 const std::list<octave_value_list>& idx,
2261 #if defined (HAVE_JAVA)
2265 JNIEnv *current_env = thread_jni_env ();
2270 if (type.length () == 1)
2276 ovl(1) = (idx.front ())(0);
2283 else if (type.length () > 2 && type[1] ==
'(')
2285 std::list<octave_value_list> new_idx;
2286 auto it = idx.begin ();
2287 new_idx.push_back (*it++);
2288 new_idx.push_back (*it++);
2291 std::list<octave_value_list> next_idx (idx);
2292 next_idx.erase (next_idx.begin ());
2293 next_idx.erase (next_idx.begin ());
2294 u(0).subsasgn (type.substr (2), next_idx, rhs);
2299 else if (type[1] ==
'.')
2303 std::list<octave_value_list> next_idx (idx);
2304 next_idx.erase (next_idx.begin ());
2305 u(0).subsasgn (type.substr (1), next_idx, rhs);
2311 error (
"invalid indexing/assignment on Java object");
2326 error (
"Java object cannot be indexed with %c", type[0]);
2334 octave_unused_parameter (type);
2335 octave_unused_parameter (idx);
2336 octave_unused_parameter (rhs);
2349 #if defined (HAVE_JAVA)
2351 JNIEnv *current_env = thread_jni_env ();
2354 return get_invoke_list (current_env,
to_java ());
2371 #if defined (HAVE_JAVA)
2373 JNIEnv *current_env = thread_jni_env ();
2383 octave_unused_parameter (force);
2384 octave_unused_parameter (type);
2404 os <<
"<Java object: " << m_java_classname << '>
';
2407 // FIXME: Need routines to actually save/load java objects through Serialize.
2411 octave_java::save_ascii (std::ostream& /* os */)
2413 warning ("save: unable to save java objects, skipping");
2419 octave_java::load_ascii (std::istream& /* is */)
2421 // Silently skip over java object that was not saved
2426 octave_java::save_binary (std::ostream& /* os */, bool /* save_as_floats */)
2428 warning ("save: unable to save java objects, skipping");
2434 octave_java::load_binary (std::istream& /* is */, bool /* swap*/,
2435 octave::mach_info::float_format /* fmt */)
2437 // Silently skip over java object that was not saved
2442 octave_java::save_hdf5 (octave_hdf5_id /* loc_id */, const char * /* name */,
2443 bool /* save_as_floats */)
2445 warning ("save: unable to save java objects, skipping");
2451 octave_java::load_hdf5 (octave_hdf5_id /* loc_id */, const char * /* name */)
2453 // Silently skip object that was not saved
2458 octave_java::do_javaMethod (void *jni_env_arg, const std::string& name,
2459 const octave_value_list& args)
2461 #if defined (HAVE_JAVA)
2463 octave_value retval;
2465 JNIEnv *jni_env = TO_JNIENV (jni_env_arg);
2469 jobjectArray_ref arg_objs (jni_env), arg_types (jni_env);
2470 if (unbox (jni_env, args, arg_objs, arg_types))
2472 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2473 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "invokeMethod",
2474 "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
2475 jstring_ref methName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2476 jobjectArray_ref resObj (jni_env,
2477 reinterpret_cast<jobjectArray> (jni_env->CallStaticObjectMethod (helperClass, mID,
2478 to_java (), jstring (methName), jobjectArray (arg_objs), jobjectArray (arg_types))));
2480 retval = box (jni_env, resObj);
2482 retval = check_exception (jni_env);
2485 octave_set_default_fpucw ();
2492 octave_unused_parameter (jni_env_arg);
2493 octave_unused_parameter (name);
2494 octave_unused_parameter (args);
2496 // This shouldn't happen because construction of
octave_java objects is
2508 #if defined (HAVE_JAVA)
2514 octave_unused_parameter (name);
2515 octave_unused_parameter (args);
2527 const std::string& class_name,
2528 const std::string& name,
2531 #if defined (HAVE_JAVA)
2535 JNIEnv *jni_env =
TO_JNIENV (jni_env_arg);
2540 if (unbox (jni_env, args, arg_objs, arg_types))
2543 find_octave_class (jni_env,
2544 "org/octave/ClassHelper"));
2545 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
2546 "invokeStaticMethod",
2547 "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
2549 jni_env->NewStringUTF (name.c_str ()));
2551 jni_env->NewStringUTF (
class_name.c_str ()));
2553 jni_env->CallStaticObjectMethod (helperClass,
2557 jobjectArray (arg_objs),
2558 jobjectArray (arg_types)));
2560 retval = box (jni_env, resObj);
2562 retval = check_exception (jni_env);
2572 octave_unused_parameter (jni_env_arg);
2574 octave_unused_parameter (name);
2575 octave_unused_parameter (args);
2587 const std::string& name,
2590 #if defined (HAVE_JAVA)
2597 octave_unused_parameter (name);
2598 octave_unused_parameter (args);
2612 #if defined (HAVE_JAVA)
2616 JNIEnv *jni_env =
TO_JNIENV (jni_env_arg);
2622 if (unbox (jni_env, args, arg_objs, arg_types))
2625 find_octave_class (jni_env,
2626 "org/octave/ClassHelper"));
2627 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
2628 "invokeConstructor",
2629 "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
2631 jni_env->NewStringUTF (name.c_str ()));
2633 jni_env->CallStaticObjectMethod (helperClass,
2636 jobjectArray (arg_objs),
2637 jobjectArray (arg_types)));
2642 check_exception (jni_env);
2652 octave_unused_parameter (jni_env_arg);
2653 octave_unused_parameter (name);
2654 octave_unused_parameter (args);
2668 #if defined (HAVE_JAVA)
2674 octave_unused_parameter (name);
2675 octave_unused_parameter (args);
2688 #if defined (HAVE_JAVA)
2692 JNIEnv *jni_env =
TO_JNIENV (jni_env_arg);
2697 find_octave_class (jni_env,
2698 "org/octave/ClassHelper"));
2699 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"getField",
2700 "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
2701 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2703 jni_env->CallStaticObjectMethod (helperClass,
2709 retval = box (jni_env, resObj);
2711 retval = check_exception (jni_env);
2720 octave_unused_parameter (jni_env_arg);
2721 octave_unused_parameter (name);
2734 #if defined (HAVE_JAVA)
2740 octave_unused_parameter (name);
2752 const std::string& name)
2754 #if defined (HAVE_JAVA)
2758 JNIEnv *jni_env =
TO_JNIENV (jni_env_arg);
2763 find_octave_class (jni_env,
2764 "org/octave/ClassHelper"));
2765 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
2767 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
2769 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2771 jni_env->CallStaticObjectMethod (helperClass, mID,
2775 retval = box (jni_env, resObj);
2777 retval = check_exception (jni_env);
2786 octave_unused_parameter (jni_env_arg);
2788 octave_unused_parameter (name);
2800 const std::string& name)
2802 #if defined (HAVE_JAVA)
2809 octave_unused_parameter (name);
2823 #if defined (HAVE_JAVA)
2827 JNIEnv *jni_env =
TO_JNIENV (jni_env_arg);
2834 if (unbox (jni_env, val, jobj, jcls))
2837 find_octave_class (jni_env,
2838 "org/octave/ClassHelper"));
2839 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"setField",
2840 "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");
2841 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2842 jni_env->CallStaticObjectMethod (helperClass, mID,
to_java (),
2843 jstring (fName), jobject (jobj));
2844 check_exception (jni_env);
2854 octave_unused_parameter (jni_env_arg);
2855 octave_unused_parameter (name);
2856 octave_unused_parameter (val);
2869 #if defined (HAVE_JAVA)
2871 return do_java_set (thread_jni_env (), name, val);
2875 octave_unused_parameter (name);
2876 octave_unused_parameter (val);
2890 #if defined (HAVE_JAVA)
2894 JNIEnv *jni_env =
TO_JNIENV (jni_env_arg);
2901 if (unbox (jni_env, val, jobj, jcls))
2904 find_octave_class (jni_env,
2905 "org/octave/ClassHelper"));
2906 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
2908 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V");
2910 jni_env->NewStringUTF (
class_name.c_str ()));
2911 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2912 jni_env->CallStaticObjectMethod (helperClass, mID, jstring (cName),
2913 jstring (fName), jobject (jobj));
2914 check_exception (jni_env);
2924 octave_unused_parameter (jni_env_arg);
2926 octave_unused_parameter (name);
2927 octave_unused_parameter (val);
2939 const std::string& name,
2942 #if defined (HAVE_JAVA)
2949 octave_unused_parameter (name);
2950 octave_unused_parameter (val);
2961 octave_java::init (
void *jobj_arg,
void *jcls_arg)
2963 #if defined (HAVE_JAVA)
2968 JNIEnv *current_env = thread_jni_env ();
2973 m_java_object = current_env->NewGlobalRef (jobj);
2976 m_java_class = current_env->NewGlobalRef (jcls);
2977 else if (m_java_object)
2980 current_env->GetObjectClass(
TO_JOBJECT (m_java_object)));
2981 m_java_class = current_env->NewGlobalRef (jclass (ocls));
2987 current_env->GetObjectClass (
TO_JCLASS (m_java_class)));
2988 jmethodID mID = current_env->GetMethodID (clsCls,
2990 "()Ljava/lang/String;");
2992 current_env->CallObjectMethod (
TO_JCLASS (m_java_class), mID));
2993 m_java_classname = jstring_to_string (current_env, resObj);
2999 octave_unused_parameter (jobj_arg);
3000 octave_unused_parameter (jcls_arg);
3011 octave_java::release ()
3013 #if defined (HAVE_JAVA)
3015 JNIEnv *current_env = thread_jni_env ();
3020 current_env->DeleteGlobalRef (
TO_JOBJECT (m_java_object));
3023 current_env->DeleteGlobalRef (
TO_JCLASS (m_java_class));
3025 m_java_object =
nullptr;
3026 m_java_class =
nullptr;
3044 DEFUN (__java_init__, , ,
3056 #if defined (HAVE_JAVA)
3073 DEFUN (__java_exit__, , ,
3081 #if defined (HAVE_JAVA)
3094 DEFUN (javaObject, args, ,
3114 #if defined (HAVE_JAVA)
3119 std::string classname = args(0).xstring_value (
"javaObject: CLASSNAME must be a string");
3123 JNIEnv *current_env = thread_jni_env ();
3126 for (
int i=1; i<args.
length (); i++)
3133 octave_unused_parameter (args);
3149 DEFUN (javaMethod, args, ,
3175 #if defined (HAVE_JAVA)
3180 std::string methodname = args(0).xstring_value (
"javaMethod: METHODNAME must be a string");
3184 JNIEnv *current_env = thread_jni_env ();
3189 for (
int i=2; i<args.
length (); i++)
3192 if (args(1).isjava ())
3195 retval = jobj->
do_javaMethod (current_env, methodname, tmp);
3197 else if (args(1).is_string ())
3199 std::string cls = args(1).string_value ();
3203 error (
"javaMethod: OBJ must be a Java object or a string");
3209 octave_unused_parameter (args);
3228 DEFUN (__java_get__, args, ,
3250 #if defined (HAVE_JAVA)
3255 std::string name = args(1).xstring_value (
"__java_get__: NAME must be a string");
3259 JNIEnv *current_env = thread_jni_env ();
3263 if (args(0).isjava ())
3268 else if (args(0).is_string ())
3270 std::string cls = args(0).string_value ();
3274 error (
"__java_get__: OBJ must be a Java object or a string");
3280 octave_unused_parameter (args);
3287 DEFUN (__java_set__, args, ,
3310 #if defined (HAVE_JAVA)
3315 std::string name = args(1).xstring_value (
"__java_set__: NAME must be a string");
3319 JNIEnv *current_env = thread_jni_env ();
3323 if (args(0).isjava ())
3326 retval = jobj->
do_java_set (current_env, name, args(2));
3328 else if (args(0).is_string ())
3330 std::string cls = args(0).string_value ();
3334 error (
"__java_set__: OBJ must be a Java object or a string");
3340 octave_unused_parameter (args);
3347 DEFUN (__java2mat__, args, ,
3353 #if defined (HAVE_JAVA)
3360 JNIEnv *current_env = thread_jni_env ();
3364 if (args(0).isjava ())
3367 retval =
ovl (box_more (current_env, jobj->
to_java (),
nullptr));
3370 retval =
ovl (args(0));
3376 octave_unused_parameter (args);
3383 DEFUN (java_matrix_autoconversion, args, nargout,
3399 #if defined (HAVE_JAVA)
3402 "java_matrix_autoconversion");
3406 octave_unused_parameter (args);
3407 octave_unused_parameter (nargout);
3414 DEFUN (java_unsigned_autoconversion, args, nargout,
3431 #if defined (HAVE_JAVA)
3434 "java_unsigned_autoconversion");
3438 octave_unused_parameter (args);
3439 octave_unused_parameter (nargout);
3446 DEFUN (debug_java, args, nargout,
3461 #if defined (HAVE_JAVA)
3467 octave_unused_parameter (args);
3468 octave_unused_parameter (nargout);
3479 DEFUN (isjava, args, ,
3489 return ovl (args(0).isjava ());
3620 OCTAVE_END_NAMESPACE(
octave)
octave_value_list F__java_set__(const octave_value_list &=octave_value_list(), int=0)
octave_value_list FjavaMethod(const octave_value_list &=octave_value_list(), int=0)
octave_value_list F__java_get__(const octave_value_list &=octave_value_list(), int=0)
octave_idx_type numel() const
Number of elements in the array.
Vector representing the dimensions (size) of an Array.
void resize(int n, int fill_value=0)
octave_idx_type ndims() const
Number of dimensions.
void newline(std::ostream &os) const
static void register_type()
octave::refcount< octave_idx_type > m_count
friend class octave_value
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
std::string class_name() const
octave_value do_java_set(void *jni_env, const std::string &name, const octave_value &val)
void print(std::ostream &os, bool pr_as_read_syntax=false)
string_vector map_keys() const
bool is_java_string() const
octave_value do_javaMethod(void *jni_env, const std::string &name, const octave_value_list &args)
octave_value convert_to_str_internal(bool pad, bool force, char type) const
octave_value do_java_get(void *jni_env, const std::string &name)
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
bool is_instance_of(const std::string &) const
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
static octave_value do_javaObject(void *jni_env, const std::string &name, const octave_value_list &args)
octave_value_list & append(const octave_value &val)
octave_idx_type length() const
bool is_function_handle() const
bool is_undefined() const
bool is_uint32_type() const
std::string class_name() const
bool is_real_scalar() const
int32NDArray int32_array_value() const
int8NDArray int8_array_value() const
octave_idx_type rows() const
bool is_int8_type() const
bool is_uint8_type() const
bool is_uint64_type() const
bool is_uint16_type() const
octave_function * function_value(bool silent=false) const
bool is_int16_type() const
octave_idx_type numel() const
bool is_real_matrix() const
std::string string_value(bool force=false) const
bool is_int64_type() const
bool is_matrix_type() const
NDArray array_value(bool frc_str_conv=false) const
octave_idx_type length() const
bool is_double_type() const
bool is_int32_type() const
bool is_bool_scalar() const
octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
uint8NDArray uint8_array_value() const
std::string xstring_value(const char *fmt,...) const
octave_idx_type columns() const
Matrix matrix_value(bool frc_str_conv=false) const
Array< std::string > cellstr_value() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void warning(const char *fmt,...)
void() error(const char *fmt,...)
#define panic_impossible()
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
void octave_set_default_fpucw(void)
octave::idx_vector idx_vector
interpreter & __get_interpreter__()
F77_RET_T const F77_INT F77_CMPLX const F77_INT F77_CMPLX * B
F77_RET_T const F77_INT const F77_INT const F77_INT F77_DBLE const F77_INT F77_DBLE const F77_INT F77_DBLE const F77_INT F77_DBLE * Z
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
char * strsave(const char *s)
std::string fcn_file_dir()
std::string dir_sep_str()
std::string tilde_expand(const std::string &name)
octave_value set_internal_variable(bool &var, const octave_value_list &args, int nargout, const char *nm)
process_execution_result run_command_and_return_output(const std::string &cmd_str)
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
#define BOX_PRIMITIVE_ARRAY(JAVA_TYPE, JAVA_ID, JAVA_TYPE_CAP, OCTAVE_ID)
JNIEXPORT jboolean JNICALL Java_org_octave_Octave_call(JNIEnv *, jclass, jstring, jobjectArray, jobjectArray)
bool Vjava_unsigned_autoconversion
java_local_ref< jthrowable > jthrowable_ref
#define UNBOX_PRIMITIVE_SCALAR(OCTAVE_T, METHOD_T, JAVA_T, JAVA_CON)
JNIEXPORT jboolean JNICALL Java_org_octave_Octave_needThreadedInvokation(JNIEnv *, jclass)
java_local_ref< jdoubleArray > jdoubleArray_ref
java_local_ref< jobjectArray > jobjectArray_ref
java_local_ref< jclass > jclass_ref
jint(JNICALL * JNI_CreateJavaVM_t)(JavaVM **pvm, JNIEnv **penv, void *args)
java_local_ref< jbyteArray > jbyteArray_ref
JNIEXPORT void JNICALL Java_org_octave_Octave_doInvoke(JNIEnv *, jclass, jint, jobjectArray)
JNIEXPORT void JNICALL Java_org_octave_Octave_doEvalString(JNIEnv *, jclass, jstring)
java_local_ref< jobject > jobject_ref
bool Vjava_matrix_autoconversion
#define UNBOX_PRIMITIVE_ARRAY(METHOD_T, OCTAVE_T, JAVA_T, JAVA_T_CAP)
JNIEXPORT void JNICALL Java_org_octave_OctaveReference_doFinalize(JNIEnv *, jclass, jint)
java_local_ref< jstring > jstring_ref
java_local_ref< jintArray > jintArray_ref
jint(JNICALL * JNI_GetCreatedJavaVMs_t)(JavaVM **pvm, jsize bufLen, jsize *nVMs)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.