📄 jni_ibss.cpp
字号:
mChars = NULL;
if(str==NULL)
return;
const jchar* w_buffer = env->GetStringChars (str, 0);
mJstr = env->NewString (w_buffer,
wcslen (w_buffer)); // Deep Copy, in usual case we only need
// Shallow Copy as we just need this class to
// provide some convenience for handling jstring
mChars = new char[wcslen (w_buffer) * 2 + 1];
WideCharToMultiByte (CP_ACP, 0, w_buffer, wcslen (w_buffer) + 1, mChars, wcslen (w_buffer) * 2 + 1,
NULL, NULL);
env->ReleaseStringChars (str, w_buffer);
mString = mChars;
};
// Create a new instance from the specified string
JNIString::JNIString(JNIEnv* env, const string& str)
{
mEnv = env;
int slen = str.length ();
jchar* buffer = new jchar[slen];
int len = MultiByteToWideChar (CP_ACP, 0, str.c_str (), str.length (), buffer, slen);
if (len > 0 && len < slen)
buffer[len] = 0;
mJstr = env->NewString (buffer, len);
delete [] buffer;
mChars = new char[str.length () + 1];
strcpy (mChars, str.c_str ());
mString.empty ();
mString = str.c_str ();
};
// Create a new instance as a copy of the specified JNIString
JNIString::JNIString(const JNIString& rhs)
{
mEnv = rhs.mEnv;
const jchar* wstr = mEnv->GetStringChars (rhs.mJstr, 0);
mJstr = mEnv->NewString (wstr, wcslen (wstr));
mEnv->ReleaseStringChars (rhs.mJstr, wstr);
mChars = new char[strlen (rhs.mChars) + 1];
strcpy (mChars, rhs.mChars);
mString = rhs.mString.c_str ();
};
// Delete the instance and release allocated storage
JNIString::~JNIString()
{
if(mChars!=NULL)
delete [] mChars;
};
// assign a new value to this instance from the given string
JNIString & JNIString::operator =(const string& rhs)
{
if(mChars!=NULL)
delete [] mChars;
mJstr = NULL;
mChars = NULL;
int slen = rhs.length ();
jchar* buffer = new jchar[slen];
int len = MultiByteToWideChar (CP_ACP, 0, rhs.c_str (), rhs.length (), buffer, slen);
if (len > 0 && len < slen)
buffer[len] = 0;
mJstr = mEnv->NewString (buffer, len);
delete [] buffer;
mChars = new char[rhs.length () + 1];
strcpy (mChars, rhs.c_str ());
mString = rhs.c_str ();
return *this;
}
// Supply operator methods for converting the JNIString to a string
// or char*, making it easy to pass JNIString arguments to functions
// that require string or char* parameters.
string & JNIString::GetString()
{
return mString;
};
JNIString::operator string()
{
return mString;
};
JNIString::operator const char* ()
{
return mString.c_str ();
};
JNIString::operator char* ()
{
return (char *)mString.c_str();
};
JNIString::operator jstring()
{
return mJstr;
};
jvalue JNU_CallMethodByName(JNIEnv *mEnv,
jboolean *hasException,
jobject obj,
const char *method_name,
const char *descriptor,
...)
{
va_list args;
jclass clazz;
jmethodID mid;
jvalue result;
result.l = NULL;
if (mEnv->EnsureLocalCapacity(2) == JNI_OK) {
clazz = mEnv->GetObjectClass(obj);
if(!clazz)
return result;
mid = mEnv->GetMethodID(clazz, method_name, descriptor);
if (mid) {
const char *p = descriptor;
/* skip over argument types to find out the
* return type */
while (*p != ')') p++;
/* skip ')' */
p++;
va_start(args, descriptor);
switch (*p) {
case 'V':
mEnv->CallVoidMethodV(obj, mid, args);
break;
case '[':
case 'L':
result.l = mEnv->CallObjectMethodV(obj, mid, args);
break;
case 'Z':
result.z = mEnv->CallBooleanMethodV(obj, mid, args);
break;
case 'B':
result.b = mEnv->CallByteMethodV(obj, mid, args);
break;
case 'C':
result.c = mEnv->CallCharMethodV(obj, mid, args);
break;
case 'S':
result.s = mEnv->CallShortMethodV(obj, mid, args);
break;
case 'I':
result.i = mEnv->CallIntMethodV(obj, mid, args);
break;
case 'J':
result.j = mEnv->CallLongMethodV(obj, mid, args);
break;
case 'F':
result.f = mEnv->CallFloatMethodV(obj, mid, args);
break;
case 'D':
result.d = mEnv->CallDoubleMethodV(obj, mid, args);
break;
default:
mEnv->FatalError("illegal descriptor");
}
va_end(args);
}
mEnv->DeleteLocalRef(clazz);
}
if (hasException) {
*hasException = mEnv->ExceptionCheck();
}
return result;
}
jvalue JNU_CallStaticMethodByName(JNIEnv *mEnv,
jboolean *hasException,
const char *obj_name,
const char *method_name,
const char *descriptor,
...)
{
va_list args;
jclass clazz;
jmethodID mid;
jvalue result;
result.l=NULL;
if (mEnv->EnsureLocalCapacity(2) == JNI_OK) {
clazz = mEnv->FindClass(obj_name);
if (clazz == NULL) {
return result; /* out of memory exception thrown */
}
mid = mEnv->GetStaticMethodID(clazz, method_name, descriptor);
if (mid) {
const char *p = descriptor;
/* skip over argument types to find out the
* return type */
while (*p != ')') p++;
/* skip ')' */
p++;
va_start(args, descriptor);
switch (*p) {
case 'V':
mEnv->CallStaticVoidMethodV(clazz, mid, args);
break;
case '[':
case 'L':
result.l = mEnv->CallStaticObjectMethodV(clazz, mid, args);
break;
case 'Z':
result.z = mEnv->CallStaticBooleanMethodV(clazz, mid, args);
break;
case 'B':
result.b = mEnv->CallStaticByteMethodV(clazz, mid, args);
break;
case 'C':
result.c = mEnv->CallStaticCharMethodV(clazz, mid, args);
break;
case 'S':
result.s = mEnv->CallStaticShortMethodV(clazz, mid, args);
break;
case 'I':
result.i = mEnv->CallStaticIntMethodV(clazz, mid, args);
break;
case 'J':
result.j = mEnv->CallStaticLongMethodV(clazz, mid, args);
break;
case 'F':
result.f = mEnv->CallStaticFloatMethodV(clazz, mid, args);
break;
case 'D':
result.d = mEnv->CallStaticDoubleMethodV(clazz, mid, args);
break;
default:
mEnv->FatalError("illegal descriptor");
}
va_end(args);
}
mEnv->DeleteLocalRef(clazz);
}
if (hasException) {
*hasException = mEnv->ExceptionCheck();
}
return result;
}
jvalue JNU_CallStaticMethodByName(JNIEnv *mEnv,
jboolean *hasException,
jobject obj,
const char *method_name,
const char *descriptor,
...)
{
va_list args;
jclass clazz;
jmethodID mid;
jvalue result;
result.l=NULL;
if (mEnv->EnsureLocalCapacity(2) == JNI_OK) {
clazz = mEnv->GetObjectClass(obj);
if(!clazz)
return result;
mid = mEnv->GetStaticMethodID(clazz, method_name, descriptor);
if (mid) {
const char *p = descriptor;
/* skip over argument types to find out the
* return type */
while (*p != ')') p++;
/* skip ')' */
p++;
va_start(args, descriptor);
switch (*p) {
case 'V':
mEnv->CallStaticVoidMethodV(clazz, mid, args);
break;
case '[':
case 'L':
result.l = mEnv->CallStaticObjectMethodV(clazz, mid, args);
break;
case 'Z':
result.z = mEnv->CallStaticBooleanMethodV(clazz, mid, args);
break;
case 'B':
result.b = mEnv->CallStaticByteMethodV(clazz, mid, args);
break;
case 'C':
result.c = mEnv->CallStaticCharMethodV(clazz, mid, args);
break;
case 'S':
result.s = mEnv->CallStaticShortMethodV(clazz, mid, args);
break;
case 'I':
result.i = mEnv->CallStaticIntMethodV(clazz, mid, args);
break;
case 'J':
result.j = mEnv->CallStaticLongMethodV(clazz, mid, args);
break;
case 'F':
result.f = mEnv->CallStaticFloatMethodV(clazz, mid, args);
break;
case 'D':
result.d = mEnv->CallStaticDoubleMethodV(clazz, mid, args);
break;
default:
mEnv->FatalError("illegal descriptor");
}
va_end(args);
}
mEnv->DeleteLocalRef(clazz);
}
if (hasException) {
*hasException = mEnv->ExceptionCheck();
}
return result;
}
jobject JNU_NewClassByName(JNIEnv *mEnv,
jboolean *hasException,
const char *obj_name,
const char *descriptor,
...)
{
va_list args;
jclass clazz;
jmethodID mid;
jobject result;
result=NULL;
if (mEnv->EnsureLocalCapacity(2) == JNI_OK) {
clazz = mEnv->FindClass(obj_name);
if (clazz == NULL) {
return result; /* out of memory exception thrown */
}
mid = mEnv->GetMethodID(clazz, "<init>", descriptor);
if (mid) {
const char *p = descriptor;
/* skip over argument types to find out the
* return type */
while (*p != ')') p++;
/* skip ')' */
p++;
va_start(args, descriptor);
result = mEnv->CallObjectMethodV(clazz, mid, args);
va_end(args);
}
mEnv->DeleteLocalRef(clazz);
}
if (hasException) {
*hasException = mEnv->ExceptionCheck();
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -