1 /* 2 * Copyright (c) 1994, 2013, 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 "java_lang_Class.h" 39 40 /* defined in libverify.so/verify.dll (src file common/check_format.c) */ 41 extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed); 42 extern jboolean VerifyFixClassname(char *utf_name); 43 44 #define OBJ "Ljava/lang/Object;" 45 #define CLS "Ljava/lang/Class;" 46 #define CPL "Lsun/reflect/ConstantPool;" 47 #define STR "Ljava/lang/String;" 48 #define JCL "Ljava/lang/ClassLoader;" 49 #define FLD "Ljava/lang/reflect/Field;" 50 #define MHD "Ljava/lang/reflect/Method;" 51 #define CTR "Ljava/lang/reflect/Constructor;" 52 #define PD "Ljava/security/ProtectionDomain;" 53 #define BA "[B" 54 55 static JNINativeMethod methods[] = { 56 {"getName0", "()" STR, (void *)&JVM_GetClassName}, 57 {"getSuperclass", "()" CLS, NULL}, 58 {"getInterfaces", "()[" CLS, (void *)&JVM_GetClassInterfaces}, 59 {"getClassLoader0", "()" JCL, (void *)&JVM_GetClassLoader}, 60 {"isInterface", "()Z", (void *)&JVM_IsInterface}, 61 {"getSigners", "()[" OBJ, (void *)&JVM_GetClassSigners}, 62 {"setSigners", "([" OBJ ")V", (void *)&JVM_SetClassSigners}, 63 {"isArray", "()Z", (void *)&JVM_IsArrayClass}, 64 {"isPrimitive", "()Z", (void *)&JVM_IsPrimitiveClass}, 65 {"getComponentType", "()" CLS, (void *)&JVM_GetComponentType}, 66 {"getModifiers", "()I", (void *)&JVM_GetClassModifiers}, 67 {"getDeclaredFields0","(Z)[" FLD, (void *)&JVM_GetClassDeclaredFields}, 68 {"getDeclaredMethods0","(Z)[" MHD, (void *)&JVM_GetClassDeclaredMethods}, 69 {"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors}, 70 {"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain}, 71 {"setProtectionDomain0", "(" PD ")V", (void *)&JVM_SetProtectionDomain}, 72 {"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses}, 73 {"getDeclaringClass", "()" CLS, (void *)&JVM_GetDeclaringClass}, 74 {"getGenericSignature", "()" STR, (void *)&JVM_GetClassSignature}, 75 {"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations}, 76 {"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool}, 77 {"desiredAssertionStatus0","("CLS")Z",(void *)&JVM_DesiredAssertionStatus}, 78 {"getEnclosingMethod0", "()[" OBJ, (void *)&JVM_GetEnclosingMethodInfo}, 79 {"getRawTypeAnnotations", "()" BA, (void *)&JVM_GetClassTypeAnnotations}, 80 }; 81 82 #undef OBJ 83 #undef CLS 84 #undef STR 85 #undef JCL 86 #undef FLD 87 #undef MHD 88 #undef CTR 89 #undef PD 90 91 JNIEXPORT void JNICALL 92 Java_java_lang_Class_registerNatives(JNIEnv *env, jclass cls) 93 { 94 methods[1].fnPtr = (void *)(*env)->GetSuperclass; 95 (*env)->RegisterNatives(env, cls, methods, 96 sizeof(methods)/sizeof(JNINativeMethod)); 97 } 98 99 JNIEXPORT jclass JNICALL 100 Java_java_lang_Class_forName0(JNIEnv *env, jclass this, jstring classname, 101 jboolean initialize, jobject loader) 102 { 103 char *clname; 104 jclass cls = 0; 105 char buf[128]; 106 jsize len; 107 jsize unicode_len; 108 109 if (classname == NULL) { 110 JNU_ThrowNullPointerException(env, 0); 111 return 0; 112 } 113 114 len = (*env)->GetStringUTFLength(env, classname); 115 unicode_len = (*env)->GetStringLength(env, classname); 116 if (len >= (jsize)sizeof(buf)) { 117 clname = malloc(len + 1); 118 if (clname == NULL) { 119 JNU_ThrowOutOfMemoryError(env, NULL); 120 return NULL; 121 } 122 } else { 123 clname = buf; 124 } 125 (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname); 126 127 if (VerifyFixClassname(clname) == JNI_TRUE) { 128 /* slashes present in clname, use name b4 translation for exception */ 129 (*env)->GetStringUTFRegion(env, classname, 0, unicode_len, clname); 130 JNU_ThrowClassNotFoundException(env, clname); 131 goto done; 132 } 133 134 if (!VerifyClassname(clname, JNI_TRUE)) { /* expects slashed name */ 135 JNU_ThrowClassNotFoundException(env, clname); 136 goto done; 137 } 138 139 cls = JVM_FindClassFromClassLoader(env, clname, initialize, 140 loader, JNI_FALSE); 141 142 done: 143 if (clname != buf) { 144 free(clname); 145 } 146 return cls; 147 } 148 149 JNIEXPORT jboolean JNICALL 150 Java_java_lang_Class_isInstance(JNIEnv *env, jobject cls, jobject obj) 151 { 152 if (obj == NULL) { 153 return JNI_FALSE; 154 } 155 return (*env)->IsInstanceOf(env, obj, (jclass)cls); 156 } 157 158 JNIEXPORT jboolean JNICALL 159 Java_java_lang_Class_isAssignableFrom(JNIEnv *env, jobject cls, jobject cls2) 160 { 161 if (cls2 == NULL) { 162 JNU_ThrowNullPointerException(env, 0); 163 return JNI_FALSE; 164 } 165 return (*env)->IsAssignableFrom(env, cls2, cls); 166 } 167 168 JNIEXPORT jclass JNICALL 169 Java_java_lang_Class_getPrimitiveClass(JNIEnv *env, 170 jclass cls, 171 jstring name) 172 { 173 const char *utfName; 174 jclass result; 175 176 if (name == NULL) { 177 JNU_ThrowNullPointerException(env, 0); 178 return NULL; 179 } 180 181 utfName = (*env)->GetStringUTFChars(env, name, 0); 182 if (utfName == 0) 183 return NULL; 184 185 result = JVM_FindPrimitiveClass(env, utfName); 186 187 (*env)->ReleaseStringUTFChars(env, name, utfName); 188 189 return result; 190 }