< prev index next >

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

Print this page
rev 14890 : 8158023: SocketExceptions contain too little information sometimes
   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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


 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     size_t n = 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             }
 170         }
 171     }
 172     if (!(*env)->ExceptionOccurred(env)) {
 173         JNU_ThrowByName(env, name, defaultDetail);
 174     }
 175 }
 176 
 177 /* Throw an IOException, using the last-error string for the detail
 178  * string.  If the last-error string is NULL, use the given default
 179  * detail string.























































 180  */
 181 JNIEXPORT void JNICALL
 182 JNU_ThrowIOExceptionWithLastError(JNIEnv *env, const char *defaultDetail)
 183 {
 184     JNU_ThrowByNameWithLastError(env, "java/io/IOException", defaultDetail);
 185 }
 186 
 187 
 188 JNIEXPORT jvalue JNICALL
 189 JNU_CallStaticMethodByName(JNIEnv *env,
 190                            jboolean *hasException,
 191                            const char *class_name,
 192                            const char *name,
 193                            const char *signature,
 194                            ...)
 195 {
 196     jclass clazz;
 197     jmethodID mid;
 198     va_list args;
 199     jvalue result;


   1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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


 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  * getLastErrorString 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     size_t n = 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             }
 170         }
 171     }
 172     if (!(*env)->ExceptionOccurred(env)) {
 173         JNU_ThrowByName(env, name, defaultDetail);
 174     }
 175 }
 176 
 177 /*
 178  * Throw an exception by name, using a given message and the string
 179  * returned by getLastErrorString to construct the detail string.
 180  */
 181 JNIEXPORT void JNICALL
 182 JNU_ThrowByNameWithMessageAndLastError
 183   (JNIEnv *env, const char *name, const char *message)
 184 {
 185     char buf[256];
 186     size_t n = getLastErrorString(buf, sizeof(buf));
 187     size_t messagelen = message == NULL ? 0 : strlen(message);
 188 
 189     if (n > 0) {
 190         jstring s = JNU_NewStringPlatform(env, buf);
 191         if (s != NULL) {
 192             jobject x = NULL;
 193             if (messagelen) {
 194                 jstring s2 = NULL;
 195                 size_t messageextlen = messagelen + 4;
 196                 char *str1 = (char *)malloc((messageextlen) * sizeof(char));
 197                 if (str1 == 0) {
 198                     JNU_ThrowOutOfMemoryError(env, 0);
 199                     return;
 200                 }
 201                 jio_snprintf(str1, messageextlen, " (%s)", message);
 202                 s2 = (*env)->NewStringUTF(env, str1);
 203                 free(str1);
 204                 if (s2 != NULL) {
 205                     jstring s3 = JNU_CallMethodByName(
 206                                      env, NULL, s, "concat",
 207                                      "(Ljava/lang/String;)Ljava/lang/String;",
 208                                      s2).l;
 209                     (*env)->DeleteLocalRef(env, s2);
 210                     if (s3 != NULL) {
 211                         (*env)->DeleteLocalRef(env, s);
 212                         s = s3;
 213                     }
 214                 }
 215             }
 216             x = JNU_NewObjectByName(env, name, "(Ljava/lang/String;)V", s);
 217             if (x != NULL) {
 218                 (*env)->Throw(env, x);
 219             }
 220         }
 221     }
 222 
 223     if (!(*env)->ExceptionOccurred(env)) {
 224         if (messagelen) {
 225             JNU_ThrowByName(env, name, message);
 226         } else {
 227             JNU_ThrowByName(env, name, "no further information");
 228         }
 229     }
 230 }
 231 
 232 /*
 233  * Convenience method.
 234  * Call JNU_ThrowByNameWithLastError for java.io.IOException.
 235  */
 236 JNIEXPORT void JNICALL
 237 JNU_ThrowIOExceptionWithLastError(JNIEnv *env, const char *defaultDetail)
 238 {
 239     JNU_ThrowByNameWithLastError(env, "java/io/IOException", defaultDetail);
 240 }
 241 
 242 
 243 JNIEXPORT jvalue JNICALL
 244 JNU_CallStaticMethodByName(JNIEnv *env,
 245                            jboolean *hasException,
 246                            const char *class_name,
 247                            const char *name,
 248                            const char *signature,
 249                            ...)
 250 {
 251     jclass clazz;
 252     jmethodID mid;
 253     va_list args;
 254     jvalue result;


< prev index next >