在JNI代码中抛出异常的最佳方法? [英] Best way to throw exceptions in JNI code?

查看:156
本文介绍了在JNI代码中抛出异常的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个一致和简单的方法来在JNI代码中抛出异常;处理链接异常的东西(隐式地从env-> ExceptionOccurred方法,或者由参数显式地,以任一方式是好的),并且每次我想要这样做时,可以节省我查找构造函数。上面所有的都是C,尽管我可以在需要时从C ++翻译它。



有没有人有这样的东西可以分享?

解决方案

我们只是为每个想要抛出的异常类型编写实用程序方法。以下是一些示例:

  jint throwNoClassDefError(JNIEnv * env,char * message)
{
jclass exClass;
char * className =java / lang / NoClassDefFoundError;

exClass =(* env) - > FindClass(env,className);
if(exClass == NULL){
return throwNoClassDefError(env,className);
}

return(* env) - > ThrowNew(env,exClass,message);
}

jint throwNoSuchMethodError(
JNIEnv * env,char * className,char * methodName,char * signature)
{

jclass exClass;
char * exClassName =java / lang / NoSuchMethodError;
LPTSTR msgBuf;
jint retCode;
size_t nMallocSize;

exClass =(* env) - > FindClass(env,exClassName);
if(exClass == NULL){
return throwNoClassDefError(env,exClassName);
}

nMallocSize = strlen(className)
+ strlen(methodName)
+ strlen(signature)+ 8;

msgBuf = malloc(nMallocSize);
if(msgBuf == NULL){
return throwOutOfMemoryError
(env,throwNoSuchMethodError:allocate msgBuf);
}
memset(msgBuf,0,nMallocSize);

strcpy(msgBuf,className);
strcat(msgBuf,。);
strcat(msgBuf,methodName);
strcat(msgBuf,。);
strcat(msgBuf,signature);

retCode =(* env) - > ThrowNew(env,exClass,msgBuf);
免费(msgBuf);
return retCode;
}

jint throwNoSuchFieldError(JNIEnv * env,char * message)
{
jclass exClass;
char * className =java / lang / NoSuchFieldError;

exClass =(* env) - > FindClass(env,className);
if(exClass == NULL){
return throwNoClassDefError(env,className);
}

return(* env) - > ThrowNew(env,exClass,message);
}

jint throwOutOfMemoryError(JNIEnv * env,char * message)
{
jclass exClass;
char * className =java / lang / OutOfMemoryError;

exClass =(* env) - > FindClass(env,className);
if(exClass == NULL){
return throwNoClassDefError(env,className);
}

return(* env) - > ThrowNew(env,exClass,message);
}

这样,很容易找到它们,你的代码完成编辑器将帮助你可以输入它们,你可以传递简单的参数。



我相信你可以扩展它来处理链接异常或其他更复杂的方法。这足以满足我们的需求。


I'd like a consistent and simple way to throw exceptions in JNI code; something that handles chained exceptions (implicitly from the env->ExceptionOccurred method, or explicitly by parameters, either way is good) and saves me looking up constructors every time I want to do this. All of the above is preferably in C, although I could translate it from C++ at need.

Does anyone on SO have something like this that they can share?

解决方案

We just code utility methods for each of the types of exceptions we want to throw. Here are some examples:

jint throwNoClassDefError( JNIEnv *env, char *message )
{
    jclass exClass;
    char *className = "java/lang/NoClassDefFoundError";

    exClass = (*env)->FindClass( env, className);
    if (exClass == NULL) {
        return throwNoClassDefError( env, className );
    }

    return (*env)->ThrowNew( env, exClass, message );
}

jint throwNoSuchMethodError(
        JNIEnv *env, char *className, char *methodName, char *signature )
{

    jclass exClass;
    char *exClassName = "java/lang/NoSuchMethodError" ;
    LPTSTR msgBuf;
    jint retCode;
    size_t nMallocSize;

    exClass = (*env)->FindClass( env, exClassName );
    if ( exClass == NULL ) {
        return throwNoClassDefError( env, exClassName );
    }

    nMallocSize = strlen(className) 
            + strlen(methodName)
            + strlen(signature) + 8;

    msgBuf = malloc( nMallocSize );
    if ( msgBuf == NULL ) {
        return throwOutOfMemoryError
                ( env, "throwNoSuchMethodError: allocating msgBuf" );
    }
    memset( msgBuf, 0, nMallocSize );

    strcpy( msgBuf, className );
    strcat( msgBuf, "." );
    strcat( msgBuf, methodName );
    strcat( msgBuf, "." );
    strcat( msgBuf, signature );

    retCode = (*env)->ThrowNew( env, exClass, msgBuf );
    free ( msgBuf );
    return retCode;
}

jint throwNoSuchFieldError( JNIEnv *env, char *message )
{
    jclass exClass;
    char *className = "java/lang/NoSuchFieldError" ;

    exClass = (*env)->FindClass( env, className );
    if ( exClass == NULL ) {
        return throwNoClassDefError( env, className );
    }

    return (*env)->ThrowNew( env, exClass, message );
}

jint throwOutOfMemoryError( JNIEnv *env, char *message )
{
    jclass exClass;
    char *className = "java/lang/OutOfMemoryError" ;

    exClass = (*env)->FindClass( env, className );
    if ( exClass == NULL ) {
        return throwNoClassDefError( env, className );
    }

    return (*env)->ThrowNew( env, exClass, message );
}

That way, it's easy to find them, your code-completion editor will help you to type them in, and you can pass simple parameters.

I'm sure you could expand this to handle chained exceptions, or other more complicated approaches. This was enough to meet our needs.

这篇关于在JNI代码中抛出异常的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆