jdk/src/share/native/java/lang/Class.c
author alanb
Thu, 07 Oct 2010 14:36:17 +0100
changeset 6850 56966b0a6a0d
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code Reviewed-by: andrew, mchung, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1994, 2004, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/*-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      Implementation of class Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *      former threadruntime.c, Sun Sep 22 12:09:39 1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "java_lang_Class.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/* defined in libverify.so/verify.dll (src file common/check_format.c) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
extern jboolean VerifyFixClassname(char *utf_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#define OBJ "Ljava/lang/Object;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#define CLS "Ljava/lang/Class;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#define CPL "Lsun/reflect/ConstantPool;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define STR "Ljava/lang/String;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#define JCL "Ljava/lang/ClassLoader;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#define FLD "Ljava/lang/reflect/Field;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define MHD "Ljava/lang/reflect/Method;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#define CTR "Ljava/lang/reflect/Constructor;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define PD  "Ljava/security/ProtectionDomain;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#define BA  "[B"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
static JNINativeMethod methods[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    {"getName0",         "()" STR,          (void *)&JVM_GetClassName},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    {"getSuperclass",    "()" CLS,          NULL},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    {"getInterfaces",    "()[" CLS,         (void *)&JVM_GetClassInterfaces},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    {"getClassLoader0",  "()" JCL,          (void *)&JVM_GetClassLoader},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    {"isInterface",      "()Z",             (void *)&JVM_IsInterface},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    {"getSigners",       "()[" OBJ,         (void *)&JVM_GetClassSigners},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    {"setSigners",       "([" OBJ ")V",     (void *)&JVM_SetClassSigners},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    {"isArray",          "()Z",             (void *)&JVM_IsArrayClass},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    {"isPrimitive",      "()Z",             (void *)&JVM_IsPrimitiveClass},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    {"getComponentType", "()" CLS,          (void *)&JVM_GetComponentType},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    {"getModifiers",     "()I",             (void *)&JVM_GetClassModifiers},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {"getDeclaredFields0","(Z)[" FLD,       (void *)&JVM_GetClassDeclaredFields},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    {"getDeclaredMethods0","(Z)[" MHD,      (void *)&JVM_GetClassDeclaredMethods},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    {"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    {"getProtectionDomain0", "()" PD,       (void *)&JVM_GetProtectionDomain},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    {"setProtectionDomain0", "(" PD ")V",   (void *)&JVM_SetProtectionDomain},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    {"getDeclaredClasses0",  "()[" CLS,      (void *)&JVM_GetDeclaredClasses},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    {"getDeclaringClass",   "()" CLS,      (void *)&JVM_GetDeclaringClass},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    {"getGenericSignature", "()" STR,       (void *)&JVM_GetClassSignature},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {"getRawAnnotations",      "()" BA,        (void *)&JVM_GetClassAnnotations},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    {"getConstantPool",     "()" CPL,       (void *)&JVM_GetClassConstantPool},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    {"desiredAssertionStatus0","("CLS")Z",(void *)&JVM_DesiredAssertionStatus},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {"getEnclosingMethod0", "()[" OBJ,      (void *)&JVM_GetEnclosingMethodInfo}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#undef OBJ
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#undef CLS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#undef STR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#undef JCL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#undef FLD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
#undef MHD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
#undef CTR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
#undef PD
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
Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    methods[1].fnPtr = (void *)(*env)->GetSuperclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    (*env)->RegisterNatives(env, cls, methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                            sizeof(methods)/sizeof(JNINativeMethod));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                              jboolean initialize, jobject loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    char *clname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    jclass cls = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    char buf[128];
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   105
    jsize len;
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   106
    jsize unicode_len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (classname == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    len = (*env)->GetStringUTFLength(env, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    unicode_len = (*env)->GetStringLength(env, classname);
6850
56966b0a6a0d 6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents: 5506
diff changeset
   115
    if (len >= (jsize)sizeof(buf)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        clname = malloc(len + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (clname == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            JNU_ThrowOutOfMemoryError(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        clname = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (VerifyFixClassname(clname) == JNI_TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        /* slashes present in clname, use name b4 translation for exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        JNU_ThrowClassNotFoundException(env, clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    if (!VerifyClassname(clname, JNI_TRUE)) {  /* expects slashed name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        JNU_ThrowClassNotFoundException(env, clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        goto done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    cls = JVM_FindClassFromClassLoader(env, clname, initialize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                       loader, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 done:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    if (clname != buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        free(clname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    if (obj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    return (*env)->IsInstanceOf(env, obj, (jclass)cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    if (cls2 == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    return (*env)->IsAssignableFrom(env, cls2, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
JNIEXPORT jclass JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
Java_java_lang_Class_getPrimitiveClass(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                       jclass cls,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                       jstring name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    const char *utfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    jclass result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    if (name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    utfName = (*env)->GetStringUTFChars(env, name, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (utfName == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    result = JVM_FindPrimitiveClass(env, utfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    (*env)->ReleaseStringUTFChars(env, name, utfName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}