Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "jni_internal.h" |
| 18 | #include "class_linker.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame^] | 19 | #include "class_loader.h" |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 20 | #include "object.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame^] | 21 | #include "ScopedUtfChars.h" |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 22 | |
| 23 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | namespace { |
| 28 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame^] | 29 | // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1". |
| 30 | jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) { |
| 31 | ScopedUtfChars name(env, javaName); |
| 32 | if (name.c_str() == NULL) { |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | // We need to validate and convert the name (from x.y.z to x/y/z). This |
| 37 | // is especially handy for array types, since we want to avoid |
| 38 | // auto-generating bogus array classes. |
| 39 | if (!IsValidClassName(name.c_str(), true, true)) { |
| 40 | Thread::Current()->ThrowNewException("Ljava/lang/ClassNotFoundException;", |
| 41 | "Invalid name: %s", name.c_str()); |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | std::string descriptor(DotToDescriptor(name.c_str())); |
| 46 | Object* loader = Decode<Object*>(env, javaLoader); |
| 47 | ClassLoader* class_loader = down_cast<ClassLoader*>(loader); |
| 48 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 49 | Class* c = class_linker->FindClass(descriptor.c_str(), class_loader); |
| 50 | if (initialize) { |
| 51 | class_linker->EnsureInitialized(c, true); |
| 52 | } |
| 53 | return AddLocalReference<jclass>(env, c); |
| 54 | } |
| 55 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 56 | jboolean Class_desiredAssertionStatus(JNIEnv* env, jobject javaThis) { |
| 57 | return JNI_FALSE; |
| 58 | } |
| 59 | |
| 60 | jobject Class_getClassLoader(JNIEnv* env, jclass, jobject javaClass) { |
| 61 | Class* c = Decode<Class*>(env, javaClass); |
| 62 | Object* result = reinterpret_cast<Object*>(const_cast<ClassLoader*>(c->GetClassLoader())); |
| 63 | return AddLocalReference<jobject>(env, result); |
| 64 | } |
| 65 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 66 | jclass Class_getComponentType(JNIEnv* env, jobject javaThis) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 67 | return AddLocalReference<jclass>(env, Decode<Class*>(env, javaThis)->GetComponentType()); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | jobjectArray Class_getDeclaredClasses(JNIEnv* env, jclass java_lang_Class_class, jclass c, jboolean publicOnly) { |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 71 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 72 | return env->NewObjectArray(0, java_lang_Class_class, NULL); |
| 73 | } |
| 74 | |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 75 | jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass, |
| 76 | jclass jklass, jstring jname, jobjectArray jsignature) { |
| 77 | Class* klass = Decode<Class*>(env, jklass); |
| 78 | DCHECK(klass->IsClass()); |
| 79 | String* name = Decode<String*>(env, jname); |
| 80 | DCHECK(name->IsString()); |
| 81 | Object* signature_obj = Decode<Object*>(env, jsignature); |
| 82 | DCHECK(signature_obj->IsArrayInstance()); |
Brian Carlstrom | 03c99df | 2011-09-18 10:52:00 -0700 | [diff] [blame] | 83 | // check that this is a Class[] by checking that component type is Class |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 84 | // foo->GetClass()->GetClass() is an idiom for getting java.lang.Class from an arbitrary object |
| 85 | DCHECK(signature_obj->GetClass()->GetComponentType() == signature_obj->GetClass()->GetClass()); |
| 86 | ObjectArray<Class>* signature = down_cast<ObjectArray<Class>*>(signature_obj); |
| 87 | |
| 88 | std::string name_string = name->ToModifiedUtf8(); |
| 89 | std::string signature_string; |
| 90 | signature_string += "("; |
| 91 | for (int i = 0; i < signature->GetLength(); i++) { |
| 92 | Class* argument_class = signature->Get(0); |
| 93 | if (argument_class == NULL) { |
| 94 | UNIMPLEMENTED(FATAL) << "throw null pointer exception?"; |
| 95 | } |
| 96 | signature_string += argument_class->GetDescriptor()->ToModifiedUtf8(); |
| 97 | } |
| 98 | signature_string += ")"; |
| 99 | |
| 100 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
| 101 | Method* method = klass->GetVirtualMethod(i); |
| 102 | if (!method->GetName()->Equals(name)) { |
| 103 | continue; |
| 104 | } |
| 105 | std::string method_signature = method->GetSignature()->ToModifiedUtf8(); |
| 106 | if (!StringPiece(method_signature).starts_with(signature_string)) { |
| 107 | continue; |
| 108 | } |
| 109 | return AddLocalReference<jobject>(env, method); |
| 110 | } |
| 111 | |
Brian Carlstrom | 03c99df | 2011-09-18 10:52:00 -0700 | [diff] [blame] | 112 | for (size_t i = 0; i < klass->NumDirectMethods(); ++i) { |
| 113 | Method* method = klass->GetDirectMethod(i); |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 114 | if (!method->GetName()->Equals(name)) { |
| 115 | continue; |
| 116 | } |
| 117 | std::string method_signature = method->GetSignature()->ToModifiedUtf8(); |
| 118 | if (!StringPiece(method_signature).starts_with(signature_string)) { |
| 119 | continue; |
| 120 | } |
| 121 | return AddLocalReference<jobject>(env, method); |
| 122 | } |
| 123 | |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | jobject Class_getDeclaredField(JNIEnv* env, jclass, jclass jklass, jobject jname) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 128 | Class* klass = Decode<Class*>(env, jklass); |
| 129 | DCHECK(klass->IsClass()); |
| 130 | String* name = Decode<String*>(env, jname); |
| 131 | DCHECK(name->IsString()); |
| 132 | |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 133 | for (size_t i = 0; i < klass->NumVirtualMethods(); ++i) { |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 134 | Field* f = klass->GetInstanceField(i); |
| 135 | if (f->GetName()->Equals(name)) { |
| 136 | return AddLocalReference<jclass>(env, f); |
| 137 | } |
| 138 | } |
| 139 | for (size_t i = 0; i < klass->NumStaticFields(); ++i) { |
| 140 | Field* f = klass->GetStaticField(i); |
| 141 | if (f->GetName()->Equals(name)) { |
| 142 | return AddLocalReference<jclass>(env, f); |
| 143 | } |
| 144 | } |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 148 | jclass Class_getDeclaringClass(JNIEnv* env, jobject javaThis) { |
| 149 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | jobject Class_getEnclosingConstructor(JNIEnv* env, jobject javaThis) { |
| 154 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | jobject Class_getEnclosingMethod(JNIEnv* env, jobject javaThis) { |
| 159 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * private native String getNameNative() |
| 165 | * |
| 166 | * Return the class' name. The exact format is bizarre, but it's the specified |
| 167 | * behavior: keywords for primitive types, regular "[I" form for primitive |
| 168 | * arrays (so "int" but "[I"), and arrays of reference types written |
| 169 | * between "L" and ";" but with dots rather than slashes (so "java.lang.String" |
| 170 | * but "[Ljava.lang.String;"). Madness. |
| 171 | */ |
| 172 | jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { |
| 173 | Class* c = Decode<Class*>(env, javaThis); |
| 174 | std::string descriptor(c->GetDescriptor()->ToModifiedUtf8()); |
| 175 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 176 | // The descriptor indicates that this is the class for |
| 177 | // a primitive type; special-case the return value. |
| 178 | const char* name = NULL; |
| 179 | switch (descriptor[0]) { |
| 180 | case 'Z': name = "boolean"; break; |
| 181 | case 'B': name = "byte"; break; |
| 182 | case 'C': name = "char"; break; |
| 183 | case 'S': name = "short"; break; |
| 184 | case 'I': name = "int"; break; |
| 185 | case 'J': name = "long"; break; |
| 186 | case 'F': name = "float"; break; |
| 187 | case 'D': name = "double"; break; |
| 188 | case 'V': name = "void"; break; |
| 189 | default: |
| 190 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 191 | } |
| 192 | return env->NewStringUTF(name); |
| 193 | } |
| 194 | |
| 195 | // Convert the UTF-8 name to a java.lang.String. The |
| 196 | // name must use '.' to separate package components. |
| 197 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 198 | descriptor.erase(0, 1); |
| 199 | descriptor.erase(descriptor.size() - 1); |
| 200 | } |
| 201 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 202 | return env->NewStringUTF(descriptor.c_str()); |
| 203 | } |
| 204 | |
| 205 | jclass Class_getSuperclass(JNIEnv* env, jobject javaThis) { |
| 206 | Class* c = Decode<Class*>(env, javaThis); |
| 207 | Class* result = c->GetSuperClass(); |
| 208 | return AddLocalReference<jclass>(env, result); |
| 209 | } |
| 210 | |
| 211 | jboolean Class_isAnonymousClass(JNIEnv* env, jobject javaThis) { |
| 212 | UNIMPLEMENTED(WARNING) << "needs annotations"; |
| 213 | return JNI_FALSE; |
| 214 | } |
| 215 | |
| 216 | jboolean Class_isInterface(JNIEnv* env, jobject javaThis) { |
| 217 | Class* c = Decode<Class*>(env, javaThis); |
| 218 | return c->IsInterface(); |
| 219 | } |
| 220 | |
| 221 | jboolean Class_isPrimitive(JNIEnv* env, jobject javaThis) { |
| 222 | Class* c = Decode<Class*>(env, javaThis); |
| 223 | return c->IsPrimitive(); |
| 224 | } |
| 225 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame^] | 226 | bool CheckClassAccess(const Class* access_from, const Class* klass) { |
| 227 | if (klass->IsPublic()) { |
| 228 | return true; |
| 229 | } |
| 230 | return access_from->IsInSamePackage(klass); |
| 231 | } |
| 232 | |
| 233 | // Validate method/field access. |
| 234 | bool CheckMemberAccess(const Class* access_from, const Class* access_to, uint32_t member_flags) { |
| 235 | // quick accept for public access */ |
| 236 | if (member_flags & kAccPublic) { |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | // quick accept for access from same class |
| 241 | if (access_from == access_to) { |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | // quick reject for private access from another class |
| 246 | if (member_flags & kAccPrivate) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | // Semi-quick test for protected access from a sub-class, which may or |
| 251 | // may not be in the same package. |
| 252 | if (member_flags & kAccProtected) { |
| 253 | if (access_from->IsSubClass(access_to)) { |
| 254 | return true; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Allow protected and private access from other classes in the same |
| 259 | return access_from->IsInSamePackage(access_to); |
| 260 | } |
| 261 | |
| 262 | jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) { |
| 263 | Class* c = Decode<Class*>(env, javaThis); |
| 264 | if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) { |
| 265 | Thread::Current()->ThrowNewException("Ljava/lang/InstantiationException;", |
| 266 | "Class %s can not be instantiated", PrettyDescriptor(c->GetDescriptor()).c_str()); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | Method* init = c->FindDirectMethod("<init>", "()V"); |
| 271 | if (init == NULL) { |
| 272 | Thread::Current()->ThrowNewException("Ljava/lang/InstantiationException;", |
| 273 | "Class %s has no default <init>()V constructor", PrettyDescriptor(c->GetDescriptor()).c_str()); |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | // Verify access from the call site. |
| 278 | // |
| 279 | // First, make sure the method invoking Class.newInstance() has permission |
| 280 | // to access the class. |
| 281 | // |
| 282 | // Second, make sure it has permission to invoke the constructor. The |
| 283 | // constructor must be public or, if the caller is in the same package, |
| 284 | // have package scope. |
| 285 | // TODO: need SmartFrame (Thread::WalkStack-like iterator). |
| 286 | Frame frame = Thread::Current()->GetTopOfStack(); |
| 287 | frame.Next(); |
| 288 | frame.Next(); |
| 289 | Method* caller_caller = frame.GetMethod(); |
| 290 | Class* caller_class = caller_caller->GetDeclaringClass(); |
| 291 | |
| 292 | if (!CheckClassAccess(c, caller_class)) { |
| 293 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessException;", |
| 294 | "Class %s is not accessible from class %s", |
| 295 | PrettyDescriptor(c->GetDescriptor()).c_str(), |
| 296 | PrettyDescriptor(caller_class->GetDescriptor()).c_str()); |
| 297 | return NULL; |
| 298 | } |
| 299 | if (!CheckMemberAccess(caller_class, init->GetDeclaringClass(), init->GetAccessFlags())) { |
| 300 | Thread::Current()->ThrowNewException("Ljava/lang/IllegalAccessException;", |
| 301 | "%s is not accessible from class %s", |
| 302 | PrettyMethod(init).c_str(), |
| 303 | PrettyDescriptor(caller_class->GetDescriptor()).c_str()); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | Object* new_obj = c->AllocObject(); |
| 308 | if (new_obj == NULL) { |
| 309 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | // invoke constructor; unlike reflection calls, we don't wrap exceptions |
| 314 | jclass jklass = AddLocalReference<jclass>(env, c); |
| 315 | jmethodID mid = EncodeMethod(init); |
| 316 | return env->NewObject(jklass, mid); |
| 317 | } |
| 318 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 319 | static JNINativeMethod gMethods[] = { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame^] | 320 | NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 321 | NATIVE_METHOD(Class, desiredAssertionStatus, "()Z"), |
| 322 | NATIVE_METHOD(Class, getClassLoader, "(Ljava/lang/Class;)Ljava/lang/ClassLoader;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 323 | NATIVE_METHOD(Class, getComponentType, "()Ljava/lang/Class;"), |
| 324 | //NATIVE_METHOD(Class, getDeclaredAnnotation, "(Ljava/lang/Class;)Ljava/lang/annotation/Annotation;"), |
| 325 | //NATIVE_METHOD(Class, getDeclaredAnnotations, "()[Ljava/lang/annotation/Annotation;"), |
| 326 | NATIVE_METHOD(Class, getDeclaredClasses, "(Ljava/lang/Class;Z)[Ljava/lang/Class;"), |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 327 | NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/Class;Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 328 | //NATIVE_METHOD(Class, getDeclaredConstructors, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Constructor;"), |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 329 | NATIVE_METHOD(Class, getDeclaredField, "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 330 | //NATIVE_METHOD(Class, getDeclaredFields, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Field;"), |
| 331 | //NATIVE_METHOD(Class, getDeclaredMethods, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Method;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 332 | NATIVE_METHOD(Class, getDeclaringClass, "()Ljava/lang/Class;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 333 | //NATIVE_METHOD(Class, getEnclosingClass, "()Ljava/lang/Class;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 334 | NATIVE_METHOD(Class, getEnclosingConstructor, "()Ljava/lang/reflect/Constructor;"), |
| 335 | NATIVE_METHOD(Class, getEnclosingMethod, "()Ljava/lang/reflect/Method;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 336 | //NATIVE_METHOD(Class, getInnerClassName, "()Ljava/lang/String;"), |
| 337 | //NATIVE_METHOD(Class, getInterfaces, "()[Ljava/lang/Class;"), |
| 338 | //NATIVE_METHOD(Class, getModifiers, "(Ljava/lang/Class;Z)I"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 339 | NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 340 | //NATIVE_METHOD(Class, getSignatureAnnotation, "()[Ljava/lang/Object;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 341 | NATIVE_METHOD(Class, getSuperclass, "()Ljava/lang/Class;"), |
| 342 | NATIVE_METHOD(Class, isAnonymousClass, "()Z"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 343 | //NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"), |
| 344 | //NATIVE_METHOD(Class, isDeclaredAnnotationPresent, "(Ljava/lang/Class;)Z"), |
| 345 | //NATIVE_METHOD(Class, isInstance, "(Ljava/lang/Object;)Z"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 346 | NATIVE_METHOD(Class, isInterface, "()Z"), |
| 347 | NATIVE_METHOD(Class, isPrimitive, "()Z"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame^] | 348 | NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | } // namespace |
| 352 | |
| 353 | void register_java_lang_Class(JNIEnv* env) { |
| 354 | jniRegisterNativeMethods(env, "java/lang/Class", gMethods, NELEM(gMethods)); |
| 355 | } |
| 356 | |
| 357 | } // namespace art |