changeset 20063:107130a0490c

isa: check parent class of java objects (bug #42702) * libinterp/octave-value/ov-java.h, libinterp/octave-value/ov-java.cc (octave_java::is_instance_of): implement method since the one in the parent octave_class is not able to handle java. * libinterp/octave-value/ov-class.cc: add tests.
author Carnë Draug <carandraug@octave.org>
date Mon, 06 Apr 2015 04:55:56 +0100
parents 70bb0bd4a53f
children 7cefb9e7bc9f
files libinterp/octave-value/ov-class.cc libinterp/octave-value/ov-java.cc libinterp/octave-value/ov-java.h
diffstat 3 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-class.cc	Sun Apr 05 02:00:59 2015 -0300
+++ b/libinterp/octave-value/ov-class.cc	Mon Apr 06 04:55:56 2015 +0100
@@ -2052,6 +2052,15 @@
 %!assert (isa ({1, 2}, "cell"))
 %!assert (isa ({1, 2}, {"numeric", "integer", "cell"}), [false false true])
 
+%!testif HAVE_JAVA
+%! ## The first and last assert() are equal on purpose.  The assert() in
+%! ## the middle with an invalid class name will cause the java code to
+%! ## throw exceptions which we then must clear properly (or all other calls
+%! ## will fail).  So we test this too.
+%! assert (isa (javaObject ("java.lang.Double", 10), "java.lang.Number"))
+%! assert (isa (javaObject ("java.lang.Double", 10), "not_a_class"), false)
+%! assert (isa (javaObject ("java.lang.Double", 10), "java.lang.Number"))
+
 %!test
 %! a.b = 1;
 %! assert (isa (a, "struct"));
--- a/libinterp/octave-value/ov-java.cc	Sun Apr 05 02:00:59 2015 -0300
+++ b/libinterp/octave-value/ov-java.cc	Mon Apr 06 04:55:56 2015 +0100
@@ -42,6 +42,7 @@
 #include <map>
 #include <iostream>
 #include <fstream>
+#include <string>
 
 #include <clocale>
 
@@ -671,6 +672,25 @@
   return false;
 }
 
+bool
+octave_java::is_instance_of (const std::string& cls_name) const
+{
+  JNIEnv *current_env = thread_jni_env ();
+
+  std::string cls_cpp = cls_name;
+  std::replace (cls_cpp.begin (), cls_cpp.end (), '.', '/');
+
+  if (current_env && java_object)
+    {
+      jclass_ref cls (current_env, current_env->FindClass (cls_cpp.c_str ()));
+      if (current_env->ExceptionCheck ())
+        current_env->ExceptionClear();
+      else
+        return current_env->IsInstanceOf (java_object, cls);
+    }
+  return false;
+}
+
 static octave_value
 check_exception (JNIEnv* jni_env)
 {
--- a/libinterp/octave-value/ov-java.h	Sun Apr 05 02:00:59 2015 -0300
+++ b/libinterp/octave-value/ov-java.h	Mon Apr 06 04:55:56 2015 +0100
@@ -139,6 +139,8 @@
   octave_base_value* clone (void) const { return new octave_java (*this); }
   octave_base_value* empty_clone (void) const { return new octave_java (); }
 
+  bool is_instance_of (const std::string&) const;
+
   bool is_defined (void) const { return true; }
 
   bool is_map (void) const { return false; }