1 /* 2 * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /*- 27 * Implementation of class Class 28 * 29 * former threadruntime.c, Sun Sep 22 12:09:39 1991 30 */ 31 32 #include <string.h> 33 #include <stdlib.h> 34 35 #include "jni.h" 36 #include "jni_util.h" 37 #include "jvm.h" 38 #include "check_classname.h" 39 #include "java_lang_Class.h" 40 41 #define OBJ "Ljava/lang/Object;" 42 #define CLS "Ljava/lang/Class;" 43 #define CPL "Ljdk/internal/reflect/ConstantPool;" 44 #define STR "Ljava/lang/String;" 45 #define FLD "Ljava/lang/reflect/Field;" 46 #define MHD "Ljava/lang/reflect/Method;" 47 #define CTR "Ljava/lang/reflect/Constructor;" 48 #define PD "Ljava/security/ProtectionDomain;" 49 #define BA "[B" 50 51 static JNINativeMethod methods[] = { 52 {"initClassName", "()" STR, (void *)&JVM_InitClassName}, 53 {"getSuperclass", "()" CLS, NULL}, 54 {"getInterfaces0", "()[" CLS, (void *)&JVM_GetClassInterfaces}, 55 {"isInterface", "()Z", (void *)&JVM_IsInterface}, 56 {"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners}, 57 {"setSigners", "([" OBJ ")V", (void *)&JVM_SetClassSigners}, 58 {"isArray", "()Z", (void *)&JVM_IsArrayClass}, 59 {"isPrimitive", "()Z", (void *)&JVM_IsPrimitiveClass}, 60 {"getModifiers", "()I", (void *)&JVM_GetClassModifiers}, 61 {"getDeclaredFields0","(Z)[" FLD, (void *)&JVM_GetClassDeclaredFields}, 62 {"getDeclaredMethods0","(Z)[" MHD, (void *)&JVM_GetClassDeclaredMethods}, 63 {"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors}, 64 {"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain}, 65 {"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses}, 66 {"getDeclaringClass0", "()" CLS, (void *)&JVM_GetDeclaringClass}, 67 {"getSimpleBinaryName0", "()" STR, (void *)&JVM_GetSimpleBinaryName}, 68 {"getGenericSignature0", "()" STR, (void *)&JVM_GetClassSignature}, 69 {"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations}, 70 {"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool}, 71 {"desiredAssertionStatus0","("CLS")Z", (void *)&JVM_DesiredAssertionStatus}, 72 {"getEnclosingMethod0", "()[" OBJ, (void *)&JVM_GetEnclosingMethodInfo}, 73 {"getRawTypeAnnotations", "()" BA, (void *)&JVM_GetClassTypeAnnotations}, 74 {"getNestHost0", "()" CLS, (void *)&JVM_GetNestHost}, 75 {"getNestMembers0", "()[" CLS, (void *)&JVM_GetNestMembers}, 76 }; 77 78 #undef OBJ 79 #undef CLS 80 #undef STR 81 #undef FLD 82 #undef MHD 83 #undef CTR 84 #undef PD 85 86 JNIEXPORT void JNICALL 87 Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls) 88 { 89 methods[1].fnPtr = (void *)(*env)->GetSuperclass; 90 (*env)->RegisterNatives(env, cls, methods, 91 sizeof(methods)/sizeof(JNINativeMethod)); 92 } 93 94 JNIEXPORT jclass JNICALL 95 Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname, 96 jboolean initialize, jobject loader, jclass caller) 97 { 98 char *clname; 99 jclass cls = 0; 100 char buf[128]; 101 jsize len; 102 jsize unicode_len; 103 104 if (classname == NULL) { 105 JNU_ThrowNullPointerException(env, 0); 106 return 0; 107 } 108 109 len = (*env)->GetStringUTFLength(env, classname); 110 unicode_len = (*env)->GetStringLength(env, classname); 111 if (len >= (jsize)sizeof(buf)) { 112 clname = malloc(len + 1); 113 if (clname == NULL) { 114 JNU_ThrowOutOfMemoryError(env, NULL); 115 return NULL; 116 } 117 } else { 118 clname = buf; 119 } 120 (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname); 121 122 if (verifyFixClassname(clname) == JNI_TRUE) { 123 /* slashes present in clname, use name b4 translation for exception */ 124 (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname); 125 JNU_ThrowClassNotFoundException(env, clname); 126 goto done; 127 } 128 129 if (!verifyClassname(clname, JNI_TRUE)) { /* expects slashed name */ 130 JNU_ThrowClassNotFoundException(env, clname); 131 goto done; 132 } 133 134 cls = JVM_FindClassFromCaller(env, clname, initialize, loader, caller); 135 136 done: 137 if (clname != buf) { 138 free(clname); 139 } 140 return cls; 141 } 142 143 JNIEXPORT jboolean JNICALL 144 Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj) 145 { 146 if (obj == NULL) { 147 return JNI_FALSE; 148 } 149 return (*env)->IsInstanceOf(env, obj, (jclass)cls); 150 } 151 152 JNIEXPORT jboolean JNICALL 153 Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2) 154 { 155 if (cls2 == NULL) { 156 JNU_ThrowNullPointerException(env, 0); 157 return JNI_FALSE; 158 } 159 return (*env)->IsAssignableFrom(env, cls2, cls); 160 } 161 162 JNIEXPORT jclass JNICALL 163 Java_java_lang_Class_getPrimitiveClass(JNIEnv *env, 164 jclass cls, 165 jstring name) 166 { 167 const char *utfName; 168 jclass result; 169 170 if (name == NULL) { 171 JNU_ThrowNullPointerException(env, 0); 172 return NULL; 173 } 174 175 utfName = (*env)->GetStringUTFChars(env, name, 0); 176 if (utfName == 0) 177 return NULL; 178 179 result = JVM_FindPrimitiveClass(env, utfName); 180 181 (*env)->ReleaseStringUTFChars(env, name, utfName); 182 183 return result; 184 }