Path: blob/master/src/java.base/share/native/libjava/Class.c
41149 views
/*1* Copyright (c) 1994, 2020, 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 "check_classname.h"38#include "java_lang_Class.h"3940/* defined in libverify.so/verify.dll (src file common/check_format.c) */41extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed);42extern jboolean VerifyFixClassname(char *utf_name);4344#define OBJ "Ljava/lang/Object;"45#define CLS "Ljava/lang/Class;"46#define CPL "Ljdk/internal/reflect/ConstantPool;"47#define STR "Ljava/lang/String;"48#define FLD "Ljava/lang/reflect/Field;"49#define MHD "Ljava/lang/reflect/Method;"50#define CTR "Ljava/lang/reflect/Constructor;"51#define PD "Ljava/security/ProtectionDomain;"52#define BA "[B"53#define RC "Ljava/lang/reflect/RecordComponent;"5455static JNINativeMethod methods[] = {56{"initClassName", "()" STR, (void *)&JVM_InitClassName},57{"getSuperclass", "()" CLS, NULL},58{"getInterfaces0", "()[" CLS, (void *)&JVM_GetClassInterfaces},59{"isInterface", "()Z", (void *)&JVM_IsInterface},60{"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners},61{"setSigners", "([" OBJ ")V", (void *)&JVM_SetClassSigners},62{"isArray", "()Z", (void *)&JVM_IsArrayClass},63{"isHidden", "()Z", (void *)&JVM_IsHiddenClass},64{"isPrimitive", "()Z", (void *)&JVM_IsPrimitiveClass},65{"getModifiers", "()I", (void *)&JVM_GetClassModifiers},66{"getDeclaredFields0","(Z)[" FLD, (void *)&JVM_GetClassDeclaredFields},67{"getDeclaredMethods0","(Z)[" MHD, (void *)&JVM_GetClassDeclaredMethods},68{"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors},69{"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain},70{"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses},71{"getDeclaringClass0", "()" CLS, (void *)&JVM_GetDeclaringClass},72{"getSimpleBinaryName0", "()" STR, (void *)&JVM_GetSimpleBinaryName},73{"getGenericSignature0", "()" STR, (void *)&JVM_GetClassSignature},74{"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations},75{"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool},76{"desiredAssertionStatus0","("CLS")Z", (void *)&JVM_DesiredAssertionStatus},77{"getEnclosingMethod0", "()[" OBJ, (void *)&JVM_GetEnclosingMethodInfo},78{"getRawTypeAnnotations", "()" BA, (void *)&JVM_GetClassTypeAnnotations},79{"getNestHost0", "()" CLS, (void *)&JVM_GetNestHost},80{"getNestMembers0", "()[" CLS, (void *)&JVM_GetNestMembers},81{"getRecordComponents0", "()[" RC, (void *)&JVM_GetRecordComponents},82{"isRecord0", "()Z", (void *)&JVM_IsRecord},83{"getPermittedSubclasses0", "()[" CLS, (void *)&JVM_GetPermittedSubclasses},84};8586#undef OBJ87#undef CLS88#undef STR89#undef FLD90#undef MHD91#undef CTR92#undef PD9394JNIEXPORT void JNICALL95Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls)96{97methods[1].fnPtr = (void *)(*env)->GetSuperclass;98(*env)->RegisterNatives(env, cls, methods,99sizeof(methods)/sizeof(JNINativeMethod));100}101102JNIEXPORT jclass JNICALL103Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname,104jboolean initialize, jobject loader, jclass caller)105{106char *clname;107jclass cls = 0;108char buf[128];109jsize len;110jsize unicode_len;111112if (classname == NULL) {113JNU_ThrowNullPointerException(env, 0);114return 0;115}116117len = (*env)->GetStringUTFLength(env, classname);118unicode_len = (*env)->GetStringLength(env, classname);119if (len >= (jsize)sizeof(buf)) {120clname = malloc(len + 1);121if (clname == NULL) {122JNU_ThrowOutOfMemoryError(env, NULL);123return NULL;124}125} else {126clname = buf;127}128(*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);129130if (verifyFixClassname(clname) == JNI_TRUE) {131/* slashes present in clname, use name b4 translation for exception */132(*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname);133JNU_ThrowClassNotFoundException(env, clname);134goto done;135}136137if (!verifyClassname(clname, JNI_TRUE)) { /* expects slashed name */138JNU_ThrowClassNotFoundException(env, clname);139goto done;140}141142cls = JVM_FindClassFromCaller(env, clname, initialize, loader, caller);143144done:145if (clname != buf) {146free(clname);147}148return cls;149}150151JNIEXPORT jboolean JNICALL152Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj)153{154if (obj == NULL) {155return JNI_FALSE;156}157return (*env)->IsInstanceOf(env, obj, (jclass)cls);158}159160JNIEXPORT jboolean JNICALL161Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2)162{163if (cls2 == NULL) {164JNU_ThrowNullPointerException(env, 0);165return JNI_FALSE;166}167return (*env)->IsAssignableFrom(env, cls2, cls);168}169170JNIEXPORT jclass JNICALL171Java_java_lang_Class_getPrimitiveClass(JNIEnv *env,172jclass cls,173jstring name)174{175const char *utfName;176jclass result;177178if (name == NULL) {179JNU_ThrowNullPointerException(env, 0);180return NULL;181}182183utfName = (*env)->GetStringUTFChars(env, name, 0);184if (utfName == 0)185return NULL;186187result = JVM_FindPrimitiveClass(env, utfName);188189(*env)->ReleaseStringUTFChars(env, name, utfName);190191return result;192}193194195