Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ansi C declaration of the variables (at the beginning of the blocks)
[simgrid.git] / src / java / jxbt_utilities.c
1 /*
2  * $Id$
3  *
4  * Various JNI helper functions
5  *
6  * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved. 
7  *
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.
10  *
11  */
12
13 #include <stdlib.h> /* abort */
14 #include "xbt/misc.h"
15 #include "xbt/sysdep.h"
16 #include "xbt/str.h"
17 #include "jxbt_utilities.h"
18
19 /* *********** */
20 /* JNI GETTERS */
21 /* *********** */
22
23 jclass jxbt_get_class(JNIEnv* env, const char*name) {
24   jclass cls = (*env)->FindClass(env, name);
25         
26   if(!cls) {
27     char *m=bprintf("Class %s not found",name);
28     jxbt_throw_jni(env, m);
29     free(m);
30     return NULL;
31   }
32
33   return cls;
34 }
35
36 jmethodID jxbt_get_jmethod(JNIEnv* env, jclass cls, 
37                            const char *name,const char *signature) {
38   jmethodID id;
39
40   if (!cls)
41     return 0;
42   id = (*env)->GetMethodID(env, cls, name,signature);
43         
44   if(!id) {
45
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);
49
50     char *m=bprintf("Cannot find method %s(%s) in %s", name, signature ,classname);
51
52     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
53
54     jxbt_throw_jni(env,m);
55
56     free(m);
57     return 0;
58   }
59
60   return id;
61 }
62
63 jmethodID jxbt_get_smethod(JNIEnv* env, const char *classname, 
64                           const char *name,const char *signature) { 
65   
66         jclass cls;
67
68         jmethodID id;
69         cls = jxbt_get_class(env,classname);
70
71   if (!cls)
72     return 0;
73
74   id = (*env)->GetMethodID(env, cls, name,signature);
75         
76   if(!id) {
77     char *m=bprintf("Cannot find method %s(%s) in %s", name, signature,classname);
78
79     jxbt_throw_jni(env,m);
80
81     free(m);
82     return 0;
83   }
84   return id;
85 }
86
87 jfieldID jxbt_get_jfield(JNIEnv* env, jclass cls, 
88                          const char *name, const char *signature) {
89   jfieldID id;
90
91   if (!cls)
92     return 0;
93
94   id = (*env)->GetFieldID(env, cls, name, signature);
95         
96   if(!id) {
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);
101
102     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
103
104     jxbt_throw_jni(env,m);
105
106     free(m);
107     return 0;
108   }
109
110   return id;
111 }
112
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);
116   jfieldID id;
117
118   if (!cls)
119     return 0;
120
121   id = (*env)->GetFieldID(env, cls, name, signature);
122         
123   if(!id) {
124     char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
125
126     jxbt_throw_jni(env,m);
127
128     free(m);
129     return 0;
130   }
131
132   return id;
133 }
134
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);
140
141    xbt_assert2(cls,"%s (Plus severe error: class %s not found)\n",
142                msg,name);
143
144    (*env)->ThrowNew(env,cls,msg);
145
146    free(msg);
147 }
148
149
150 /* Errors in 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));
155 }
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));
160 }
161
162 void jxbt_throw_native(JNIEnv* env,char* msg) {
163   jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
164 }
165
166 /* *** */
167 void jxbt_throw_null(JNIEnv* env, char* msg) {
168   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
169 }
170
171
172 /* Errors on user side */
173 void jxbt_throw_illegal(JNIEnv* env, char* msg) {
174   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
175 }
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));
180 }
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));
185 }