blob: dded7872148f940a49bd4e18c023822b18185567 [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"
Elliott Hugheseac76672012-05-24 21:56:51 -070018#include "jni_internal.h"
Elliott Hughes6a144332012-04-03 13:07:11 -070019#include "nth_caller_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/class.h"
21#include "mirror/class_loader.h"
22#include "mirror/object-inl.h"
23#include "mirror/proxy.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080024#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.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 Rogers2dd0e2c2013-01-24 12:42:14 -080032static mirror::Class* DecodeClass(const ScopedObjectAccess& 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".
Elliott Hughes0512f022012-03-15 22:10:52 -070044static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 ScopedObjectAccess soa(env);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070046 ScopedUtfChars name(env, javaName);
47 if (name.c_str() == NULL) {
48 return NULL;
49 }
50
51 // We need to validate and convert the name (from x.y.z to x/y/z). This
52 // is especially handy for array types, since we want to avoid
53 // auto-generating bogus array classes.
Elliott Hughes906e6852011-10-28 14:52:10 -070054 if (!IsValidBinaryClassName(name.c_str())) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -070055 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;",
Brian Carlstromf91c8c32011-09-21 17:30:34 -070056 "Invalid name: %s", name.c_str());
57 return NULL;
58 }
59
60 std::string descriptor(DotToDescriptor(name.c_str()));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 mirror::ClassLoader* class_loader = soa.Decode<mirror::ClassLoader*>(javaLoader);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070062 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::Class* c = class_linker->FindClass(descriptor.c_str(), class_loader);
Brian Carlstrom395520e2011-09-25 19:35:00 -070064 if (c == NULL) {
Elliott Hughes844f9a02012-01-24 20:19:58 -080065 ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred());
Brian Carlstrom395520e2011-09-25 19:35:00 -070066 env->ExceptionClear();
Elliott Hugheseac76672012-05-24 21:56:51 -070067 jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException,
68 WellKnownClasses::java_lang_ClassNotFoundException_init,
69 javaName, cause.get()));
Elliott Hughes844f9a02012-01-24 20:19:58 -080070 env->Throw(cnfe);
Brian Carlstrom395520e2011-09-25 19:35:00 -070071 return NULL;
72 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070073 if (initialize) {
Ian Rogers0045a292012-03-31 21:08:41 -070074 class_linker->EnsureInitialized(c, true, true);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 return soa.AddLocalReference<jclass>(c);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070077}
78
Elliott Hughes0512f022012-03-15 22:10:52 -070079static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070080 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 mirror::Class* c = DecodeClass(soa, javaClass);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080082 if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) {
83 return 0; // primitive, array and proxy classes don't have class definitions
84 }
85 const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef();
86 if (class_def == NULL) {
87 return 0; // not found
88 } else {
89 return class_def->annotations_off_;
90 }
91}
92
Elliott Hughes0512f022012-03-15 22:10:52 -070093static jobject Class_getDex(JNIEnv* env, jobject javaClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070094 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 mirror::Class* c = DecodeClass(soa, javaClass);
Jesse Wilson6bf19152011-09-29 13:12:33 -040096
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 mirror::DexCache* dex_cache = c->GetDexCache();
Jesse Wilson6bf19152011-09-29 13:12:33 -040098 if (dex_cache == NULL) {
99 return NULL;
100 }
Ian Rogers4445a7e2012-10-05 17:19:13 -0700101 const DexFile* dex_file = dex_cache->GetDexFile();
102 if (dex_file == NULL) {
Jesse Wilson8ea36f82011-11-21 10:35:06 -0500103 return NULL;
104 }
Ian Rogers4445a7e2012-10-05 17:19:13 -0700105 return dex_file->GetDexObject(env);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700106}
107
Elliott Hughes0512f022012-03-15 22:10:52 -0700108static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700109 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::Class* c = DecodeClass(soa, javaThis);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 return soa.AddLocalReference<jstring>(c->ComputeName());
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700112}
113
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700114static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 mirror::SynthesizedProxyClass* c =
117 down_cast<mirror::SynthesizedProxyClass*>(DecodeClass(soa, javaThis));
Ian Rogers50b35e22012-10-04 10:09:15 -0700118 return soa.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone(soa.Self()));
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700119}
120
Elliott Hughesd369bb72011-09-12 14:41:14 -0700121static JNINativeMethod gMethods[] = {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700122 NATIVE_METHOD(Class, classForName, "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800123 NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"),
Jesse Wilson6bf19152011-09-29 13:12:33 -0400124 NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
Elliott Hughes6bdc3b22011-09-16 19:24:10 -0700125 NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700126 NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"),
Elliott Hughesd369bb72011-09-12 14:41:14 -0700127};
128
Elliott Hughesd369bb72011-09-12 14:41:14 -0700129void register_java_lang_Class(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700130 REGISTER_NATIVE_METHODS("java/lang/Class");
Elliott Hughesd369bb72011-09-12 14:41:14 -0700131}
132
133} // namespace art