void get_row(std::vector<std::string>& row) { jint i; row.clear(); char str_value[32]; jboolean flag = env->CallBooleanMethod(instances, jMID_Enumeration_hasMoreElements); if( flag ) { jobject instance = env->CallObjectMethod(instances, jMID_Enumeration_nextElement); jint d = env->CallIntMethod(instance, jMID_WekaInstance_numAttributes); jdouble value; jboolean iscopy; for(i = 0; i < d; i++) { value = env->CallDoubleMethod(instance, jMID_WekaInstance_value, i); sprintf(str_value, "%g", (double) value); row.push_back(std::string(str_value)); // row push_back classIndex } } return; }
jdouble CinderDelegate::callDoubleMethodOnJavaListener(const char *name, const char *sig, ...) { JNIEnv *env; mJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4); jclass cls = env->GetObjectClass(mJavaListener); jmethodID method = env->GetMethodID(cls, name, sig); va_list args; va_start(args, sig); jdouble ret = env->CallDoubleMethod(mJavaListener, method, args); va_end(args); return ret; }
RHO_GLOBAL int rho_sysimpl_get_property(char* szPropName, VALUE* resValue) { JNIEnv *env = jnienv(); jclass cls = getJNIClass(RHODES_JAVA_CLASS_RHODES_SERVICE); if (!cls) return 0; jmethodID mid = getJNIClassStaticMethod(env, cls, "getProperty", "(Ljava/lang/String;)Ljava/lang/Object;"); if (!mid) return 0; jhstring propNameObj = rho_cast<jhstring>(szPropName); jhobject result = jhobject(env->CallStaticObjectMethod(cls, mid, propNameObj.get())); if (!result) return 0; jclass clsBoolean = getJNIClass(RHODES_JAVA_CLASS_BOOLEAN); jclass clsInteger = getJNIClass(RHODES_JAVA_CLASS_INTEGER); jclass clsFloat = getJNIClass(RHODES_JAVA_CLASS_FLOAT); jclass clsDouble = getJNIClass(RHODES_JAVA_CLASS_DOUBLE); jclass clsString = getJNIClass(RHODES_JAVA_CLASS_STRING); if (env->IsInstanceOf(result.get(), clsBoolean)) { jmethodID midValue = getJNIClassMethod(env, clsBoolean, "booleanValue", "()Z"); *resValue = rho_ruby_create_boolean((int)env->CallBooleanMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsInteger)) { jmethodID midValue = getJNIClassMethod(env, clsInteger, "intValue", "()I"); *resValue = rho_ruby_create_integer((int)env->CallIntMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsFloat)) { jmethodID midValue = getJNIClassMethod(env, clsFloat, "floatValue", "()F"); *resValue = rho_ruby_create_double((double)env->CallFloatMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsDouble)) { jmethodID midValue = getJNIClassMethod(env, clsDouble, "doubleValue", "()D"); *resValue = rho_ruby_create_double((double)env->CallDoubleMethod(result.get(), midValue)); return 1; } else if (env->IsInstanceOf(result.get(), clsString)) { jstring resStrObj = (jstring)result.get(); *resValue = rho_ruby_create_string(rho_cast<std::string>(resStrObj).c_str()); return 1; } return 0; }
double JSONObject::getDouble(const std::string& name) const { #if ENABLE_VERBOSE_LOGGING __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "double JSONObject::getDouble(const std::string& name) const"); #endif FindJNI(); JNIEnv* env = cocos2d::JniHelper::getEnv(); if (!_instance) { __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Instance is not valid!"); return 0; } if (!_mGetDouble) { __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "_mGetDouble is not valid!"); return 0; } jstring arg1 = env->NewStringUTF(name.c_str()); jdouble result = env->CallDoubleMethod(_instance, _mGetDouble, arg1); if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Failed to get double"); return 0; } #if ENABLE_VERBOSE_LOGGING __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Success get double: %f", (float)result); #endif env->DeleteLocalRef(arg1); return (double)result; }
// converts java object to js value and recursively converts sub objects if this // object is a container type v8::Handle<v8::Value> TypeConverter::javaObjectToJsValue(jobject javaObject) { if (!javaObject) { return v8::Null(); } JNIEnv *env = JNIScope::getEnv(); if (!env) { return v8::Handle<v8::Value>(); } if (env->IsInstanceOf(javaObject, JNIUtil::booleanClass)) { jboolean javaBoolean = env->CallBooleanMethod(javaObject, JNIUtil::booleanBooleanValueMethod); return javaBoolean ? v8::True() : v8::False(); } else if (env->IsInstanceOf(javaObject, JNIUtil::numberClass)) { jdouble javaDouble = env->CallDoubleMethod(javaObject, JNIUtil::numberDoubleValueMethod); return v8::Number::New((double) javaDouble); } else if (env->IsInstanceOf(javaObject, JNIUtil::stringClass)) { return TypeConverter::javaStringToJsString((jstring) javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::dateClass)) { return TypeConverter::javaDateToJsDate(javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::hashMapClass)) { v8::Handle<v8::Object> jsObject = v8::Object::New(); jobject hashMapSet = env->CallObjectMethod(javaObject, JNIUtil::hashMapKeySetMethod); jobjectArray hashMapKeys = (jobjectArray) env->CallObjectMethod(hashMapSet, JNIUtil::setToArrayMethod); env->DeleteLocalRef(hashMapSet); int hashMapKeysLength = env->GetArrayLength(hashMapKeys); for (int i = 0; i < hashMapKeysLength; i++) { jobject javaPairKey = env->GetObjectArrayElement(hashMapKeys, i); v8::Handle<v8::Value> jsPairKey = TypeConverter::javaObjectToJsValue(javaPairKey); jobject javaPairValue = env->CallObjectMethod(javaObject, JNIUtil::hashMapGetMethod, javaPairKey); env->DeleteLocalRef(javaPairKey); jsObject->Set(jsPairKey, TypeConverter::javaObjectToJsValue(javaPairValue)); env->DeleteLocalRef(javaPairValue); } env->DeleteLocalRef(hashMapKeys); return jsObject; } else if (env->IsInstanceOf(javaObject, JNIUtil::krollProxyClass)) { jobject krollObject = env->GetObjectField(javaObject, JNIUtil::krollProxyKrollObjectField); if (krollObject) { jlong v8ObjectPointer = env->GetLongField(krollObject, JNIUtil::v8ObjectPtrField); env->DeleteLocalRef(krollObject); if (v8ObjectPointer != 0) { return Persistent<Object>((Object *) v8ObjectPointer); } } jclass javaObjectClass = env->GetObjectClass(javaObject); v8::Handle<v8::Object> proxyHandle = ProxyFactory::createV8Proxy(javaObjectClass, javaObject); env->DeleteLocalRef(javaObjectClass); return proxyHandle; } else if (env->IsInstanceOf(javaObject, JNIUtil::v8FunctionClass)) { return javaObjectToJsFunction(javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::objectArrayClass)) { return javaArrayToJsArray((jobjectArray) javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::shortArrayClass)) { return javaArrayToJsArray((jshortArray) javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::intArrayClass)) { return javaArrayToJsArray((jintArray) javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::longArrayClass)) { return javaArrayToJsArray((jlongArray) javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::floatArrayClass)) { return javaArrayToJsArray((jfloatArray) javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::doubleArrayClass)) { return javaArrayToJsArray((jdoubleArray) javaObject); } else if (env->IsInstanceOf(javaObject, JNIUtil::booleanArrayClass)) { return javaArrayToJsArray((jbooleanArray) javaObject); } JNIUtil::logClassName("!!! Unable to convert unknown Java object class '%s' to Js value !!!", env->GetObjectClass(javaObject), true); return v8::Handle<v8::Value>(); }