# HG changeset patch
# User Ernst Reissner <rei3ner@arcor.de>
# Date 1471603255 -7200
#      Fri Aug 19 12:40:55 2016 +0200
# Node ID bf99617e2b1880cbfb374296689fc5431a422542
# Parent  0eca64fed68df102e54a7766c9c872410d80618e
fix for 48749

diff -r 0eca64fed68d -r bf99617e2b18 libinterp/octave-value/ov-java.cc
--- a/libinterp/octave-value/ov-java.cc	Fri Aug 19 11:48:01 2016 +0200
+++ b/libinterp/octave-value/ov-java.cc	Fri Aug 19 12:40:55 2016 +0200
@@ -882,42 +882,6 @@
   return jcls;
 }
 
-// bug #48750 
-static dim_vector
-compute_array_dimensions (JNIEnv *jni_env, jobject obj)
-{
-  jobjectArray_ref jobj (jni_env, reinterpret_cast<jobjectArray> (obj));
-  jclass_ref jcls (jni_env, jni_env->GetObjectClass (obj));
-  jclass_ref ccls (jni_env, jni_env->GetObjectClass (jcls));
-  jmethodID isArray_ID = jni_env->GetMethodID (ccls, "isArray", "()Z");
-  jmethodID getComponentType_ID = jni_env->GetMethodID (ccls,
-                                                        "getComponentType",
-                                                        "()Ljava/lang/Class;");
-
-  dim_vector dv (1, 1);
-  int idx = 0;
-
-  jobj.detach ();
-  while (jcls && jni_env->CallBooleanMethod (jcls, isArray_ID))
-    {
-      int len = (jobj ? jni_env->GetArrayLength (jobj) : 0);
-      if (idx >= dv.ndims ())
-        dv.resize (idx+1);
-      dv(idx) = len;
-      jcls = reinterpret_cast<jclass>
-        (jni_env->CallObjectMethod (jcls, getComponentType_ID));
-      jobj = len > 0
-        ? reinterpret_cast<jobjectArray> (jni_env->GetObjectArrayElement (jobj,
-                                                                          0))
-        : 0;
-      idx++;
-    }
-
-  octave_set_default_fpucw ();
-
-  return dv;
-}
-
 static jobject
 make_java_index (JNIEnv *jni_env, const octave_value_list& idx)
 {
@@ -2059,17 +2023,41 @@
 #endif
 }
 
+//! Return the dimension of this java object.
+//! If this is an array, it is interpreted like an Octave row vector
+//! with dimensions @code{[l 1]}, where @code{l} is the length,
+//! even if this is a nested array.
+//! If this is a scalar, the dimensions are just @code{[1 1]}.
 dim_vector
 octave_java::dims (void) const
 {
 #if defined (HAVE_JAVA)
 
-  JNIEnv *current_env = thread_jni_env ();
-
-  if (current_env && java_object)
-    return compute_array_dimensions (current_env, TO_JOBJECT (java_object));
-  else
-    return dim_vector (1, 1);
+  JNIEnv *jni_env = thread_jni_env ();
+
+  dim_vector dv (1, 1);// value for null and for scalars 
+  // FIXME: java_object seems to be non-null always. 
+
+  if (jni_env && java_object)
+    {
+      jobject obj = TO_JOBJECT (java_object);
+      jobjectArray_ref jobj (jni_env, reinterpret_cast<jobjectArray> (obj));
+      jobj.detach ();
+
+      jclass_ref jcls (jni_env, jni_env->GetObjectClass (obj));
+      jclass_ref ccls (jni_env, jni_env->GetObjectClass (jcls));
+      jmethodID isArray_ID = jni_env->GetMethodID (ccls, "isArray", "()Z");
+
+      if (jni_env->CallBooleanMethod (jcls, isArray_ID))
+        {
+          // modification for arrays 
+          dv(0) = jni_env->GetArrayLength (jobj);
+        }
+
+      octave_set_default_fpucw ();
+    }
+
+    return dv;
 
 #else
 
@@ -2550,7 +2538,8 @@
 }
 
 octave_value
-octave_java::do_javaObject (const std::string& name, const octave_value_list& args)
+octave_java::do_javaObject (const std::string& name, 
+                            const octave_value_list& args)
 {
 #if defined (HAVE_JAVA)
 
@@ -2580,12 +2569,14 @@
 
   if (jni_env)
     {
-      jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
-      jmethodID mID = jni_env->GetStaticMethodID (helperClass, "getField",
+      jclass_ref helperClass (jni_env, 
+                              find_octave_class (jni_env, 
+                                                 "org/octave/ClassHelper"));
+      jmethodID mID = jni_env->GetStaticMethodID (helperClass, "getFieldObj",
           "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;");
       jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
-      jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
-          to_java (), jstring (fName)));
+      jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod 
+                          (helperClass, mID, to_java (), jstring (fName)));
 
       if (resObj)
         retval = java2octType (jni_env, resObj);
@@ -2641,12 +2632,14 @@
 
   if (jni_env)
     {
-      jclass_ref helperClass (jni_env, find_octave_class (jni_env, "org/octave/ClassHelper"));
+      jclass_ref helperClass (jni_env, find_octave_class
+                              (jni_env, "org/octave/ClassHelper"));
       jmethodID mID = jni_env->GetStaticMethodID (helperClass, "getStaticField",
           "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");
       jstring_ref cName (jni_env, jni_env->NewStringUTF (class_name.c_str ()));
       jstring_ref fName (jni_env, jni_env->NewStringUTF (name.c_str ()));
-      jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod (helperClass, mID,
+      jobject_ref resObj (jni_env, jni_env->CallStaticObjectMethod 
+                          (helperClass, mID,
           jstring (cName), jstring (fName)));
       if (resObj)
         retval = java2octType (jni_env, resObj);
