< prev index next >

src/java.base/share/native/libjava/jni_util.c

Print this page
rev 14618 : 8158023: SocketExceptions contain too little information sometimes

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -52,138 +52,157 @@
 /* JNU_Throw common exceptions */
 
 JNIEXPORT void JNICALL
 JNU_ThrowNullPointerException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/NullPointerException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "NullPointerException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowArrayIndexOutOfBoundsException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/ArrayIndexOutOfBoundsException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "ArrayIndexOutOfBoundsException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowOutOfMemoryError(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/OutOfMemoryError", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "OutOfMemoryError", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowIllegalArgumentException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/IllegalArgumentException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "IllegalArgumentException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowIllegalAccessError(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/IllegalAccessError", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "IllegalAccessError", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowIllegalAccessException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/IllegalAccessException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "IllegalAccessException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowInternalError(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/InternalError", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "InternalError", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowNoSuchFieldException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/NoSuchFieldException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "NoSuchFieldException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowNoSuchMethodException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/NoSuchMethodException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "NoSuchMethodException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowClassNotFoundException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/ClassNotFoundException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "ClassNotFoundException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowNumberFormatException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/NumberFormatException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "NumberFormatException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowIOException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/io/IOException", msg);
+    JNU_ThrowByName(env, JNU_JAVAIOPKG "IOException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowNoSuchFieldError(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/NoSuchFieldError", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "NoSuchFieldError", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowNoSuchMethodError(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/NoSuchMethodError", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "NoSuchMethodError", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowStringIndexOutOfBoundsException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/StringIndexOutOfBoundsException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "StringIndexOutOfBoundsException", msg);
 }
 
 JNIEXPORT void JNICALL
 JNU_ThrowInstantiationException(JNIEnv *env, const char *msg)
 {
-    JNU_ThrowByName(env, "java/lang/InstantiationException", msg);
+    JNU_ThrowByName(env, JNU_JAVAPKG "InstantiationException", msg);
 }
 
-
-/* Throw an exception by name, using the string returned by
- * JVM_LastErrorString for the detail string.  If the last-error
+/*
+ * Throw an exception by name, using the string returned by
+ * getLastErrorString for the detail string. If the last-error
  * string is NULL, use the given default detail string.
  */
 JNIEXPORT void JNICALL
 JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,
                              const char *defaultDetail)
 {
-    char buf[256];
+    char buf[255 + 1]; // 256 characters, including terminating 0
     size_t n = getLastErrorString(buf, sizeof(buf));
 
     if (n > 0) {
-        jstring s = JNU_NewStringPlatform(env, buf);
-        if (s != NULL) {
-            jobject x = JNU_NewObjectByName(env, name,
-                                            "(Ljava/lang/String;)V", s);
-            if (x != NULL) {
-                (*env)->Throw(env, x);
-            }
-        }
-    }
-    if (!(*env)->ExceptionOccurred(env)) {
+        JNU_ThrowByName(env, name, buf);
+    } else {
         JNU_ThrowByName(env, name, defaultDetail);
     }
 }
 
-/* Throw an IOException, using the last-error string for the detail
- * string.  If the last-error string is NULL, use the given default
- * detail string.
+/*
+ * Throw an exception by name, using a given message and the string
+ * returned by getLastErrorString to construct the detail string.
+ */
+JNIEXPORT void JNICALL
+JNU_ThrowByNameWithMessageAndLastError
+  (JNIEnv *env, const char *name, const char *message)
+{
+    char buf[254 + 2 + 255 + 1]; // 512 characters, including terminating 0
+    char errorBuf[255 + 1];
+    size_t errorLen = getLastErrorString(errorBuf, sizeof(errorBuf));
+
+    if (message != NULL && strlen(message) != 0) {
+        if (errorLen != 0) {
+            jio_snprintf(buf, sizeof(buf), "%.254s: %.255s", message, errorBuf);
+        } else {
+            jio_snprintf(buf, sizeof(buf), "%s", message);
+        }
+    } else if (errorLen != 0) {
+        jio_snprintf(buf, sizeof(buf), "%s", errorBuf);
+    } else {
+        jio_snprintf(buf, sizeof(buf), "no further information");
+    }
+
+    JNU_ThrowByName(env, name, buf);
+}
+
+/*
+ * Convenience method.
+ * Call JNU_ThrowByNameWithLastError for java.io.IOException.
  */
 JNIEXPORT void JNICALL
 JNU_ThrowIOExceptionWithLastError(JNIEnv *env, const char *defaultDetail)
 {
-    JNU_ThrowByNameWithLastError(env, "java/io/IOException", defaultDetail);
+    JNU_ThrowByNameWithLastError(env, JNU_JAVAIOPKG "IOException", defaultDetail);
 }
 
 
 JNIEXPORT jvalue JNICALL
 JNU_CallStaticMethodByName(JNIEnv *env,
< prev index next >