blob: b11cbdfb92cac641cc8c7724d7484618a55def6f [file] [log] [blame]
Elliott Hughesd369bb72011-09-12 14:41:14 -07001/*
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 Hughesd369bb72011-09-12 14:41:14 -070017#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070018#include "dex_file-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070019#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070020#include "nth_caller_visitor.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/class_loader.h"
23#include "mirror/object-inl.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070025#include "scoped_fast_native_object_access.h"
Elliott Hughes80609252011-09-23 17:24:51 -070026#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070027#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070028#include "well_known_classes.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070029
30namespace art {
31
Ian Rogers1eb512d2013-10-18 15:42:20 -070032static mirror::Class* DecodeClass(const ScopedFastNativeObjectAccess& soa, jobject java_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes15216932012-03-21 21:53:06 -070035 DCHECK(c != NULL);
36 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070037 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
38 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
39 // every time probably doesn't make much difference to reflection performance anyway.
40 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070041}
42
Brian Carlstromf91c8c32011-09-21 17:30:34 -070043// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Ian Rogers98379392014-02-24 16:53:16 -080044static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize,
45 jobject javaLoader) {
Ian Rogers53b8b092014-03-13 23:45:53 -070046 ScopedFastNativeObjectAccess soa(env);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070047 ScopedUtfChars name(env, javaName);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080048 if (name.c_str() == nullptr) {
49 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070050 }
51
52 // We need to validate and convert the name (from x.y.z to x/y/z). This
53 // is especially handy for array types, since we want to avoid
54 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070055 if (!IsValidBinaryClassName(name.c_str())) {
Ian Rogers62d6c772013-02-27 08:32:07 -080056 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
57 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ClassNotFoundException;",
58 "Invalid name: %s", name.c_str());
Mathieu Chartierc528dba2013-11-26 12:00:11 -080059 return nullptr;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070060 }
61
62 std::string descriptor(DotToDescriptor(name.c_str()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070063 StackHandleScope<2> hs(soa.Self());
64 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Brian Carlstromf91c8c32011-09-21 17:30:34 -070065 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070066 Handle<mirror::Class> c(
67 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor.c_str(), class_loader)));
68 if (c.Get() == nullptr) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080069 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070070 env->ExceptionClear();
Elliott Hugheseac76672012-05-24 21:56:51 -070071 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
72 WellKnownClasses::java_lang_ClassNotFoundException_init,
73 javaName, cause.get()));
Ian Rogersc114b5f2014-07-21 08:55:01 -070074 if (cnfe != nullptr) {
75 // Make sure allocation didn't fail with an OOME.
76 env->Throw(cnfe);
77 }
Mathieu Chartierc528dba2013-11-26 12:00:11 -080078 return nullptr;
Brian Carlstrom395520e2011-09-25 19:35:00 -070079 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070080 if (initialize) {
Ian Rogers7b078e82014-09-10 14:44:24 -070081 class_linker->EnsureInitialized(soa.Self(), c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070082 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 return soa.AddLocalReference<jclass>(c.Get());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070084}
85
Elliott Hughes0512f022012-03-15 22:10:52 -070086static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070087 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartierf8322842014-05-16 10:59:25 -070088 StackHandleScope<1> hs(soa.Self());
89 mirror::Class* const c = DecodeClass(soa, javaThis);
90 return soa.AddLocalReference<jstring>(mirror::Class::ComputeName(hs.NewHandle(c)));
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070091}
92
Elliott Hughes2ed52c42012-03-21 16:56:56 -070093static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070094 ScopedFastNativeObjectAccess soa(env);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070095 mirror::Class* c = DecodeClass(soa, javaThis);
Ian Rogers50b35e22012-10-04 10:09:15 -070096 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
Elliott Hughes2ed52c42012-03-21 16:56:56 -070097}
98
Elliott Hughesd369bb72011-09-12 14:41:14 -070099static JNINativeMethod gMethods[] = {
Ian Rogers53b8b092014-03-13 23:45:53 -0700100 NATIVE_METHOD(Class, classForName, "!(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700101 NATIVE_METHOD(Class, getNameNative, "!()Ljava/lang/String;"),
102 NATIVE_METHOD(Class, getProxyInterfaces, "!()[Ljava/lang/Class;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700103};
104
Elliott Hughesd369bb72011-09-12 14:41:14 -0700105void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700106 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700107}
108
109} // namespace art