Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/lang/Class.c
38829 views
/*1* Copyright (c) 1994, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*-26* Implementation of class Class27*28* former threadruntime.c, Sun Sep 22 12:09:39 199129*/3031#include <string.h>32#include <stdlib.h>3334#include "jni.h"35#include "jni_util.h"36#include "jvm.h"37#include "java_lang_Class.h"3839/* defined in libverify.so/verify.dll (src file common/check_format.c) */40extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed);41extern jboolean VerifyFixClassname(char *utf_name);4243#define OBJ "Ljava/lang/Object;"44#define CLS "Ljava/lang/Class;"45#define CPL "Lsun/reflect/ConstantPool;"46#define STR "Ljava/lang/String;"47#define FLD "Ljava/lang/reflect/Field;"48#define MHD "Ljava/lang/reflect/Method;"49#define CTR "Ljava/lang/reflect/Constructor;"50#define PD "Ljava/security/ProtectionDomain;"51#define BA "[B"5253static JNINativeMethod methods[] = {54{"getName0", "()" STR, (void *)&JVM_GetClassName},55{"getSuperclass", "()" CLS, NULL},56{"getInterfaces0", "()[" CLS, (void *)&JVM_GetClassInterfaces},57{"isInterface", "()Z", (void *)&JVM_IsInterface},58{"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners},59{"setSigners", "([" OBJ ")V", (void *)&JVM_SetClassSigners},60{"isArray", "()Z", (void *)&JVM_IsArrayClass},61{"isPrimitive", "()Z", (void *)&JVM_IsPrimitiveClass},62{"getComponentType", "()" CLS, (void *)&JVM_GetComponentType},63{"getModifiers", "()I", (void *)&JVM_GetClassModifiers},64{"getDeclaredFields0","(Z)[" FLD, (void *)&JVM_GetClassDeclaredFields},65{"getDeclaredMethods0","(Z)[" MHD, (void *)&JVM_GetClassDeclaredMethods},66{"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors},67{"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain},68{"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses},69{"getDeclaringClass0", "()" CLS, (void *)&JVM_GetDeclaringClass},70{"getGenericSignature0", "()" STR, (void *)&JVM_GetClassSignature},71{"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations},72{"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool},73{"desiredAssertionStatus0","("CLS")Z",(void *)&JVM_DesiredAssertionStatus},74{"getEnclosingMethod0", "()[" OBJ, (void *)&JVM_GetEnclosingMethodInfo},75{"getRawTypeAnnotations", "()" BA, (void *)&JVM_GetClassTypeAnnotations},76};7778#undef OBJ79#undef CLS80#undef STR81#undef FLD82#undef MHD83#undef CTR84#undef PD8586JNIEXPORT void JNICALL87Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls)88{89methods[1].fnPtr = (void *)(*env)->GetSuperclass;90(*env)->RegisterNatives(env, cls, methods,91sizeof(methods)/sizeof(JNINativeMethod));92}9394JNIEXPORT jclass JNICALL95Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname,96jboolean initialize, jobject loader, jclass caller)97{98char *clname;99jclass cls = 0;100char buf[128];101jsize len;102jsize unicode_len;103104if (classname == NULL) {105JNU_ThrowNullPointerException(env, 0);106return 0;107}108109len = (*env)->GetStringUTFLength(env, classname);110unicode_len = (*env)->GetStringLength(env, classname);111if (len >= (jsize)sizeof(buf)) {112clname = malloc(len + 1);113if (clname == NULL) {114JNU_ThrowOutOfMemoryError(env, NULL);115return NULL;116}117} else {118clname = buf;119}120(*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);121122if (VerifyFixClassname(clname) == JNI_TRUE) {123/* slashes present in clname, use name b4 translation for exception */124(*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);125JNU_ThrowClassNotFoundException(env, clname);126goto done;127}128129if (!VerifyClassname(clname, JNI_TRUE)) { /* expects slashed name */130JNU_ThrowClassNotFoundException(env, clname);131goto done;132}133134cls = JVM_FindClassFromCaller(env, clname, initialize, loader, caller);135136done:137if (clname != buf) {138free(clname);139}140return cls;141}142143JNIEXPORT jboolean JNICALL144Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj)145{146if (obj == NULL) {147return JNI_FALSE;148}149return (*env)->IsInstanceOf(env, obj, (jclass)cls);150}151152JNIEXPORT jboolean JNICALL153Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2)154{155if (cls2 == NULL) {156JNU_ThrowNullPointerException(env, 0);157return JNI_FALSE;158}159return (*env)->IsAssignableFrom(env, cls2, cls);160}161162JNIEXPORT jclass JNICALL163Java_java_lang_Class_getPrimitiveClass(JNIEnv *env,164jclass cls,165jstring name)166{167const char *utfName;168jclass result;169170if (name == NULL) {171JNU_ThrowNullPointerException(env, 0);172return NULL;173}174175utfName = (*env)->GetStringUTFChars(env, name, 0);176if (utfName == 0)177return NULL;178179result = JVM_FindPrimitiveClass(env, utfName);180181(*env)->ReleaseStringUTFChars(env, name, utfName);182183return result;184}185186187