10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "jvm.h"
30 #include "jni.h"
31 #include "jni_util.h"
32
33 /* Due to a bug in the win32 C runtime library strings
34 * such as "z:" need to be appended with a "." so we
35 * must allocate at least 4 bytes to allow room for
36 * this expansion. See 4235353 for details.
37 */
38 #define MALLOC_MIN4(len) ((char *)malloc((len) + 1 < 4 ? 4 : (len) + 1))
39
40 /**
41 * Throw a Java exception by name. Similar to SignalError.
42 */
43 JNIEXPORT void JNICALL
44 JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg)
45 {
46 jclass cls = (*env)->FindClass(env, name);
47
48 if (cls != 0) /* Otherwise an exception has already been thrown */
49 (*env)->ThrowNew(env, cls, msg);
50 }
51
130 }
131
132 JNIEXPORT void JNICALL
133 JNU_ThrowNoSuchMethodError(JNIEnv *env, const char *msg)
134 {
135 JNU_ThrowByName(env, "java/lang/NoSuchMethodError", msg);
136 }
137
138 JNIEXPORT void JNICALL
139 JNU_ThrowStringIndexOutOfBoundsException(JNIEnv *env, const char *msg)
140 {
141 JNU_ThrowByName(env, "java/lang/StringIndexOutOfBoundsException", msg);
142 }
143
144 JNIEXPORT void JNICALL
145 JNU_ThrowInstantiationException(JNIEnv *env, const char *msg)
146 {
147 JNU_ThrowByName(env, "java/lang/InstantiationException", msg);
148 }
149
150
151 /* Throw an exception by name, using the string returned by
152 * JVM_LastErrorString for the detail string. If the last-error
153 * string is NULL, use the given default detail string.
154 */
155 JNIEXPORT void JNICALL
156 JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,
157 const char *defaultDetail)
158 {
159 char buf[256];
160 int n = JVM_GetLastErrorString(buf, sizeof(buf));
161
162 if (n > 0) {
163 jstring s = JNU_NewStringPlatform(env, buf);
164 if (s != NULL) {
165 jobject x = JNU_NewObjectByName(env, name,
166 "(Ljava/lang/String;)V", s);
167 if (x != NULL) {
168 (*env)->Throw(env, x);
169 }
|
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "jvm.h"
30 #include "io_util.h"
31
32 /* Due to a bug in the win32 C runtime library strings
33 * such as "z:" need to be appended with a "." so we
34 * must allocate at least 4 bytes to allow room for
35 * this expansion. See 4235353 for details.
36 */
37 #define MALLOC_MIN4(len) ((char *)malloc((len) + 1 < 4 ? 4 : (len) + 1))
38
39 /**
40 * Throw a Java exception by name. Similar to SignalError.
41 */
42 JNIEXPORT void JNICALL
43 JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg)
44 {
45 jclass cls = (*env)->FindClass(env, name);
46
47 if (cls != 0) /* Otherwise an exception has already been thrown */
48 (*env)->ThrowNew(env, cls, msg);
49 }
50
129 }
130
131 JNIEXPORT void JNICALL
132 JNU_ThrowNoSuchMethodError(JNIEnv *env, const char *msg)
133 {
134 JNU_ThrowByName(env, "java/lang/NoSuchMethodError", msg);
135 }
136
137 JNIEXPORT void JNICALL
138 JNU_ThrowStringIndexOutOfBoundsException(JNIEnv *env, const char *msg)
139 {
140 JNU_ThrowByName(env, "java/lang/StringIndexOutOfBoundsException", msg);
141 }
142
143 JNIEXPORT void JNICALL
144 JNU_ThrowInstantiationException(JNIEnv *env, const char *msg)
145 {
146 JNU_ThrowByName(env, "java/lang/InstantiationException", msg);
147 }
148
149
150 /*
151 * Throw an exception by name, using a given message and the string
152 * returned by getLastErrorString to construct the detail string.
153 */
154 JNIEXPORT void JNICALL
155 JNU_ThrowByNameWithMessageAndLastError
156 (JNIEnv *env, const char *name, const char *message)
157 {
158 char buf[256];
159 size_t n = getLastErrorString(buf, sizeof(buf));
160 size_t messagelen = message == NULL ? 0 : strlen(message);
161
162 if (n > 0) {
163 jstring s = JNU_NewStringPlatform(env, buf);
164 if (s != NULL) {
165 jobject x = NULL;
166 if (messagelen) {
167 jstring s2 = NULL;
168 size_t messageextlen = messagelen + 4;
169 char *str1 = (char *)malloc((messageextlen) * sizeof(char));
170 if (str1 == 0) {
171 JNU_ThrowOutOfMemoryError(env, 0);
172 return;
173 }
174 jio_snprintf(str1, messageextlen, " (%s)", message);
175 s2 = (*env)->NewStringUTF(env, str1);
176 free(str1);
177 if (s2 != NULL) {
178 jstring s3 = JNU_CallMethodByName(
179 env, NULL, s, "concat",
180 "(Ljava/lang/String;)Ljava/lang/String;",
181 s2).l;
182 (*env)->DeleteLocalRef(env, s2);
183 if (s3 != NULL) {
184 (*env)->DeleteLocalRef(env, s);
185 s = s3;
186 }
187 }
188 }
189 x = JNU_NewObjectByName(env, name, "(Ljava/lang/String;)V", s);
190 if (x != NULL) {
191 (*env)->Throw(env, x);
192 }
193 }
194 }
195
196 if (!(*env)->ExceptionOccurred(env)) {
197 if (messagelen) {
198 JNU_ThrowByName(env, name, message);
199 } else {
200 JNU_ThrowByName(env, name, "no further information");
201 }
202 }
203 }
204
205 /* Throw an exception by name, using the string returned by
206 * JVM_LastErrorString for the detail string. If the last-error
207 * string is NULL, use the given default detail string.
208 */
209 JNIEXPORT void JNICALL
210 JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,
211 const char *defaultDetail)
212 {
213 char buf[256];
214 int n = JVM_GetLastErrorString(buf, sizeof(buf));
215
216 if (n > 0) {
217 jstring s = JNU_NewStringPlatform(env, buf);
218 if (s != NULL) {
219 jobject x = JNU_NewObjectByName(env, name,
220 "(Ljava/lang/String;)V", s);
221 if (x != NULL) {
222 (*env)->Throw(env, x);
223 }
|