blob: d3011cb01322fa21811be4a06a0747f53163cef9 [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"
24#include "mirror/proxy.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080025#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070026#include "scoped_thread_state_change.h"
Elliott Hughes80609252011-09-23 17:24:51 -070027#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070028#include "ScopedUtfChars.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070029#include "well_known_classes.h"
Elliott Hughesd369bb72011-09-12 14:41:14 -070030
31namespace art {
32
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033static mirror::Class* DecodeClass(const ScopedObjectAccess& soa, jobject java_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 mirror::Class* c = soa.Decode<mirror::Class*>(java_class);
Elliott Hughes15216932012-03-21 21:53:06 -070036 DCHECK(c != NULL);
37 DCHECK(c->IsClass());
Elliott Hughes923e8b82012-03-23 11:44:07 -070038 // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke .
39 // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check
40 // every time probably doesn't make much difference to reflection performance anyway.
41 return c;
Elliott Hughes15216932012-03-21 21:53:06 -070042}
43
Brian Carlstromf91c8c32011-09-21 17:30:34 -070044// "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
Elliott Hughes0512f022012-03-15 22:10:52 -070045static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 ScopedObjectAccess soa(env);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070047 ScopedUtfChars name(env, javaName);
48 if (name.c_str() == NULL) {
49 return NULL;
50 }
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());
Brian Carlstromf91c8c32011-09-21 17:30:34 -070059 return NULL;
60 }
61
62 std::string descriptor(DotToDescriptor(name.c_str()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070064 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065 mirror::Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstrom395520e2011-09-25 19:35:00 -070066 if (c == NULL) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080067 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070068 env->ExceptionClear();
Elliott Hugheseac76672012-05-24 21:56:51 -070069 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
70 WellKnownClasses::java_lang_ClassNotFoundException_init,
71 javaName, cause.get()));
Elliott Hughes844f9a02012-01-24 20:19:58 -080072 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070073 return NULL;
74 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075 if (initialize) {
Ian Rogers0045a292012-03-31 21:08:41 -070076 class_linker->EnsureInitialized(c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070077 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 return soa.AddLocalReference<jclass>(c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070079}
80
Elliott Hughes0512f022012-03-15 22:10:52 -070081static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 mirror::Class* c = DecodeClass(soa, javaThis);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 return soa.AddLocalReference<jstring>(c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070085}
86
Elliott Hughes2ed52c42012-03-21 16:56:56 -070087static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 mirror::SynthesizedProxyClass* c =
90 down_cast<mirror::SynthesizedProxyClass*>(DecodeClass(soa, javaThis));
Ian Rogers50b35e22012-10-04 10:09:15 -070091 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
Elliott Hughes2ed52c42012-03-21 16:56:56 -070092}
93
Elliott Hughesd369bb72011-09-12 14:41:14 -070094static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070095 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -070096 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -070097 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -070098};
99
Elliott Hughesd369bb72011-09-12 14:41:14 -0700100void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700101 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700102}
103
104} // namespace art