Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix stupid comments
[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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
20
21 /* *********** */
22 /* JNI GETTERS */
23 /* *********** */
24
25 jclass jxbt_get_class(JNIEnv* env, const char*name) {
26   jclass cls = (*env)->FindClass(env, name);
27         
28   if(!cls) {
29     char *m=bprintf("Class %s not found",name);
30     jxbt_throw_jni(env, m);
31     free(m);
32     return NULL;
33   }
34
35   return cls;
36 }
37
38 jmethodID jxbt_get_jmethod(JNIEnv* env, jclass cls, 
39                            const char *name,const char *signature) {
40   jmethodID id;
41
42   if (!cls)
43     return 0;
44   id = (*env)->GetMethodID(env, cls, name,signature);
45         
46   if(!id) {
47
48     jmethodID tostr_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
49     jstring jclassname = (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL);
50     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
51
52     char *m=bprintf("Cannot find method %s(%s) in %s", name, signature ,classname);
53
54     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
55
56     jxbt_throw_jni(env,m);
57
58     free(m);
59     return 0;
60   }
61
62   return id;
63 }
64
65 jmethodID jxbt_get_smethod(JNIEnv* env, const char *classname, 
66                           const char *name,const char *signature) { 
67   
68         jclass cls;
69
70         jmethodID id;
71         cls = jxbt_get_class(env,classname);
72
73   if (!cls)
74     return 0;
75
76   id = (*env)->GetMethodID(env, cls, name,signature);
77         
78   if(!id) {
79     char *m=bprintf("Cannot find method %s(%s) in %s", name, signature,classname);
80
81     jxbt_throw_jni(env,m);
82
83     free(m);
84     return 0;
85   }
86   return id;
87 }
88
89 jfieldID jxbt_get_jfield(JNIEnv* env, jclass cls, 
90                          const char *name, const char *signature) {
91   jfieldID id;
92
93   if (!cls)
94     return 0;
95
96   id = (*env)->GetFieldID(env, cls, name, signature);
97         
98   if(!id) {
99     jmethodID getname_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;");
100     jstring jclassname = (jstring) (*env)->CallObjectMethod(env,cls, getname_id, NULL);
101     const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0);
102     char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
103
104     (*env)->ReleaseStringUTFChars(env, jclassname, classname);  
105
106     jxbt_throw_jni(env,m);
107
108     free(m);
109     return 0;
110   }
111
112   return id;
113 }
114
115 jfieldID jxbt_get_sfield(JNIEnv* env, const char *classname, 
116                         const char *name, const char *signature) {
117   jclass cls = jxbt_get_class(env,classname);
118   jfieldID id;
119
120   if (!cls)
121     return 0;
122
123   id = (*env)->GetFieldID(env, cls, name, signature);
124         
125   if(!id) {
126     char *m=bprintf("Cannot find field %s %s in %s",signature, name, classname);
127
128     jxbt_throw_jni(env,m);
129
130     free(m);
131     return 0;
132   }
133
134   return id;
135 }
136
137 /* ***************** */
138 /* EXCEPTION RAISING */
139 /* ***************** */
140 static void jxbt_throw_by_name(JNIEnv* env,const char* name, char *msg) {
141    jclass cls = (*env)->FindClass(env, name);
142
143    xbt_assert2(cls,"%s (Plus severe error: class %s not found)\n",
144                msg,name);
145
146    (*env)->ThrowNew(env,cls,msg);
147
148    free(msg);
149 }
150
151
152 /* Errors in MSG */
153 void jxbt_throw_jni(JNIEnv* env,const char* msg) {
154   jxbt_throw_by_name(env,
155                      "simgrid/msg/JniException",
156                      bprintf("Internal or JNI error: %s",msg));
157 }
158 void jxbt_throw_notbound(JNIEnv* env,const char* kind, void *pointer) {
159   jxbt_throw_by_name(env,
160                      "simgrid/msg/JniException",
161                      bprintf("Internal error: %s %p not bound",kind, pointer));
162 }
163
164 void jxbt_throw_native(JNIEnv* env,char* msg) {
165   jxbt_throw_by_name(env, "simgrid/msg/NativeException", msg);
166 }
167
168 /* *** */
169 void jxbt_throw_null(JNIEnv* env, char* msg) {
170   jxbt_throw_by_name(env, "java/lang/NullPointerException", msg);
171 }
172
173
174 /* Errors on user side */
175 void jxbt_throw_illegal(JNIEnv* env, char* msg) {
176   jxbt_throw_by_name(env, "java/lang/IllegalArgumentException", msg);
177 }
178 void jxbt_throw_host_not_found(JNIEnv* env, const char *invalid_name) {
179   jxbt_throw_by_name(env,
180                      "simgrid/msg/HostNotFoundException",
181                      bprintf("No such host: %s",invalid_name));
182 }
183 void jxbt_throw_process_not_found(JNIEnv* env, const char *invalid_name) {
184   jxbt_throw_by_name(env,
185                      "simgrid/msg/ProcessNotFoundException",
186                      bprintf("No such process: %s",invalid_name));
187 }