Mercurial > hg > octave-nkf
comparison libinterp/octave-value/ov-java.cc @ 20330:2db2db2df55b
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.
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Mon, 20 Apr 2015 15:01:27 +0100 |
parents | 107130a0490c |
children | 41064c150724 |
comparison
equal
deleted
inserted
replaced
20329:f46f6d906654 | 20330:2db2db2df55b |
---|---|
1044 jmethodID m = jni_env->GetMethodID (cls, "charValue", "()C"); | 1044 jmethodID m = jni_env->GetMethodID (cls, "charValue", "()C"); |
1045 retval = jni_env->CallCharMethod (jobj, m); | 1045 retval = jni_env->CallCharMethod (jobj, m); |
1046 retval = retval.convert_to_str (false, true); | 1046 retval = retval.convert_to_str (false, true); |
1047 break; | 1047 break; |
1048 } | 1048 } |
1049 | |
1050 #define BOX_PRIMITIVE_ARRAY(JAVA_TYPE, JAVA_ID, JAVA_TYPE_CAP, OCTAVE_ID) \ | |
1051 cls = jni_env->FindClass (JAVA_ID); \ | |
1052 if (jni_env->IsInstanceOf (jobj, cls)) \ | |
1053 { \ | |
1054 const JAVA_TYPE ## Array jarr = reinterpret_cast<JAVA_TYPE ## Array> (jobj); \ | |
1055 const jsize len = jni_env->GetArrayLength (jarr); \ | |
1056 OCTAVE_ID ## NDArray d (dim_vector (len, 1)); \ | |
1057 JAVA_TYPE * buffer = reinterpret_cast<JAVA_TYPE *> (d.fortran_vec ()); \ | |
1058 jni_env->Get ## JAVA_TYPE_CAP ## ArrayRegion (jarr, 0, len, buffer); \ | |
1059 retval = d; \ | |
1060 break; \ | |
1061 } | |
1062 | |
1063 BOX_PRIMITIVE_ARRAY (jboolean, "[Z", Boolean, bool) | |
1064 BOX_PRIMITIVE_ARRAY (jchar, "[C", Char, char) | |
1065 BOX_PRIMITIVE_ARRAY (jbyte, "[B", Byte, int8) | |
1066 BOX_PRIMITIVE_ARRAY (jshort, "[S", Short, int16) | |
1067 BOX_PRIMITIVE_ARRAY (jint, "[I", Int, int32) | |
1068 BOX_PRIMITIVE_ARRAY (jlong, "[J", Long, int64) | |
1069 BOX_PRIMITIVE_ARRAY (jfloat, "[F", Float, Float) | |
1070 BOX_PRIMITIVE_ARRAY (jdouble, "[D", Double, ) | |
1071 | |
1072 #undef BOX_PRIMITIVE_ARRAY | |
1049 | 1073 |
1050 if (Vjava_matrix_autoconversion) | 1074 if (Vjava_matrix_autoconversion) |
1051 { | 1075 { |
1052 cls = find_octave_class (jni_env, "org/octave/Matrix"); | 1076 cls = find_octave_class (jni_env, "org/octave/Matrix"); |
1053 | 1077 |
2435 retval = args(0).is_java (); | 2459 retval = args(0).is_java (); |
2436 | 2460 |
2437 return retval; | 2461 return retval; |
2438 } | 2462 } |
2439 | 2463 |
2464 /* | |
2465 ## Check automatic conversion of java primitive arrays into octave types | |
2466 %!assert (javaObject ("java.lang.String", "hello").getBytes (), | |
2467 %! int8 ([104 101 108 108 111]')) | |
2468 */ |