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