Mercurial > hg > octave-lyh
comparison libinterp/octave-value/ov-java.h @ 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__.h@4be890c5527c |
children | e61405133a76 |
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 #if !defined (octave_java_h) | |
24 #define octave_java_h 1 | |
25 | |
26 #include <jni.h> | |
27 | |
28 #include <oct-obj.h> | |
29 #include <ov.h> | |
30 | |
31 #ifdef JAVAPKG_BUILD | |
32 # define JAVAPKG_API OCTAVE_EXPORT | |
33 #else | |
34 # define JAVAPKG_API OCTAVE_IMPORT | |
35 #endif | |
36 | |
37 template <class T> | |
38 class java_local_ref | |
39 { | |
40 public: | |
41 | |
42 java_local_ref (JNIEnv *_env) | |
43 : jobj (0), detached (false), env (_env) { } | |
44 | |
45 java_local_ref (JNIEnv *_env, T obj) | |
46 : jobj (obj), detached (false), env (_env) { } | |
47 | |
48 ~java_local_ref (void) { release (); } | |
49 | |
50 T& operator= (T obj) | |
51 { | |
52 release (); | |
53 jobj = obj; | |
54 detached = false; | |
55 return jobj; | |
56 } | |
57 | |
58 operator bool () const { return (jobj != 0); } | |
59 operator T () { return jobj; } | |
60 | |
61 void detach (void) { detached = true; } | |
62 | |
63 private: | |
64 | |
65 void release (void) | |
66 { | |
67 if (env && jobj && ! detached) | |
68 env->DeleteLocalRef (jobj); | |
69 | |
70 jobj = 0; | |
71 } | |
72 | |
73 java_local_ref (void) | |
74 : jobj (0), detached (false), env (0) | |
75 { } | |
76 | |
77 protected: | |
78 T jobj; | |
79 bool detached; | |
80 JNIEnv *env; | |
81 }; | |
82 | |
83 typedef java_local_ref<jobject> jobject_ref; | |
84 typedef java_local_ref<jclass> jclass_ref; | |
85 typedef java_local_ref<jstring> jstring_ref; | |
86 typedef java_local_ref<jobjectArray> jobjectArray_ref; | |
87 typedef java_local_ref<jintArray> jintArray_ref; | |
88 typedef java_local_ref<jbyteArray> jbyteArray_ref; | |
89 typedef java_local_ref<jdoubleArray> jdoubleArray_ref; | |
90 typedef java_local_ref<jthrowable> jthrowable_ref; | |
91 | |
92 extern JAVAPKG_API std::string | |
93 jstring_to_string (JNIEnv* jni_env, jstring s); | |
94 | |
95 extern JAVAPKG_API std::string | |
96 jstring_to_string (JNIEnv* jni_env, jobject obj); | |
97 | |
98 extern JAVAPKG_API octave_value | |
99 box (JNIEnv* jni_env, jobject jobj, jclass jcls = 0); | |
100 | |
101 extern JAVAPKG_API octave_value | |
102 box_more (JNIEnv* jni_env, jobject jobj, jclass jcls = 0); | |
103 | |
104 extern JAVAPKG_API int | |
105 unbox (JNIEnv* jni_env, const octave_value& val, jobject_ref& jobj, | |
106 jclass_ref& jcls); | |
107 | |
108 extern JAVAPKG_API int | |
109 unbox (JNIEnv* jni_env, const octave_value_list& args, | |
110 jobjectArray_ref& jobjs, jobjectArray_ref& jclss); | |
111 | |
112 extern JAVAPKG_API bool Vjava_convert_matrix; | |
113 | |
114 extern JAVAPKG_API bool Vjava_unsigned_conversion; | |
115 | |
116 extern JAVAPKG_API bool Vjava_debug; | |
117 | |
118 class JAVAPKG_API octave_java : public octave_base_value | |
119 { | |
120 public: | |
121 | |
122 octave_java (void) | |
123 : octave_base_value (), java_object (0), java_class (0) | |
124 { } | |
125 | |
126 octave_java (const octave_java& jobj) | |
127 : octave_base_value (jobj), java_object (0), java_class (0) | |
128 { | |
129 init (jobj.java_object, jobj.java_class); | |
130 } | |
131 | |
132 octave_java (const jobject& obj, jclass cls = 0) | |
133 : octave_base_value (), java_object (0) | |
134 { | |
135 init (obj, cls); | |
136 } | |
137 | |
138 ~octave_java (void) { release (); } | |
139 | |
140 jobject to_java (void) const { return java_object; } | |
141 jclass to_class (void) const { return java_class; } | |
142 | |
143 std::string java_class_name (void) const { return java_type; } | |
144 | |
145 octave_base_value* clone (void) const { return new octave_java (*this); } | |
146 octave_base_value* empty_clone (void) const { return new octave_java (); } | |
147 | |
148 bool is_defined (void) const { return true; } | |
149 | |
150 bool is_map (void) const { return true; } | |
151 | |
152 string_vector map_keys (void) const; | |
153 | |
154 dim_vector dims (void) const; | |
155 | |
156 void print (std::ostream& os, bool pr_as_read_syntax = false) const | |
157 { | |
158 os << "<Java object: " << java_type << ">"; | |
159 newline(os); | |
160 } | |
161 | |
162 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const | |
163 { | |
164 print(os, pr_as_read_syntax); | |
165 } | |
166 | |
167 octave_value_list | |
168 subsref (const std::string& type, | |
169 const std::list<octave_value_list>& idx, int nargout); | |
170 | |
171 octave_value | |
172 subsref (const std::string& type, const std::list<octave_value_list>& idx) | |
173 { | |
174 octave_value_list retval = subsref (type, idx, 1); | |
175 return (retval.length () > 0 ? retval(0) : octave_value ()); | |
176 } | |
177 | |
178 octave_value subsasgn (const std::string& type, | |
179 const std::list<octave_value_list>& idx, | |
180 const octave_value& rhs); | |
181 | |
182 octave_value convert_to_str_internal (bool pad, bool force, char type) const; | |
183 | |
184 bool is_string (void) const | |
185 { | |
186 JNIEnv *current_env = thread_jni_env (); | |
187 | |
188 if (current_env && java_object) | |
189 { | |
190 jclass_ref cls (current_env, current_env->FindClass ("java/lang/String")); | |
191 return current_env->IsInstanceOf (java_object, cls); | |
192 } | |
193 | |
194 return false; | |
195 } | |
196 | |
197 static JNIEnv* thread_jni_env (void); | |
198 | |
199 octave_value do_java_invoke (JNIEnv* jni_env, const std::string& name, | |
200 const octave_value_list& args); | |
201 | |
202 octave_value | |
203 do_java_invoke (const std::string& name, const octave_value_list& args) | |
204 { | |
205 return do_java_invoke(thread_jni_env (), name, args); | |
206 } | |
207 | |
208 static octave_value | |
209 do_java_invoke (JNIEnv* jni_env, const std::string& class_name, | |
210 const std::string& name, const octave_value_list& args); | |
211 | |
212 static octave_value | |
213 do_java_invoke (const std::string& class_name, const std::string& name, | |
214 const octave_value_list& args) | |
215 { | |
216 return do_java_invoke(thread_jni_env (), class_name, name, args); | |
217 } | |
218 | |
219 static octave_value | |
220 do_java_create (JNIEnv* jni_env, const std::string& name, | |
221 const octave_value_list& args); | |
222 | |
223 static octave_value | |
224 do_java_create (const std::string& name, const octave_value_list& args) | |
225 { | |
226 return do_java_create (thread_jni_env (), name, args); | |
227 } | |
228 | |
229 octave_value do_java_get (JNIEnv* jni_env, const std::string& name); | |
230 | |
231 octave_value do_java_get (const std::string& name) | |
232 { | |
233 return do_java_get (thread_jni_env (), name); | |
234 } | |
235 | |
236 static octave_value | |
237 do_java_get (JNIEnv* jni_env, const std::string& class_name, | |
238 const std::string& name); | |
239 | |
240 static octave_value | |
241 do_java_get (const std::string& class_name, const std::string& name) | |
242 { | |
243 return do_java_get (thread_jni_env (), class_name, name); | |
244 } | |
245 | |
246 octave_value do_java_set (JNIEnv* jni_env, const std::string& name, | |
247 const octave_value& val); | |
248 | |
249 octave_value do_java_set (const std::string& name, const octave_value& val) | |
250 { | |
251 return do_java_set (thread_jni_env (), name, val); | |
252 } | |
253 | |
254 static octave_value | |
255 do_java_set (JNIEnv* jni_env, const std::string& class_name, | |
256 const std::string& name, const octave_value& val); | |
257 | |
258 static octave_value | |
259 do_java_set (const std::string& class_name, const std::string& name, | |
260 const octave_value& val) | |
261 { | |
262 return do_java_set (thread_jni_env (), class_name, name, val); | |
263 } | |
264 | |
265 private: | |
266 | |
267 void init (jobject jobj, jclass jcls) | |
268 { | |
269 JNIEnv *current_env = thread_jni_env (); | |
270 | |
271 if (current_env) | |
272 { | |
273 if (jobj) | |
274 java_object = current_env->NewGlobalRef (jobj); | |
275 | |
276 if (jcls) | |
277 java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jcls)); | |
278 else if (java_object) | |
279 { | |
280 jclass_ref ocls (current_env, current_env->GetObjectClass (java_object)); | |
281 java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jclass (ocls))); | |
282 } | |
283 | |
284 if (java_class) | |
285 { | |
286 jclass_ref clsCls (current_env, current_env->GetObjectClass (java_class)); | |
287 jmethodID mID = current_env->GetMethodID (clsCls, "getCanonicalName", "()Ljava/lang/String;"); | |
288 jobject_ref resObj (current_env, current_env->CallObjectMethod (java_class, mID)); | |
289 java_type = jstring_to_string (current_env, resObj); | |
290 } | |
291 } | |
292 } | |
293 | |
294 void release (void) | |
295 { | |
296 JNIEnv *current_env = thread_jni_env (); | |
297 | |
298 if (current_env) | |
299 { | |
300 if (java_object) | |
301 current_env->DeleteGlobalRef (java_object); | |
302 | |
303 if (java_class) | |
304 current_env->DeleteGlobalRef (java_class); | |
305 | |
306 java_object = 0; | |
307 java_class = 0; | |
308 } | |
309 } | |
310 | |
311 private: | |
312 | |
313 DECLARE_OCTAVE_ALLOCATOR | |
314 | |
315 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA | |
316 | |
317 jobject java_object; | |
318 | |
319 jclass java_class; | |
320 | |
321 std::string java_type; | |
322 }; | |
323 | |
324 #endif |