简体   繁体   English

如何通过JNI将Java CharSequence转换为C ++ char *

[英]How to convert java CharSequence to c++ char* through JNI

I feel that this should have a simple answer as I can do it easily with a java String , but something is going wrong with the CharSequence . 我觉得这应该有一个简单的答案,因为我可以使用java String轻松完成此操作,但是CharSequence出了点问题。 My java code initializes a cppGenerator , passes a functionBlock object to its generateImplementation method , and returns a CharSequence . 我的Java代码初始化cppGenerator ,将functionBlock object传递给它的generateImplementation method ,然后返回CharSequence Here is my code: 这是我的代码:

jobject fBlock = env->CallObjectMethod(structuredTextModel, getFunction);
jclass CPPGenerator = lookForClass(env, "Path/CPPGenerator");
jobject CPPGeneratorImpl = invokeClassObj(env, CPPGenerator);
    jmethodID generateImplementation = env->GetMethodID(CPPGenerator, "generateImplementation", "(LPath/FunctionBlock;)Ljava/lang/CharSequence;");
jobject mid = env->CallObjectMethod(CPPGeneratorImpl, generateImplementation, fBlock);
jstring jresult = (jstring)mid;
char* result = (char*)env->GetStringUTFChars(jresult, 0);

This logic has worked for me when using String but this function returns a CharSequence . 使用String时,此逻辑对我有用,但此函数返回CharSequence No classes, objects, or methods are returning null. 没有类,对象或方法返回null。 The program crashes in jni.h attempting to call GetStringUTFChars . 尝试调用GetStringUTFChars该程序在jni.h崩溃。 So i know I cannot use this method for CharSequence. 所以我知道我不能对CharSequence使用此方法。 Is there just a jni method I am missing or do I have to do something completely different for a CharSequence? 我只是缺少一种jni方法,还是必须对CharSequence做完全不同的事情?

You can't just cast mid to jstring and call GetStringUTFChars (since mid might not be a String , which seems to be the case). 您不能只将mid jstringjstring并调用GetStringUTFChars (因为mid可能不是String ,这似乎是事实)。 There is no convenience method like GetStringUTFChars for CharSequence s like there is for String s. 对于CharSequence没有像GetStringUTFChars这样的便捷方法,就象String

You'd have to do it the inconvenient way of calling charAt : 您必须以一种不方便的方式调用charAt

jclass cCharSequence = env->FindClass("java/lang/CharSequence");
jmethodID mLength = env->GetMethodID(cCharSequence, "length", "()I");
jmethodID mCharAt = env->GetMethodID(cCharSequence, "charAt", "(I)C");

int length = env->CallIntMethod(mid, mLength);
std::string str;
str.reserve(length);
for(int i = 0; i < length; i++) {
    str += env->CallCharMethod(mid, mCharAt, i);
}

// use 'str.c_str()'...

Why not use toString directly? 为什么不直接使用toString

jclass clzCharSequence = env->GetObjectClass(mid);
jmethodID toString = env->GetMethodID(clzCharSequence, "toString", "()Ljava/lang/String;");
jobject s = env->CallObjectMethod(mid, toString);
const char* cstr = env->GetStringUTFChars(static_cast<jstring>(s), NULL);
// do something with cstr
env->ReleaseStringUTFChars(static_cast<jstring>(s), cstr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM