またJNI
5207 ワード
/**
*
*/
bool instanceof(JNIEnv *env, jobject obj, const char *clsName)
{
jclass targetcls = env->FindClass(clsName);
return env->IsInstanceOf( obj, targetcls);
}
/**
* Field
*/
void setIntField(JNIEnv *env, jobject obj, const char *fieldName, int value)
{
jobject refobj = env->NewLocalRef(obj);
jclass targetcls = env->GetObjectClass(refobj);
jfieldID fieldid = env->GetFieldID(targetcls, fieldName, "I");
if(fieldid == NULL)
return;
env->SetIntField(obj, fieldid, value);
env->DeleteLocalRef(targetcls);
env->DeleteLocalRef(refobj);
}
/**
* Field
*/
void setStringField(JNIEnv *env, jobject obj, const char *fieldName, const char* value)
{
jobject refobj = env->NewLocalRef(obj);
jclass targetcls = env->GetObjectClass(refobj);
jfieldID fieldid = env->GetFieldID(targetcls, fieldName, "Ljava/lang/String;");
if(fieldid == NULL)
return;
env->SetObjectField(obj, fieldid, env->NewStringUTF(value));
env->DeleteLocalRef(targetcls);
env->DeleteLocalRef(refobj);
}
/**
* Field
*/
void setBooleanField(JNIEnv *env, jobject obj, const char *fieldName, bool value)
{
jobject refobj = env->NewLocalRef(obj);
jclass targetcls = env->GetObjectClass(refobj);
jfieldID fieldid = env->GetFieldID(targetcls, fieldName, "Z");
if(fieldid == NULL)
return;
env->SetBooleanField(obj, fieldid, value);
env->DeleteLocalRef(targetcls);
env->DeleteLocalRef(refobj);
}
/**
* void
*/
void callVoidMethod(JNIEnv *env, jobject obj, const char *methodName, const char *sig, ...)
{
jobject refobj = env->NewLocalRef(obj);
jclass targetcls = env->GetObjectClass(refobj);
jmethodID mid = env->GetMethodID(targetcls, methodName, sig);
if(mid == NULL){
return;
}
va_list ap;
va_start(ap, sig);
env->CallVoidMethodV(refobj, mid, ap);
va_end(ap);
env->DeleteLocalRef(targetcls);
env->DeleteLocalRef(refobj);
}
/**
* Object
*/
jobject callJObjectMethod(JNIEnv *env, jobject obj, const char *methodName, const char *sig, ...)
{
jobject rtn;
jobject refobj = env->NewLocalRef(obj);
jclass targetcls = env->GetObjectClass(refobj);
jmethodID mid = env->GetMethodID(targetcls, methodName, sig);
if(mid == NULL){
return NULL;
}
va_list ap;
va_start(ap, sig);
rtn = env->CallObjectMethodV(refobj, mid, ap);
va_end(ap);
env->DeleteLocalRef(targetcls);
env->DeleteLocalRef(refobj);
return rtn;
}
/**
*
*/
void setField(JNIEnv *env, jboolean *hasException, jobject obj, const char *fieldName, const char *descriptor, const jvalue value)
{
jobject refobj;
jclass targetcls;
jfieldID fieldid;
//
if(env->EnsureLocalCapacity(2) != JNI_OK)
{
goto error;
}
refobj = env->NewLocalRef(obj);
targetcls = env->GetObjectClass(refobj);
fieldid = env->GetFieldID(targetcls, fieldName, descriptor);
if(fieldid == NULL)
goto error;
switch(*descriptor){
case '[':
case 'L':
env->SetObjectField(obj, fieldid, (jobject)value.l);
break;
case 'Z':
env->SetBooleanField(obj, fieldid, (jboolean)value.z);
break;
case 'B':
env->SetByteField(obj, fieldid, (jbyte)value.b);
break;
case 'C':
env->SetCharField(obj, fieldid, (jchar)value.c);
break;
case 'S':
env->SetShortField(obj, fieldid, (jshort)value.s);
break;
case 'I':
env->SetIntField(obj, fieldid, (jint)value.i);
break;
case 'J':
env->SetLongField(obj, fieldid, (jlong)value.j);
break;
case 'F':
env->SetFloatField(obj, fieldid, (jfloat)value.f);
break;
case 'D':
env->SetDoubleField(obj, fieldid, (jdouble)value.d);
break;
}
env->DeleteLocalRef(targetcls);
env->DeleteLocalRef(refobj);
error:
if(env->ExceptionOccurred())
{
*hasException = env->ExceptionCheck();
// *hasException = true;
// env->ExceptionDescribe();
// env->ExceptionClear();
// env->ThrowNew(targetcls, "error");
}
}