# HG changeset patch # User Carnë Draug # Date 1428292556 -3600 # Node ID 107130a0490cd3705e70b73c32fb30c6b6af729d # Parent 70bb0bd4a53fb9fa83f29a47a9d6bc3563bcb831 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. diff -r 70bb0bd4a53f -r 107130a0490c libinterp/octave-value/ov-class.cc --- 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")); diff -r 70bb0bd4a53f -r 107130a0490c libinterp/octave-value/ov-java.cc --- 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 #include #include +#include #include @@ -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) { diff -r 70bb0bd4a53f -r 107130a0490c libinterp/octave-value/ov-java.h --- 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; }