src/java.base/share/native/libjava/jni_util.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45572 jdk/src/java.base/share/native/libjava/jni_util.c@07f412070bd9
child 49121 10f447530d32
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3111
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jni_util.h"
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
    32
#include "java_lang_String.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/* Due to a bug in the win32 C runtime library strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * such as "z:" need to be appended with a "." so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * must allocate at least 4 bytes to allow room for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * this expansion. See 4235353 for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#define MALLOC_MIN4(len) ((char *)malloc((len) + 1 < 4 ? 4 : (len) + 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Throw a Java exception by name. Similar to SignalError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    jclass cls = (*env)->FindClass(env, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if (cls != 0) /* Otherwise an exception has already been thrown */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        (*env)->ThrowNew(env, cls, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/* JNU_Throw common exceptions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
JNU_ThrowNullPointerException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    JNU_ThrowByName(env, "java/lang/NullPointerException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
JNU_ThrowArrayIndexOutOfBoundsException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    JNU_ThrowByName(env, "java/lang/ArrayIndexOutOfBoundsException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
JNU_ThrowOutOfMemoryError(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    JNU_ThrowByName(env, "java/lang/OutOfMemoryError", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
JNU_ThrowIllegalArgumentException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    JNU_ThrowByName(env, "java/lang/IllegalArgumentException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
JNU_ThrowIllegalAccessError(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    JNU_ThrowByName(env, "java/lang/IllegalAccessError", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
JNU_ThrowIllegalAccessException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    JNU_ThrowByName(env, "java/lang/IllegalAccessException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
JNU_ThrowInternalError(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    JNU_ThrowByName(env, "java/lang/InternalError", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
JNU_ThrowNoSuchFieldException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    JNU_ThrowByName(env, "java/lang/NoSuchFieldException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
JNU_ThrowNoSuchMethodException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    JNU_ThrowByName(env, "java/lang/NoSuchMethodException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
JNU_ThrowClassNotFoundException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    JNU_ThrowByName(env, "java/lang/ClassNotFoundException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
JNU_ThrowNumberFormatException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    JNU_ThrowByName(env, "java/lang/NumberFormatException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
JNU_ThrowIOException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    JNU_ThrowByName(env, "java/io/IOException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
JNU_ThrowNoSuchFieldError(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    JNU_ThrowByName(env, "java/lang/NoSuchFieldError", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
JNU_ThrowNoSuchMethodError(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    JNU_ThrowByName(env, "java/lang/NoSuchMethodError", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
JNU_ThrowStringIndexOutOfBoundsException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    JNU_ThrowByName(env, "java/lang/StringIndexOutOfBoundsException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
JNU_ThrowInstantiationException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    JNU_ThrowByName(env, "java/lang/InstantiationException", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   151
/*
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   152
 * Throw an exception by name, using the string returned by
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   153
 * getLastErrorString for the detail string. If the last-error
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * string is NULL, use the given default detail string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
JNU_ThrowByNameWithLastError(JNIEnv *env, const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                             const char *defaultDetail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    char buf[256];
30784
28105f71beb2 8074818: Resolve disabled warnings for libjava
rriggs
parents: 27184
diff changeset
   161
    size_t n = getLastErrorString(buf, sizeof(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        jstring s = JNU_NewStringPlatform(env, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (s != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            jobject x = JNU_NewObjectByName(env, name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                            "(Ljava/lang/String;)V", s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (x != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                (*env)->Throw(env, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    if (!(*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        JNU_ThrowByName(env, name, defaultDetail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   178
/*
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   179
 * Throw an exception by name, using a given message and the string
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   180
 * returned by getLastErrorString to construct the detail string.
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   181
 */
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   182
JNIEXPORT void JNICALL
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   183
JNU_ThrowByNameWithMessageAndLastError
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   184
  (JNIEnv *env, const char *name, const char *message)
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   185
{
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   186
    char buf[256];
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   187
    size_t n = getLastErrorString(buf, sizeof(buf));
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   188
    size_t messagelen = message == NULL ? 0 : strlen(message);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   189
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   190
    if (n > 0) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   191
        jstring s = JNU_NewStringPlatform(env, buf);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   192
        if (s != NULL) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   193
            jobject x = NULL;
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   194
            if (messagelen) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   195
                jstring s2 = NULL;
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   196
                size_t messageextlen = messagelen + 4;
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   197
                char *str1 = (char *)malloc((messageextlen) * sizeof(char));
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   198
                if (str1 == 0) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   199
                    JNU_ThrowOutOfMemoryError(env, 0);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   200
                    return;
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   201
                }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   202
                jio_snprintf(str1, messageextlen, " (%s)", message);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   203
                s2 = (*env)->NewStringUTF(env, str1);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   204
                free(str1);
42442
0e6009a5b092 8170465: JNI exception pending in jni_util.c:190
naoto
parents: 39318
diff changeset
   205
                JNU_CHECK_EXCEPTION(env);
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   206
                if (s2 != NULL) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   207
                    jstring s3 = JNU_CallMethodByName(
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   208
                                     env, NULL, s, "concat",
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   209
                                     "(Ljava/lang/String;)Ljava/lang/String;",
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   210
                                     s2).l;
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   211
                    (*env)->DeleteLocalRef(env, s2);
42442
0e6009a5b092 8170465: JNI exception pending in jni_util.c:190
naoto
parents: 39318
diff changeset
   212
                    JNU_CHECK_EXCEPTION(env);
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   213
                    if (s3 != NULL) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   214
                        (*env)->DeleteLocalRef(env, s);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   215
                        s = s3;
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   216
                    }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   217
                }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   218
            }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   219
            x = JNU_NewObjectByName(env, name, "(Ljava/lang/String;)V", s);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   220
            if (x != NULL) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   221
                (*env)->Throw(env, x);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   222
            }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   223
        }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   224
    }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   225
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   226
    if (!(*env)->ExceptionOccurred(env)) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   227
        if (messagelen) {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   228
            JNU_ThrowByName(env, name, message);
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   229
        } else {
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   230
            JNU_ThrowByName(env, name, "no further information");
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   231
        }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   232
    }
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   233
}
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   234
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   235
/*
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   236
 * Convenience method.
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 35260
diff changeset
   237
 * Call JNU_ThrowByNameWithLastError for java.io.IOException.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
JNU_ThrowIOExceptionWithLastError(JNIEnv *env, const char *defaultDetail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    JNU_ThrowByNameWithLastError(env, "java/io/IOException", defaultDetail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
JNIEXPORT jvalue JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
JNU_CallStaticMethodByName(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                           jboolean *hasException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                           const char *class_name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                           const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                           const char *signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                           ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    jclass clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    jmethodID mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    jvalue result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    const char *p = signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /* find out the return type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    while (*p && *p != ')')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    result.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if ((*env)->EnsureLocalCapacity(env, 3) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    clazz = (*env)->FindClass(env, class_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    if (clazz == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    mid = (*env)->GetStaticMethodID(env, clazz, name, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    if (mid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        goto done1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    va_start(args, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    switch (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    case 'V':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        (*env)->CallStaticVoidMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        result.l = (*env)->CallStaticObjectMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        result.z = (*env)->CallStaticBooleanMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        result.b = (*env)->CallStaticByteMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        result.c = (*env)->CallStaticCharMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        result.s = (*env)->CallStaticShortMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        result.i = (*env)->CallStaticIntMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        result.j = (*env)->CallStaticLongMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        result.f = (*env)->CallStaticFloatMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        result.d = (*env)->CallStaticDoubleMethodV(env, clazz, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        (*env)->FatalError(env, "JNU_CallStaticMethodByName: illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 done1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    (*env)->DeleteLocalRef(env, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 done2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    if (hasException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        *hasException = (*env)->ExceptionCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
JNIEXPORT jvalue JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
JNU_CallMethodByName(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                     jboolean *hasException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                     jobject obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                     const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                     const char *signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                     ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    jvalue result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    va_start(args, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    result = JNU_CallMethodByNameV(env, hasException, obj, name, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                   args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
JNIEXPORT jvalue JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
JNU_CallMethodByNameV(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                      jboolean *hasException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                      jobject obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                      const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                      const char *signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                      va_list args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    jclass clazz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    jmethodID mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    jvalue result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    const char *p = signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /* find out the return type */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    while (*p && *p != ')')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    p++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    result.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    if ((*env)->EnsureLocalCapacity(env, 3) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    clazz = (*env)->GetObjectClass(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    mid = (*env)->GetMethodID(env, clazz, name, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    if (mid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        goto done1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    switch (*p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    case 'V':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        (*env)->CallVoidMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        result.l = (*env)->CallObjectMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        result.z = (*env)->CallBooleanMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        result.b = (*env)->CallByteMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        result.c = (*env)->CallCharMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        result.s = (*env)->CallShortMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        result.i = (*env)->CallIntMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        result.j = (*env)->CallLongMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        result.f = (*env)->CallFloatMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        result.d = (*env)->CallDoubleMethodV(env, obj, mid, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        (*env)->FatalError(env, "JNU_CallMethodByNameV: illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
 done1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    (*env)->DeleteLocalRef(env, clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 done2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    if (hasException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        *hasException = (*env)->ExceptionCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
JNIEXPORT jobject JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
JNU_NewObjectByName(JNIEnv *env, const char *class_name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    const char *constructor_sig, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    jobject obj = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    jclass cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    jmethodID cls_initMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    if ((*env)->EnsureLocalCapacity(env, 2) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    cls = (*env)->FindClass(env, class_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    if (cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    cls_initMID  = (*env)->GetMethodID(env, cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                       "<init>", constructor_sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    if (cls_initMID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    va_start(args, constructor_sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    obj = (*env)->NewObjectV(env, cls, cls_initMID, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    (*env)->DeleteLocalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   446
/* Optimized for charset ISO_8559_1 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
static jstring
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   448
newSizedString8859_1(JNIEnv *env, const char *str, const int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    jchar buf[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    jchar *str1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    jstring result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   455
    if ((*env)->EnsureLocalCapacity(env, 1) < 0)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   456
        return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   457
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    if (len > 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        str1 = (jchar *)malloc(len * sizeof(jchar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (str1 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        str1 = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    for (i=0;i<len;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        str1[i] = (unsigned char)str[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    result = (*env)->NewString(env, str1, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    if (str1 != buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        free(str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   475
static jstring
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   476
newString8859_1(JNIEnv *env, const char *str)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   477
{
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   478
    int len = (int)strlen(str);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   479
    return newSizedString8859_1(env, str, len);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   480
}
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   481
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
static const char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
getString8859_1Chars(JNIEnv *env, jstring jstr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    char *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    jint len = (*env)->GetStringLength(env, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    const jchar *str = (*env)->GetStringCritical(env, jstr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    if (str == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    result = MALLOC_MIN4(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        (*env)->ReleaseStringCritical(env, jstr, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    for (i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        jchar unicode = str[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (unicode <= 0x00ff)
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   503
            result[i] = (char)unicode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            result[i] = '?';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    result[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    (*env)->ReleaseStringCritical(env, jstr, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   514
/* Optimized for charset ISO646-US (us-ascii) */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
static jstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
newString646_US(JNIEnv *env, const char *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
{
30784
28105f71beb2 8074818: Resolve disabled warnings for libjava
rriggs
parents: 27184
diff changeset
   518
    int len = (int)strlen(str);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    jchar buf[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    jchar *str1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    jstring result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    if (len > 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        str1 = (jchar *)malloc(len * sizeof(jchar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (str1 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        str1 = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    for (i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        unsigned char c = (unsigned char)str[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (c <= 0x7f)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            str1[i] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            str1[i] = '?';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    result = (*env)->NewString(env, str1, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    if (str1 != buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        free(str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
static const char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
getString646_USChars(JNIEnv *env, jstring jstr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    char *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    jint len = (*env)->GetStringLength(env, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    const jchar *str = (*env)->GetStringCritical(env, jstr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    if (str == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    result = MALLOC_MIN4(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        (*env)->ReleaseStringCritical(env, jstr, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    for (i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        jchar unicode = str[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (unicode <= 0x007f )
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   568
            result[i] = (char)unicode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            result[i] = '?';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    result[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    (*env)->ReleaseStringCritical(env, jstr, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
/* enumeration of c1 row from Cp1252 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
static int cp1252c1chars[32] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    0x20AC,0xFFFD,0x201A,0x0192,0x201E,0x2026,0x2020,0x2021,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    0x02C6,0x2030,0x0160,0x2039,0x0152,0xFFFD,0x017D,0xFFFD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    0xFFFD,0x2018,0x2019,0x201C,0x201D,0x2022,0x2013,0x2014,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    0x02Dc,0x2122,0x0161,0x203A,0x0153,0xFFFD,0x017E,0x0178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   586
/* Optimized for charset Cp1252 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
static jstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
newStringCp1252(JNIEnv *env, const char *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    int len = (int) strlen(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    jchar buf[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    jchar *str1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    jstring result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    int i;
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   595
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   596
    if ((*env)->EnsureLocalCapacity(env, 1) < 0)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   597
        return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   598
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    if (len > 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        str1 = (jchar *)malloc(len * sizeof(jchar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (str1 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        str1 = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    for (i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        unsigned char c = (unsigned char)str[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if ((c >= 0x80) && (c <= 0x9f))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            str1[i] = cp1252c1chars[c-128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            str1[i] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    result = (*env)->NewString(env, str1, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    if (str1 != buf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        free(str1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
static const char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
getStringCp1252Chars(JNIEnv *env, jstring jstr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    char *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    jint len = (*env)->GetStringLength(env, jstr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    const jchar *str = (*env)->GetStringCritical(env, jstr, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    if (str == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    result = MALLOC_MIN4(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        (*env)->ReleaseStringCritical(env, jstr, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    for (i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        jchar c = str[i];
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   642
        if (c < 256) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   643
            if ((c >= 0x80) && (c <= 0x9f)) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   644
                result[i] = '?';
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   645
            } else {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   646
                result[i] = (char)c;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   647
            }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   648
        } else switch(c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            case 0x20AC: result[i] = (char)0x80; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            case 0x201A: result[i] = (char)0x82; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            case 0x0192: result[i] = (char)0x83; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            case 0x201E: result[i] = (char)0x84; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            case 0x2026: result[i] = (char)0x85; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            case 0x2020: result[i] = (char)0x86; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            case 0x2021: result[i] = (char)0x87; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            case 0x02C6: result[i] = (char)0x88; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            case 0x2030: result[i] = (char)0x89; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            case 0x0160: result[i] = (char)0x8A; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            case 0x2039: result[i] = (char)0x8B; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            case 0x0152: result[i] = (char)0x8C; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            case 0x017D: result[i] = (char)0x8E; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            case 0x2018: result[i] = (char)0x91; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            case 0x2019: result[i] = (char)0x92; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            case 0x201C: result[i] = (char)0x93; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            case 0x201D: result[i] = (char)0x94; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            case 0x2022: result[i] = (char)0x95; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            case 0x2013: result[i] = (char)0x96; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            case 0x2014: result[i] = (char)0x97; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            case 0x02DC: result[i] = (char)0x98; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            case 0x2122: result[i] = (char)0x99; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            case 0x0161: result[i] = (char)0x9A; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            case 0x203A: result[i] = (char)0x9B; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            case 0x0153: result[i] = (char)0x9C; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            case 0x017E: result[i] = (char)0x9E; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            case 0x0178: result[i] = (char)0x9F; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            default:     result[i] = '?';  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    result[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    (*env)->ReleaseStringCritical(env, jstr, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
static int fastEncoding = NO_ENCODING_YET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
static jstring jnuEncoding = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
/* Cached method IDs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
static jmethodID String_init_ID;        /* String(byte[], enc) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
static jmethodID String_getBytes_ID;    /* String.getBytes(enc) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   692
/* Cached field IDs */
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   693
static jfieldID String_coder_ID;        /* String.coder */
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   694
static jfieldID String_value_ID;        /* String.value */
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   695
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   696
static jboolean isJNUEncodingSupported = JNI_FALSE;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   697
static jboolean jnuEncodingSupported(JNIEnv *env) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   698
    jboolean exe;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   699
    if (isJNUEncodingSupported == JNI_TRUE) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   700
        return JNI_TRUE;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   701
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   702
    isJNUEncodingSupported = (jboolean) JNU_CallStaticMethodByName (
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   703
                                    env, &exe,
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   704
                                    "java/nio/charset/Charset",
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   705
                                    "isSupported",
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   706
                                    "(Ljava/lang/String;)Z",
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   707
                                    jnuEncoding).z;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   708
    return isJNUEncodingSupported;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   709
}
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   710
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   711
/* Create a new string by converting str to a heap-allocated byte array and
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   712
 * calling the appropriate String constructor.
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   713
 */
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   714
static jstring
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   715
newSizedStringJava(JNIEnv *env, const char *str, const int len)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   716
{
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   717
    jstring result = NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   718
    jbyteArray bytes = 0;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   719
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   720
    if ((*env)->EnsureLocalCapacity(env, 2) < 0)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   721
        return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   722
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   723
    bytes = (*env)->NewByteArray(env, len);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   724
    if (bytes != NULL) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   725
        jclass strClazz = JNU_ClassString(env);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   726
        CHECK_NULL_RETURN(strClazz, 0);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   727
        (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte *)str);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   728
        if (jnuEncodingSupported(env)) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   729
            result = (*env)->NewObject(env, strClazz,
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   730
                                       String_init_ID, bytes, jnuEncoding);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   731
        } else {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   732
            /*If the encoding specified in sun.jnu.encoding is not endorsed
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   733
              by "Charset.isSupported" we have to fall back to use String(byte[])
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   734
              explicitly here without specifying the encoding name, in which the
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   735
              StringCoding class will pickup the iso-8859-1 as the fallback
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   736
              converter for us.
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   737
             */
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   738
            jmethodID mid = (*env)->GetMethodID(env, strClazz,
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   739
                                                "<init>", "([B)V");
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   740
            if (mid != NULL) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   741
                result = (*env)->NewObject(env, strClazz, mid, bytes);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   742
            }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   743
        }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   744
        (*env)->DeleteLocalRef(env, bytes);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   745
        return result;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   746
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   747
    return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   748
}
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   749
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   750
static jstring
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   751
newStringJava(JNIEnv *env, const char *str)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   752
{
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   753
    int len = (int)strlen(str);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   754
    return newSizedStringJava(env, str, len);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   755
}
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   756
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   757
/* Optimized for charset UTF-8 */
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   758
static jstring
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   759
newStringUTF8(JNIEnv *env, const char *str)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   760
{
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   761
    int len;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   762
    const unsigned char *p;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   763
    unsigned char asciiCheck;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   764
    for (asciiCheck = 0, p = (const unsigned char*)str; *p != '\0'; p++) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   765
        asciiCheck |= *p;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   766
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   767
    len = (int)((const char*)p - str);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   768
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   769
    if (asciiCheck < 0x80) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   770
        // ascii fast-path
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   771
        return newSizedString8859_1(env, str, len);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   772
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   773
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   774
    return newSizedStringJava(env, str, len);
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   775
}
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   776
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
/* Initialize the fast encoding.  If the "sun.jnu.encoding" property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
 * has not yet been set, we leave fastEncoding == NO_ENCODING_YET.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
 */
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   780
void
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
initializeEncoding(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    jstring propname = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    jstring enc = 0;
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
   785
    jclass strClazz = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    if ((*env)->EnsureLocalCapacity(env, 3) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
   790
    strClazz = JNU_ClassString(env);
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
   791
    CHECK_NULL(strClazz);
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
   792
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    if (propname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        jboolean exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        enc = JNU_CallStaticMethodByName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                       (env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                        &exc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                        "java/lang/System",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                        "getProperty",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                        "(Ljava/lang/String;)Ljava/lang/String;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                        propname).l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if (!exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            if (enc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                const char* encname = (*env)->GetStringUTFChars(env, enc, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                if (encname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
           /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            * On Solaris with nl_langinfo() called in GetJavaProperties():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            *   locale undefined -> NULL -> hardcoded default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            *   "C" locale       -> "" -> hardcoded default     (on 2.6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            *   "C" locale       -> "ISO646-US"                 (on Sol 7/8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            *   "en_US" locale -> "ISO8859-1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            *   "en_GB" locale -> "ISO8859-1"                   (on Sol 7/8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            *   "en_UK" locale -> "ISO8859-1"                   (on 2.6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    if ((strcmp(encname, "8859_1") == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        (strcmp(encname, "ISO8859-1") == 0) ||
35260
2d073b8f1a16 8145212: ISO-8859-1 isn't properly handled as 'fastEncoding' in jni_util.c
simonis
parents: 30784
diff changeset
   819
                        (strcmp(encname, "ISO8859_1") == 0) ||
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   820
                        (strcmp(encname, "ISO-8859-1") == 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                        fastEncoding = FAST_8859_1;
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   822
                    } else if (strcmp(encname, "UTF-8") == 0) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   823
                        fastEncoding = FAST_UTF_8;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   824
                        jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   825
                    } else if (strcmp(encname, "ISO646-US") == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                        fastEncoding = FAST_646_US;
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   827
                    } else if (strcmp(encname, "Cp1252") == 0 ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                             /* This is a temporary fix until we move */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                             /* to wide character versions of all Windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                             /* calls. */
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   831
                             strcmp(encname, "utf-16le") == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        fastEncoding = FAST_CP1252;
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   833
                    } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        fastEncoding = NO_FAST_ENCODING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        jnuEncoding = (jstring)(*env)->NewGlobalRef(env, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    (*env)->ReleaseStringUTFChars(env, enc, encname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            (*env)->ExceptionClear(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        (*env)->ExceptionClear(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    (*env)->DeleteLocalRef(env, propname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    (*env)->DeleteLocalRef(env, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    /* Initialize method-id cache */
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
   850
    String_getBytes_ID = (*env)->GetMethodID(env, strClazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                             "getBytes", "(Ljava/lang/String;)[B");
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
   852
    CHECK_NULL(String_getBytes_ID);
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
   853
    String_init_ID = (*env)->GetMethodID(env, strClazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                                         "<init>", "([BLjava/lang/String;)V");
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   855
    String_coder_ID = (*env)->GetFieldID(env, strClazz, "coder", "B");
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   856
    String_value_ID = (*env)->GetFieldID(env, strClazz, "value", "[B");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
JNIEXPORT jstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
NewStringPlatform(JNIEnv *env, const char *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    return JNU_NewStringPlatform(env, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
JNU_NewStringPlatform(JNIEnv *env, const char *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
{
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   868
    if (fastEncoding == NO_ENCODING_YET) {
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   869
        initializeEncoding(env);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   870
        JNU_CHECK_EXCEPTION_RETURN(env, NULL);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   871
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   873
    if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   874
        return newString8859_1(env, str);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   875
    if (fastEncoding == FAST_646_US)
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   876
        return newString646_US(env, str);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   877
    if (fastEncoding == FAST_CP1252)
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   878
        return newStringCp1252(env, str);
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   879
    if (fastEncoding == FAST_UTF_8)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   880
        return newStringUTF8(env, str);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   881
    return newStringJava(env, str);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
JNIEXPORT const char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    return JNU_GetStringPlatformChars(env, jstr, isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   890
static const char* getStringBytes(JNIEnv *env, jstring jstr) {
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   891
    char *result = NULL;
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   892
    jbyteArray hab = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   894
    if ((*env)->EnsureLocalCapacity(env, 2) < 0)
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   895
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   897
    if (jnuEncodingSupported(env)) {
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   898
        hab = (*env)->CallObjectMethod(env, jstr, String_getBytes_ID, jnuEncoding);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   899
    } else {
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   900
        jmethodID mid;
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   901
        jclass strClazz = JNU_ClassString(env);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   902
        CHECK_NULL_RETURN(strClazz, 0);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   903
        mid = (*env)->GetMethodID(env, strClazz,
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   904
                                       "getBytes", "()[B");
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   905
        if (mid != NULL) {
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   906
            hab = (*env)->CallObjectMethod(env, jstr, mid);
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   907
        }
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   908
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   910
    if (!(*env)->ExceptionCheck(env)) {
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   911
        jint len = (*env)->GetArrayLength(env, hab);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   912
        result = MALLOC_MIN4(len);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   913
        if (result == 0) {
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   914
            JNU_ThrowOutOfMemoryError(env, 0);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   915
            (*env)->DeleteLocalRef(env, hab);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   916
            return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   918
        (*env)->GetByteArrayRegion(env, hab, 0, len, (jbyte *)result);
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   919
        result[len] = 0; /* NULL-terminate */
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   920
    }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2
diff changeset
   921
22938
dde13f2c082d 8033565: Remove unused nativeNewStringPlatform
rriggs
parents: 22653
diff changeset
   922
    (*env)->DeleteLocalRef(env, hab);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
45572
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   926
static const char*
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   927
getStringUTF8(JNIEnv *env, jstring jstr)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   928
{
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   929
    int i;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   930
    char *result;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   931
    jbyteArray value;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   932
    jint len;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   933
    jbyte *str;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   934
    jint rlen;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   935
    int ri;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   936
    jbyte coder = (*env)->GetByteField(env, jstr, String_coder_ID);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   937
    if (coder != java_lang_String_LATIN1) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   938
        return getStringBytes(env, jstr);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   939
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   940
    if ((*env)->EnsureLocalCapacity(env, 2) < 0) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   941
        return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   942
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   943
    value = (*env)->GetObjectField(env, jstr, String_value_ID);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   944
    if (value == NULL)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   945
        return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   946
    len = (*env)->GetArrayLength(env, value);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   947
    str = (*env)->GetPrimitiveArrayCritical(env, value, NULL);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   948
    if (str == NULL) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   949
        return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   950
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   951
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   952
    rlen = len;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   953
    // we need two bytes for each latin-1 char above 127 (negative jbytes)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   954
    for (i = 0; i < len; i++) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   955
        if (str[i] < 0) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   956
            rlen++;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   957
        }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   958
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   959
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   960
    result = MALLOC_MIN4(rlen);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   961
    if (result == NULL) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   962
        (*env)->ReleasePrimitiveArrayCritical(env, value, str, 0);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   963
        JNU_ThrowOutOfMemoryError(env, 0);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   964
        return NULL;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   965
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   966
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   967
    for (ri = 0, i = 0; i < len; i++) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   968
        jbyte c = str[i];
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   969
        if (c < 0) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   970
            result[ri++] = (char)(0xc0 | ((c & 0xff) >> 6));
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   971
            result[ri++] = (char)(0x80 | (c & 0x3f));
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   972
        } else {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   973
            result[ri++] = c;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   974
        }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   975
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   976
    (*env)->ReleasePrimitiveArrayCritical(env, value, str, 0);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   977
    result[rlen] = '\0';
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   978
    return result;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   979
}
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   980
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   981
JNIEXPORT const char * JNICALL
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   982
JNU_GetStringPlatformChars(JNIEnv *env, jstring jstr, jboolean *isCopy)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   983
{
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   984
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   985
    if (isCopy)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   986
        *isCopy = JNI_TRUE;
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   987
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   988
    if (fastEncoding == NO_ENCODING_YET) {
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   989
        initializeEncoding(env);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   990
        JNU_CHECK_EXCEPTION_RETURN(env, 0);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   991
    }
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   992
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   993
    if ((fastEncoding == FAST_8859_1) || (fastEncoding == NO_ENCODING_YET))
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   994
        return getString8859_1Chars(env, jstr);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   995
    if (fastEncoding == FAST_646_US)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   996
        return getString646_USChars(env, jstr);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   997
    if (fastEncoding == FAST_CP1252)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   998
        return getStringCp1252Chars(env, jstr);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
   999
    if (fastEncoding == FAST_UTF_8)
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
  1000
        return getStringUTF8(env, jstr);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
  1001
    else
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
  1002
        return getStringBytes(env, jstr);
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
  1003
}
07f412070bd9 8181147: JNI_GetStringPlatformChars should have a fast path for UTF-8
redestad
parents: 42442
diff changeset
  1004
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
JNU_ReleaseStringPlatformChars(JNIEnv *env, jstring jstr, const char *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    free((void *)str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
 * Export the platform dependent path canonicalization so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
 * VM can find it when loading system classes.
23235
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22938
diff changeset
  1014
 * This function is also used by the instrumentation agent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
extern int canonicalize(char *path, const char *out, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
JNIEXPORT int
23235
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22938
diff changeset
  1019
Canonicalize(JNIEnv *unused, char *orig, char *out, int len)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    /* canonicalize an already natived path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    return canonicalize(orig, out, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
JNU_ClassString(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    static jclass cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    if (cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        jclass c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if ((*env)->EnsureLocalCapacity(env, 1) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        c = (*env)->FindClass(env, "java/lang/String");
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1034
        CHECK_NULL_RETURN(c, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
JNU_ClassClass(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    static jclass cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    if (cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        jclass c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if ((*env)->EnsureLocalCapacity(env, 1) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        c = (*env)->FindClass(env, "java/lang/Class");
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1050
        CHECK_NULL_RETURN(c, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
JNU_ClassObject(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    static jclass cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    if (cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        jclass c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        if ((*env)->EnsureLocalCapacity(env, 1) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        c = (*env)->FindClass(env, "java/lang/Object");
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1066
        CHECK_NULL_RETURN(c, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
JNU_ClassThrowable(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    static jclass cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    if (cls == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        jclass c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if ((*env)->EnsureLocalCapacity(env, 1) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        c = (*env)->FindClass(env, "java/lang/Throwable");
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1082
        CHECK_NULL_RETURN(c, NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        cls = (*env)->NewGlobalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        (*env)->DeleteLocalRef(env, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
JNU_CopyObjectArray(JNIEnv *env, jobjectArray dst, jobjectArray src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                         jint count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    if ((*env)->EnsureLocalCapacity(env, 1) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    for (i=0; i<count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        jstring p = (*env)->GetObjectArrayElement(env, src, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        (*env)->SetObjectArrayElement(env, dst, i, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        (*env)->DeleteLocalRef(env, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
JNIEXPORT void * JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
JNU_GetEnv(JavaVM *vm, jint version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    void *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    (*vm)->GetEnv(vm, &env, version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    return env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
JNU_IsInstanceOfByName(JNIEnv *env, jobject object, char* classname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    if ((*env)->EnsureLocalCapacity(env, 1) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    cls = (*env)->FindClass(env, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    if (cls != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        jint result = (*env)->IsInstanceOf(env, object, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        (*env)->DeleteLocalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
JNU_Equals(JNIEnv *env, jobject object1, jobject object2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    static jmethodID mid = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    if (mid == NULL) {
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1132
        jclass objClazz = JNU_ClassObject(env);
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1133
        CHECK_NULL_RETURN(objClazz, JNI_FALSE);
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1134
        mid = (*env)->GetMethodID(env, objClazz, "equals",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                                  "(Ljava/lang/Object;)Z");
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1136
        CHECK_NULL_RETURN(mid, JNI_FALSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    return (*env)->CallBooleanMethod(env, object1, mid, object2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
 * Thread calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
static jmethodID Object_waitMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
static jmethodID Object_notifyMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
static jmethodID Object_notifyAllMID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
JNU_MonitorWait(JNIEnv *env, jobject object, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        JNU_ThrowNullPointerException(env, "JNU_MonitorWait argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    if (Object_waitMID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        jclass cls = JNU_ClassObject(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        Object_waitMID = (*env)->GetMethodID(env, cls, "wait", "(J)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        if (Object_waitMID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    (*env)->CallVoidMethod(env, object, Object_waitMID, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
JNU_Notify(JNIEnv *env, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        JNU_ThrowNullPointerException(env, "JNU_Notify argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    if (Object_notifyMID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        jclass cls = JNU_ClassObject(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        Object_notifyMID = (*env)->GetMethodID(env, cls, "notify", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (Object_notifyMID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    (*env)->CallVoidMethod(env, object, Object_notifyMID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
JNU_NotifyAll(JNIEnv *env, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        JNU_ThrowNullPointerException(env, "JNU_NotifyAll argument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    if (Object_notifyAllMID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        jclass cls = JNU_ClassObject(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        if (cls == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        Object_notifyAllMID = (*env)->GetMethodID(env, cls,"notifyAll", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        if (Object_notifyAllMID == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    (*env)->CallVoidMethod(env, object, Object_notifyAllMID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
 * Debugging utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
JNU_PrintString(JNIEnv *env, char *hdr, jstring string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    if (string == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        fprintf(stderr, "%s: is NULL\n", hdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        const char *stringPtr = JNU_GetStringPlatformChars(env, string, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        if (stringPtr == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        fprintf(stderr, "%s: %s\n", hdr, stringPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        JNU_ReleaseStringPlatformChars(env, string, stringPtr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
JNU_PrintClass(JNIEnv *env, char* hdr, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        fprintf(stderr, "%s: object is NULL\n", hdr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        jclass cls = (*env)->GetObjectClass(env, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        jstring clsName = JNU_ToString(env, cls);
22653
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1238
        if (clsName == NULL) {
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1239
            JNU_PrintString(env, hdr, clsName);
7a464b795af5 8030993: Check jdk/src/share/native/common/jni_util.c for JNI pending exceptions
rriggs
parents: 7668
diff changeset
  1240
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        (*env)->DeleteLocalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        (*env)->DeleteLocalRef(env, clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
JNU_ToString(JNIEnv *env, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    if (object == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        return (*env)->NewStringUTF(env, "NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        return (jstring)JNU_CallMethodByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                                             NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                                             object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                                             "toString",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                                             "()Ljava/lang/String;").l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
JNIEXPORT jvalue JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
JNU_GetFieldByName(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                   jboolean *hasException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                   jobject obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                   const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                   const char *signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    jvalue result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    result.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    if ((*env)->EnsureLocalCapacity(env, 3) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    cls = (*env)->GetObjectClass(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    fid = (*env)->GetFieldID(env, cls, name, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
    if (fid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        goto done1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    switch (*signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        result.l = (*env)->GetObjectField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        result.z = (*env)->GetBooleanField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        result.b = (*env)->GetByteField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        result.c = (*env)->GetCharField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        result.s = (*env)->GetShortField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        result.i = (*env)->GetIntField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        result.j = (*env)->GetLongField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        result.f = (*env)->GetFloatField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        result.d = (*env)->GetDoubleField(env, obj, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        (*env)->FatalError(env, "JNU_GetFieldByName: illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
 done1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    (*env)->DeleteLocalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
 done2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    if (hasException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        *hasException = (*env)->ExceptionCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
JNU_SetFieldByName(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                   jboolean *hasException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                   jobject obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                   const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                   const char *signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                   ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    if ((*env)->EnsureLocalCapacity(env, 3) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    cls = (*env)->GetObjectClass(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    fid = (*env)->GetFieldID(env, cls, name, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    if (fid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        goto done1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    va_start(args, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    switch (*signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        (*env)->SetObjectField(env, obj, fid, va_arg(args, jobject));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        (*env)->SetBooleanField(env, obj, fid, (jboolean)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        (*env)->SetByteField(env, obj, fid, (jbyte)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        (*env)->SetCharField(env, obj, fid, (jchar)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        (*env)->SetShortField(env, obj, fid, (jshort)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        (*env)->SetIntField(env, obj, fid, va_arg(args, jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        (*env)->SetLongField(env, obj, fid, va_arg(args, jlong));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        (*env)->SetFloatField(env, obj, fid, (jfloat)va_arg(args, jdouble));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        (*env)->SetDoubleField(env, obj, fid, va_arg(args, jdouble));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        (*env)->FatalError(env, "JNU_SetFieldByName: illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
 done1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    (*env)->DeleteLocalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
 done2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    if (hasException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        *hasException = (*env)->ExceptionCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
JNIEXPORT jvalue JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
JNU_GetStaticFieldByName(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                         jboolean *hasException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                         const char *classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                         const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                         const char *signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    jvalue result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    result.i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    if ((*env)->EnsureLocalCapacity(env, 3) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    cls = (*env)->FindClass(env, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    if (cls == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    fid = (*env)->GetStaticFieldID(env, cls, name, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    if (fid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        goto done1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    switch (*signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        result.l = (*env)->GetStaticObjectField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        result.z = (*env)->GetStaticBooleanField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        result.b = (*env)->GetStaticByteField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        result.c = (*env)->GetStaticCharField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        result.s = (*env)->GetStaticShortField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        result.i = (*env)->GetStaticIntField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        result.j = (*env)->GetStaticLongField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        result.f = (*env)->GetStaticFloatField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        result.d = (*env)->GetStaticDoubleField(env, cls, fid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        (*env)->FatalError(env, "JNU_GetStaticFieldByName: illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
 done1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    (*env)->DeleteLocalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
 done2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    if (hasException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        *hasException = (*env)->ExceptionCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
JNU_SetStaticFieldByName(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                         jboolean *hasException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                         const char *classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                         const char *name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                         const char *signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                         ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    jclass cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    jfieldID fid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    va_list args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    if ((*env)->EnsureLocalCapacity(env, 3) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    cls = (*env)->FindClass(env, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    if (cls == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        goto done2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    fid = (*env)->GetStaticFieldID(env, cls, name, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    if (fid == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        goto done1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    va_start(args, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    switch (*signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        (*env)->SetStaticObjectField(env, cls, fid, va_arg(args, jobject));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        (*env)->SetStaticBooleanField(env, cls, fid, (jboolean)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        (*env)->SetStaticByteField(env, cls, fid, (jbyte)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        (*env)->SetStaticCharField(env, cls, fid, (jchar)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    case 'S':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        (*env)->SetStaticShortField(env, cls, fid, (jshort)va_arg(args, int));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        (*env)->SetStaticIntField(env, cls, fid, va_arg(args, jint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        (*env)->SetStaticLongField(env, cls, fid, va_arg(args, jlong));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        (*env)->SetStaticFloatField(env, cls, fid, (jfloat)va_arg(args, jdouble));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    case 'D':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        (*env)->SetStaticDoubleField(env, cls, fid, va_arg(args, jdouble));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        (*env)->FatalError(env, "JNU_SetStaticFieldByName: illegal signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    va_end(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
 done1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    (*env)->DeleteLocalRef(env, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
 done2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    if (hasException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        *hasException = (*env)->ExceptionCheck(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
}