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" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 18 | #include "dex_file-inl.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" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/class_loader.h" |
| 23 | #include "mirror/object-inl.h" |
| 24 | #include "mirror/proxy.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 25 | #include "object_utils.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 26 | #include "scoped_thread_state_change.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 27 | #include "scoped_fast_native_object_access.h" |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 28 | #include "ScopedLocalRef.h" |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 29 | #include "ScopedUtfChars.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 30 | #include "well_known_classes.h" |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 34 | static mirror::Class* DecodeClass(const ScopedFastNativeObjectAccess& soa, jobject java_class) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 35 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | mirror::Class* c = soa.Decode<mirror::Class*>(java_class); |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 37 | DCHECK(c != NULL); |
| 38 | DCHECK(c->IsClass()); |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 39 | // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke . |
| 40 | // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check |
| 41 | // every time probably doesn't make much difference to reflection performance anyway. |
| 42 | return c; |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 45 | // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1". |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 46 | static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, |
| 47 | jobject javaLoader) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 48 | ScopedFastNativeObjectAccess soa(env); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 49 | ScopedUtfChars name(env, javaName); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 50 | if (name.c_str() == nullptr) { |
| 51 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // We need to validate and convert the name (from x.y.z to x/y/z). This |
| 55 | // is especially handy for array types, since we want to avoid |
| 56 | // auto-generating bogus array classes. |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 57 | if (!IsValidBinaryClassName(name.c_str())) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 58 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 59 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ClassNotFoundException;", |
| 60 | "Invalid name: %s", name.c_str()); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 61 | return nullptr; |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | std::string descriptor(DotToDescriptor(name.c_str())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 65 | StackHandleScope<2> hs(soa.Self()); |
| 66 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader))); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 67 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 68 | Handle<mirror::Class> c( |
| 69 | hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader))); |
| 70 | if (c.Get() == nullptr) { |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 71 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 72 | env->ExceptionClear(); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 73 | jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException, |
| 74 | WellKnownClasses::java_lang_ClassNotFoundException_init, |
| 75 | javaName, cause.get())); |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 76 | env->Throw(cnfe); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 77 | return nullptr; |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 78 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 79 | if (initialize) { |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 80 | class_linker->EnsureInitialized(c, true, true); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 81 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 82 | return soa.AddLocalReference<jclass>(c.Get()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 85 | static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 86 | ScopedFastNativeObjectAccess soa(env); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 87 | StackHandleScope<1> hs(soa.Self()); |
| 88 | mirror::Class* const c = DecodeClass(soa, javaThis); |
| 89 | return soa.AddLocalReference<jstring>(mirror::Class::ComputeName(hs.NewHandle(c))); |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 92 | static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 93 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | mirror::SynthesizedProxyClass* c = |
| 95 | down_cast<mirror::SynthesizedProxyClass*>(DecodeClass(soa, javaThis)); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 96 | return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self())); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 99 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 100 | NATIVE_METHOD(Class, classForName, "!(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 101 | NATIVE_METHOD(Class, getNameNative, "!()Ljava/lang/String;"), |
| 102 | NATIVE_METHOD(Class, getProxyInterfaces, "!()[Ljava/lang/Class;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 105 | void register_java_lang_Class(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 106 | REGISTER_NATIVE_METHODS("java/lang/Class"); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } // namespace art |