4 * Various JNI helper functions
6 * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package.
13 #include <stdlib.h> /* abort */
15 #include "xbt/sysdep.h"
17 #include "jxbt_utilities.h"
23 jclass jxbt_get_class(JNIEnv* env, const char*name) {
24 jclass cls = (*env)->FindClass(env, name);
27 char *m=bprintf("Class %s not found",name);
28 jxbt_throw_jni(env, m);
36 jmethodID jxbt_get_jmethod(JNIEnv* env, jclass cls,
37 const char *name,const char *signature) {
42 id = (*env)->GetMethodID(env, cls, name,signature);
46 jmethodID tostr_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
47 jstring jclassname = (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
48 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
50 char *m=bprintf("Cannot find method %s(%s) in %s", name, signature ,classname);
52 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
54 jxbt_throw_jni(env,m);
63 jmethodID jxbt_get_smethod(JNIEnv* env, const char *classname,
64 const char *name,const char *signature) {
69 cls = jxbt_get_class(env,classname);
74 id = (*env)->GetMethodID(env, cls, name,signature);
77 char *m=bprintf("Cannot find method %s(%s) in %s", name, signature,classname);
79 jxbt_throw_jni(env,m);
87 jfieldID jxbt_get_jfield(JNIEnv* env, jclass cls,
88 const char *name, const char *signature) {
94 id = (*env)->GetFieldID(env, cls, name, signature);
97 jmethodID getname_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
98 jstring jclassname = (jstring) (*env)->CallObjectMethod(env,cls, getname_id, NULL);
99 const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
100 char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
102 (*env)->ReleaseStringUTFChars(env, jclassname, classname);
104 jxbt_throw_jni(env,m);
113 jfieldID jxbt_get_sfield(JNIEnv* env, const char *classname,
114 const char *name, const char *signature) {
115 jclass cls = jxbt_get_class(env,classname);
121 id = (*env)->GetFieldID(env, cls, name, signature);
124 char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
126 jxbt_throw_jni(env,m);
135 /* ***************** */
136 /* EXCEPTION RAISING */
137 /* ***************** */
138 static void jxbt_throw_by_name(JNIEnv* env,const char* name, char *msg) {
139 jclass cls = (*env)->FindClass(env, name);
141 xbt_assert2(cls,"%s (Plus severe error: class %s not found)\n",
144 (*env)->ThrowNew(env,cls,msg);
151 void jxbt_throw_jni(JNIEnv* env,const char* msg) {
152 jxbt_throw_by_name(env,
153 "simgrid/msg/JniException",
154 bprintf("Internal or JNI error: %s",msg));
156 void jxbt_throw_notbound(JNIEnv* env,const char* kind, void *pointer) {
157 jxbt_throw_by_name(env,
158 "simgrid/msg/JniException",
159 bprintf("Internal error: %s %p not bound",kind, pointer));
162 void jxbt_throw_native(JNIEnv* env,char* msg) {
163 jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
167 void jxbt_throw_null(JNIEnv* env, char* msg) {
168 jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
172 /* Errors on user side */
173 void jxbt_throw_illegal(JNIEnv* env, char* msg) {
174 jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
176 void jxbt_throw_host_not_found(JNIEnv* env, const char *invalid_name) {
177 jxbt_throw_by_name(env,
178 "simgrid/msg/HostNotFoundException",
179 bprintf("No such host: %s",invalid_name));
181 void jxbt_throw_process_not_found(JNIEnv* env, const char *invalid_name) {
182 jxbt_throw_by_name(env,
183 "simgrid/msg/ProcessNotFoundException",
184 bprintf("No such process: %s",invalid_name));