本文整理汇总了C++中JEnv::NewString方法的典型用法代码示例。如果您正苦于以下问题:C++ JEnv::NewString方法的具体用法?C++ JEnv::NewString怎么用?C++ JEnv::NewString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JEnv
的用法示例。
在下文中一共展示了JEnv::NewString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleScope
Local<Value> FieldAccessor::GetJavaField(Isolate *isolate, const Local<Object>& target, FieldCallbackData *fieldData)
{
JEnv env;
EscapableHandleScope handleScope(isolate);
auto runtime = Runtime::GetRuntime(isolate);
auto objectManager = runtime->GetObjectManager();
Local<Value> fieldResult;
JniLocalRef targetJavaObject;
const auto& fieldTypeName = fieldData->signature;
auto isStatic = fieldData->isStatic;
auto isPrimitiveType = fieldTypeName.size() == 1;
auto isFieldArray = fieldTypeName[0] == '[';
if (fieldData->fid == nullptr)
{
auto fieldJniSig = isPrimitiveType
? fieldTypeName
:
(isFieldArray
? fieldTypeName
:
("L" + fieldTypeName + ";"));
if (isStatic)
{
fieldData->clazz = env.FindClass(fieldData->declaringType);
fieldData->fid = env.GetStaticFieldID(fieldData->clazz, fieldData->name, fieldJniSig);
}
else
{
fieldData->clazz = env.FindClass(fieldData->declaringType);
fieldData->fid = env.GetFieldID(fieldData->clazz, fieldData->name, fieldJniSig);
}
}
if (!isStatic)
{
targetJavaObject = objectManager->GetJavaObjectByJsObject(target);
if (targetJavaObject.IsNull())
{
stringstream ss;
ss << "Cannot access property '" << fieldData->name << "' because there is no corresponding Java object";
throw NativeScriptException(ss.str());
}
}
auto fieldId = fieldData->fid;
auto clazz = fieldData->clazz;
if (isPrimitiveType)
{
switch (fieldTypeName[0])
{
case 'Z': //bool
{
jboolean result;
if (isStatic)
{
result = env.GetStaticBooleanField(clazz, fieldId);
}
else
{
result = env.GetBooleanField(targetJavaObject, fieldId);
}
fieldResult = Boolean::New(isolate, (result == JNI_TRUE));
break;
}
case 'B': //byte
{
jbyte result;
if (isStatic)
{
result = env.GetStaticByteField(clazz, fieldId);
}
else
{
result = env.GetByteField(targetJavaObject, fieldId);
}
fieldResult = handleScope.Escape(Int32::New(isolate, result));
break;
}
case 'C': //char
{
jchar result;
if (isStatic)
{
result = env.GetStaticCharField(clazz, fieldId);
}
else
{
result = env.GetCharField(targetJavaObject, fieldId);
}
JniLocalRef str(env.NewString(&result, 1));
//.........这里部分代码省略.........
示例2: handleScope
Local<Value> ArrayElementAccessor::GetArrayElement(const Local<Object>& array, uint32_t index, const string& arraySignature)
{
JEnv env;
Isolate* isolate = Isolate::GetCurrent();
EscapableHandleScope handleScope(isolate);
jweak arr = objectManager->GetJavaObjectByJsObject(array);
Local<Value> value;
jsize startIndex = index;
const jsize length = 1;
const string elementSignature = arraySignature.substr(1);
jboolean isCopy = false;
if (elementSignature == "Z")
{
jbooleanArray boolArr = reinterpret_cast<jbooleanArray>(arr);
jboolean boolArrValue;
env.GetBooleanArrayRegion(boolArr, startIndex, length, &boolArrValue);
value = ConvertToJsValue(env, elementSignature, &boolArrValue);
}
else if (elementSignature == "B")
{
jbyteArray byteArr = reinterpret_cast<jbyteArray>(arr);
jbyte byteArrValue;
env.GetByteArrayRegion(byteArr, startIndex, length, &byteArrValue);
value = ConvertToJsValue(env, elementSignature, &byteArrValue);
}
else if (elementSignature == "C")
{
jcharArray charArr = reinterpret_cast<jcharArray>(arr);
jchar charArrValue;
env.GetCharArrayRegion(charArr, startIndex, length, &charArrValue);
JniLocalRef s(env.NewString(&charArrValue, 1));
const char* singleChar = env.GetStringUTFChars(s, &isCopy);
value = ConvertToJsValue(env, elementSignature, singleChar);
env.ReleaseStringUTFChars(s, singleChar);
}
else if (elementSignature == "S")
{
jshortArray shortArr = reinterpret_cast<jshortArray>(arr);
jshort shortArrValue;
env.GetShortArrayRegion(shortArr, startIndex, length, &shortArrValue);
value = ConvertToJsValue(env, elementSignature, &shortArrValue);
}
else if (elementSignature == "I")
{
jintArray intArr = reinterpret_cast<jintArray>(arr);
jint intArrValue;
env.GetIntArrayRegion(intArr, startIndex, length, &intArrValue);
value = ConvertToJsValue(env, elementSignature, &intArrValue);
}
else if (elementSignature == "J")
{
jlongArray longArr = reinterpret_cast<jlongArray>(arr);
jlong longArrValue;
env.GetLongArrayRegion(longArr, startIndex, length, &longArrValue);
value = ConvertToJsValue(env, elementSignature, &longArrValue);
}
else if (elementSignature == "F")
{
jfloatArray floatArr = reinterpret_cast<jfloatArray>(arr);
jfloat floatArrValue;
env.GetFloatArrayRegion(floatArr, startIndex, length, &floatArrValue);
value = ConvertToJsValue(env, elementSignature, &floatArrValue);
}
else if (elementSignature == "D")
{
jdoubleArray doubleArr = reinterpret_cast<jdoubleArray>(arr);
jdouble doubleArrValue;
env.GetDoubleArrayRegion(doubleArr, startIndex, length, &doubleArrValue);
value = ConvertToJsValue(env, elementSignature, &doubleArrValue);
}
else
{
jobject result = env.GetObjectArrayElement(reinterpret_cast<jobjectArray>(arr), index);
value = ConvertToJsValue(env, elementSignature, &result);
env.DeleteLocalRef(result);
}
return handleScope.Escape(value);
}
示例3: CallJavaMethod
//.........这里部分代码省略.........
{
jbyte result;
if (isStatic)
{
result = env.CallStaticByteMethodA(clazz, mid, javaArgs);
}
else if (isSuper)
{
result = env.CallNonvirtualByteMethodA(callerJavaObject, clazz, mid, javaArgs);
}
else
{
result = env.CallByteMethodA(callerJavaObject, mid, javaArgs);
}
args.GetReturnValue().Set(result);
break;
}
case MethodReturnType::Char:
{
jchar result;
if (isStatic)
{
result = env.CallStaticCharMethodA(clazz, mid, javaArgs);
}
else if (isSuper)
{
result = env.CallNonvirtualCharMethodA(callerJavaObject, clazz, mid, javaArgs);
}
else
{
result = env.CallCharMethodA(callerJavaObject, mid, javaArgs);
}
JniLocalRef str(env.NewString(&result, 1));
jboolean bol = true;
const char* resP = env.GetStringUTFChars(str, &bol);
args.GetReturnValue().Set(ConvertToV8String(resP, 1));
env.ReleaseStringUTFChars(str, resP);
break;
}
case MethodReturnType::Short:
{
jshort result;
if (isStatic)
{
result = env.CallStaticShortMethodA(clazz, mid, javaArgs);
}
else if (isSuper)
{
result = env.CallNonvirtualShortMethodA(callerJavaObject, clazz, mid, javaArgs);
}
else
{
result = env.CallShortMethodA(callerJavaObject, mid, javaArgs);
}
args.GetReturnValue().Set(result);
break;
}
case MethodReturnType::Int:
{
jint result;
if (isStatic)
{
result = env.CallStaticIntMethodA(clazz, mid, javaArgs);
}
else if (isSuper)
示例4: SetArrayElement
void ArrayElementAccessor::SetArrayElement(const Local<Object>& array, uint32_t index, const string& arraySignature, Local<Value>& value)
{
JEnv env;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
jweak arr = objectManager->GetJavaObjectByJsObject(array);
const string elementSignature = arraySignature.substr(1);
jboolean isCopy = false;
if (elementSignature == "Z") //bool
{
jboolean boolElementValue = (jboolean) value->BooleanValue();
jbooleanArray boolArr = reinterpret_cast<jbooleanArray>(arr);
env.SetBooleanArrayRegion(boolArr, index, 1, &boolElementValue);
}
else if (elementSignature == "B") //byte
{
jbyte byteElementValue = (jbyte) value->Int32Value();
jbyteArray byteArr = reinterpret_cast<jbyteArray>(arr);
env.SetByteArrayRegion(byteArr, index, 1, &byteElementValue);
}
else if (elementSignature == "C") //char
{
String::Utf8Value utf8(value->ToString());
JniLocalRef s(env.NewString((jchar*) *utf8, 1));
const char* singleChar = env.GetStringUTFChars(s, &isCopy);
jchar charElementValue = *singleChar;
env.ReleaseStringUTFChars(s, singleChar);
jcharArray charArr = reinterpret_cast<jcharArray>(arr);
env.SetCharArrayRegion(charArr, index, 1, &charElementValue);
}
else if (elementSignature == "S") //short
{
jshort shortElementValue = (jshort) value->Int32Value();
jshortArray shortArr = reinterpret_cast<jshortArray>(arr);
env.SetShortArrayRegion(shortArr, index, 1, &shortElementValue);
}
else if (elementSignature == "I") //int
{
jint intElementValue = value->Int32Value();
jintArray intArr = reinterpret_cast<jintArray>(arr);
env.SetIntArrayRegion(intArr, index, 1, &intElementValue);
}
else if (elementSignature == "J") //long
{
jlong longElementValue;
if (value->IsObject())
{
longElementValue = (jlong) ArgConverter::ConvertToJavaLong(value);
}
else
{
longElementValue = (jlong) value->IntegerValue();
}
jlongArray longArr = reinterpret_cast<jlongArray>(arr);
env.SetLongArrayRegion(longArr, index, 1, &longElementValue);
}
else if (elementSignature == "F") //float
{
jfloat floatElementValue = (jfloat) value->NumberValue();
jfloatArray floatArr = reinterpret_cast<jfloatArray>(arr);
env.SetFloatArrayRegion(floatArr, index, 1, &floatElementValue);
}
else if (elementSignature == "D") //double
{
jdouble doubleElementValue = (jdouble) value->NumberValue();
jdoubleArray doubleArr = reinterpret_cast<jdoubleArray>(arr);
env.SetDoubleArrayRegion(doubleArr, index, 1, &doubleElementValue);
}
else //string or object
{
bool isReferenceType = value->IsObject() || value->IsString();
if (isReferenceType)
{
auto object = value.As<Object>();
JsArgToArrayConverter argConverter(value, false, (int)Type::Null);
if (argConverter.IsValid())
{
jobjectArray objArr = reinterpret_cast<jobjectArray>(arr);
jobject objectElementValue = argConverter.GetConvertedArg();
env.SetObjectArrayElement(objArr, index, objectElementValue);
}
else
{
JsArgToArrayConverter::Error err = argConverter.GetError();
throw NativeScriptException(string(err.msg));
}
}
else
{
throw NativeScriptException(string("Cannot assign primitive value to array of objects."));
}
}
}
示例5: CallJavaMethod
//.........这里部分代码省略.........
{
jbyte result;
if (isStatic)
{
result = env.CallStaticByteMethodA(clazz, mid, javaArgs);
}
else if (isSuper)
{
result = env.CallNonvirtualByteMethodA(callerJavaObject, clazz, mid, javaArgs);
}
else
{
result = env.CallByteMethodA(callerJavaObject, mid, javaArgs);
}
args.GetReturnValue().Set(result);
break;
}
case 'C': //char
{
jchar result;
if (isStatic)
{
result = env.CallStaticCharMethodA(clazz, mid, javaArgs);
}
else if (isSuper)
{
result = env.CallNonvirtualCharMethodA(callerJavaObject, clazz, mid, javaArgs);
}
else
{
result = env.CallCharMethodA(callerJavaObject, mid, javaArgs);
}
JniLocalRef str(env.NewString(&result, 1));
jboolean bol = true;
const char* resP = env.GetStringUTFChars(str, &bol);
args.GetReturnValue().Set(ConvertToV8String(resP, 1));
env.ReleaseStringUTFChars(str, resP);
break;
}
case 'S': //short
{
jshort result;
if (isStatic)
{
result = env.CallStaticShortMethodA(clazz, mid, javaArgs);
}
else if (isSuper)
{
result = env.CallNonvirtualShortMethodA(callerJavaObject, clazz, mid, javaArgs);
}
else
{
result = env.CallShortMethodA(callerJavaObject, mid, javaArgs);
}
args.GetReturnValue().Set(result);
break;
}
case 'I': //int
{
jint result;
if (isStatic)
{
result = env.CallStaticIntMethodA(clazz, mid, javaArgs);
}
else if (isSuper)