comparison libinterp/octave-value/ov-java.cc @ 15752:f96faf028d90

make java functions part of core octave_value classes * run-octave.in: Set OCTAVE_JAVA_DIR. * octave-value/ov-java.cc: Rename from dldfcn/__java__.cc. (get_module_path): Delete. (initial_java_dir): Don't set architecture dependent directory. Check environment or use Vfcn_file_dir to set default directory. (initial_class_path): Don't search load path for octave.jar. (initialize_jvm): Don't add -Doctave.java.path=DIR to vm_args. Use DEFUN, not DEFUN_DLD. (F__java__): Delete. * octave-value/ov-java.h: Rename from dldfcn/__java__.h. * dldfcn/module-files: Delete entry for __java__.cc. * libinterp/Makefile.am (octinclude_HEADERS): Delete $(DLDFCN_INC) from the list. * octave-value/module.mk (OCTAVE_VALUE_INC): Add ov-java.h to the list. (OCTAVE_VALUE_SRC) Add ov-java.cc to the list. (octave_value_liboctave_value_la_LIBADD): New variable. (octave_value_liboctave_value_la_CPPFLAGS): Add $(JAVA_CPPFLAGS) to the list. * dldfcn/config-module.awk: Don't print DLDFCN_INC variable. * Octave.java, OctaveReference.java: Don't grab a reference to __java__.oct.
author John W. Eaton <jwe@octave.org>
date Sat, 08 Dec 2012 00:23:13 -0500
parents libinterp/dldfcn/__java__.cc@4be890c5527c
children bf77f7f66bdf
comparison
equal deleted inserted replaced
15751:72968fb32c82 15752:f96faf028d90
1 /*
2
3 Copyright (C) 2007 Michael Goffioul
4
5 This file is part of Octave.
6
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #if defined HAVE_JAVA
28
29 #if defined (HAVE_WINDWOS_H)
30 #include <windows.h>
31 #endif
32
33 #include <algorithm>
34 #include <map>
35 #include <iostream>
36 #include <fstream>
37
38 #include <clocale>
39
40 #include "Cell.h"
41 #include "cmd-edit.h"
42 #include "defaults.h"
43 #include "defun.h"
44 #include "file-ops.h"
45 #include "file-stat.h"
46 #include "load-path.h"
47 #include "oct-env.h"
48 #include "oct-shlib.h"
49 #include "ov-java.h"
50 #include "parse.h"
51 #include "variables.h"
52
53 typedef jint (JNICALL *JNI_CreateJavaVM_t) (JavaVM **pvm, JNIEnv **penv, void *args);
54 typedef jint (JNICALL *JNI_GetCreatedJavaVMs_t) (JavaVM **pvm, jsize bufLen, jsize *nVMs);
55
56 extern "C"
57 {
58 JNIEXPORT jboolean JNICALL
59 Java_org_octave_Octave_call (JNIEnv *, jclass, jstring, jobjectArray,
60 jobjectArray);
61 JNIEXPORT void JNICALL
62 Java_org_octave_OctaveReference_doFinalize (JNIEnv *, jclass, jint);
63
64 JNIEXPORT void JNICALL
65 Java_org_octave_Octave_doInvoke (JNIEnv *, jclass, jint, jobjectArray);
66
67 JNIEXPORT void JNICALL
68 Java_org_octave_Octave_doEvalString (JNIEnv *, jclass, jstring);
69
70 JNIEXPORT jboolean JNICALL
71 Java_org_octave_Octave_needThreadedInvokation (JNIEnv *, jclass);
72 }
73
74 static octave_value _java_new (const octave_value_list& args);
75 static octave_value _java_invoke (const octave_value_list& args);
76
77 static JavaVM *jvm = 0;
78 static bool jvm_attached = false;
79
80 // Need to keep hold of the shared library handle until exit.
81 static octave_shlib jvm_lib;
82
83 static std::map<int,octave_value> listener_map;
84 static std::map<int,octave_value> octave_ref_map;
85 static int octave_java_refcount = 0;
86 static long octave_thread_ID = -1;
87
88 bool Vjava_convert_matrix = false;
89 bool Vjava_unsigned_conversion = true;
90 bool Vjava_debug = false;
91
92 class JVMArgs
93 {
94 public:
95
96 JVMArgs (void)
97 {
98 vm_args.version = JNI_VERSION_1_2;
99 vm_args.nOptions = 0;
100 vm_args.options = 0;
101 vm_args.ignoreUnrecognized = false;
102 }
103
104 ~JVMArgs (void)
105 {
106 clean ();
107 }
108
109 JavaVMInitArgs* to_args ()
110 {
111 update ();
112 return &vm_args;
113 }
114
115 void add (const std::string& opt)
116 {
117 java_opts.push_back (opt);
118 }
119
120 void read_java_opts (const std::string& filename)
121 {
122 std::ifstream js (filename.c_str ());
123
124 if (! js.bad () && ! js.fail ())
125 {
126 std::string line;
127
128 while (! js.eof () && ! js.fail ())
129 {
130 std::getline (js, line);
131 if (line.length () > 2
132 && (line.find ("-D") == 0 || line.find ("-X") == 0))
133 java_opts.push_back (line);
134 else if (line.length () > 0 && Vjava_debug)
135 std::cerr << "invalid JVM option, skipping: " << line << std::endl;
136 }
137 }
138 }
139
140 private:
141
142 void clean (void)
143 {
144 if (vm_args.options != 0)
145 {
146 for (int i = 0; i < vm_args.nOptions; i++)
147 delete [] vm_args.options[i].optionString;
148 delete [] vm_args.options;
149
150 vm_args.options = 0;
151 vm_args.nOptions = 0;
152 }
153 }
154
155 void update (void)
156 {
157 clean ();
158
159 if (java_opts.size () > 0)
160 {
161 int index = 0;
162
163 vm_args.nOptions = java_opts.size ();
164 vm_args.options = new JavaVMOption [vm_args.nOptions];
165 for (std::list<std::string>::const_iterator it = java_opts.begin (); it != java_opts.end (); ++it)
166 {
167 if (Vjava_debug)
168 std::cout << *it << std::endl;
169 vm_args.options[index++].optionString = strsave ((*it).c_str ());
170 }
171 java_opts.clear ();
172 }
173 }
174
175 private:
176
177 JavaVMInitArgs vm_args;
178
179 std::list<std::string> java_opts;
180 };
181
182 static dim_vector compute_array_dimensions (JNIEnv* jni_env, jobject obj);
183
184 #ifdef __WIN32__
185 static std::string
186 read_registry_string (const std::string& key, const std::string& value)
187 {
188 HKEY hkey;
189 DWORD len;
190
191 std::string retval;
192
193 if (! RegOpenKeyEx (HKEY_LOCAL_MACHINE, key.c_str (), 0, KEY_READ, &hkey))
194 {
195 if (! RegQueryValueEx (hkey, value.c_str (), 0, 0, 0, &len))
196 {
197 retval.resize (len);
198 if (RegQueryValueEx (hkey, value.c_str (), 0, 0, (LPBYTE)&retval[0], &len))
199 retval = "";
200 else if (retval[len-1] == '\0')
201 retval.resize (--len);
202 }
203 RegCloseKey (hkey);
204 }
205
206 return retval;
207 }
208
209 static std::string
210 get_module_filename (HMODULE hMod)
211 {
212 int n = 1024;
213 std::string retval (n, '\0');
214 bool found = false;
215
216 while (n < 65536)
217 {
218 int status = GetModuleFileName(hMod, &retval[0], n);
219
220 if (status < n)
221 {
222 retval.resize(n);
223 found = true;
224 break;
225 }
226 else
227 {
228 n *= 2;
229 retval.resize(n);
230 }
231 }
232 return (found ? retval : "");
233 }
234
235 static void
236 set_dll_directory (const std::string& dir = "")
237 {
238 typedef BOOL (WINAPI *dllfcn_t) (LPCTSTR path);
239
240 static dllfcn_t dllfcn = NULL;
241 static bool first = true;
242
243 if (! dllfcn && first)
244 {
245 HINSTANCE hKernel32 = GetModuleHandle ("kernel32");
246 dllfcn = reinterpret_cast<dllfcn_t> (GetProcAddress (hKernel32, "SetDllDirectoryA"));
247 first = false;
248 }
249
250 if (dllfcn)
251 dllfcn (dir.empty () ? NULL : dir.c_str ());
252 }
253 #endif
254
255 static std::string
256 initial_java_dir (void)
257 {
258 static std::string java_dir;
259
260 if (java_dir.empty ())
261 {
262 java_dir = octave_env::getenv ("OCTAVE_JAVA_DIR");
263
264 if (java_dir.empty ())
265 java_dir = Vfcn_file_dir + file_ops::dir_sep_str () + "java";
266 }
267
268 return java_dir;
269 }
270
271 // Read the content of a file filename (usually "classpath.txt")
272 //
273 // Returns a string with all lines concatenated and separated
274 // by the path separator character.
275 // The return string also starts with a path separator so that
276 // it can be appended easily to a base classpath.
277 //
278 // The file "classpath.txt" must contain single lines, each
279 // with a classpath.
280 // Comment lines starting with a '#' or a '%' in column 1 are allowed.
281
282 static std::string
283 read_classpath_txt (const std::string& filepath)
284 {
285 std::string classpath;
286
287 std::ifstream fs (filepath.c_str ());
288
289 if (! fs.bad () && ! fs.fail ())
290 {
291 std::string line;
292
293 while (! fs.eof () && ! fs.fail ())
294 {
295 std::getline (fs, line);
296
297 if (line.length () > 1)
298 {
299 if (line.at(0) == '#' || line.at(0) == '%')
300 ; // skip comments
301 else
302 {
303 // prepend separator character
304 classpath.append (dir_path::path_sep_str ());
305
306 // append content of line without trailing blanks
307 int iLast = line.find_last_not_of (' ');
308
309 classpath.append (file_ops::tilde_expand (line.substr (0, iLast+1)));
310 }
311 }
312 }
313 }
314
315 return (classpath);
316 }
317
318
319 static std::string
320 initial_class_path (void)
321 {
322 std::string java_dir = initial_java_dir ();
323
324 std::string retval = java_dir;
325
326 // find octave.jar file
327 if (! retval.empty ())
328 {
329 std::string sep = file_ops::dir_sep_str ();
330
331 std::string jar_file = java_dir + sep + "octave.jar";
332
333 file_stat jar_exists (jar_file);
334
335 if (jar_exists)
336 {
337 // initialize static classpath to octave.jar
338 retval = jar_file;
339
340 // The base classpath has been set. Try to find the optional
341 // file "classpath.txt" in two places. The users classes will
342 // take precedence over the settings defined in the package
343 // directory
344
345 std::string str_filename = "classpath.txt";
346 std::string cp_file;
347 file_stat cp_exists;
348
349 // Try to read the file "classpath.txt" in the user's home
350 // directory.
351
352 std::string home_dir = "~" + sep + str_filename;
353 cp_file = file_ops::tilde_expand (home_dir);
354 cp_exists = file_stat (cp_file);
355 if (cp_exists)
356 {
357 // The file "classpath.txt" has been found: add its
358 // contents to the static classpath.
359
360 std::string theClassPath = read_classpath_txt (cp_file);
361 retval.append (theClassPath);
362 }
363
364 // Try to read a file "classpath.txt" in the package directory.
365
366 cp_file = java_dir + sep + str_filename;
367 cp_exists = file_stat (cp_file);
368 if (cp_exists)
369 {
370 // The file "classpath.txt" has been found: add its
371 // contents to the static classpath.
372
373 std::string theClassPath = read_classpath_txt (cp_file);
374 retval.append (theClassPath);
375 }
376 }
377 else
378 throw std::string ("octave jar does not exist: ") + jar_file;
379 }
380 else
381 throw std::string ("initial java dir is empty");
382
383 return retval;
384 }
385
386 static void
387 initialize_jvm (void)
388 {
389 JNIEnv *current_env;
390
391 if (jvm)
392 return;
393
394 const char *static_locale = setlocale (LC_ALL, NULL);
395 const std::string locale (static_locale);
396
397 #if defined (__WIN32__)
398
399 HMODULE hMod = GetModuleHandle ("jvm.dll");
400 std::string jvm_lib_path;
401 std::string old_cwd;
402
403 if (hMod == NULL)
404 {
405 // In windows, find the location of the JRE from the registry
406 // and load the symbol from the dll.
407 std::string key, value;
408
409 key = "software\\javasoft\\java runtime environment";
410
411 value = octave_env::getenv ("JAVA_VERSION");
412 if (value.empty ())
413 {
414 value = "Currentversion";
415 std::string regval = read_registry_string (key,value);
416
417 if (regval.empty ())
418 throw std::string ("unable to find Java Runtime Environment: ") + key + "::" + value;
419 value = regval;
420 }
421
422 key = key + "\\" + value;
423 value = "RuntimeLib";
424 jvm_lib_path = read_registry_string (key, value);
425 if (jvm_lib_path.empty ())
426 throw std::string ("unable to find Java Runtime Environment: ") + key + "::" + value;
427
428 std::string jvm_bin_path;
429
430 value = "JavaHome";
431 jvm_bin_path = read_registry_string (key, value);
432 if (! jvm_bin_path.empty ())
433 {
434 jvm_bin_path = (jvm_bin_path + std::string ("\\bin"));
435
436 old_cwd = octave_env::get_current_directory ();
437
438 set_dll_directory (jvm_bin_path);
439 octave_env::chdir (jvm_bin_path);
440 }
441 }
442 else
443 {
444 // JVM seems to be already loaded, better to use that DLL instead
445 // of looking in the registry, to avoid opening a different JVM.
446 jvm_lib_path = get_module_filename (hMod);
447
448 if (jvm_lib_path.empty ())
449 throw std::string ("unable to find Java Runtime Environment");
450 }
451
452 #else
453
454 std::string jvm_lib_path = JAVA_HOME + std::string ("/jre/lib/") + JAVA_ARCH + "/server/libjvm.so";
455
456 #endif
457
458 jsize nVMs = 0;
459
460 # if !defined (__APPLE__) && !defined (__MACH__)
461
462 octave_shlib lib (jvm_lib_path);
463
464 if (!lib)
465 throw std::string ("unable to load Java Runtime Environment from ") + jvm_lib_path;
466
467 #if defined (__WIN32__)
468
469 set_dll_directory ();
470
471 if (! old_cwd.empty ())
472 octave_env::chdir (old_cwd);
473
474 #endif
475
476 JNI_CreateJavaVM_t create_vm = reinterpret_cast<JNI_CreateJavaVM_t> (lib.search ("JNI_CreateJavaVM"));
477 JNI_GetCreatedJavaVMs_t get_vm = reinterpret_cast<JNI_GetCreatedJavaVMs_t> (lib.search ("JNI_GetCreatedJavaVMs"));
478
479 if (!create_vm)
480 throw std::string ("unable to find JNI_CreateJavaVM in ") + jvm_lib_path;
481
482 if (!get_vm)
483 throw std::string ("unable to find JNI_GetCreatedJavaVMs in ") + jvm_lib_path;
484
485 if (get_vm (&jvm, 1, &nVMs) == 0 && nVMs > 0)
486
487 #else
488
489 // FIXME: There exists a problem on the Mac platform that
490 // octave_shlib lib (jvm_lib_path)
491 // doesn't work with 'not-bundled' *.oct files.
492
493 if (JNI_GetCreatedJavaVMs (&jvm, 1, &nVMs) == 0 && nVMs > 0)
494
495 #endif
496
497 {
498 // At least one JVM exists, try to attach to it
499
500 switch (jvm->GetEnv (reinterpret_cast<void **> (&current_env), JNI_VERSION_1_2))
501 {
502 case JNI_EDETACHED:
503 // Attach the current thread
504 JavaVMAttachArgs vm_args;
505 vm_args.version = JNI_VERSION_1_2;
506 vm_args.name = const_cast<char *> ("octave");
507 vm_args.group = NULL;
508 if (jvm->AttachCurrentThread (reinterpret_cast<void **> (&current_env), &vm_args) < 0)
509 throw std::string ("JVM internal error, unable to attach octave to existing JVM");
510 break;
511
512 case JNI_EVERSION:
513 throw std::string ("JVM internal error, the required JNI version is not supported");
514 break;
515
516 case JNI_OK:
517 // Don't do anything, the current thread is already attached to JVM
518 break;
519 }
520
521 jvm_attached = true;
522 //printf ("JVM attached\n");
523 }
524 else
525 {
526 // No JVM exists, create one
527
528 JVMArgs vm_args;
529
530 vm_args.add ("-Djava.class.path=" + initial_class_path ());
531 vm_args.add ("-Xrs");
532 vm_args.add ("-Djava.system.class.loader=org.octave.OctClassLoader");
533 vm_args.read_java_opts (initial_java_dir () + file_ops::dir_sep_str () + "java.opts");
534
535 # if !defined (__APPLE__) && !defined (__MACH__)
536
537 if (create_vm (&jvm, &current_env, vm_args.to_args ()) != JNI_OK)
538 throw std::string ("unable to start Java VM in ")+jvm_lib_path;
539 //printf ("JVM created\n");
540 }
541
542 jvm_lib = lib;
543
544 #else
545
546 if (JNI_CreateJavaVM (&jvm, reinterpret_cast<void **> (&current_env),
547 vm_args.to_args ()) != JNI_OK)
548 throw std::string ("unable to start Java VM in ")+jvm_lib_path;
549
550 }
551
552 #endif
553
554 setlocale (LC_ALL, locale.c_str ());
555 }
556
557 static void
558 terminate_jvm (void)
559 {
560 if (jvm)
561 {
562 if (jvm_attached)
563 jvm->DetachCurrentThread ();
564 else
565 jvm->DestroyJavaVM ();
566
567 jvm = 0;
568 jvm_attached = false;
569
570 if (jvm_lib)
571 jvm_lib.close ();
572 }
573 }
574
575 std::string
576 jstring_to_string (JNIEnv* jni_env, jstring s)
577 {
578 std::string retval;
579
580 if (jni_env)
581 {
582 const char *cstr = jni_env->GetStringUTFChars (s, 0);
583 retval = cstr;
584 jni_env->ReleaseStringUTFChars (s, cstr);
585 }
586
587 return retval;
588 }
589
590 std::string
591 jstring_to_string (JNIEnv* jni_env, jobject obj)
592 {
593 std::string retval;
594
595 if (jni_env && obj)
596 {
597 jclass_ref cls (jni_env, jni_env->FindClass ("java/lang/String"));
598 if (cls)
599 {
600 if (jni_env->IsInstanceOf (obj, cls))
601 retval = jstring_to_string (jni_env, reinterpret_cast<jstring> (obj));
602 }
603 }
604
605 return retval;
606 }
607
608 static octave_value
609 check_exception (JNIEnv* jni_env)
610 {
611 octave_value retval;
612
613 jthrowable_ref ex (jni_env, jni_env->ExceptionOccurred ());
614
615 if (ex)
616 {
617 if (Vjava_debug)
618 jni_env->ExceptionDescribe ();
619
620 jni_env->ExceptionClear ();
621
622 jclass_ref jcls (jni_env, jni_env->GetObjectClass (ex));
623 jmethodID mID = jni_env->GetMethodID (jcls, "toString", "()Ljava/lang/String;");
624 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (ex, mID)));
625 std::string msg = jstring_to_string (jni_env, js);
626
627 error ("[java] %s", msg.c_str ());
628 }
629 else
630 retval = Matrix ();
631
632 return retval;
633 }
634
635 static jclass
636 find_octave_class (JNIEnv *jni_env, const char *name)
637 {
638 static std::string class_loader;
639 static jclass uiClass = 0;
640
641 jclass jcls = jni_env->FindClass (name);
642
643 if (jcls == 0)
644 {
645 jni_env->ExceptionClear ();
646
647 if (! uiClass)
648 {
649 if (class_loader.empty ())
650 {
651 jclass_ref syscls (jni_env, jni_env->FindClass ("java/lang/System"));
652 jmethodID mID = jni_env->GetStaticMethodID (syscls, "getProperty", "(Ljava/lang/String;)Ljava/lang/String;");
653 jstring_ref js (jni_env, jni_env->NewStringUTF ("octave.class.loader"));
654 js = reinterpret_cast<jstring> (jni_env->CallStaticObjectMethod (syscls, mID, jstring (js)));
655 class_loader = jstring_to_string (jni_env, jstring (js));
656 std::replace (class_loader.begin (), class_loader.end (), '.', '/');
657 }
658
659 jclass_ref uicls (jni_env, jni_env->FindClass (class_loader.c_str ()));
660
661 if (! uicls)
662 {
663 jni_env->ExceptionClear ();
664
665 /* Try the netbeans way */
666 std::replace (class_loader.begin (), class_loader.end (), '/', '.');
667 jclass_ref jcls2 (jni_env, jni_env->FindClass ("org/openide/util/Lookup"));
668 jmethodID mID = jni_env->GetStaticMethodID (jcls2, "getDefault", "()Lorg/openide/util/Lookup;");
669 jobject_ref lObj (jni_env, jni_env->CallStaticObjectMethod (jcls2, mID));
670 mID = jni_env->GetMethodID (jcls2, "lookup", "(Ljava/lang/Class;)Ljava/lang/Object;");
671 jclass_ref cLoaderCls (jni_env, jni_env->FindClass ("java/lang/ClassLoader"));
672 jobject_ref cLoader (jni_env, jni_env->CallObjectMethod (lObj, mID, jclass (cLoaderCls)));
673 mID = jni_env->GetMethodID (cLoaderCls, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
674 jstring_ref js (jni_env, jni_env->NewStringUTF (class_loader.c_str ()));
675 uicls = reinterpret_cast<jclass> (jni_env->CallObjectMethod (cLoader, mID, jstring (js)));
676 }
677
678 if (uicls)
679 uiClass = reinterpret_cast<jclass> (jni_env->NewGlobalRef (jclass (uicls)));
680 }
681
682 if (uiClass)
683 {
684 jmethodID mID = jni_env->GetStaticMethodID (uiClass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
685 jstring_ref js (jni_env, jni_env->NewStringUTF (name));
686 jcls = reinterpret_cast<jclass> (jni_env->CallStaticObjectMethod (uiClass, mID, jstring (js)));
687 }
688 }
689
690 return jcls;
691 }
692
693 static dim_vector
694 compute_array_dimensions (JNIEnv* jni_env, jobject obj)
695 {
696 jobjectArray_ref jobj (jni_env, reinterpret_cast<jobjectArray> (obj));
697 jclass_ref jcls (jni_env, jni_env->GetObjectClass (obj));
698 jclass_ref ccls (jni_env, jni_env->GetObjectClass (jcls));
699 jmethodID isArray_ID = jni_env->GetMethodID (ccls, "isArray", "()Z"),
700 getComponentType_ID = jni_env->GetMethodID (ccls, "getComponentType", "()Ljava/lang/Class;");
701
702 dim_vector dv (1, 1);
703 int idx = 0;
704
705 jobj.detach ();
706 while (jcls && jni_env->CallBooleanMethod (jcls, isArray_ID))
707 {
708 int len = (jobj ? jni_env->GetArrayLength (jobj) : 0);
709 if (idx >= dv.length ())
710 dv.resize (idx+1);
711 dv(idx) = len;
712 jcls = reinterpret_cast<jclass> (jni_env->CallObjectMethod (jcls, getComponentType_ID));
713 jobj = (len > 0 ? reinterpret_cast<jobjectArray> (jni_env->GetObjectArrayElement (jobj, 0)) : 0);
714 idx++;
715 }
716
717 return dv;
718 }
719
720 static jobject
721 make_java_index (JNIEnv* jni_env, const octave_value_list& idx)
722 {
723 jclass_ref ocls (jni_env, jni_env->FindClass ("[I"));
724 jobjectArray retval = jni_env->NewObjectArray (idx.length (), ocls, 0);
725
726 for (int i = 0; i < idx.length (); i++)
727 {
728 idx_vector v = idx(i).index_vector ();
729
730 if (! error_state)
731 {
732 jintArray_ref i_array (jni_env, jni_env->NewIntArray (v.length ()));
733 jint *buf = jni_env->GetIntArrayElements (i_array, 0);
734
735 for (int k = 0; k < v.length (); k++)
736 buf[k] = v(k);
737
738 jni_env->ReleaseIntArrayElements (i_array, buf, 0);
739 jni_env->SetObjectArrayElement (retval, i, i_array);
740
741 check_exception (jni_env);
742
743 if (error_state)
744 break;
745 }
746 else
747 break;
748 }
749
750 return retval;
751 }
752
753 static octave_value
754 get_array_elements (JNIEnv* jni_env, jobject jobj,
755 const octave_value_list& idx)
756 {
757 octave_value retval;
758 jobject_ref resObj (jni_env);
759 jobject_ref java_idx (jni_env, make_java_index (jni_env, idx));
760
761 if (! error_state)
762 {
763 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
764 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "arraySubsref", "(Ljava/lang/Object;[[I)Ljava/lang/Object;");
765 resObj = jni_env->CallStaticObjectMethod (helperClass, mID, jobj, jobject (java_idx));
766 }
767
768 if (resObj)
769 retval = box (jni_env, resObj);
770 else
771 retval = check_exception (jni_env);
772
773 return retval;
774 }
775
776 static octave_value
777 set_array_elements (JNIEnv* jni_env, jobject jobj,
778 const octave_value_list& idx, const octave_value& rhs)
779 {
780 octave_value retval;
781
782 jclass_ref rhsCls (jni_env);
783 jobject_ref resObj (jni_env), rhsObj (jni_env);
784 jobject_ref java_idx (jni_env, make_java_index (jni_env, idx));
785
786 if (! error_state && unbox (jni_env, rhs, rhsObj, rhsCls))
787 {
788 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
789 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "arraySubsasgn",
790 "(Ljava/lang/Object;[[ILjava/lang/Object;)Ljava/lang/Object;");
791 resObj = jni_env->CallStaticObjectMethod (helperClass, mID,
792 jobj, jobject (java_idx), jobject (rhsObj));
793 }
794
795 if (resObj)
796 retval = box (jni_env, resObj);
797 else
798 retval = check_exception (jni_env);
799
800 return retval;
801 }
802
803 static string_vector
804 get_invoke_list (JNIEnv* jni_env, jobject jobj)
805 {
806 std::list<std::string> name_list;
807
808 if (jni_env)
809 {
810 jclass_ref cls (jni_env, jni_env->GetObjectClass (jobj));
811 jclass_ref ccls (jni_env, jni_env->GetObjectClass (cls));
812 jmethodID getMethods_ID = jni_env->GetMethodID (ccls, "getMethods", "()[Ljava/lang/reflect/Method;"),
813 getFields_ID = jni_env->GetMethodID (ccls, "getFields", "()[Ljava/lang/reflect/Field;");
814 jobjectArray_ref mList (jni_env, reinterpret_cast<jobjectArray> (jni_env->CallObjectMethod (cls, getMethods_ID))),
815 fList (jni_env, reinterpret_cast<jobjectArray> (jni_env->CallObjectMethod (cls, getFields_ID)));
816 int mLen = jni_env->GetArrayLength (mList), fLen = jni_env->GetArrayLength (fList);
817 jclass_ref mCls (jni_env, jni_env->FindClass ("java/lang/reflect/Method")),
818 fCls (jni_env, jni_env->FindClass ("java/lang/reflect/Field"));
819 jmethodID m_getName_ID = jni_env->GetMethodID (mCls, "getName", "()Ljava/lang/String;"),
820 f_getName_ID = jni_env->GetMethodID (fCls, "getName", "()Ljava/lang/String;");
821
822 for (int i = 0; i < mLen; i++)
823 {
824 jobject_ref meth (jni_env, jni_env->GetObjectArrayElement (mList, i));
825 jstring_ref methName (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (meth, m_getName_ID)));
826 name_list.push_back (jstring_to_string (jni_env, methName));
827 }
828
829 for (int i = 0; i < fLen; i++)
830 {
831 jobject_ref field (jni_env, jni_env->GetObjectArrayElement (fList, i));
832 jstring_ref fieldName (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (field, f_getName_ID)));
833 name_list.push_back (jstring_to_string (jni_env, fieldName));
834 }
835 }
836
837 string_vector v (name_list);
838
839 return v.sort (true);
840 }
841
842 static octave_value
843 convert_to_string (JNIEnv *jni_env, jobject java_object, bool force, char type)
844 {
845 octave_value retval;
846
847 if (jni_env && java_object)
848 {
849 jclass_ref cls (jni_env, jni_env->FindClass ("java/lang/String"));
850
851 if (jni_env->IsInstanceOf (java_object, cls))
852 retval = octave_value (jstring_to_string (jni_env, java_object), type);
853 else if (force)
854 {
855 cls = jni_env->FindClass ("[Ljava/lang/String;");
856
857 if (jni_env->IsInstanceOf (java_object, cls))
858 {
859 jobjectArray array = reinterpret_cast<jobjectArray> (java_object);
860 int len = jni_env->GetArrayLength (array);
861 Cell c (len, 1);
862
863 for (int i = 0; i < len; i++)
864 {
865 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->GetObjectArrayElement (array, i)));
866
867 if (js)
868 c(i) = octave_value (jstring_to_string (jni_env, js), type);
869 else
870 {
871 c(i) = check_exception (jni_env);
872
873 if (error_state)
874 break;
875 }
876 }
877
878 retval = octave_value (c);
879 }
880 else
881 {
882 cls = jni_env->FindClass ("java/lang/Object");
883 jmethodID mID = jni_env->GetMethodID (cls, "toString", "()Ljava/lang/String;");
884 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (java_object, mID)));
885
886 if (js)
887 retval = octave_value (jstring_to_string (jni_env, js), type);
888 else
889 retval = check_exception (jni_env);
890 }
891 }
892 else
893 error ("unable to convert Java object to string");
894 }
895
896 return retval;
897 }
898
899 #define TO_JAVA(obj) dynamic_cast<octave_java*> ((obj).internal_rep ())
900
901 octave_value
902 box (JNIEnv* jni_env, jobject jobj, jclass jcls)
903 {
904 octave_value retval;
905 jclass_ref cls (jni_env);
906
907 if (! jobj)
908 retval = Matrix ();
909
910 if (retval.is_undefined ())
911 {
912 cls = jni_env->FindClass ("java/lang/Integer");
913
914 if (jni_env->IsInstanceOf (jobj, cls))
915 {
916 jmethodID m = jni_env->GetMethodID (cls, "intValue", "()I");
917 retval = jni_env->CallIntMethod (jobj, m);
918 }
919 }
920
921 if (retval.is_undefined ())
922 {
923 cls = jni_env->FindClass ("java/lang/Double");
924
925 if (jni_env->IsInstanceOf (jobj, cls))
926 {
927 jmethodID m = jni_env->GetMethodID (cls, "doubleValue", "()D");
928 retval = jni_env->CallDoubleMethod (jobj, m);
929 }
930 }
931
932 if (retval.is_undefined ())
933 {
934 cls = jni_env->FindClass ("java/lang/Boolean");
935
936 if (jni_env->IsInstanceOf (jobj, cls))
937 {
938 jmethodID m = jni_env->GetMethodID (cls, "booleanValue", "()Z");
939 // MH retval = jni_env->CallBooleanMethod (jobj, m);
940 retval = (jni_env->CallBooleanMethod (jobj, m) ? true : false);
941 }
942 }
943
944 if (retval.is_undefined ())
945 {
946 cls = jni_env->FindClass ("java/lang/String");
947
948 if (jni_env->IsInstanceOf (jobj, cls))
949 {
950 retval = jstring_to_string (jni_env, jobj);
951 }
952 }
953
954 if (retval.is_undefined () && Vjava_convert_matrix)
955 {
956 cls = find_octave_class (jni_env, "org/octave/Matrix");
957
958 if (jni_env->IsInstanceOf (jobj, cls))
959 {
960 jmethodID mID = jni_env->GetMethodID (cls, "getDims", "()[I");
961 jintArray_ref iv (jni_env, reinterpret_cast<jintArray> (jni_env->CallObjectMethod (jobj, mID)));
962 jint *iv_data = jni_env->GetIntArrayElements (jintArray (iv), 0);
963 dim_vector dims;
964 dims.resize (jni_env->GetArrayLength (jintArray (iv)));
965
966 for (int i = 0; i < dims.length (); i++)
967 dims(i) = iv_data[i];
968
969 jni_env->ReleaseIntArrayElements (jintArray (iv), iv_data, 0);
970 mID = jni_env->GetMethodID (cls, "getClassName", "()Ljava/lang/String;");
971 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->CallObjectMethod (jobj, mID)));
972
973 std::string s = jstring_to_string (jni_env, js);
974
975 if (s == "double")
976 {
977 NDArray m (dims);
978 mID = jni_env->GetMethodID (cls, "toDouble", "()[D");
979 jdoubleArray_ref dv (jni_env, reinterpret_cast<jdoubleArray> (jni_env->CallObjectMethod (jobj, mID)));
980 jni_env->GetDoubleArrayRegion (dv, 0, m.length (), m.fortran_vec ());
981 retval = m;
982 }
983 else if (s == "byte")
984 {
985 if (Vjava_unsigned_conversion)
986 {
987 uint8NDArray m (dims);
988 mID = jni_env->GetMethodID (cls, "toByte", "()[B");
989 jbyteArray_ref dv (jni_env, reinterpret_cast<jbyteArray> (jni_env->CallObjectMethod (jobj, mID)));
990 jni_env->GetByteArrayRegion (dv, 0, m.length (), reinterpret_cast<jbyte *> (m.fortran_vec ()));
991 retval = m;
992 }
993 else
994 {
995 int8NDArray m (dims);
996 mID = jni_env->GetMethodID (cls, "toByte", "()[B");
997 jbyteArray_ref dv (jni_env, reinterpret_cast<jbyteArray> (jni_env->CallObjectMethod (jobj, mID)));
998 jni_env->GetByteArrayRegion (dv, 0, m.length (), reinterpret_cast<jbyte *> (m.fortran_vec ()));
999 retval = m;
1000 }
1001 }
1002 else if (s == "integer")
1003 {
1004 if (Vjava_unsigned_conversion)
1005 {
1006 uint32NDArray m (dims);
1007 mID = jni_env->GetMethodID (cls, "toInt", "()[I");
1008 jintArray_ref dv (jni_env, reinterpret_cast<jintArray> (jni_env->CallObjectMethod (jobj, mID)));
1009 jni_env->GetIntArrayRegion (dv, 0, m.length (), reinterpret_cast<jint *> (m.fortran_vec ()));
1010 retval = m;
1011 }
1012 else
1013 {
1014 int32NDArray m (dims);
1015 mID = jni_env->GetMethodID (cls, "toInt", "()[I");
1016 jintArray_ref dv (jni_env, reinterpret_cast<jintArray> (jni_env->CallObjectMethod (jobj, mID)));
1017 jni_env->GetIntArrayRegion (dv, 0, m.length (), reinterpret_cast<jint *> (m.fortran_vec ()));
1018 retval = m;
1019 }
1020 }
1021 }
1022 }
1023
1024 if (retval.is_undefined ())
1025 {
1026 cls = find_octave_class (jni_env, "org/octave/OctaveReference");
1027
1028 if (jni_env->IsInstanceOf (jobj, cls))
1029 {
1030 jmethodID mID = jni_env->GetMethodID (cls, "getID", "()I");
1031 int ID = jni_env->CallIntMethod (jobj, mID);
1032 std::map<int,octave_value>::iterator it = octave_ref_map.find (ID);
1033
1034 if (it != octave_ref_map.end ())
1035 retval = it->second;
1036 }
1037 }
1038
1039 if (retval.is_undefined ())
1040 retval = octave_value (new octave_java (jobj, jcls));
1041
1042 return retval;
1043 }
1044
1045 octave_value
1046 box_more (JNIEnv* jni_env, jobject jobj, jclass jcls)
1047 {
1048 octave_value retval = box (jni_env, jobj, jcls);
1049
1050 if (retval.class_name () == "octave_java")
1051 {
1052 retval = octave_value ();
1053
1054 jclass_ref cls (jni_env);
1055
1056 if (retval.is_undefined ())
1057 {
1058 cls = jni_env->FindClass ("[D");
1059
1060 if (jni_env->IsInstanceOf (jobj, cls))
1061 {
1062 jdoubleArray jarr = reinterpret_cast<jdoubleArray> (jobj);
1063 int len = jni_env->GetArrayLength (jarr);
1064
1065 if (len > 0)
1066 {
1067 Matrix m (1, len);
1068 jni_env->GetDoubleArrayRegion (jarr, 0, len, m.fortran_vec ());
1069 retval = m;
1070 }
1071 else
1072 retval = Matrix ();
1073 }
1074 }
1075
1076 if (retval.is_undefined ())
1077 {
1078 cls = jni_env->FindClass ("[[D");
1079
1080 if (jni_env->IsInstanceOf (jobj, cls))
1081 {
1082 jobjectArray jarr = reinterpret_cast<jobjectArray> (jobj);
1083 int rows = jni_env->GetArrayLength (jarr), cols = 0;
1084
1085 if (rows > 0)
1086 {
1087 Matrix m;
1088
1089 for (int r = 0; r < rows; r++)
1090 {
1091 jdoubleArray_ref row (jni_env, reinterpret_cast<jdoubleArray> (jni_env->GetObjectArrayElement (jarr, r)));
1092
1093 if (m.length () == 0)
1094 {
1095 cols = jni_env->GetArrayLength (row);
1096 m.resize (cols, rows);
1097 }
1098 jni_env->GetDoubleArrayRegion (row, 0, cols, m.fortran_vec () + r * cols);
1099 }
1100 retval = m.transpose ();
1101 }
1102 else
1103 retval = Matrix ();
1104 }
1105 }
1106
1107 if (retval.is_undefined ())
1108 {
1109 cls = jni_env->FindClass ("[Ljava/lang/String;");
1110
1111 if (jni_env->IsInstanceOf (jobj, cls))
1112 {
1113 jobjectArray jarr = reinterpret_cast<jobjectArray> (jobj);
1114 int len = jni_env->GetArrayLength (jarr);
1115 Cell m (len, 1);
1116
1117 for (int i = 0; i < len; i++)
1118 {
1119 jstring_ref js (jni_env, reinterpret_cast<jstring> (jni_env->GetObjectArrayElement (jarr, i)));
1120 m(i) = jstring_to_string (jni_env, js);
1121 }
1122
1123 retval = m;
1124 }
1125 }
1126 }
1127
1128 if (retval.is_undefined ())
1129 retval = octave_value (new octave_java (jobj, jcls));
1130
1131 return retval;
1132 }
1133
1134 int
1135 unbox (JNIEnv* jni_env, const octave_value& val, jobject_ref& jobj,
1136 jclass_ref& jcls)
1137 {
1138 int found = 1;
1139
1140 if (val.class_name () == "octave_java")
1141 {
1142 octave_java *ovj = TO_JAVA (val);
1143 jobj = ovj->to_java ();
1144 jobj.detach ();
1145 jcls = jni_env->GetObjectClass (jobj);
1146 }
1147 else if (val.is_string ())
1148 {
1149 std::string s = val.string_value ();
1150
1151 jobj = jni_env->NewStringUTF (s.c_str ());
1152 jcls = jni_env->GetObjectClass (jobj);
1153 }
1154 else if (val.is_bool_scalar ())
1155 {
1156 bool bval = val.bool_value ();
1157 jclass_ref bcls (jni_env, jni_env->FindClass ("java/lang/Boolean"));
1158 jfieldID fid = jni_env->GetStaticFieldID (bcls, "TYPE", "Ljava/lang/Class;");
1159 jmethodID mid = jni_env->GetMethodID (bcls, "<init>", "(Z)V");
1160 jcls = reinterpret_cast<jclass> (jni_env->GetStaticObjectField (bcls, fid));
1161 jobj = jni_env->NewObject (bcls, mid, bval);
1162 }
1163 else if (val.is_real_scalar ())
1164 {
1165 double dval = val.double_value ();
1166 jclass_ref dcls (jni_env, jni_env->FindClass ("java/lang/Double"));
1167 jfieldID fid = jni_env->GetStaticFieldID (dcls, "TYPE", "Ljava/lang/Class;");
1168 jmethodID mid = jni_env->GetMethodID (dcls, "<init>", "(D)V");
1169 jcls = reinterpret_cast<jclass> (jni_env->GetStaticObjectField (dcls, fid));
1170 jobj = jni_env->NewObject (dcls, mid, dval);
1171 }
1172 else if (val.is_empty ())
1173 {
1174 jobj = 0;
1175 //jcls = jni_env->FindClass ("java/lang/Object");
1176 jcls = 0;
1177 }
1178 else if (!Vjava_convert_matrix
1179 && ((val.is_real_matrix ()
1180 && (val.rows () == 1 || val.columns () == 1))
1181 || val.is_range ()))
1182 {
1183 Matrix m = val.matrix_value ();
1184 jdoubleArray dv = jni_env->NewDoubleArray (m.length ());
1185 //for (int i = 0; i < m.length (); i++)
1186 jni_env->SetDoubleArrayRegion (dv, 0, m.length (), m.fortran_vec ());
1187 jobj = dv;
1188 jcls = jni_env->GetObjectClass (jobj);
1189 }
1190 else if (Vjava_convert_matrix
1191 && (val.is_matrix_type () || val.is_range ()) && val.is_real_type ())
1192 {
1193 jclass_ref mcls (jni_env, find_octave_class (jni_env, "org/octave/Matrix"));
1194 dim_vector dims = val.dims ();
1195 jintArray_ref iv (jni_env, jni_env->NewIntArray (dims.length ()));
1196 jint *iv_data = jni_env->GetIntArrayElements (jintArray (iv), 0);
1197
1198 for (int i = 0; i < dims.length (); i++)
1199 iv_data[i] = dims(i);
1200
1201 jni_env->ReleaseIntArrayElements (jintArray (iv), iv_data, 0);
1202
1203 if (val.is_double_type ())
1204 {
1205 NDArray m = val.array_value ();
1206 jdoubleArray_ref dv (jni_env, jni_env->NewDoubleArray (m.length ()));
1207 jni_env->SetDoubleArrayRegion (jdoubleArray (dv), 0, m.length (), m.fortran_vec ());
1208 jmethodID mID = jni_env->GetMethodID (mcls, "<init>", "([D[I)V");
1209 jobj = jni_env->NewObject (jclass (mcls), mID, jdoubleArray (dv), jintArray (iv));
1210 jcls = jni_env->GetObjectClass (jobj);
1211 }
1212 else if (val.is_int8_type ())
1213 {
1214 int8NDArray m = val.int8_array_value ();
1215 jbyteArray_ref bv (jni_env, jni_env->NewByteArray (m.length ()));
1216 jni_env->SetByteArrayRegion (jbyteArray (bv), 0, m.length (), reinterpret_cast <jbyte *> (m.fortran_vec ()));
1217 jmethodID mID = jni_env->GetMethodID (mcls, "<init>", "([B[I)V");
1218 jobj = jni_env->NewObject (jclass (mcls), mID, jbyteArray (bv), jintArray (iv));
1219 jcls = jni_env->GetObjectClass (jobj);
1220 }
1221 else if (val.is_uint8_type ())
1222 {
1223 uint8NDArray m = val.uint8_array_value ();
1224 jbyteArray_ref bv (jni_env, jni_env->NewByteArray (m.length ()));
1225 jni_env->SetByteArrayRegion (jbyteArray (bv), 0, m.length (), reinterpret_cast<jbyte *> (m.fortran_vec ()));
1226 jmethodID mID = jni_env->GetMethodID (mcls, "<init>", "([B[I)V");
1227 jobj = jni_env->NewObject (jclass (mcls), mID, jbyteArray (bv), jintArray (iv));
1228 jcls = jni_env->GetObjectClass (jobj);
1229 }
1230 else if (val.is_int32_type ())
1231 {
1232 int32NDArray m = val.int32_array_value ();
1233 jintArray_ref v (jni_env, jni_env->NewIntArray (m.length ()));
1234 jni_env->SetIntArrayRegion (jintArray (v), 0, m.length (), reinterpret_cast<jint *> (m.fortran_vec ()));
1235 jmethodID mID = jni_env->GetMethodID (mcls, "<init>", "([I[I)V");
1236 jobj = jni_env->NewObject (jclass (mcls), mID, jintArray (v), jintArray (iv));
1237 jcls = jni_env->GetObjectClass (jobj);
1238 }
1239 else
1240 {
1241 found = 0;
1242 error ("cannot convert matrix of type `%s'", val.class_name ().c_str ());
1243 }
1244 }
1245 else if (val.is_cellstr ())
1246 {
1247 Cell cellStr = val.cell_value ();
1248 jclass_ref scls (jni_env, jni_env->FindClass ("java/lang/String"));
1249 jobjectArray array = jni_env->NewObjectArray (cellStr.length (), scls, 0);
1250 for (int i = 0; i < cellStr.length (); i++)
1251 {
1252 jstring_ref jstr (jni_env, jni_env->NewStringUTF (cellStr(i).string_value().c_str ()));
1253 jni_env->SetObjectArrayElement (array, i, jstr);
1254 }
1255 jobj = array;
1256 jcls = jni_env->GetObjectClass (jobj);
1257 }
1258 else
1259 {
1260 jclass rcls = find_octave_class (jni_env, "org/octave/OctaveReference");
1261 jmethodID mID = jni_env->GetMethodID (rcls, "<init>", "(I)V");
1262 int ID = octave_java_refcount++;
1263
1264 jobj = jni_env->NewObject (rcls, mID, ID);
1265 jcls = rcls;
1266 octave_ref_map[ID] = val;
1267 }
1268
1269 return found;
1270 }
1271
1272 int
1273 unbox (JNIEnv* jni_env, const octave_value_list& args,
1274 jobjectArray_ref& jobjs, jobjectArray_ref& jclss)
1275 {
1276 int found = 1;
1277
1278 jclass_ref ocls (jni_env, jni_env->FindClass ("java/lang/Object"));
1279 jclass_ref ccls (jni_env, jni_env->FindClass ("java/lang/Class"));
1280
1281 if (! jobjs)
1282 jobjs = jni_env->NewObjectArray (args.length (), ocls, 0);
1283
1284 if (! jclss)
1285 jclss = jni_env->NewObjectArray (args.length (), ccls, 0);
1286
1287 for (int i = 0; i < args.length (); i++)
1288 {
1289 jobject_ref jobj (jni_env);
1290 jclass_ref jcls (jni_env);
1291
1292 if (! unbox (jni_env, args(i), jobj, jcls))
1293 {
1294 found = 0;
1295 break;
1296 }
1297
1298 jni_env->SetObjectArrayElement (jobjs, i, jobj);
1299 jni_env->SetObjectArrayElement (jclss, i, jcls);
1300 }
1301
1302 return found;
1303 }
1304
1305
1306 static long
1307 get_current_thread_ID (JNIEnv *jni_env)
1308 {
1309 if (jni_env)
1310 {
1311 jclass_ref cls (jni_env, jni_env->FindClass ("java/lang/Thread"));
1312 jmethodID mID = jni_env->GetStaticMethodID (cls, "currentThread", "()Ljava/lang/Thread;");
1313 jobject_ref jthread (jni_env, jni_env->CallStaticObjectMethod (cls, mID));
1314
1315 if (jthread)
1316 {
1317 jclass_ref jth_cls (jni_env, jni_env->GetObjectClass (jthread));
1318 mID = jni_env->GetMethodID (jth_cls, "getId", "()J");
1319 long result = jni_env->CallLongMethod (jthread, mID);
1320 //printf ("current java thread ID = %ld\n", result);
1321 return result;
1322 }
1323 }
1324
1325 return -1;
1326 }
1327
1328 static int
1329 java_event_hook (void)
1330 {
1331 JNIEnv *current_env = octave_java::thread_jni_env ();
1332
1333 if (current_env)
1334 {
1335 jclass_ref cls (current_env, find_octave_class (current_env, "org/octave/Octave"));
1336 jmethodID mID = current_env->GetStaticMethodID (cls, "checkPendingAction", "()V");
1337 current_env->CallStaticVoidMethod (cls, mID);
1338 }
1339
1340 return 0;
1341 }
1342
1343 static void
1344 initialize_java (void)
1345 {
1346 if (! jvm)
1347 {
1348 try
1349 {
1350 initialize_jvm ();
1351
1352 JNIEnv *current_env = octave_java::thread_jni_env ();
1353
1354 octave_java::register_type ();
1355 command_editor::add_event_hook (java_event_hook);
1356 octave_thread_ID = get_current_thread_ID (current_env);
1357 //printf ("octave thread ID=%ld\n", octave_thread_ID);
1358 }
1359 catch (std::string msg)
1360 {
1361 error (msg.c_str ());
1362 }
1363 }
1364 }
1365
1366 DEFUN (java_init, , ,
1367 "-*- texinfo -*-\n\
1368 @deftypefn {Loadable Function} {} java_init ()\n\
1369 Undocumented internal function.\n\
1370 @end deftypefn")
1371 {
1372 octave_value retval;
1373
1374 retval = 0;
1375 initialize_java ();
1376 if (! error_state)
1377 retval = 1;
1378
1379 return retval;
1380 }
1381
1382 DEFUN (java_exit, , ,
1383 "-*- texinfo -*-\n\
1384 @deftypefn {Loadable Function} {} java_exit ()\n\
1385 Undocumented internal function.\n\
1386 @end deftypefn")
1387 {
1388 octave_value retval;
1389
1390 terminate_jvm ();
1391
1392 return retval;
1393 }
1394
1395 DEFUN (java_new, args, ,
1396 "-*- texinfo -*-\n\
1397 @deftypefn {Loadable Function} {@var{obj} =} java_new (@var{name}, @var{arg1}, ...)\n\
1398 Create a Java object of class @var{name}, by calling the class constructor with the\n\
1399 arguments @var{arg1}, ...\n\
1400 \n\
1401 @example\n\
1402 x = java_new (\"java.lang.StringBuffer\", \"Initial string\")\n\
1403 @end example\n\
1404 \n\
1405 @seealso{java_invoke, java_get, java_set}\n\
1406 @end deftypefn")
1407 {
1408 return _java_new (args);
1409 }
1410
1411 DEFUN (javaObject, args, ,
1412 "-*- texinfo -*-\n\
1413 @deftypefn {Loadable Function} {@var{obj} =} javaObject (@var{name}, @var{arg1}, ...)\n\
1414 Create a Java object of class @var{name}, by calling the class constructor with the\n\
1415 arguments @var{arg1}, ...\n\
1416 The first example creates an unitialized object, \
1417 while the second example supplies an initializer argument.\n\
1418 \n\
1419 @example\n\
1420 x = javaObject (\"java.lang.StringBuffer\")\n\
1421 x = javaObject (\"java.lang.StringBuffer\", \"Initial string\")\n\
1422 @end example\n\
1423 \n\
1424 @seealso{java_invoke, java_new, java_get, java_set}\n\
1425 @end deftypefn")
1426 {
1427 return _java_new (args);
1428 }
1429
1430 // internally called from java_new and javaObject for backward compatibility
1431 static octave_value _java_new (const octave_value_list& args)
1432 {
1433 octave_value retval;
1434
1435 initialize_java ();
1436 if (! error_state)
1437 {
1438 JNIEnv *current_env = octave_java::thread_jni_env ();
1439
1440 if (args.length () > 0)
1441 {
1442 std::string name = args(0).string_value ();
1443 if (! error_state)
1444 {
1445 octave_value_list tmp;
1446 for (int i=1; i<args.length (); i++)
1447 tmp(i-1) = args(i);
1448 retval = octave_java::do_java_create (current_env, name, tmp);
1449 }
1450 else
1451 error ("java_new: first argument must be a string");
1452 }
1453 else
1454 print_usage ();
1455 }
1456
1457 return retval;
1458 }
1459
1460 DEFUN (java_invoke, args, ,
1461 "-*- texinfo -*-\n\
1462 @deftypefn {Loadable Function} {@var{ret} =} java_invoke (@var{obj}, @var{name}, @var{arg1}, ...)\n\
1463 Invoke the method @var{name} on the Java object @var{obj} with the arguments\n\
1464 @var{arg1}, ... For static methods, @var{obj} can be a string representing the\n\
1465 fully qualified name of the corresponding class. The function returns the result\n\
1466 of the method invocation.\n\
1467 \n\
1468 When @var{obj} is a regular Java object, the structure-like indexing can be used\n\
1469 as a shortcut syntax. For instance, the two following statements are equivalent\n\
1470 \n\
1471 @example\n\
1472 ret = java_invoke (x, \"method1\", 1.0, \"a string\")\n\
1473 ret = x.method1 (1.0, \"a string\")\n\
1474 @end example\n\
1475 \n\
1476 @seealso{java_get, java_set, java_new}\n\
1477 @end deftypefn")
1478 {
1479 return _java_invoke (args);
1480 }
1481
1482 DEFUN (javaMethod, args, ,
1483 "-*- texinfo -*-\n\
1484 @deftypefn {Loadable Function} {@var{ret} =} javaMethod (@var{name}, @var{obj}, @var{arg1}, ...)\n\
1485 Invoke the method @var{name} on the Java object @var{obj} with the arguments\n\
1486 @var{arg1}, ... For static methods, @var{obj} can be a string representing the\n\
1487 fully qualified name of the corresponding class. The function returns the result\n\
1488 of the method invocation.\n\
1489 \n\
1490 When @var{obj} is a regular Java object, the structure-like indexing can be used\n\
1491 as a shortcut syntax. For instance, the two following statements are equivalent\n\
1492 \n\
1493 @example\n\
1494 ret = javaMethod (\"method1\", x, 1.0, \"a string\")\n\
1495 ret = x.method1 (1.0, \"a string\")\n\
1496 @end example\n\
1497 \n\
1498 @seealso{java_get, java_set, java_new}\n\
1499 @end deftypefn")
1500 {
1501 octave_value retval;
1502
1503 if (args.length () > 1)
1504 {
1505 // swap first two arguments
1506 octave_value_list tmp;
1507 tmp(0) = args(1);
1508 tmp(1) = args(0);
1509
1510 // copy remaining arguments
1511 for (int i=2; i<args.length (); i++)
1512 tmp(i) = args(i);
1513
1514 retval = _java_invoke (tmp);
1515 }
1516 else
1517 {
1518 print_usage ();
1519 }
1520
1521 return retval;
1522 }
1523
1524 // internally called from java_invoke and javaMethod for backward
1525 // compatibility.
1526
1527 static octave_value
1528 _java_invoke (const octave_value_list& args)
1529 {
1530 octave_value retval;
1531
1532 initialize_java ();
1533
1534 if (! error_state)
1535 {
1536 JNIEnv *current_env = octave_java::thread_jni_env ();
1537
1538 if (args.length () > 1)
1539 {
1540 std::string name = args(1).string_value ();
1541 if (! error_state)
1542 {
1543 octave_value_list tmp;
1544 for (int i=2; i<args.length (); i++)
1545 tmp(i-2) = args(i);
1546
1547 if (args(0).class_name () == "octave_java")
1548 {
1549 octave_java *jobj = TO_JAVA (args(0));
1550 retval = jobj->do_java_invoke (current_env, name, tmp);
1551 }
1552 else if (args(0).is_string ())
1553 {
1554 std::string cls = args(0).string_value ();
1555 retval = octave_java::do_java_invoke (current_env, cls, name, tmp);
1556 }
1557 else
1558 error ("java_invoke: first argument must be a Java object or a string");
1559 }
1560 else
1561 error ("java_invoke: second argument must be a string");
1562 }
1563 else
1564 print_usage ();
1565 }
1566
1567 return retval;
1568 }
1569
1570 DEFUN (java_get, args, ,
1571 "-*- texinfo -*-\n\
1572 @deftypefn {Loadable Function} {@var{val} =} java_get (@var{obj}, @var{name})\n\
1573 Get the value of the field @var{name} of the Java object @var{obj}. For\n\
1574 static fields, @var{obj} can be a string representing the fully qualified\n\
1575 name of the corresponding class.\n\
1576 \n\
1577 When @var{obj} is a regular Java object, the structure-like indexing can be used\n\
1578 as a shortcut syntax. For instance, the two following statements are equivalent\n\
1579 \n\
1580 @example\n\
1581 java_get (x, \"field1\")\n\
1582 x.field1\n\
1583 @end example\n\
1584 \n\
1585 @seealso{java_set, java_invoke, java_new}\n\
1586 @end deftypefn")
1587 {
1588 octave_value retval;
1589
1590 initialize_java ();
1591
1592 if (! error_state)
1593 {
1594 JNIEnv *current_env = octave_java::thread_jni_env ();
1595
1596 if (args.length () == 2)
1597 {
1598 std::string name = args(1).string_value ();
1599 if (! error_state)
1600 {
1601 if (args(0).class_name () == "octave_java")
1602 {
1603 octave_java *jobj = TO_JAVA (args(0));
1604 retval = jobj->do_java_get (current_env, name);
1605 }
1606 else if (args(0).is_string ())
1607 {
1608 std::string cls = args(0).string_value ();
1609 retval = octave_java::do_java_get (current_env, cls, name);
1610 }
1611 else
1612 error ("java_get: first argument must be a Java object or a string");
1613 }
1614 else
1615 error ("java_get: second argument must be a string");
1616 }
1617 else
1618 print_usage ();
1619 }
1620
1621 return retval;
1622 }
1623
1624 DEFUN (java_set, args, ,
1625 "-*- texinfo -*-\n\
1626 @deftypefn {Loadable Function} {@var{obj} =} java_set (@var{obj}, @var{name}, @var{val})\n\
1627 Set the value of the field @var{name} of the Java object @var{obj} to @var{val}.\n\
1628 For static fields, @var{obj} can be a string representing the fully qualified named\n\
1629 of the corresponding Java class.\n\
1630 \n\
1631 When @var{obj} is a regular Java object, the structure-like indexing can be used as\n\
1632 a shortcut syntax. For instance, the two following statements are equivalent\n\
1633 \n\
1634 @example\n\
1635 java_set (x, \"field1\", val)\n\
1636 x.field1 = val\n\
1637 @end example\n\
1638 \n\
1639 @seealso{java_get, java_invoke, java_new}\n\
1640 @end deftypefn")
1641 {
1642 octave_value retval;
1643
1644 initialize_java ();
1645
1646 if (! error_state)
1647 {
1648 JNIEnv *current_env = octave_java::thread_jni_env ();
1649
1650 if (args.length () == 3)
1651 {
1652 std::string name = args(1).string_value ();
1653 if (! error_state)
1654 {
1655 if (args(0).class_name () == "octave_java")
1656 {
1657 octave_java *jobj = TO_JAVA (args(0));
1658 retval = jobj->do_java_set (current_env, name, args(2));
1659 }
1660 else if (args(0).is_string ())
1661 {
1662 std::string cls = args(0).string_value ();
1663 retval = octave_java::do_java_set (current_env, cls, name, args(2));
1664 }
1665 else
1666 error ("java_set: first argument must be a Java object or a string");
1667 }
1668 else
1669 error ("java_set: second argument must be a string");
1670 }
1671 else
1672 print_usage ();
1673 }
1674
1675 return retval;
1676 }
1677
1678 DEFUN (java2mat, args, ,
1679 "-*- texinfo -*-\n\
1680 @deftypefn {Loadable Function} {} java2mat (@var{obj})\n\
1681 Undocumented internal function.\n\
1682 @end deftypefn")
1683 {
1684 octave_value_list retval;
1685
1686 initialize_java ();
1687
1688 if (! error_state)
1689 {
1690 JNIEnv *current_env = octave_java::thread_jni_env ();
1691
1692 if (args.length () == 1)
1693 {
1694 if (args(0).class_name () == "octave_java")
1695 {
1696 octave_java *jobj = TO_JAVA (args(0));
1697 retval(0) = box_more (current_env, jobj->to_java (), 0);
1698 }
1699 else
1700 retval(0) = args(0);
1701 }
1702 else
1703 print_usage ();
1704 }
1705
1706 return retval;
1707 }
1708
1709 DEFUN (java_convert_matrix, args, nargout,
1710 "-*- texinfo -*-\n\
1711 @deftypefn {Built-in Function} {} java_convert_matrix ()\n\
1712 Query or set the internal variable that determines FIXME.\n\
1713 @end deftypefn")
1714 {
1715 return SET_INTERNAL_VARIABLE (java_convert_matrix);
1716 }
1717
1718 DEFUN (java_unsigned_conversion, args, nargout,
1719 "-*- texinfo -*-\n\
1720 @deftypefn {Built-in Function} {} java_unsigned_conversion ()\n\
1721 Query or set the internal variable that determines FIXME.\n\
1722 @end deftypefn")
1723 {
1724 return SET_INTERNAL_VARIABLE (java_unsigned_conversion);
1725 }
1726
1727 DEFUN (java_debug, args, nargout,
1728 "-*- texinfo -*-\n\
1729 @deftypefn {Built-in Function} {} java_debug ()\n\
1730 Query or set the internal variable that determines FIXME.\n\
1731 @end deftypefn")
1732 {
1733 return SET_INTERNAL_VARIABLE (java_debug);
1734 }
1735
1736 JNIEXPORT jboolean JNICALL
1737 Java_org_octave_Octave_call (JNIEnv *env, jclass, jstring funcName,
1738 jobjectArray argin, jobjectArray argout)
1739 {
1740 std::string fname = jstring_to_string (env, funcName);
1741
1742 int nargout = env->GetArrayLength (argout);
1743 int nargin = env->GetArrayLength (argin);
1744
1745 octave_value_list varargin, varargout;
1746
1747 for (int i = 0; i < nargin; i++)
1748 varargin(i) = box (env, env->GetObjectArrayElement (argin, i), 0);
1749
1750 varargout = feval (fname, varargin, nargout);
1751
1752 if (! error_state)
1753 {
1754 jobjectArray_ref out_objs (env, argout), out_clss (env);
1755
1756 out_objs.detach ();
1757
1758 if (unbox (env, varargout, out_objs, out_clss))
1759 return true;
1760 }
1761
1762 return false;
1763 }
1764
1765 JNIEXPORT void JNICALL
1766 Java_org_octave_OctaveReference_doFinalize (JNIEnv *, jclass, jint ID)
1767 {
1768 octave_ref_map.erase (ID);
1769 }
1770
1771 JNIEXPORT void JNICALL
1772 Java_org_octave_Octave_doInvoke (JNIEnv *env, jclass, jint ID,
1773 jobjectArray args)
1774 {
1775 std::map<int,octave_value>::iterator it = octave_ref_map.find (ID);
1776
1777 if (it != octave_ref_map.end ())
1778 {
1779 octave_value val = it->second;
1780 int len = env->GetArrayLength (args);
1781 octave_value_list oct_args;
1782
1783 for (int i = 0; i < len; i++)
1784 {
1785 jobject_ref jobj (env, env->GetObjectArrayElement (args, i));
1786 oct_args(i) = box (env, jobj, 0);
1787
1788 if (error_state)
1789 break;
1790 }
1791
1792 if (! error_state)
1793 {
1794 BEGIN_INTERRUPT_WITH_EXCEPTIONS;
1795
1796 if (val.is_function_handle ())
1797 {
1798 octave_function *fcn = val.function_value ();
1799 feval (fcn, oct_args);
1800 }
1801 else if (val.is_cell () && val.length () > 0
1802 && (val.rows () == 1 || val.columns () == 1)
1803 && val.cell_value()(0).is_function_handle ())
1804 {
1805 Cell c = val.cell_value ();
1806 octave_function *fcn = c(0).function_value ();
1807
1808 for (int i=1; i<c.length (); i++)
1809 oct_args(len+i-1) = c(i);
1810
1811 if (! error_state)
1812 feval (fcn, oct_args);
1813 }
1814 else
1815 error ("trying to invoke non-invocable object");
1816
1817 END_INTERRUPT_WITH_EXCEPTIONS;
1818 }
1819 }
1820 }
1821
1822 JNIEXPORT void JNICALL
1823 Java_org_octave_Octave_doEvalString (JNIEnv *env, jclass, jstring cmd)
1824 {
1825 std::string s = jstring_to_string (env, cmd);
1826 int pstatus;
1827 eval_string (s, false, pstatus, 0);
1828 }
1829
1830 JNIEXPORT jboolean JNICALL
1831 Java_org_octave_Octave_needThreadedInvokation (JNIEnv *env, jclass)
1832 {
1833 return (get_current_thread_ID (env) != octave_thread_ID);
1834 }
1835
1836 // octave_java class definition
1837
1838 DEFINE_OCTAVE_ALLOCATOR (octave_java);
1839
1840 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_java, "octave_java", "octave_java");
1841
1842 dim_vector
1843 octave_java::dims (void) const
1844 {
1845 JNIEnv *current_env = thread_jni_env ();
1846
1847 if (current_env && java_object)
1848 return compute_array_dimensions (current_env, java_object);
1849 else
1850 return dim_vector (1, 1);
1851 }
1852
1853 JNIEnv *
1854 octave_java::thread_jni_env (void)
1855 {
1856 JNIEnv *env = NULL;
1857
1858 if (jvm)
1859 jvm->GetEnv (reinterpret_cast<void **> (&env), JNI_VERSION_1_2);
1860
1861 return env;
1862 }
1863
1864 octave_value_list
1865 octave_java::subsref (const std::string& type,
1866 const std::list<octave_value_list>& idx, int nargout)
1867 {
1868 octave_value_list retval;
1869 int skip = 1;
1870
1871 JNIEnv *current_env = thread_jni_env ();
1872
1873 switch (type[0])
1874 {
1875 case '.':
1876 if (type.length () > 1 && type[1] == '(')
1877 {
1878 octave_value_list ovl;
1879 count++;
1880 ovl(0) = octave_value (this);
1881 ovl(1) = (idx.front ())(0);
1882 std::list<octave_value_list>::const_iterator it = idx.begin ();
1883 ovl.append (*++it);
1884 retval = feval (std::string ("java_invoke"), ovl, 1);
1885 skip++;
1886 }
1887 else
1888 {
1889 octave_value_list ovl;
1890 count++;
1891 ovl(0) = octave_value (this);
1892 ovl(1) = (idx.front ())(0);
1893 retval = feval (std::string ("java_get"), ovl, 1);
1894 }
1895 break;
1896
1897 case '(':
1898 if (current_env)
1899 retval = get_array_elements (current_env, to_java (), idx.front ());
1900 break;
1901
1902 default:
1903 error ("subsref: Java object cannot be indexed with %c", type[0]);
1904 break;
1905 }
1906
1907 if (idx.size () > 1 && type.length () > 1)
1908 retval = retval(0).next_subsref (nargout, type, idx, skip);
1909
1910 return retval;
1911 }
1912
1913 octave_value
1914 octave_java::subsasgn (const std::string& type,
1915 const std::list<octave_value_list>&idx,
1916 const octave_value &rhs)
1917 {
1918 octave_value retval;
1919
1920 JNIEnv *current_env = thread_jni_env ();
1921
1922 switch (type[0])
1923 {
1924 case '.':
1925 if (type.length () == 1)
1926 {
1927 // field assignment
1928 octave_value_list ovl;
1929 count++;
1930 ovl(0) = octave_value (this);
1931 ovl(1) = (idx.front ())(0);
1932 ovl(2) = rhs;
1933 feval ("java_set", ovl, 0);
1934 if (! error_state)
1935 {
1936 count++;
1937 retval = octave_value (this);
1938 }
1939 }
1940 else if (type.length () > 2 && type[1] == '(')
1941 {
1942 std::list<octave_value_list> new_idx;
1943 std::list<octave_value_list>::const_iterator it = idx.begin ();
1944 new_idx.push_back (*it++);
1945 new_idx.push_back (*it++);
1946 octave_value_list u = subsref (type.substr (0, 2), new_idx, 1);
1947 if (! error_state)
1948 {
1949 std::list<octave_value_list> next_idx (idx);
1950 next_idx.erase (next_idx.begin ());
1951 next_idx.erase (next_idx.begin ());
1952 u(0).subsasgn (type.substr (2), next_idx, rhs);
1953 if (! error_state)
1954 {
1955 count++;
1956 retval = octave_value (this);
1957 }
1958 }
1959 }
1960 else if (type[1] == '.')
1961 {
1962 octave_value_list u = subsref (type.substr (0, 1), idx, 1);
1963 if (! error_state)
1964 {
1965 std::list<octave_value_list> next_idx (idx);
1966 next_idx.erase (next_idx.begin ());
1967 u(0).subsasgn (type.substr (1), next_idx, rhs);
1968 if (! error_state)
1969 {
1970 count++;
1971 retval = octave_value (this);
1972 }
1973 }
1974 }
1975 else
1976 error ("invalid indexing/assignment on Java object");
1977 break;
1978
1979 case '(':
1980 if (current_env)
1981 {
1982 set_array_elements (current_env, to_java (), idx.front (), rhs);
1983 if (! error_state)
1984 {
1985 count++;
1986 retval = octave_value (this);
1987 }
1988 }
1989 break;
1990
1991 default:
1992 error ("Java object cannot be indexed with %c", type[0]);
1993 break;
1994 }
1995
1996 return retval;
1997 }
1998
1999 string_vector
2000 octave_java::map_keys (void) const
2001 {
2002 JNIEnv *current_env = thread_jni_env ();
2003
2004 if (current_env)
2005 return get_invoke_list (current_env, to_java ());
2006 else
2007 return string_vector ();
2008 }
2009
2010 octave_value
2011 octave_java::convert_to_str_internal (bool, bool force, char type) const
2012 {
2013 JNIEnv *current_env = thread_jni_env ();
2014
2015 if (current_env)
2016 return convert_to_string (current_env, to_java (), force, type);
2017 else
2018 return octave_value ("");
2019 }
2020
2021 octave_value
2022 octave_java::do_java_invoke (JNIEnv* jni_env, const std::string& name,
2023 const octave_value_list& args)
2024 {
2025 octave_value retval;
2026
2027 if (jni_env)
2028 {
2029 jobjectArray_ref arg_objs (jni_env), arg_types (jni_env);
2030 if (unbox (jni_env, args, arg_objs, arg_types))
2031 {
2032 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2033 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "invokeMethod",
2034 "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
2035 jstring_ref methName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2036 jobjectArray_ref resObj (jni_env, reinterpret_cast<jobjectArray> (jni_env->CallStaticObjectMethod (helperClass, mID,
2037 to_java (), jstring (methName), jobjectArray (arg_objs), jobjectArray (arg_types))));
2038 if (resObj)
2039 retval = box (jni_env, resObj);
2040 else
2041 retval = check_exception (jni_env);
2042 }
2043 }
2044
2045 return retval;
2046 }
2047
2048 octave_value
2049 octave_java:: do_java_invoke (JNIEnv* jni_env,
2050 const std::string& class_name,
2051 const std::string& name,
2052 const octave_value_list& args)
2053 {
2054 octave_value retval;
2055
2056 if (jni_env)
2057 {
2058 jobjectArray_ref arg_objs (jni_env), arg_types (jni_env);
2059 if (unbox (jni_env, args, arg_objs, arg_types))
2060 {
2061 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2062 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "invokeStaticMethod",
2063 "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
2064 jstring_ref methName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2065 jstring_ref clsName (jni_env, jni_env->NewStringUTF (class_name.c_str ()));
2066 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
2067 jstring (clsName), jstring (methName), jobjectArray (arg_objs), jobjectArray (arg_types)));
2068 if (resObj)
2069 retval = box (jni_env, resObj);
2070 else
2071 retval = check_exception (jni_env);
2072 }
2073 }
2074
2075 return retval;
2076 }
2077
2078 octave_value
2079 octave_java::do_java_create (JNIEnv* jni_env, const std::string& name,
2080 const octave_value_list& args)
2081 {
2082 octave_value retval;
2083
2084 if (jni_env)
2085 {
2086 jobjectArray_ref arg_objs (jni_env), arg_types (jni_env);
2087
2088 if (unbox (jni_env, args, arg_objs, arg_types))
2089 {
2090 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2091 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "invokeConstructor",
2092 "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;");
2093 jstring_ref clsName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2094 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
2095 jstring (clsName), jobjectArray (arg_objs), jobjectArray (arg_types)));
2096
2097 if (resObj)
2098 retval = box (jni_env, resObj);
2099 else
2100 check_exception (jni_env);
2101 }
2102 }
2103
2104 return retval;
2105 }
2106
2107 octave_value
2108 octave_java::do_java_get (JNIEnv* jni_env, const std::string& name)
2109 {
2110 octave_value retval;
2111
2112 if (jni_env)
2113 {
2114 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2115 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "getField",
2116 "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
2117 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2118 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
2119 to_java (), jstring (fName)));
2120
2121 if (resObj)
2122 retval = box (jni_env, resObj);
2123 else
2124 retval = check_exception (jni_env);
2125 }
2126
2127 return retval;
2128 }
2129
2130 octave_value
2131 octave_java::do_java_get (JNIEnv* jni_env, const std::string& class_name,
2132 const std::string& name)
2133 {
2134 octave_value retval;
2135
2136 if (jni_env)
2137 {
2138 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2139 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "getStaticField",
2140 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
2141 jstring_ref cName (jni_env, jni_env->NewStringUTF (class_name.c_str ()));
2142 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2143 jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
2144 jstring (cName), jstring (fName)));
2145 if (resObj)
2146 retval = box (jni_env, resObj);
2147 else
2148 retval = check_exception (jni_env);
2149 }
2150
2151 return retval;
2152 }
2153
2154 octave_value
2155 octave_java::do_java_set (JNIEnv* jni_env, const std::string& name,
2156 const octave_value& val)
2157 {
2158 octave_value retval;
2159
2160 if (jni_env)
2161 {
2162 jobject_ref jobj (jni_env);
2163 jclass_ref jcls (jni_env);
2164
2165 if (unbox (jni_env, val, jobj, jcls))
2166 {
2167 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2168 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "setField",
2169 "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V");
2170 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2171 jni_env->CallStaticObjectMethod (helperClass, mID, to_java (), jstring (fName), jobject (jobj));
2172 check_exception (jni_env);
2173 }
2174 }
2175
2176 return retval;
2177 }
2178
2179 octave_value
2180 octave_java::do_java_set (JNIEnv* jni_env, const std::string& class_name,
2181 const std::string& name, const octave_value& val)
2182 {
2183 octave_value retval;
2184
2185 if (jni_env)
2186 {
2187 jobject_ref jobj (jni_env);
2188 jclass_ref jcls (jni_env);
2189
2190 if (unbox (jni_env, val, jobj, jcls))
2191 {
2192 jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
2193 jmethodID mID = jni_env->GetStaticMethodID (helperClass, "setStaticField",
2194 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V");
2195 jstring_ref cName (jni_env, jni_env->NewStringUTF (class_name.c_str ()));
2196 jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
2197 jni_env->CallStaticObjectMethod (helperClass, mID, jstring (cName), jstring (fName), jobject (jobj));
2198 check_exception (jni_env);
2199 }
2200 }
2201
2202 return retval;
2203 }
2204
2205 #endif
2206