GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-java.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007, 2013 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_ov_java_h)
24 #define octave_ov_java_h 1
25 
26 #include <jni.h>
27 
28 #include <oct-obj.h>
29 #include <ov.h>
30 
31 template <class T>
33 {
34 public:
35 
36  java_local_ref (JNIEnv *_env)
37  : jobj (0), detached (false), env (_env) { }
38 
39  java_local_ref (JNIEnv *_env, T obj)
40  : jobj (obj), detached (false), env (_env) { }
41 
42  ~java_local_ref (void) { release (); }
43 
44  T& operator= (T obj)
45  {
46  release ();
47  jobj = obj;
48  detached = false;
49  return jobj;
50  }
51 
52  operator bool () const { return (jobj != 0); }
53  operator T () { return jobj; }
54 
55  void detach (void) { detached = true; }
56 
57 private:
58 
59  void release (void)
60  {
61  if (env && jobj && ! detached)
62  env->DeleteLocalRef (jobj);
63 
64  jobj = 0;
65  }
66 
68  : jobj (0), detached (false), env (0)
69  { }
70 
71 protected:
72  T jobj;
73  bool detached;
74  JNIEnv *env;
75 };
76 
85 
86 extern OCTINTERP_API std::string
87 jstring_to_string (JNIEnv* jni_env, jstring s);
88 
89 extern OCTINTERP_API std::string
90 jstring_to_string (JNIEnv* jni_env, jobject obj);
91 
93 box (JNIEnv* jni_env, jobject jobj, jclass jcls = 0);
94 
96 box_more (JNIEnv* jni_env, jobject jobj, jclass jcls = 0);
97 
98 extern OCTINTERP_API int
99 unbox (JNIEnv* jni_env, const octave_value& val, jobject_ref& jobj,
100  jclass_ref& jcls);
101 
102 extern OCTINTERP_API int
103 unbox (JNIEnv* jni_env, const octave_value_list& args,
104  jobjectArray_ref& jobjs, jobjectArray_ref& jclss);
105 
107 
109 
110 extern OCTINTERP_API bool Vdebug_java;
111 
113 {
114 public:
115 
116  octave_java (void)
117  : octave_base_value (), java_object (0), java_class (0)
118  { }
119 
120  octave_java (const octave_java& jobj)
121  : octave_base_value (jobj), java_object (0), java_class (0)
122  {
123  init (jobj.java_object, jobj.java_class);
124  }
125 
126  octave_java (const jobject& obj, jclass cls = 0)
127  : octave_base_value (), java_object (0)
128  {
129  init (obj, cls);
130  }
131 
132  ~octave_java (void) { release (); }
133 
134  jobject to_java (void) const { return java_object; }
135  jclass to_class (void) const { return java_class; }
136 
137  std::string java_class_name (void) const { return java_classname; }
138 
139  octave_base_value* clone (void) const { return new octave_java (*this); }
140  octave_base_value* empty_clone (void) const { return new octave_java (); }
141 
142  bool is_defined (void) const { return true; }
143 
144  bool is_map (void) const { return false; }
145 
146  bool is_java (void) const { return true; }
147 
148  string_vector map_keys (void) const;
149 
150  dim_vector dims (void) const;
151 
152  void print (std::ostream& os, bool pr_as_read_syntax = false) const;
153 
154  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
155 
157  subsref (const std::string& type,
158  const std::list<octave_value_list>& idx, int nargout);
159 
161  subsref (const std::string& type, const std::list<octave_value_list>& idx)
162  {
163  octave_value_list retval = subsref (type, idx, 1);
164  return (retval.length () > 0 ? retval(0) : octave_value ());
165  }
166 
167  octave_value subsasgn (const std::string& type,
168  const std::list<octave_value_list>& idx,
169  const octave_value& rhs);
170 
171  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
172 
173  bool is_java_string (void) const;
174 
175  static JNIEnv* thread_jni_env (void);
176 
177  octave_value do_javaMethod (JNIEnv* jni_env, const std::string& name,
178  const octave_value_list& args);
179 
181  do_javaMethod (const std::string& name, const octave_value_list& args)
182  {
183  return do_javaMethod(thread_jni_env (), name, args);
184  }
185 
186  static octave_value
187  do_javaMethod (JNIEnv* jni_env, const std::string& class_name,
188  const std::string& name, const octave_value_list& args);
189 
190  static octave_value
191  do_javaMethod (const std::string& class_name, const std::string& name,
192  const octave_value_list& args)
193  {
194  return do_javaMethod(thread_jni_env (), class_name, name, args);
195  }
196 
197  static octave_value
198  do_javaObject (JNIEnv* jni_env, const std::string& name,
199  const octave_value_list& args);
200 
201  static octave_value
202  do_javaObject (const std::string& name, const octave_value_list& args)
203  {
204  return do_javaObject (thread_jni_env (), name, args);
205  }
206 
207  octave_value do_java_get (JNIEnv* jni_env, const std::string& name);
208 
209  octave_value do_java_get (const std::string& name)
210  {
211  return do_java_get (thread_jni_env (), name);
212  }
213 
214  static octave_value
215  do_java_get (JNIEnv* jni_env, const std::string& class_name,
216  const std::string& name);
217 
218  static octave_value
219  do_java_get (const std::string& class_name, const std::string& name)
220  {
221  return do_java_get (thread_jni_env (), class_name, name);
222  }
223 
224  octave_value do_java_set (JNIEnv* jni_env, const std::string& name,
225  const octave_value& val);
226 
227  octave_value do_java_set (const std::string& name, const octave_value& val)
228  {
229  return do_java_set (thread_jni_env (), name, val);
230  }
231 
232  static octave_value
233  do_java_set (JNIEnv* jni_env, const std::string& class_name,
234  const std::string& name, const octave_value& val);
235 
236  static octave_value
237  do_java_set (const std::string& class_name, const std::string& name,
238  const octave_value& val)
239  {
240  return do_java_set (thread_jni_env (), class_name, name, val);
241  }
242 
243 private:
244 
245  void init (jobject jobj, jclass jcls)
246  {
247  JNIEnv *current_env = thread_jni_env ();
248 
249  if (current_env)
250  {
251  if (jobj)
252  java_object = current_env->NewGlobalRef (jobj);
253 
254  if (jcls)
255  java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jcls));
256  else if (java_object)
257  {
258  jclass_ref ocls (current_env, current_env->GetObjectClass (java_object));
259  java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jclass (ocls)));
260  }
261 
262  if (java_class)
263  {
264  jclass_ref clsCls (current_env, current_env->GetObjectClass (java_class));
265  jmethodID mID = current_env->GetMethodID (clsCls, "getCanonicalName", "()Ljava/lang/String;");
266  jobject_ref resObj (current_env, current_env->CallObjectMethod (java_class, mID));
267  java_classname = jstring_to_string (current_env, resObj);
268  }
269  }
270  }
271 
272  void release (void)
273  {
274  JNIEnv *current_env = thread_jni_env ();
275 
276  if (current_env)
277  {
278  if (java_object)
279  current_env->DeleteGlobalRef (java_object);
280 
281  if (java_class)
282  current_env->DeleteGlobalRef (java_class);
283 
284  java_object = 0;
285  java_class = 0;
286  }
287  }
288 
289 private:
290 
291  jobject java_object;
292 
293  jclass java_class;
294 
295  std::string java_classname;
296 
298 
299 public:
300  int type_id (void) const { return t_id; }
301  std::string type_name (void) const { return t_name; }
302  std::string class_name (void) const { return java_classname; }
303 
304  static int static_type_id (void) { return t_id; }
305  static std::string static_type_name (void) { return t_name; }
306  static std::string static_class_name (void) { return "<unknown>"; }
307  static void register_type (void);
308 
309 private:
310  static int t_id;
311  static const std::string t_name;
312 };
313 
314 #endif