GNU Octave  4.0.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 bool
99 unbox (JNIEnv* jni_env, const octave_value& val, jobject_ref& jobj,
100  jclass_ref& jcls);
101 
102 extern OCTINTERP_API bool
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_instance_of (const std::string&) const;
143 
144  bool is_defined (void) const { return true; }
145 
146  bool is_constant (void) const { return true; }
147 
148  bool is_map (void) const { return false; }
149 
150  bool is_java (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);
157 
158  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
159 
161  subsref (const std::string& type,
162  const std::list<octave_value_list>& idx, int nargout);
163 
165  subsref (const std::string& type, const std::list<octave_value_list>& idx)
166  {
167  octave_value_list retval = subsref (type, idx, 1);
168  return (retval.length () > 0 ? retval(0) : octave_value ());
169  }
170 
171  octave_value subsasgn (const std::string& type,
172  const std::list<octave_value_list>& idx,
173  const octave_value& rhs);
174 
175  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
176 
177  bool is_java_string (void) const;
178 
179  static JNIEnv* thread_jni_env (void);
180 
181  octave_value do_javaMethod (JNIEnv* jni_env, const std::string& name,
182  const octave_value_list& args);
183 
185  do_javaMethod (const std::string& name, const octave_value_list& args)
186  {
187  return do_javaMethod(thread_jni_env (), name, args);
188  }
189 
190  static octave_value
191  do_javaMethod (JNIEnv* jni_env, const std::string& class_name,
192  const std::string& name, const octave_value_list& args);
193 
194  static octave_value
195  do_javaMethod (const std::string& class_name, const std::string& name,
196  const octave_value_list& args)
197  {
198  return do_javaMethod(thread_jni_env (), class_name, name, args);
199  }
200 
201  static octave_value
202  do_javaObject (JNIEnv* jni_env, const std::string& name,
203  const octave_value_list& args);
204 
205  static octave_value
206  do_javaObject (const std::string& name, const octave_value_list& args)
207  {
208  return do_javaObject (thread_jni_env (), name, args);
209  }
210 
211  octave_value do_java_get (JNIEnv* jni_env, const std::string& name);
212 
213  octave_value do_java_get (const std::string& name)
214  {
215  return do_java_get (thread_jni_env (), name);
216  }
217 
218  static octave_value
219  do_java_get (JNIEnv* jni_env, const std::string& class_name,
220  const std::string& name);
221 
222  static octave_value
223  do_java_get (const std::string& class_name, const std::string& name)
224  {
225  return do_java_get (thread_jni_env (), class_name, name);
226  }
227 
228  octave_value do_java_set (JNIEnv* jni_env, const std::string& name,
229  const octave_value& val);
230 
231  octave_value do_java_set (const std::string& name, const octave_value& val)
232  {
233  return do_java_set (thread_jni_env (), name, val);
234  }
235 
236  static octave_value
237  do_java_set (JNIEnv* jni_env, const std::string& class_name,
238  const std::string& name, const octave_value& val);
239 
240  static octave_value
241  do_java_set (const std::string& class_name, const std::string& name,
242  const octave_value& val)
243  {
244  return do_java_set (thread_jni_env (), class_name, name, val);
245  }
246 
247 private:
248 
249  void init (jobject jobj, jclass jcls)
250  {
251  JNIEnv *current_env = thread_jni_env ();
252 
253  if (current_env)
254  {
255  if (jobj)
256  java_object = current_env->NewGlobalRef (jobj);
257 
258  if (jcls)
259  java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jcls));
260  else if (java_object)
261  {
262  jclass_ref ocls (current_env, current_env->GetObjectClass (java_object));
263  java_class = reinterpret_cast<jclass> (current_env->NewGlobalRef (jclass (ocls)));
264  }
265 
266  if (java_class)
267  {
268  jclass_ref clsCls (current_env, current_env->GetObjectClass (java_class));
269  jmethodID mID = current_env->GetMethodID (clsCls, "getCanonicalName", "()Ljava/lang/String;");
270  jobject_ref resObj (current_env, current_env->CallObjectMethod (java_class, mID));
271  java_classname = jstring_to_string (current_env, resObj);
272  }
273  }
274  }
275 
276  void release (void)
277  {
278  JNIEnv *current_env = thread_jni_env ();
279 
280  if (current_env)
281  {
282  if (java_object)
283  current_env->DeleteGlobalRef (java_object);
284 
285  if (java_class)
286  current_env->DeleteGlobalRef (java_class);
287 
288  java_object = 0;
289  java_class = 0;
290  }
291  }
292 
293 private:
294 
295  jobject java_object;
296 
297  jclass java_class;
298 
299  std::string java_classname;
300 
301 
302 public:
303  int type_id (void) const { return t_id; }
304  std::string type_name (void) const { return t_name; }
305  std::string class_name (void) const { return java_classname; }
306 
307  static int static_type_id (void) { return t_id; }
308  static std::string static_type_name (void) { return t_name; }
309  static std::string static_class_name (void) { return "<unknown>"; }
310  static void register_type (void);
311 
312 private:
313  static int t_id;
314  static const std::string t_name;
315 };
316 
317 #endif
JNIEnv * env
Definition: ov-java.h:74
java_local_ref< jobject > jobject_ref
Definition: ov-java.h:77
virtual dim_vector dims(void) const
Definition: ov-base.h:292
java_local_ref(JNIEnv *_env, T obj)
Definition: ov-java.h:39
int bool
Definition: mex.h:56
static octave_value do_java_set(const std::string &class_name, const std::string &name, const octave_value &val)
Definition: ov-java.h:241
bool is_defined(void) const
Definition: ov-java.h:144
jobject java_object
Definition: ov-java.h:295
java_local_ref< jthrowable > jthrowable_ref
Definition: ov-java.h:84
static std::string static_class_name(void)
Definition: ov-java.h:309
java_local_ref(void)
Definition: ov-java.h:67
~octave_java(void)
Definition: ov-java.h:132
static octave_value do_java_get(const std::string &class_name, const std::string &name)
Definition: ov-java.h:223
octave_java(const jobject &obj, jclass cls=0)
Definition: ov-java.h:126
OCTINTERP_API octave_value box(JNIEnv *jni_env, jobject jobj, jclass jcls=0)
octave_idx_type length(void) const
Definition: oct-obj.h:89
static std::string static_type_name(void)
Definition: ov-java.h:308
bool detached
Definition: ov-java.h:73
OCTINTERP_API bool Vjava_unsigned_autoconversion
octave_java(void)
Definition: ov-java.h:116
static int static_type_id(void)
Definition: ov-java.h:307
void detach(void)
Definition: ov-java.h:55
java_local_ref< jintArray > jintArray_ref
Definition: ov-java.h:81
bool is_map(void) const
Definition: ov-java.h:148
octave_value do_java_set(const std::string &name, const octave_value &val)
Definition: ov-java.h:231
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:158
virtual string_vector map_keys(void) const
Definition: ov-base.cc:958
virtual octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
Definition: ov-base.cc:227
OCTINTERP_API std::string jstring_to_string(JNIEnv *jni_env, jstring s)
virtual void print(std::ostream &os, bool pr_as_read_syntax=false)
Definition: ov-base.cc:401
java_local_ref< jobjectArray > jobjectArray_ref
Definition: ov-java.h:80
java_local_ref< jdoubleArray > jdoubleArray_ref
Definition: ov-java.h:83
octave_base_value * empty_clone(void) const
Definition: ov-java.h:140
jobject to_java(void) const
Definition: ov-java.h:134
bool is_constant(void) const
Definition: ov-java.h:146
octave_java(const octave_java &jobj)
Definition: ov-java.h:120
void release(void)
Definition: ov-java.h:59
#define OCTINTERP_API
Definition: mexproto.h:66
~java_local_ref(void)
Definition: ov-java.h:42
octave_value do_java_get(const std::string &name)
Definition: ov-java.h:213
bool is_java(void) const
Definition: ov-java.h:150
static int t_id
Definition: ov-base.h:830
static const std::string t_name
Definition: ov-base.h:830
int type_id(void) const
Definition: ov-java.h:303
virtual bool is_instance_of(const std::string &) const
Definition: ov-base.h:588
octave_base_value * clone(void) const
Definition: ov-java.h:139
static void register_type(void)
Definition: ov-base.cc:114
void release(void)
Definition: ov-java.h:276
static octave_value do_javaMethod(const std::string &class_name, const std::string &name, const octave_value_list &args)
Definition: ov-java.h:195
java_local_ref(JNIEnv *_env)
Definition: ov-java.h:36
static octave_value do_javaObject(const std::string &name, const octave_value_list &args)
Definition: ov-java.h:206
java_local_ref< jclass > jclass_ref
Definition: ov-java.h:78
std::string type_name(void) const
Definition: ov-java.h:304
friend class octave_value
Definition: ov-base.h:206
OCTINTERP_API bool Vdebug_java
T & operator=(T obj)
Definition: ov-java.h:44
OCTINTERP_API bool unbox(JNIEnv *jni_env, const octave_value &val, jobject_ref &jobj, jclass_ref &jcls)
octave_value do_javaMethod(const std::string &name, const octave_value_list &args)
Definition: ov-java.h:185
static const std::string t_name
Definition: ov-java.h:314
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-java.h:165
std::string class_name(void) const
Definition: ov-java.h:305
std::string java_class_name(void) const
Definition: ov-java.h:137
java_local_ref< jstring > jstring_ref
Definition: ov-java.h:79
std::string java_classname
Definition: ov-java.h:299
virtual octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-base.cc:385
OCTINTERP_API bool Vjava_matrix_autoconversion
static int t_id
Definition: ov-java.h:313
virtual void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov-base.cc:407
java_local_ref< jbyteArray > jbyteArray_ref
Definition: ov-java.h:82
void release(void)
Definition: ov-base.h:782
jclass to_class(void) const
Definition: ov-java.h:135
OCTINTERP_API octave_value box_more(JNIEnv *jni_env, jobject jobj, jclass jcls=0)
void init(jobject jobj, jclass jcls)
Definition: ov-java.h:249
jclass java_class
Definition: ov-java.h:297