# HG changeset patch # User Carnë Draug # Date 1429538487 -3600 # Node ID 2db2db2df55bff44e205772d033f9e8397acf2b1 # Parent f46f6d90665485510f87118eadb6ee69f31941c9 Automatically convert arrays of java primitives into Octave types (bug #44882) * libinterp/octave-value/ov-java.cc (box): when the result of a java method is a java primitive type, these are converted to octave types automatically. We seem to be handling this correctly for scalars but not for arrays yet. This fixes it on the java -> octave direction. diff -r f46f6d906654 -r 2db2db2df55b libinterp/octave-value/ov-java.cc --- a/libinterp/octave-value/ov-java.cc Thu Apr 23 09:14:10 2015 -0700 +++ b/libinterp/octave-value/ov-java.cc Mon Apr 20 15:01:27 2015 +0100 @@ -1047,6 +1047,30 @@ break; } +#define BOX_PRIMITIVE_ARRAY(JAVA_TYPE, JAVA_ID, JAVA_TYPE_CAP, OCTAVE_ID) \ + cls = jni_env->FindClass (JAVA_ID); \ + if (jni_env->IsInstanceOf (jobj, cls)) \ + { \ + const JAVA_TYPE ## Array jarr = reinterpret_cast (jobj); \ + const jsize len = jni_env->GetArrayLength (jarr); \ + OCTAVE_ID ## NDArray d (dim_vector (len, 1)); \ + JAVA_TYPE * buffer = reinterpret_cast (d.fortran_vec ()); \ + jni_env->Get ## JAVA_TYPE_CAP ## ArrayRegion (jarr, 0, len, buffer); \ + retval = d; \ + break; \ + } + +BOX_PRIMITIVE_ARRAY (jboolean, "[Z", Boolean, bool) +BOX_PRIMITIVE_ARRAY (jchar, "[C", Char, char) +BOX_PRIMITIVE_ARRAY (jbyte, "[B", Byte, int8) +BOX_PRIMITIVE_ARRAY (jshort, "[S", Short, int16) +BOX_PRIMITIVE_ARRAY (jint, "[I", Int, int32) +BOX_PRIMITIVE_ARRAY (jlong, "[J", Long, int64) +BOX_PRIMITIVE_ARRAY (jfloat, "[F", Float, Float) +BOX_PRIMITIVE_ARRAY (jdouble, "[D", Double, ) + +#undef BOX_PRIMITIVE_ARRAY + if (Vjava_matrix_autoconversion) { cls = find_octave_class (jni_env, "org/octave/Matrix"); @@ -2437,3 +2461,8 @@ return retval; } +/* +## Check automatic conversion of java primitive arrays into octave types +%!assert (javaObject ("java.lang.String", "hello").getBytes (), +%! int8 ([104 101 108 108 111]')) +*/