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 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 18 | #include "class_loader.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 19 | #include "jni_internal.h" |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 20 | #include "nth_caller_visitor.h" |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 21 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 22 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change.h" |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 24 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 25 | #include "ScopedUtfChars.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 26 | #include "well_known_classes.h" |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 30 | static Class* DecodeClass(const ScopedObjectAccess& soa, jobject java_class) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 31 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 32 | Class* c = soa.Decode<Class*>(java_class); |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 33 | DCHECK(c != NULL); |
| 34 | DCHECK(c->IsClass()); |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 35 | // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke . |
| 36 | // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check |
| 37 | // every time probably doesn't make much difference to reflection performance anyway. |
| 38 | return c; |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 41 | // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1". |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 42 | static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 43 | ScopedObjectAccess soa(env); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 44 | ScopedUtfChars name(env, javaName); |
| 45 | if (name.c_str() == NULL) { |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | // We need to validate and convert the name (from x.y.z to x/y/z). This |
| 50 | // is especially handy for array types, since we want to avoid |
| 51 | // auto-generating bogus array classes. |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 52 | if (!IsValidBinaryClassName(name.c_str())) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 53 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 54 | "Invalid name: %s", name.c_str()); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | std::string descriptor(DotToDescriptor(name.c_str())); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | ClassLoader* class_loader = soa.Decode<ClassLoader*>(javaLoader); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 60 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 61 | Class* c = class_linker->FindClass(descriptor.c_str(), class_loader); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 62 | if (c == NULL) { |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 63 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 64 | env->ExceptionClear(); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 65 | jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException, |
| 66 | WellKnownClasses::java_lang_ClassNotFoundException_init, |
| 67 | javaName, cause.get())); |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 68 | env->Throw(cnfe); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 69 | return NULL; |
| 70 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 71 | if (initialize) { |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 72 | class_linker->EnsureInitialized(c, true, true); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 73 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 74 | return soa.AddLocalReference<jclass>(c); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 77 | static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | ScopedObjectAccess soa(env); |
| 79 | Class* c = DecodeClass(soa, javaClass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 80 | if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) { |
| 81 | return 0; // primitive, array and proxy classes don't have class definitions |
| 82 | } |
| 83 | const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef(); |
| 84 | if (class_def == NULL) { |
| 85 | return 0; // not found |
| 86 | } else { |
| 87 | return class_def->annotations_off_; |
| 88 | } |
| 89 | } |
| 90 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 91 | static jobject Class_getDex(JNIEnv* env, jobject javaClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 92 | ScopedObjectAccess soa(env); |
| 93 | Class* c = DecodeClass(soa, javaClass); |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 94 | |
| 95 | DexCache* dex_cache = c->GetDexCache(); |
| 96 | if (dex_cache == NULL) { |
| 97 | return NULL; |
| 98 | } |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 99 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 100 | if (dex_file == NULL) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 103 | return dex_file->GetDexObject(env); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 106 | static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 107 | ScopedObjectAccess soa(env); |
| 108 | Class* c = DecodeClass(soa, javaThis); |
| 109 | return soa.AddLocalReference<jstring>(c->ComputeName()); |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 112 | static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | ScopedObjectAccess soa(env); |
| 114 | SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(soa, javaThis)); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 115 | return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 118 | static JNINativeMethod gMethods[] = { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 119 | NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 120 | NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"), |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 121 | NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 122 | NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"), |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 123 | NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 126 | void register_java_lang_Class(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 127 | REGISTER_NATIVE_METHODS("java/lang/Class"); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } // namespace art |