31 #if HAVE_FPU_CONTROL_H
32 #include <fpu_control.h>
37 #if defined (HAVE_WINDOWS_H)
61 typedef jint (JNICALL *JNI_CreateJavaVM_t) (JavaVM **pvm, JNIEnv **penv,
63 typedef jint (JNICALL *JNI_GetCreatedJavaVMs_t) (JavaVM **pvm, jsize bufLen,
68 JNIEXPORT jboolean JNICALL
69 Java_org_octave_Octave_call (JNIEnv *, jclass, jstring, jobjectArray,
71 JNIEXPORT
void JNICALL
72 Java_org_octave_OctaveReference_doFinalize (JNIEnv *, jclass, jint);
74 JNIEXPORT
void JNICALL
75 Java_org_octave_Octave_doInvoke (JNIEnv *, jclass, jint, jobjectArray);
77 JNIEXPORT
void JNICALL
78 Java_org_octave_Octave_doEvalString (JNIEnv *, jclass, jstring);
80 JNIEXPORT jboolean JNICALL
81 Java_org_octave_Octave_needThreadedInvokation (JNIEnv *, jclass);
84 static JavaVM *jvm = 0;
85 static bool jvm_attached =
false;
90 static std::map<int,octave_value> listener_map;
91 static std::map<int,octave_value> octave_ref_map;
92 static int octave_java_refcount = 0;
93 static long octave_thread_ID = -1;
105 vm_args.version = JNI_VERSION_1_2;
106 vm_args.nOptions = 0;
108 vm_args.ignoreUnrecognized =
false;
116 JavaVMInitArgs* to_args ()
122 void add (
const std::string& opt)
124 java_opts.push_back (opt);
127 void read_java_opts (
const std::string& filename)
129 std::ifstream js (filename.c_str ());
131 if (! js.bad () && ! js.fail ())
135 while (! js.eof () && ! js.fail ())
137 std::getline (js, line);
138 if (line.length () > 2
139 && (line.find (
"-D") == 0 || line.find (
"-X") == 0))
140 java_opts.push_back (line);
142 std::cerr <<
"invalid JVM option, skipping: " << line << std::endl;
151 if (vm_args.options != 0)
153 for (
int i = 0; i < vm_args.nOptions; i++)
154 delete [] vm_args.options[i].optionString;
155 delete [] vm_args.options;
158 vm_args.nOptions = 0;
166 if (java_opts.size () > 0)
170 vm_args.nOptions = java_opts.size ();
171 vm_args.options =
new JavaVMOption [vm_args.nOptions];
172 for (std::list<std::string>::const_iterator it = java_opts.begin ();
173 it != java_opts.end (); ++it)
176 std::cout << *it << std::endl;
177 vm_args.options[index++].optionString =
strsave ((*it).c_str ());
185 JavaVMInitArgs vm_args;
187 std::list<std::string> java_opts;
192 read_registry_string (
const std::string& key,
const std::string& value)
199 if (! RegOpenKeyEx (HKEY_LOCAL_MACHINE, key.c_str (), 0, KEY_READ, &hkey))
201 if (! RegQueryValueEx (hkey, value.c_str (), 0, 0, 0, &len))
204 if (RegQueryValueEx (hkey, value.c_str (), 0, 0,
205 (LPBYTE)&retval[0], &len))
207 else if (retval[len-1] ==
'\0')
208 retval.resize (--len);
217 get_module_filename (HMODULE hMod)
220 std::string retval (n,
'\0');
225 int status = GetModuleFileName(hMod, &retval[0], n);
239 return (found ? retval :
"");
243 set_dll_directory (
const std::string& dir =
"")
245 typedef BOOL (WINAPI *dllfcn_t) (LPCTSTR path);
247 static dllfcn_t dllfcn = 0;
248 static bool first =
true;
250 if (! dllfcn && first)
252 HINSTANCE hKernel32 = GetModuleHandle (
"kernel32");
253 dllfcn =
reinterpret_cast<dllfcn_t
> (GetProcAddress (hKernel32,
254 "SetDllDirectoryA"));
259 dllfcn (dir.empty () ? 0 : dir.c_str ());
264 initial_java_dir (
void)
266 static std::string java_dir;
268 if (java_dir.empty ())
272 if (java_dir.empty ())
291 read_classpath_txt (
const std::string& filepath)
293 std::string classpath;
295 std::ifstream fs (filepath.c_str ());
297 if (! fs.bad () && ! fs.fail ())
301 while (! fs.eof () && ! fs.fail ())
303 std::getline (fs, line);
305 if (line.length () > 0)
307 if (line[0] ==
'#' || line[0] ==
'%')
315 int last = line.find_last_not_of (
" \t\f\v\r\n");
327 initial_class_path (
void)
329 std::string java_dir = initial_java_dir ();
331 std::string retval = java_dir;
334 if (! retval.empty ())
338 std::string jar_file = java_dir + sep +
"octave.jar";
355 std::string cp_list[] = {
"javaclasspath.txt",
"classpath.txt"};
357 for (
int i=0; i<2; i++)
359 std::string filename = cp_list[i];
360 std::string cp_file = filename;
369 std::string classpath = read_classpath_txt (cp_file);
370 retval.append (classpath);
375 cp_file =
"~" + sep + filename;
381 std::string classpath = read_classpath_txt (cp_file);
382 retval.append (classpath);
387 cp_file = java_dir + sep + filename;
392 std::string classpath = read_classpath_txt (cp_file);
393 retval.append (classpath);
398 throw std::string (
"octave.jar does not exist: ") + jar_file;
401 throw std::string (
"initial java dir is empty");
407 #if defined __i386__ || defined __x86_64__
408 #define _FPU_DEFAULT 0x037f
410 #define _FPU_DEFAULT 0
415 restore_fpu_state (
void)
417 fpucw_t cw = GET_FPUCW ();
418 if (cw != _FPU_DEFAULT)
419 SET_FPUCW (_FPU_DEFAULT);
423 initialize_jvm (
void)
430 const char *static_locale = setlocale (LC_ALL, 0);
431 const std::string locale (static_locale);
433 #if defined (__WIN32__)
435 HMODULE hMod = GetModuleHandle (
"jvm.dll");
436 std::string jvm_lib_path;
443 jvm_lib_path = get_module_filename (hMod);
445 if (jvm_lib_path.empty ())
446 throw std::string (
"unable to find Java Runtime Environment");
452 std::string key, value;
454 key =
"software\\javasoft\\java runtime environment";
459 value =
"Currentversion";
460 std::string regval = read_registry_string (key,value);
463 throw std::string (
"unable to find Java Runtime Environment: ")
464 + key +
"::" + value;
468 key = key +
"\\" + value;
469 value =
"RuntimeLib";
470 jvm_lib_path = read_registry_string (key, value);
471 if (jvm_lib_path.empty ())
472 throw std::string (
"unable to find Java Runtime Environment: ")
473 + key +
"::" + value;
475 std::string jvm_bin_path;
478 jvm_bin_path = read_registry_string (key, value);
479 if (! jvm_bin_path.empty ())
481 jvm_bin_path = (jvm_bin_path + std::string (
"\\bin"));
485 set_dll_directory (jvm_bin_path);
490 #else // Not Win32 system
493 #if defined (__APPLE__)
494 std::string jvm_lib_path = JAVA_LDPATH + std::string (
"/libjvm.dylib");
496 std::string jvm_lib_path = JAVA_LDPATH + std::string (
"/libjvm.so");
503 # if !defined (__APPLE__) && !defined (__MACH__)
508 throw std::string (
"unable to load Java Runtime Environment from ")
511 #if defined (__WIN32__)
513 set_dll_directory ();
515 if (! old_cwd.empty ())
520 JNI_CreateJavaVM_t create_vm =
521 reinterpret_cast<JNI_CreateJavaVM_t
> (lib.search (
"JNI_CreateJavaVM"));
522 JNI_GetCreatedJavaVMs_t get_vm =
523 reinterpret_cast<JNI_GetCreatedJavaVMs_t
> (lib.search (
"JNI_GetCreatedJavaVMs"));
526 throw std::string (
"unable to find JNI_CreateJavaVM in ") + jvm_lib_path;
529 throw std::string (
"unable to find JNI_GetCreatedJavaVMs in ")
532 if (get_vm (&jvm, 1, &nVMs) == 0 && nVMs > 0)
540 if (JNI_GetCreatedJavaVMs (&jvm, 1, &nVMs) == 0 && nVMs > 0)
547 switch (jvm->GetEnv (reinterpret_cast<void **> (¤t_env),
552 JavaVMAttachArgs vm_args;
553 vm_args.version = JNI_VERSION_1_2;
554 vm_args.name =
const_cast<char *
> (
"octave");
556 if (jvm->AttachCurrentThread (reinterpret_cast<void **> (¤t_env),
558 throw std::string (
"JVM internal error, unable to attach octave to existing JVM");
562 throw std::string (
"JVM internal error, the required JNI version is not supported");
579 vm_args.add (
"-Djava.class.path=" + initial_class_path ());
580 vm_args.add (
"-Xrs");
581 vm_args.add (
"-Djava.system.class.loader=org.octave.OctClassLoader");
585 # if !defined (__APPLE__) && !defined (__MACH__)
587 if (create_vm (&jvm, ¤t_env, vm_args.to_args ()) != JNI_OK)
588 throw std::string (
"unable to start Java VM in ")+jvm_lib_path;
596 if (JNI_CreateJavaVM (&jvm, reinterpret_cast<void **> (¤t_env),
597 vm_args.to_args ()) != JNI_OK)
598 throw std::string (
"unable to start Java VM in ")+jvm_lib_path;
604 setlocale (LC_ALL, locale.c_str ());
613 jvm->DetachCurrentThread ();
615 jvm->DestroyJavaVM ();
618 jvm_attached =
false;
623 restore_fpu_state ();
634 const char *cstr = jni_env->GetStringUTFChars (s, 0);
636 jni_env->ReleaseStringUTFChars (s, cstr);
649 jclass_ref cls (jni_env, jni_env->FindClass (
"java/lang/String"));
652 if (jni_env->IsInstanceOf (obj, cls))
654 reinterpret_cast<jstring> (obj));
668 jclass_ref cls (current_env, current_env->FindClass (
"java/lang/String"));
669 return current_env->IsInstanceOf (
java_object, cls);
680 std::string cls_cpp = cls_name;
681 std::replace (cls_cpp.begin (), cls_cpp.end (),
'.',
'/');
685 jclass_ref cls (current_env, current_env->FindClass (cls_cpp.c_str ()));
686 if (current_env->ExceptionCheck ())
687 current_env->ExceptionClear();
689 return current_env->IsInstanceOf (
java_object, cls);
695 check_exception (JNIEnv* jni_env)
704 jni_env->ExceptionDescribe ();
706 jni_env->ExceptionClear ();
708 jclass_ref jcls (jni_env, jni_env->GetObjectClass (ex));
709 jmethodID mID = jni_env->GetMethodID (jcls,
"toString",
710 "()Ljava/lang/String;");
712 reinterpret_cast<jstring> (jni_env->CallObjectMethod (ex, mID)));
715 error (
"[java] %s", msg.c_str ());
724 find_octave_class (JNIEnv *jni_env,
const char *name)
726 static std::string class_loader;
727 static jclass uiClass = 0;
729 jclass jcls = jni_env->FindClass (name);
733 jni_env->ExceptionClear ();
737 if (class_loader.empty ())
740 jni_env->FindClass (
"java/lang/System"));
741 jmethodID mID = jni_env->GetStaticMethodID (syscls,
"getProperty",
"(Ljava/lang/String;)Ljava/lang/String;");
742 jstring_ref js (jni_env, jni_env->NewStringUTF (
"octave.class.loader"));
743 js =
reinterpret_cast<jstring
> (jni_env->CallStaticObjectMethod (syscls, mID, jstring (js)));
745 std::replace (class_loader.begin (), class_loader.end (),
749 jclass_ref uicls (jni_env, jni_env->FindClass (class_loader.c_str ()));
753 jni_env->ExceptionClear ();
756 std::replace (class_loader.begin (), class_loader.end (),
758 jclass_ref jcls2 (jni_env, jni_env->FindClass (
"org/openide/util/Lookup"));
759 jmethodID mID = jni_env->GetStaticMethodID (jcls2,
"getDefault",
"()Lorg/openide/util/Lookup;");
760 jobject_ref lObj (jni_env, jni_env->CallStaticObjectMethod (jcls2, mID));
761 mID = jni_env->GetMethodID (jcls2,
"lookup",
762 "(Ljava/lang/Class;)Ljava/lang/Object;");
763 jclass_ref cLoaderCls (jni_env, jni_env->FindClass (
"java/lang/ClassLoader"));
764 jobject_ref cLoader (jni_env, jni_env->CallObjectMethod (lObj, mID, jclass (cLoaderCls)));
765 mID = jni_env->GetMethodID (cLoaderCls,
"loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;");
766 jstring_ref js (jni_env, jni_env->NewStringUTF (class_loader.c_str ()));
767 uicls =
reinterpret_cast<jclass
> (jni_env->CallObjectMethod (cLoader, mID, jstring (js)));
771 uiClass =
reinterpret_cast<jclass
> (jni_env->NewGlobalRef (jclass (uicls)));
776 jmethodID mID = jni_env->GetStaticMethodID (uiClass,
"findClass",
"(Ljava/lang/String;)Ljava/lang/Class;");
777 jstring_ref js (jni_env, jni_env->NewStringUTF (name));
778 jcls =
reinterpret_cast<jclass
> (jni_env->CallStaticObjectMethod (uiClass, mID, jstring (js)));
786 compute_array_dimensions (JNIEnv* jni_env, jobject obj)
789 jclass_ref jcls (jni_env, jni_env->GetObjectClass (obj));
790 jclass_ref ccls (jni_env, jni_env->GetObjectClass (jcls));
791 jmethodID isArray_ID = jni_env->GetMethodID (ccls,
"isArray",
"()Z");
792 jmethodID getComponentType_ID = jni_env->GetMethodID (ccls,
"getComponentType",
"()Ljava/lang/Class;");
798 while (jcls && jni_env->CallBooleanMethod (jcls, isArray_ID))
800 int len = (jobj ? jni_env->GetArrayLength (jobj) : 0);
801 if (idx >= dv.length ())
804 jcls =
reinterpret_cast<jclass
> (jni_env->CallObjectMethod (jcls, getComponentType_ID));
805 jobj = (len > 0 ?
reinterpret_cast<jobjectArray
> (jni_env->GetObjectArrayElement (jobj, 0)) : 0);
809 restore_fpu_state ();
817 jclass_ref ocls (jni_env, jni_env->FindClass (
"[I"));
818 jobjectArray retval = jni_env->NewObjectArray (idx.
length (), ocls, 0);
820 for (
int i = 0; i < idx.
length (); i++)
827 jint *buf = jni_env->GetIntArrayElements (i_array, 0);
829 for (
int k = 0; k < v.
length (); k++)
832 jni_env->ReleaseIntArrayElements (i_array, buf, 0);
833 jni_env->SetObjectArrayElement (retval, i, i_array);
835 check_exception (jni_env);
848 get_array_elements (JNIEnv* jni_env, jobject jobj,
853 jobject_ref java_idx (jni_env, make_java_index (jni_env, idx));
857 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
858 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"arraySubsref",
"(Ljava/lang/Object;[[I)Ljava/lang/Object;");
859 resObj = jni_env->CallStaticObjectMethod (helperClass, mID, jobj, jobject (java_idx));
863 retval =
box (jni_env, resObj);
865 retval = check_exception (jni_env);
867 restore_fpu_state ();
873 set_array_elements (JNIEnv* jni_env, jobject jobj,
881 jobject_ref java_idx (jni_env, make_java_index (jni_env, idx));
885 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
886 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"arraySubsasgn",
887 "(Ljava/lang/Object;[[ILjava/lang/Object;)Ljava/lang/Object;");
888 resObj = jni_env->CallStaticObjectMethod (helperClass, mID,
889 jobj, jobject (java_idx), jobject (rhsObj));
893 retval =
box (jni_env, resObj);
895 retval = check_exception (jni_env);
897 restore_fpu_state ();
903 get_invoke_list (JNIEnv* jni_env, jobject jobj)
905 std::list<std::string> name_list;
909 jclass_ref cls (jni_env, jni_env->GetObjectClass (jobj));
910 jclass_ref ccls (jni_env, jni_env->GetObjectClass (cls));
911 jmethodID getMethods_ID = jni_env->GetMethodID (ccls,
"getMethods",
"()[Ljava/lang/reflect/Method;");
912 jmethodID getFields_ID = jni_env->GetMethodID (ccls,
"getFields",
"()[Ljava/lang/reflect/Field;");
913 jobjectArray_ref mList (jni_env, reinterpret_cast<jobjectArray> (jni_env->CallObjectMethod (cls, getMethods_ID)));
914 jobjectArray_ref fList (jni_env, reinterpret_cast<jobjectArray> (jni_env->CallObjectMethod (cls, getFields_ID)));
915 int mLen = jni_env->GetArrayLength (mList);
916 int fLen = jni_env->GetArrayLength (fList);
917 jclass_ref mCls (jni_env, jni_env->FindClass (
"java/lang/reflect/Method"));
918 jclass_ref fCls (jni_env, jni_env->FindClass (
"java/lang/reflect/Field"));
919 jmethodID m_getName_ID = jni_env->GetMethodID (mCls,
"getName",
"()Ljava/lang/String;");
920 jmethodID f_getName_ID = jni_env->GetMethodID (fCls,
"getName",
"()Ljava/lang/String;");
922 for (
int i = 0; i < mLen; i++)
924 jobject_ref meth (jni_env, jni_env->GetObjectArrayElement (mList, i));
925 jstring_ref methName (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (meth, m_getName_ID)));
929 for (
int i = 0; i < fLen; i++)
931 jobject_ref field (jni_env, jni_env->GetObjectArrayElement (fList, i));
932 jstring_ref fieldName (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (field, f_getName_ID)));
936 restore_fpu_state ();
941 return v.
sort (
true);
945 convert_to_string (JNIEnv *jni_env, jobject java_object,
bool force,
char type)
949 if (jni_env && java_object)
951 jclass_ref cls (jni_env, jni_env->FindClass (
"java/lang/String"));
953 if (jni_env->IsInstanceOf (java_object, cls))
957 cls = jni_env->FindClass (
"[Ljava/lang/String;");
959 if (jni_env->IsInstanceOf (java_object, cls))
961 jobjectArray array =
reinterpret_cast<jobjectArray
> (java_object);
962 int len = jni_env->GetArrayLength (array);
965 for (
int i = 0; i < len; i++)
967 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->GetObjectArrayElement (array, i)));
973 c(i) = check_exception (jni_env);
984 cls = jni_env->FindClass (
"java/lang/Object");
985 jmethodID mID = jni_env->GetMethodID (cls,
"toString",
"()Ljava/lang/String;");
986 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (java_object, mID)));
991 retval = check_exception (jni_env);
995 error (
"unable to convert Java object to string");
997 restore_fpu_state ();
1003 #define TO_JAVA(obj) dynamic_cast<octave_java*> ((obj).internal_rep ())
1006 box (JNIEnv* jni_env, jobject jobj, jclass jcls)
1018 cls = jni_env->FindClass (
"java/lang/Number");
1019 if (jni_env->IsInstanceOf (jobj, cls))
1021 jmethodID m = jni_env->GetMethodID (cls,
"doubleValue",
"()D");
1022 retval = jni_env->CallDoubleMethod (jobj, m);
1026 cls = jni_env->FindClass (
"java/lang/Boolean");
1027 if (jni_env->IsInstanceOf (jobj, cls))
1029 jmethodID m = jni_env->GetMethodID (cls,
"booleanValue",
"()Z");
1030 retval = (jni_env->CallBooleanMethod (jobj, m) ?
true :
false);
1034 cls = jni_env->FindClass (
"java/lang/String");
1035 if (jni_env->IsInstanceOf (jobj, cls))
1041 cls = jni_env->FindClass (
"java/lang/Character");
1042 if (jni_env->IsInstanceOf (jobj, cls))
1044 jmethodID m = jni_env->GetMethodID (cls,
"charValue",
"()C");
1045 retval = jni_env->CallCharMethod (jobj, m);
1050 #define BOX_PRIMITIVE_ARRAY(JAVA_TYPE, JAVA_ID, JAVA_TYPE_CAP, OCTAVE_ID) \
1051 cls = jni_env->FindClass (JAVA_ID); \
1052 if (jni_env->IsInstanceOf (jobj, cls)) \
1054 const JAVA_TYPE ## Array jarr = reinterpret_cast<JAVA_TYPE ## Array> (jobj); \
1055 const jsize len = jni_env->GetArrayLength (jarr); \
1056 OCTAVE_ID ## NDArray d (dim_vector (len, 1)); \
1057 JAVA_TYPE * buffer = reinterpret_cast<JAVA_TYPE *> (d.fortran_vec ()); \
1058 jni_env->Get ## JAVA_TYPE_CAP ## ArrayRegion (jarr, 0, len, buffer); \
1063 BOX_PRIMITIVE_ARRAY (jboolean,
"[Z", Boolean,
bool)
1064 BOX_PRIMITIVE_ARRAY (jchar, "[
C", Char,
char)
1065 BOX_PRIMITIVE_ARRAY (jbyte, "[
B", Byte, int8)
1066 BOX_PRIMITIVE_ARRAY (jshort, "[S", Short, int16)
1067 BOX_PRIMITIVE_ARRAY (jint, "[I", Int, int32)
1068 BOX_PRIMITIVE_ARRAY (jlong, "[J", Long, int64)
1069 BOX_PRIMITIVE_ARRAY (jfloat, "[
F", Float, Float)
1070 BOX_PRIMITIVE_ARRAY (jdouble, "[D", Double, )
1072 #undef BOX_PRIMITIVE_ARRAY
1076 cls = find_octave_class (jni_env,
"org/octave/Matrix");
1078 if (jni_env->IsInstanceOf (jobj, cls))
1080 jmethodID mID = jni_env->GetMethodID (cls,
"getDims",
"()[I");
1081 jintArray_ref iv (jni_env, reinterpret_cast<jintArray> (jni_env->CallObjectMethod (jobj, mID)));
1082 jint *iv_data = jni_env->GetIntArrayElements (jintArray (iv), 0);
1084 dims.
resize (jni_env->GetArrayLength (jintArray (iv)));
1086 for (
int i = 0; i < dims.
length (); i++)
1087 dims(i) = iv_data[i];
1089 jni_env->ReleaseIntArrayElements (jintArray (iv), iv_data, 0);
1090 mID = jni_env->GetMethodID (cls,
"getClassName",
"()Ljava/lang/String;");
1091 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (jobj, mID)));
1098 mID = jni_env->GetMethodID (cls,
"toDouble",
"()[D");
1099 jdoubleArray_ref dv (jni_env, reinterpret_cast<jdoubleArray> (jni_env->CallObjectMethod (jobj, mID)));
1100 jni_env->GetDoubleArrayRegion (dv, 0, m.length (), m.fortran_vec ());
1104 else if (s ==
"byte")
1109 mID = jni_env->GetMethodID (cls,
"toByte",
"()[B");
1110 jbyteArray_ref dv (jni_env, reinterpret_cast<jbyteArray> (jni_env->CallObjectMethod (jobj, mID)));
1111 jni_env->GetByteArrayRegion (dv, 0, m.length (),
reinterpret_cast<jbyte *
> (m.fortran_vec ()));
1118 mID = jni_env->GetMethodID (cls,
"toByte",
"()[B");
1119 jbyteArray_ref dv (jni_env, reinterpret_cast<jbyteArray> (jni_env->CallObjectMethod (jobj, mID)));
1120 jni_env->GetByteArrayRegion (dv, 0, m.length (),
reinterpret_cast<jbyte *
> (m.fortran_vec ()));
1125 else if (s ==
"integer")
1130 mID = jni_env->GetMethodID (cls,
"toInt",
"()[I");
1131 jintArray_ref dv (jni_env, reinterpret_cast<jintArray> (jni_env->CallObjectMethod (jobj, mID)));
1132 jni_env->GetIntArrayRegion (dv, 0, m.length (),
reinterpret_cast<jint *
> (m.fortran_vec ()));
1139 mID = jni_env->GetMethodID (cls,
"toInt",
"()[I");
1140 jintArray_ref dv (jni_env, reinterpret_cast<jintArray> (jni_env->CallObjectMethod (jobj, mID)));
1141 jni_env->GetIntArrayRegion (dv, 0, m.length (),
reinterpret_cast<jint *
> (m.fortran_vec ()));
1149 cls = find_octave_class (jni_env,
"org/octave/OctaveReference");
1150 if (jni_env->IsInstanceOf (jobj, cls))
1152 jmethodID mID = jni_env->GetMethodID (cls,
"getID",
"()I");
1153 int ID = jni_env->CallIntMethod (jobj, mID);
1154 std::map<int,octave_value>::iterator it = octave_ref_map.find (ID);
1156 if (it != octave_ref_map.end ())
1157 retval = it->second;
1171 box_more (JNIEnv* jni_env, jobject jobj, jclass jcls)
1183 cls = jni_env->FindClass (
"[D");
1185 if (jni_env->IsInstanceOf (jobj, cls))
1187 jdoubleArray jarr =
reinterpret_cast<jdoubleArray
> (jobj);
1188 int len = jni_env->GetArrayLength (jarr);
1193 jni_env->GetDoubleArrayRegion (jarr, 0, len, m.fortran_vec ());
1203 cls = jni_env->FindClass (
"[[D");
1205 if (jni_env->IsInstanceOf (jobj, cls))
1207 jobjectArray jarr =
reinterpret_cast<jobjectArray
> (jobj);
1208 int rows = jni_env->GetArrayLength (jarr);
1215 for (
int r = 0; r < rows; r++)
1218 reinterpret_cast<jdoubleArray> (jni_env->GetObjectArrayElement (jarr, r)));
1222 cols = jni_env->GetArrayLength (row);
1225 jni_env->GetDoubleArrayRegion (row, 0, cols, m.
fortran_vec () + r * cols);
1236 cls = jni_env->FindClass (
"[Ljava/lang/String;");
1238 if (jni_env->IsInstanceOf (jobj, cls))
1240 jobjectArray jarr =
reinterpret_cast<jobjectArray
> (jobj);
1241 int len = jni_env->GetArrayLength (jarr);
1244 for (
int i = 0; i < len; i++)
1247 reinterpret_cast<jstring> (jni_env->GetObjectArrayElement (jarr, i)));
1259 restore_fpu_state ();
1275 jcls = jni_env->GetObjectClass (jobj);
1281 jobj = jni_env->NewStringUTF (s.c_str ());
1282 jcls = jni_env->GetObjectClass (jobj);
1286 #define IF_UNBOX_PRIMITIVE_ARRAY(CHECK_TYPE, METHOD_TYPE, OCTAVE_TYPE, JAVA_TYPE, JAVA_TYPE_CAP) \
1287 if (val.is_ ## CHECK_TYPE ## _type ()) \
1289 const OCTAVE_TYPE ## NDArray v = val.METHOD_TYPE ## array_value (); \
1290 JAVA_TYPE ## Array jarr = jni_env->New ## JAVA_TYPE_CAP ## Array (v.numel ()); \
1291 const JAVA_TYPE* jv = reinterpret_cast<const JAVA_TYPE*> (v.data ()); \
1292 jni_env->Set ## JAVA_TYPE_CAP ## ArrayRegion (jarr, 0, v.numel (), jv); \
1293 jobj = reinterpret_cast<jobject> (jarr); \
1294 jcls = jni_env->GetObjectClass (jobj); \
1299 IF_UNBOX_PRIMITIVE_ARRAY(
bool, bool_,
bool, jboolean, Boolean)
1300 else IF_UNBOX_PRIMITIVE_ARRAY(
float, float_, Float, jfloat, Float)
1301 else IF_UNBOX_PRIMITIVE_ARRAY(int8, int8_, int8, jbyte, Byte)
1302 else IF_UNBOX_PRIMITIVE_ARRAY(uint8, uint8_, uint8, jbyte, Byte)
1303 else IF_UNBOX_PRIMITIVE_ARRAY(int16, int16_, int16, jshort, Short)
1304 else IF_UNBOX_PRIMITIVE_ARRAY(uint16, uint16_, uint16, jshort, Short)
1305 else IF_UNBOX_PRIMITIVE_ARRAY(int32, int32_, int32, jint, Int)
1306 else IF_UNBOX_PRIMITIVE_ARRAY(uint32, uint32_, uint32, jint, Int)
1307 else IF_UNBOX_PRIMITIVE_ARRAY(int64, int64_, int64, jlong, Long)
1308 else IF_UNBOX_PRIMITIVE_ARRAY(uint64, uint64_, uint64, jlong, Long)
1310 #undef IF_UNBOX_PRIMITIVE_ARRAY
1314 #define IF_UNBOX_PRIMITIVE_SCALAR(CHECK_TYPE, OCTAVE_TYPE, METHOD_TYPE, JAVA_TYPE, JAVA_CON) \
1315 if (val.is_ ## CHECK_TYPE ## _type ()) \
1317 const OCTAVE_TYPE ov = val.METHOD_TYPE ## _value (); \
1318 jclass_ref dcls (jni_env, jni_env->FindClass (JAVA_TYPE)); \
1319 const jfieldID fid = jni_env->GetStaticFieldID (dcls, "TYPE", "Ljava/lang/Class;"); \
1320 const jmethodID mid = jni_env->GetMethodID (dcls, "<init>", JAVA_CON); \
1321 jcls = reinterpret_cast<jclass> (jni_env->GetStaticObjectField (dcls, fid)); \
1322 jobj = jni_env->NewObject (dcls, mid, ov); \
1325 IF_UNBOX_PRIMITIVE_SCALAR(
double,
double,
double,
"java/lang/Double",
"(D)V")
1326 else IF_UNBOX_PRIMITIVE_SCALAR(
float,
float,
float, "java/lang/Float", "(F)
V")
1327 else IF_UNBOX_PRIMITIVE_SCALAR(
bool,
bool,
bool, "java/lang/Boolean", "(
Z)
V")
1328 else IF_UNBOX_PRIMITIVE_SCALAR(int8, int8_t, int8_scalar, "java/lang/Byte", "(B)
V")
1329 else IF_UNBOX_PRIMITIVE_SCALAR(uint8, uint8_t, uint8_scalar, "java/lang/Byte", "(B)
V")
1330 else IF_UNBOX_PRIMITIVE_SCALAR(int16, int16_t, int16_scalar, "java/lang/Short", "(S)
V")
1331 else IF_UNBOX_PRIMITIVE_SCALAR(uint16, uint16_t, uint16_scalar, "java/lang/Short", "(S)
V")
1332 else IF_UNBOX_PRIMITIVE_SCALAR(int32, int32_t, int32_scalar, "java/lang/Int", "(I)
V")
1333 else IF_UNBOX_PRIMITIVE_SCALAR(uint32, uint32_t, uint32_scalar, "java/lang/Int", "(I)
V")
1334 else IF_UNBOX_PRIMITIVE_SCALAR(int64, int64_t, int64_scalar, "java/lang/Long", "(L)
V")
1335 else IF_UNBOX_PRIMITIVE_SCALAR(uint64, uint64_t, uint64_scalar, "java/lang/Long", "(L)
V")
1337 #undef IF_UNBOX_PRIMITIVE_SCALAR
1351 jdoubleArray dv = jni_env->NewDoubleArray (m.
length ());
1354 jcls = jni_env->GetObjectClass (jobj);
1359 jclass_ref mcls (jni_env, find_octave_class (jni_env,
"org/octave/Matrix"));
1362 jint *iv_data = jni_env->GetIntArrayElements (jintArray (iv), 0);
1364 for (
int i = 0; i < dims.
length (); i++)
1365 iv_data[i] = dims(i);
1367 jni_env->ReleaseIntArrayElements (jintArray (iv), iv_data, 0);
1373 jni_env->SetDoubleArrayRegion (jdoubleArray (dv), 0, m.
length (),
1375 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([D[I)V");
1376 jobj = jni_env->NewObject (jclass (mcls), mID, jdoubleArray (dv),
1378 jcls = jni_env->GetObjectClass (jobj);
1384 jni_env->SetByteArrayRegion (jbyteArray (bv), 0, m.
length (),
1386 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([B[I)V");
1387 jobj = jni_env->NewObject (jclass (mcls), mID, jbyteArray (bv), jintArray (iv));
1388 jcls = jni_env->GetObjectClass (jobj);
1394 jni_env->SetByteArrayRegion (jbyteArray (bv), 0, m.
length (),
1396 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([B[I)V");
1397 jobj = jni_env->NewObject (jclass (mcls), mID, jbyteArray (bv), jintArray (iv));
1398 jcls = jni_env->GetObjectClass (jobj);
1404 jni_env->SetIntArrayRegion (jintArray (v), 0, m.
length (),
1406 jmethodID mID = jni_env->GetMethodID (mcls,
"<init>",
"([I[I)V");
1407 jobj = jni_env->NewObject (jclass (mcls), mID, jintArray (v), jintArray (iv));
1408 jcls = jni_env->GetObjectClass (jobj);
1413 error (
"cannot convert matrix of type '%s'", val.
class_name ().c_str ());
1419 jclass_ref scls (jni_env, jni_env->FindClass (
"java/lang/String"));
1420 jobjectArray array = jni_env->NewObjectArray (cellStr.
length (), scls, 0);
1421 for (
int i = 0; i < cellStr.
length (); i++)
1424 jni_env->NewStringUTF (cellStr(i).string_value().c_str ()));
1425 jni_env->SetObjectArrayElement (array, i, jstr);
1428 jcls = jni_env->GetObjectClass (jobj);
1432 jclass rcls = find_octave_class (jni_env,
"org/octave/OctaveReference");
1433 jmethodID mID = jni_env->GetMethodID (rcls,
"<init>",
"(I)V");
1434 int ID = octave_java_refcount++;
1436 jobj = jni_env->NewObject (rcls, mID, ID);
1438 octave_ref_map[ID] = val;
1450 jclass_ref ocls (jni_env, jni_env->FindClass (
"java/lang/Object"));
1451 jclass_ref ccls (jni_env, jni_env->FindClass (
"java/lang/Class"));
1454 jobjs = jni_env->NewObjectArray (args.
length (), ocls, 0);
1457 jclss = jni_env->NewObjectArray (args.
length (), ccls, 0);
1459 for (
int i = 0; i < args.
length (); i++)
1464 found =
unbox (jni_env, args(i), jobj, jcls);
1468 jni_env->SetObjectArrayElement (jobjs, i, jobj);
1469 jni_env->SetObjectArrayElement (jclss, i, jcls);
1476 get_current_thread_ID (JNIEnv *jni_env)
1480 jclass_ref cls (jni_env, jni_env->FindClass (
"java/lang/Thread"));
1481 jmethodID mID = jni_env->GetStaticMethodID (cls,
"currentThread",
"()Ljava/lang/Thread;");
1482 jobject_ref jthread (jni_env, jni_env->CallStaticObjectMethod (cls, mID));
1486 jclass_ref jth_cls (jni_env, jni_env->GetObjectClass (jthread));
1487 mID = jni_env->GetMethodID (jth_cls,
"getId",
"()J");
1488 long result = jni_env->CallLongMethod (jthread, mID);
1498 java_event_hook (
void)
1504 jclass_ref cls (current_env, find_octave_class (current_env,
"org/octave/Octave"));
1505 jmethodID mID = current_env->GetStaticMethodID (cls,
"checkPendingAction",
"()V");
1506 current_env->CallStaticVoidMethod (cls, mID);
1508 restore_fpu_state ();
1515 initialize_java (
void)
1527 octave_thread_ID = get_current_thread_ID (current_env);
1530 catch (std::string msg)
1532 error (msg.c_str ());
1535 restore_fpu_state ();
1539 JNIEXPORT jboolean JNICALL
1540 Java_org_octave_Octave_call (JNIEnv *env, jclass, jstring funcName,
1541 jobjectArray argin, jobjectArray argout)
1545 int nargout = env->GetArrayLength (argout);
1546 int nargin = env->GetArrayLength (argin);
1550 for (
int i = 0; i < nargin; i++)
1551 varargin(i) =
box (env, env->GetObjectArrayElement (argin, i), 0);
1553 varargout =
feval (fname, varargin, nargout);
1559 return unbox (env, varargout, out_objs, out_clss);
1562 JNIEXPORT
void JNICALL
1563 Java_org_octave_OctaveReference_doFinalize (JNIEnv *, jclass, jint ID)
1565 octave_ref_map.erase (ID);
1568 JNIEXPORT
void JNICALL
1569 Java_org_octave_Octave_doInvoke (JNIEnv *env, jclass, jint ID,
1572 std::map<int,octave_value>::iterator it = octave_ref_map.find (ID);
1574 if (it != octave_ref_map.end ())
1577 int len = env->GetArrayLength (args);
1580 for (
int i = 0; i < len; i++)
1582 jobject_ref jobj (env, env->GetObjectArrayElement (args, i));
1583 oct_args(i) =
box (env, jobj, 0);
1591 BEGIN_INTERRUPT_WITH_EXCEPTIONS;
1596 feval (fcn, oct_args);
1600 && val.
cell_value()(0).is_function_handle ())
1605 for (
int i=1; i<c.
length (); i++)
1606 oct_args(len+i-1) = c(i);
1609 feval (fcn, oct_args);
1612 error (
"trying to invoke non-invocable object");
1614 END_INTERRUPT_WITH_EXCEPTIONS;
1619 JNIEXPORT
void JNICALL
1620 Java_org_octave_Octave_doEvalString (JNIEnv *env, jclass, jstring cmd)
1627 JNIEXPORT jboolean JNICALL
1628 Java_org_octave_Octave_needThreadedInvokation (JNIEnv *env, jclass)
1630 return (get_current_thread_ID (env) != octave_thread_ID);
1652 if (current_env && java_object)
1653 return compute_array_dimensions (current_env, java_object);
1664 jvm->GetEnv (reinterpret_cast<void **> (&env), JNI_VERSION_1_2);
1671 const std::list<octave_value_list>& idx,
int nargout)
1681 if (type.length () > 1 && type[1] ==
'(')
1686 ovl(0) = (idx.front ())(0);
1687 std::list<octave_value_list>::const_iterator it = idx.begin ();
1689 retval =
feval (std::string (
"javaMethod"), ovl, 1);
1697 ovl(1) = (idx.front ())(0);
1698 retval =
feval (std::string (
"__java_get__"), ovl, 1);
1704 retval = get_array_elements (current_env,
to_java (), idx.front ());
1708 error (
"subsref: Java object cannot be indexed with %c", type[0]);
1712 if (idx.size () > 1 && type.length () > 1)
1713 retval = retval(0).next_subsref (nargout, type, idx, skip);
1720 const std::list<octave_value_list>&idx,
1730 if (type.length () == 1)
1736 ovl(1) = (idx.front ())(0);
1738 feval (
"__java_set__", ovl, 0);
1745 else if (type.length () > 2 && type[1] ==
'(')
1747 std::list<octave_value_list> new_idx;
1748 std::list<octave_value_list>::const_iterator it = idx.begin ();
1749 new_idx.push_back (*it++);
1750 new_idx.push_back (*it++);
1754 std::list<octave_value_list> next_idx (idx);
1755 next_idx.erase (next_idx.begin ());
1756 next_idx.erase (next_idx.begin ());
1757 u(0).subsasgn (type.substr (2), next_idx, rhs);
1765 else if (type[1] ==
'.')
1770 std::list<octave_value_list> next_idx (idx);
1771 next_idx.erase (next_idx.begin ());
1772 u(0).subsasgn (type.substr (1), next_idx, rhs);
1781 error (
"invalid indexing/assignment on Java object");
1787 set_array_elements (current_env,
to_java (), idx.front (), rhs);
1797 error (
"Java object cannot be indexed with %c", type[0]);
1810 return get_invoke_list (current_env,
to_java ());
1821 return convert_to_string (current_env,
to_java (), force, type);
1848 if (
unbox (jni_env, args, arg_objs, arg_types))
1850 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
1851 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"invokeMethod",
1852 "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
1853 jstring_ref methName (jni_env, jni_env->NewStringUTF (name.c_str ()));
1854 jobjectArray_ref resObj (jni_env, reinterpret_cast<jobjectArray> (jni_env->CallStaticObjectMethod (helperClass, mID,
1855 to_java (), jstring (methName), jobjectArray (arg_objs), jobjectArray (arg_types))));
1857 retval =
box (jni_env, resObj);
1859 retval = check_exception (jni_env);
1862 restore_fpu_state ();
1870 const std::string& class_name,
1871 const std::string& name,
1879 if (
unbox (jni_env, args, arg_objs, arg_types))
1881 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
1882 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"invokeStaticMethod",
1883 "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
1884 jstring_ref methName (jni_env, jni_env->NewStringUTF (name.c_str ()));
1885 jstring_ref clsName (jni_env, jni_env->NewStringUTF (class_name.c_str ()));
1886 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
1887 jstring (clsName), jstring (methName), jobjectArray (arg_objs), jobjectArray (arg_types)));
1889 retval =
box (jni_env, resObj);
1891 retval = check_exception (jni_env);
1894 restore_fpu_state ();
1910 if (
unbox (jni_env, args, arg_objs, arg_types))
1912 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
1913 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"invokeConstructor",
1914 "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
1915 jstring_ref clsName (jni_env, jni_env->NewStringUTF (name.c_str ()));
1916 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
1917 jstring (clsName), jobjectArray (arg_objs), jobjectArray (arg_types)));
1922 check_exception (jni_env);
1925 restore_fpu_state ();
1938 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
1939 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"getField",
1940 "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
1941 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
1942 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
1943 to_java (), jstring (fName)));
1946 retval =
box (jni_env, resObj);
1948 retval = check_exception (jni_env);
1950 restore_fpu_state ();
1958 const std::string& name)
1964 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
1965 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"getStaticField",
1966 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
1967 jstring_ref cName (jni_env, jni_env->NewStringUTF (class_name.c_str ()));
1968 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
1969 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
1970 jstring (cName), jstring (fName)));
1972 retval =
box (jni_env, resObj);
1974 retval = check_exception (jni_env);
1976 restore_fpu_state ();
1993 if (
unbox (jni_env, val, jobj, jcls))
1995 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
1996 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"setField",
1997 "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");
1998 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
1999 jni_env->CallStaticObjectMethod (helperClass, mID,
to_java (), jstring (fName), jobject (jobj));
2000 check_exception (jni_env);
2003 restore_fpu_state ();
2020 if (
unbox (jni_env, val, jobj, jcls))
2022 jclass_ref helperClass (jni_env, find_octave_class (jni_env,
"org/octave/ClassHelper"));
2023 jmethodID mID = jni_env->GetStaticMethodID (helperClass,
"setStaticField",
2024 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V");
2025 jstring_ref cName (jni_env, jni_env->NewStringUTF (class_name.c_str ()));
2026 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2027 jni_env->CallStaticObjectMethod (helperClass, mID, jstring (cName), jstring (fName), jobject (jobj));
2028 check_exception (jni_env);
2031 restore_fpu_state ();
2037 #endif // endif on HAVE_JAVA
2042 DEFUN (__java_init__, , ,
2044 @deftypefn {Built-in Function} {} __java_init__ ()\n\
2045 Internal function used @strong{only} when debugging Java interface.\n\
2047 Function will directly call initialize_java() to create an instance of a JVM.\n\
2063 error (
"__java_init__: Octave was not compiled with Java interface");
2068 DEFUN (__java_exit__, , ,
2070 @deftypefn {Built-in Function} {} __java_exit__ ()\n\
2071 Internal function used @strong{only} when debugging Java interface.\n\
2073 Function will directly call terminate_jvm() to destroy the current JVM\n\
2080 error (
"__java_init__: Octave was not compiled with Java interface");
2086 DEFUN (javaObject, args, ,
2088 @deftypefn {Built-in Function} {@var{jobj} =} javaObject (@var{classname})\n\
2089 @deftypefnx {Built-in Function} {@var{jobj} =} javaObject (@var{classname}, @var{arg1}, @dots{})\n\
2090 Create a Java object of class @var{classsname}, by calling the class\n\
2091 constructor with the arguments @var{arg1}, @dots{}\n\
2093 The first example below creates an uninitialized object, while the second\n\
2094 example supplies an initial argument to the constructor.\n\
2098 x = javaObject (\"java.lang.StringBuffer\")\n\
2099 x = javaObject (\"java.lang.StringBuffer\", \"Initial string\")\n\
2103 @seealso{javaMethod, javaArray}\n\
2117 if (args(0).is_string ())
2119 std::string classname = args(0).string_value ();
2122 for (
int i=1; i<args.
length (); i++)
2127 error (
"javaObject: CLASSNAME must be a string");
2135 error (
"javaObject: Octave was not compiled with Java interface");
2148 DEFUN (javaMethod, args, ,
2150 @deftypefn {Built-in Function} {@var{ret} =} javaMethod (@var{methodname}, @var{obj})\n\
2151 @deftypefnx {Built-in Function} {@var{ret} =} javaMethod (@var{methodname}, @var{obj}, @var{arg1}, @dots{})\n\
2152 Invoke the method @var{methodname} on the Java object @var{obj} with the\n\
2153 arguments @var{arg1}, @dots{}.\n\
2155 For static methods, @var{obj} can be a string representing the fully\n\
2156 qualified name of the corresponding class.\n\
2158 When @var{obj} is a regular Java object, structure-like indexing can be\n\
2159 used as a shortcut syntax. For instance, the two following statements are\n\
2164 ret = javaMethod (\"method1\", x, 1.0, \"a string\")\n\
2165 ret = x.method1 (1.0, \"a string\")\n\
2169 @code{javaMethod} returns the result of the method invocation.\n\
2171 @seealso{methods, javaObject}\n\
2185 if (args(0).is_string ())
2187 std::string methodname = args(0).string_value ();
2190 for (
int i=2; i<args.
length (); i++)
2193 if (args(1).is_java ())
2196 retval = jobj->
do_javaMethod (current_env, methodname, tmp);
2198 else if (args(1).is_string ())
2200 std::string cls = args(1).string_value ();
2204 error (
"javaMethod: OBJ must be a Java object or a string");
2207 error (
"javaMethod: METHODNAME must be a string");
2215 error (
"javaMethod: Octave was not compiled with Java interface");
2227 DEFUN (__java_get__, args, ,
2229 @deftypefn {Built-in Function} {@var{val} =} __java_get__ (@var{obj}, @var{name})\n\
2230 Get the value of the field @var{name} of the Java object @var{obj}.\n\
2232 For static fields, @var{obj} can be a string representing the fully qualified\n\
2233 name of the corresponding class.\n\
2235 When @var{obj} is a regular Java object, structure-like indexing can be used\n\
2236 as a shortcut syntax. For instance, the two following statements are\n\
2241 __java_get__ (x, \"field1\")\n\
2246 @seealso{__java_set__, javaMethod, javaObject}\n\
2260 if (args(1).is_string ())
2262 std::string name = args(1).string_value ();
2264 if (args(0).is_java ())
2269 else if (args(0).is_string ())
2271 std::string cls = args(0).string_value ();
2275 error (
"__java_get__: OBJ must be a Java object or a string");
2278 error (
"__java_get__: NAME must be a string");
2286 error (
"__java_get__: Octave was not compiled with Java interface");
2291 DEFUN (__java_set__, args, ,
2293 @deftypefn {Built-in Function} {@var{obj} =} __java_set__ (@var{obj}, @var{name}, @var{val})\n\
2294 Set the value of the field @var{name} of the Java object @var{obj} to\n\
2297 For static fields, @var{obj} can be a string representing the fully\n\
2298 qualified named of the corresponding Java class.\n\
2300 When @var{obj} is a regular Java object, structure-like indexing can be\n\
2301 used as a shortcut syntax. For instance, the two following statements are\n\
2306 __java_set__ (x, \"field1\", val)\n\
2311 @seealso{__java_get__, javaMethod, javaObject}\n\
2325 if (args(1).is_string ())
2327 std::string name = args(1).string_value ();
2329 if (args(0).is_java ())
2332 retval = jobj->
do_java_set (current_env, name, args(2));
2334 else if (args(0).is_string ())
2336 std::string cls = args(0).string_value ();
2340 error (
"__java_set__: OBJ must be a Java object or a string");
2343 error (
"__java_set__: NAME must be a string");
2351 error (
"__java_set__: Octave was not compiled with Java interface");
2356 DEFUN (java2mat, args, ,
2358 @deftypefn {Built-in Function} {} java2mat (@var{javaobj})\n\
2359 Undocumented internal function.\n\
2373 if (args(0).is_java ())
2379 retval(0) = args(0);
2387 error (
"java2mat: Octave was not compiled with Java interface");
2392 DEFUN (java_matrix_autoconversion, args, nargout,
2394 @deftypefn {Built-in Function} {@var{val} =} java_matrix_autoconversion ()\n\
2395 @deftypefnx {Built-in Function} {@var{old_val} =} java_matrix_autoconversion (@var{new_val})\n\
2396 @deftypefnx {Built-in Function} {} java_matrix_autoconversion (@var{new_val}, \"local\")\n\
2397 Query or set the internal variable that controls whether Java arrays are\n\
2398 automatically converted to Octave matrices.\n\
2400 The default value is false.\n\
2402 When called from inside a function with the @qcode{\"local\"} option, the\n\
2403 variable is changed locally for the function and any subroutines it calls.\n\
2404 The original variable value is restored when exiting the function.\n\
2405 @seealso{java_unsigned_autoconversion, debug_java}\n\
2411 error (
"java_matrix_autoconversion: Octave was not compiled with Java interface");
2416 DEFUN (java_unsigned_autoconversion, args, nargout,
2418 @deftypefn {Built-in Function} {@var{val} =} java_unsigned_autoconversion ()\n\
2419 @deftypefnx {Built-in Function} {@var{old_val} =} java_unsigned_autoconversion (@var{new_val})\n\
2420 @deftypefnx {Built-in Function} {} java_unsigned_autoconversion (@var{new_val}, \"local\")\n\
2421 Query or set the internal variable that controls how integer classes are\n\
2422 converted when @code{java_matrix_autoconversion} is enabled.\n\
2424 When enabled, Java arrays of class Byte or Integer are converted to matrices\n\
2425 of class uint8 or uint32 respectively. The default value is true.\n\
2427 When called from inside a function with the @qcode{\"local\"} option, the\n\
2428 variable is changed locally for the function and any subroutines it calls.\n\
2429 The original variable value is restored when exiting the function.\n\
2430 @seealso{java_matrix_autoconversion, debug_java}\n\
2436 error (
"java_unsigned_autoconversion: Octave was not compiled with Java interface");
2441 DEFUN (debug_java, args, nargout,
2443 @deftypefn {Built-in Function} {@var{val} =} debug_java ()\n\
2444 @deftypefnx {Built-in Function} {@var{old_val} =} debug_java (@var{new_val})\n\
2445 @deftypefnx {Built-in Function} {} debug_java (@var{new_val}, \"local\")\n\
2446 Query or set the internal variable that determines whether extra debugging\n\
2447 information regarding the initialization of the JVM and any Java exceptions\n\
2450 When called from inside a function with the @qcode{\"local\"} option, the\n\
2451 variable is changed locally for the function and any subroutines it calls.\n\
2452 The original variable value is restored when exiting the function.\n\
2453 @seealso{java_matrix_autoconversion, java_unsigned_autoconversion}\n\
2459 error (
"debug_java: Octave was not compiled with Java interface");
2466 DEFUN (isjava, args, ,
2468 @deftypefn {Built-in Function} {} isjava (@var{x})\n\
2469 Return true if @var{x} is a Java object.\n\
2470 @seealso{class, typeinfo, isa, javaObject}\n\
2478 retval = args(0).is_java ();
uint8NDArray uint8_array_value(void) const
bool is_range(void) const
static void register_type(void)
octave_idx_type length(octave_idx_type n=0) const
static JNIEnv * thread_jni_env(void)
octave_refcount< octave_idx_type > count
bool is_real_type(void) const
octave_idx_type rows(void) const
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
int8NDArray int8_array_value(void) const
bool is_vector(void) const
static std::string path_sep_str(void)
octave_value do_java_set(JNIEnv *jni_env, const std::string &name, const octave_value &val)
OCTINTERP_API void print_usage(void)
OCTINTERP_API octave_value box(JNIEnv *jni_env, jobject jobj, jclass jcls=0)
octave_idx_type length(void) const
octave_value_list & append(const octave_value &val)
OCTINTERP_API bool Vjava_unsigned_autoconversion
static char * strsave(const char *s)
void resize(int n, int fill_value=0)
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
static void add_event_hook(event_hook_fcn f)
int32NDArray int32_array_value(void) const
#define SET_INTERNAL_VARIABLE(NM)
bool is_int8_type(void) const
octave_value_list feval(const std::string &name, const octave_value_list &args, int nargout)
std::string Vfcn_file_dir
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
octave_value_list eval_string(const std::string &eval_str, bool silent, int &parse_status, int nargout)
F77_RET_T const octave_idx_type const octave_idx_type const octave_idx_type double const octave_idx_type double const octave_idx_type double const octave_idx_type double * Z
bool is_int32_type(void) const
void newline(std::ostream &os) const
bool is_function_handle(void) const
OCTINTERP_API std::string jstring_to_string(JNIEnv *jni_env, jstring s)
static std::string tilde_expand(const std::string &)
Cell cell_value(void) const
octave_idx_type columns(void) const
jobject to_java(void) const
static void replace(QString &text, const QRegExp &re, const QString &after)
octave_value convert_to_str(bool pad=false, bool force=false, char type= '\'') const
static std::string get_current_directory(void)
octave_idx_type numel(const octave_value_list &idx)
bool is_real_scalar(void) const
std::string string_value(bool force=false) const
static bool chdir(const std::string &newdir)
bool is_matrix_type(void) const
bool is_string(void) const
bool is_double_type(void) const
static std::string getenv(const std::string &name)
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
bool is_cellstr(void) const
Matrix transpose(void) const
dim_vector dims(void) const
octave_idx_type length(void) const
dim_vector dims(void) const
void sort(bool uniq=false)
Matrix matrix_value(bool frc_str_conv=false) const
friend class octave_value
OCTINTERP_API bool Vdebug_java
octave_function * function_value(bool silent=false) const
OCTINTERP_API bool unbox(JNIEnv *jni_env, const octave_value &val, jobject_ref &jobj, jclass_ref &jcls)
static const std::string t_name
octave_value do_javaMethod(JNIEnv *jni_env, const std::string &name, const octave_value_list &args)
octave_idx_type length(void) const
Number of elements in the array.
bool is_empty(void) const
bool is_bool_scalar(void) const
NDArray array_value(bool frc_str_conv=false) const
octave_value_list ovl(const octave_value &a0)
F77_RET_T const octave_idx_type Complex const octave_idx_type Complex * B
void print(std::ostream &os, bool pr_as_read_syntax=false)
bool is_java_string(void) const
F77_RET_T const octave_idx_type const octave_idx_type const octave_idx_type const double const double octave_idx_type double * V
octave_value convert_to_str_internal(bool pad, bool force, char type) const
std::string java_classname
bool is_uint8_type(void) const
string_vector map_keys(void) const
static int register_type(const std::string &, const std::string &, const octave_value &)
OCTINTERP_API bool Vjava_matrix_autoconversion
std::string class_name(void) const
octave_value do_java_get(JNIEnv *jni_env, const std::string &name)
bool is_instance_of(const std::string &) const
bool is_undefined(void) const
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
void close(close_hook cl_hook=0)
static std::string dir_sep_str(void)
const T * fortran_vec(void) const
static octave_value do_javaObject(JNIEnv *jni_env, const std::string &name, const octave_value_list &args)
OCTINTERP_API octave_value box_more(JNIEnv *jni_env, jobject jobj, jclass jcls=0)
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
bool is_real_matrix(void) const
void F(const TSRC *v, TRES *r, octave_idx_type m, octave_idx_type n)