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 | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 23 | #include "scoped_jni_thread_state.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 | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 30 | static Class* DecodeClass(const ScopedJniThreadState& ts, jobject java_class) { |
| 31 | Class* c = ts.Decode<Class*>(java_class); |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 32 | DCHECK(c != NULL); |
| 33 | DCHECK(c->IsClass()); |
Elliott Hughes | 923e8b8 | 2012-03-23 11:44:07 -0700 | [diff] [blame] | 34 | // TODO: we could EnsureInitialized here, rather than on every reflective get/set or invoke . |
| 35 | // For now, we conservatively preserve the old dalvik behavior. A quick "IsInitialized" check |
| 36 | // every time probably doesn't make much difference to reflection performance anyway. |
| 37 | return c; |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 40 | // "name" is in "binary name" format, e.g. "dalvik.system.Debug$1". |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 41 | static jclass Class_classForName(JNIEnv* env, jclass, jstring javaName, jboolean initialize, jobject javaLoader) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 42 | ScopedJniThreadState ts(env); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 43 | ScopedUtfChars name(env, javaName); |
| 44 | if (name.c_str() == NULL) { |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | // We need to validate and convert the name (from x.y.z to x/y/z). This |
| 49 | // is especially handy for array types, since we want to avoid |
| 50 | // auto-generating bogus array classes. |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 51 | if (!IsValidBinaryClassName(name.c_str())) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 52 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 53 | "Invalid name: %s", name.c_str()); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | std::string descriptor(DotToDescriptor(name.c_str())); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 58 | ClassLoader* class_loader = ts.Decode<ClassLoader*>(javaLoader); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 59 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 60 | Class* c = class_linker->FindClass(descriptor.c_str(), class_loader); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 61 | if (c == NULL) { |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 62 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 63 | env->ExceptionClear(); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 64 | jthrowable cnfe = reinterpret_cast<jthrowable>(env->NewObject(WellKnownClasses::java_lang_ClassNotFoundException, |
| 65 | WellKnownClasses::java_lang_ClassNotFoundException_init, |
| 66 | javaName, cause.get())); |
Elliott Hughes | 844f9a0 | 2012-01-24 20:19:58 -0800 | [diff] [blame] | 67 | env->Throw(cnfe); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 70 | if (initialize) { |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 71 | class_linker->EnsureInitialized(c, true, true); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 72 | } |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 73 | return ts.AddLocalReference<jclass>(c); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 76 | static jint Class_getAnnotationDirectoryOffset(JNIEnv* env, jclass javaClass) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 77 | ScopedJniThreadState ts(env); |
| 78 | Class* c = DecodeClass(ts, javaClass); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 79 | if (c->IsPrimitive() || c->IsArrayClass() || c->IsProxyClass()) { |
| 80 | return 0; // primitive, array and proxy classes don't have class definitions |
| 81 | } |
| 82 | const DexFile::ClassDef* class_def = ClassHelper(c).GetClassDef(); |
| 83 | if (class_def == NULL) { |
| 84 | return 0; // not found |
| 85 | } else { |
| 86 | return class_def->annotations_off_; |
| 87 | } |
| 88 | } |
| 89 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 90 | template<typename T> |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 91 | static jobjectArray ToArray(const ScopedJniThreadState& ts, const char* array_class_name, |
| 92 | const std::vector<T*>& objects) { |
| 93 | ScopedLocalRef<jclass> array_class(ts.Env(), ts.Env()->FindClass(array_class_name)); |
| 94 | jobjectArray result = ts.Env()->NewObjectArray(objects.size(), array_class.get(), NULL); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 95 | for (size_t i = 0; i < objects.size(); ++i) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 96 | ScopedLocalRef<jobject> object(ts.Env(), ts.AddLocalReference<jobject>(objects[i])); |
| 97 | ts.Env()->SetObjectArrayElement(result, i, object.get()); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 98 | } |
| 99 | return result; |
| 100 | } |
| 101 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 102 | static bool IsVisibleConstructor(Method* m, bool public_only) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 103 | if (public_only && !m->IsPublic()) { |
| 104 | return false; |
| 105 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 106 | if (m->IsStatic()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 109 | return m->IsConstructor(); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 112 | static jobjectArray Class_getDeclaredConstructors(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 113 | ScopedJniThreadState ts(env); |
| 114 | Class* c = DecodeClass(ts, javaClass); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 115 | std::vector<Method*> constructors; |
| 116 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 117 | Method* m = c->GetDirectMethod(i); |
| 118 | if (IsVisibleConstructor(m, publicOnly)) { |
| 119 | constructors.push_back(m); |
| 120 | } |
| 121 | } |
| 122 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 123 | return ToArray(ts, "java/lang/reflect/Constructor", constructors); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 126 | static bool IsVisibleField(Field* f, bool public_only) { |
Elliott Hughes | c0dd312 | 2011-09-26 10:15:43 -0700 | [diff] [blame] | 127 | if (public_only && !f->IsPublic()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 128 | return false; |
| 129 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 133 | static jobjectArray Class_getDeclaredFields(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 134 | ScopedJniThreadState ts(env); |
| 135 | Class* c = DecodeClass(ts, javaClass); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 136 | std::vector<Field*> fields; |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 137 | FieldHelper fh; |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 138 | for (size_t i = 0; i < c->NumInstanceFields(); ++i) { |
| 139 | Field* f = c->GetInstanceField(i); |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 140 | fh.ChangeField(f); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 141 | if (IsVisibleField(f, publicOnly)) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 142 | if (fh.GetType() == NULL) { |
| 143 | DCHECK(env->ExceptionOccurred()); |
| 144 | return NULL; |
| 145 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 146 | fields.push_back(f); |
| 147 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 148 | if (env->ExceptionOccurred()) { |
| 149 | return NULL; |
| 150 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 151 | } |
| 152 | for (size_t i = 0; i < c->NumStaticFields(); ++i) { |
| 153 | Field* f = c->GetStaticField(i); |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 154 | fh.ChangeField(f); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 155 | if (IsVisibleField(f, publicOnly)) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 156 | if (fh.GetType() == NULL) { |
| 157 | DCHECK(env->ExceptionOccurred()); |
| 158 | return NULL; |
| 159 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 160 | fields.push_back(f); |
| 161 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 162 | if (env->ExceptionOccurred()) { |
| 163 | return NULL; |
| 164 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 167 | return ToArray(ts, "java/lang/reflect/Field", fields); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 170 | static bool IsVisibleMethod(Method* m, bool public_only) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 171 | if (public_only && !m->IsPublic()) { |
| 172 | return false; |
| 173 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 174 | if (m->IsConstructor()) { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 175 | return false; |
| 176 | } |
Ian Rogers | 5643742 | 2012-02-06 21:46:00 -0800 | [diff] [blame] | 177 | if (m->IsMiranda()) { |
| 178 | return false; |
| 179 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 183 | static jobjectArray Class_getDeclaredMethods(JNIEnv* env, jclass javaClass, jboolean publicOnly) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 184 | ScopedJniThreadState ts(env); |
| 185 | Class* c = DecodeClass(ts, javaClass); |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 186 | if (c == NULL) { |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 190 | std::vector<Method*> methods; |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 191 | MethodHelper mh; |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 192 | for (size_t i = 0; i < c->NumVirtualMethods(); ++i) { |
| 193 | Method* m = c->GetVirtualMethod(i); |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 194 | mh.ChangeMethod(m); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 195 | if (IsVisibleMethod(m, publicOnly)) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 196 | if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) { |
| 197 | DCHECK(env->ExceptionOccurred()); |
| 198 | return NULL; |
| 199 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 200 | methods.push_back(m); |
| 201 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 202 | if (env->ExceptionOccurred()) { |
| 203 | return NULL; |
| 204 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 205 | } |
| 206 | for (size_t i = 0; i < c->NumDirectMethods(); ++i) { |
| 207 | Method* m = c->GetDirectMethod(i); |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 208 | mh.ChangeMethod(m); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 209 | if (IsVisibleMethod(m, publicOnly)) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 210 | if (mh.GetReturnType() == NULL || mh.GetParameterTypes() == NULL) { |
| 211 | DCHECK(env->ExceptionOccurred()); |
| 212 | return NULL; |
| 213 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 214 | methods.push_back(m); |
| 215 | } |
Jesse Wilson | 5349431 | 2011-11-29 16:43:09 -0500 | [diff] [blame] | 216 | if (env->ExceptionOccurred()) { |
| 217 | return NULL; |
| 218 | } |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 221 | return ToArray(ts, "java/lang/reflect/Method", methods); |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 224 | static jobject Class_getDex(JNIEnv* env, jobject javaClass) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 225 | ScopedJniThreadState ts(env); |
| 226 | Class* c = DecodeClass(ts, javaClass); |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 227 | |
| 228 | DexCache* dex_cache = c->GetDexCache(); |
| 229 | if (dex_cache == NULL) { |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env); |
| 234 | } |
| 235 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 236 | static bool MethodMatches(MethodHelper* mh, const std::string& name, ObjectArray<Class>* arg_array) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 237 | if (name != mh->GetName()) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 238 | return false; |
| 239 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 240 | const DexFile::TypeList* m_type_list = mh->GetParameterTypeList(); |
| 241 | uint32_t m_type_list_size = m_type_list == NULL ? 0 : m_type_list->Size(); |
| 242 | uint32_t sig_length = arg_array->GetLength(); |
| 243 | |
| 244 | if (m_type_list_size != sig_length) { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 247 | |
| 248 | for (uint32_t i = 0; i < sig_length; i++) { |
| 249 | if (mh->GetClassFromTypeIdx(m_type_list->GetTypeItem(i).type_idx_) != arg_array->Get(i)) { |
| 250 | return false; |
| 251 | } |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 252 | } |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 256 | static Method* FindConstructorOrMethodInArray(ObjectArray<Method>* methods, const std::string& name, |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 257 | ObjectArray<Class>* arg_array) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 258 | if (methods == NULL) { |
| 259 | return NULL; |
| 260 | } |
| 261 | Method* result = NULL; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 262 | MethodHelper mh; |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 263 | for (int32_t i = 0; i < methods->GetLength(); ++i) { |
| 264 | Method* method = methods->Get(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 265 | mh.ChangeMethod(method); |
| 266 | if (method->IsMiranda() || !MethodMatches(&mh, name, arg_array)) { |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 267 | continue; |
| 268 | } |
| 269 | |
| 270 | result = method; |
| 271 | |
| 272 | // Covariant return types permit the class to define multiple |
| 273 | // methods with the same name and parameter types. Prefer to return |
| 274 | // a non-synthetic method in such situations. We may still return |
| 275 | // a synthetic method to handle situations like escalated visibility. |
| 276 | if (!method->IsSynthetic()) { |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | return result; |
| 281 | } |
| 282 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 283 | static jobject Class_getDeclaredConstructorOrMethod(JNIEnv* env, jclass javaClass, jstring javaName, |
| 284 | jobjectArray javaArgs) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 285 | ScopedJniThreadState ts(env); |
| 286 | Class* c = DecodeClass(ts, javaClass); |
| 287 | std::string name(ts.Decode<String*>(javaName)->ToModifiedUtf8()); |
| 288 | ObjectArray<Class>* arg_array = ts.Decode<ObjectArray<Class>*>(javaArgs); |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 289 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 290 | Method* m = FindConstructorOrMethodInArray(c->GetDirectMethods(), name, arg_array); |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 291 | if (m == NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 292 | m = FindConstructorOrMethodInArray(c->GetVirtualMethods(), name, arg_array); |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 295 | if (m != NULL) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 296 | return ts.AddLocalReference<jobject>(m); |
Jesse Wilson | 8ea36f8 | 2011-11-21 10:35:06 -0500 | [diff] [blame] | 297 | } else { |
| 298 | return NULL; |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 299 | } |
Brian Carlstrom | 3a7b4f2 | 2011-09-17 15:01:57 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 302 | static jobject Class_getDeclaredFieldNative(JNIEnv* env, jclass java_class, jobject jname) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 303 | ScopedJniThreadState ts(env); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 304 | Class* c = DecodeClass(ts, java_class); |
| 305 | String* name = ts.Decode<String*>(jname); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 306 | DCHECK(name->GetClass()->IsStringClass()); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 307 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 308 | FieldHelper fh; |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 309 | for (size_t i = 0; i < c->NumInstanceFields(); ++i) { |
| 310 | Field* f = c->GetInstanceField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 311 | fh.ChangeField(f); |
| 312 | if (name->Equals(fh.GetName())) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 313 | if (fh.GetType() == NULL) { |
| 314 | DCHECK(env->ExceptionOccurred()); |
| 315 | return NULL; |
| 316 | } |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 317 | return ts.AddLocalReference<jclass>(f); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 320 | for (size_t i = 0; i < c->NumStaticFields(); ++i) { |
| 321 | Field* f = c->GetStaticField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 322 | fh.ChangeField(f); |
| 323 | if (name->Equals(fh.GetName())) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 324 | if (fh.GetType() == NULL) { |
| 325 | DCHECK(env->ExceptionOccurred()); |
| 326 | return NULL; |
| 327 | } |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 328 | return ts.AddLocalReference<jclass>(f); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | return NULL; |
| 332 | } |
| 333 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 334 | static jstring Class_getNameNative(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 335 | ScopedJniThreadState ts(env); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 336 | Class* c = DecodeClass(ts, javaThis); |
| 337 | return ts.AddLocalReference<jstring>(c->ComputeName()); |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 340 | static jobjectArray Class_getProxyInterfaces(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 341 | ScopedJniThreadState ts(env); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 342 | SynthesizedProxyClass* c = down_cast<SynthesizedProxyClass*>(DecodeClass(ts, javaThis)); |
| 343 | return ts.AddLocalReference<jobjectArray>(c->GetInterfaces()->Clone()); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 346 | static jboolean Class_isAssignableFrom(JNIEnv* env, jobject javaLhs, jclass javaRhs) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 347 | ScopedJniThreadState ts(env); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 348 | Class* lhs = DecodeClass(ts, javaLhs); |
| 349 | Class* rhs = ts.Decode<Class*>(javaRhs); // Can be null. |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 350 | if (rhs == NULL) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 351 | ts.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "class == null"); |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 352 | return JNI_FALSE; |
| 353 | } |
| 354 | return lhs->IsAssignableFrom(rhs) ? JNI_TRUE : JNI_FALSE; |
| 355 | } |
| 356 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 357 | static jobject Class_newInstanceImpl(JNIEnv* env, jobject javaThis) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 358 | ScopedJniThreadState ts(env); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 359 | Class* c = DecodeClass(ts, javaThis); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 360 | if (c->IsPrimitive() || c->IsInterface() || c->IsArrayClass() || c->IsAbstract()) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 361 | ts.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 362 | "Class %s can not be instantiated", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 363 | return NULL; |
| 364 | } |
| 365 | |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 366 | if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(c, true, true)) { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 367 | return NULL; |
| 368 | } |
| 369 | |
Brian Carlstrom | 01a9658 | 2012-03-12 15:17:29 -0700 | [diff] [blame] | 370 | Method* init = c->FindDeclaredDirectMethod("<init>", "()V"); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 371 | if (init == NULL) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 372 | ts.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 373 | "Class %s has no default <init>()V constructor", PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | // Verify access from the call site. |
| 378 | // |
| 379 | // First, make sure the method invoking Class.newInstance() has permission |
| 380 | // to access the class. |
| 381 | // |
| 382 | // Second, make sure it has permission to invoke the constructor. The |
| 383 | // constructor must be public or, if the caller is in the same package, |
| 384 | // have package scope. |
Elliott Hughes | 6a14433 | 2012-04-03 13:07:11 -0700 | [diff] [blame] | 385 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 386 | NthCallerVisitor visitor(ts.Self()->GetManagedStack(), ts.Self()->GetTraceStack(), 2); |
| 387 | visitor.WalkStack(); |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 388 | Class* caller_class = visitor.caller->GetDeclaringClass(); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 389 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 390 | ClassHelper caller_ch(caller_class); |
Brian Carlstrom | bc2f3e3 | 2011-09-22 17:16:54 -0700 | [diff] [blame] | 391 | if (!caller_class->CanAccess(c)) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 392 | ts.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;", |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 393 | "Class %s is not accessible from class %s", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 394 | PrettyDescriptor(ClassHelper(c).GetDescriptor()).c_str(), |
| 395 | PrettyDescriptor(caller_ch.GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 396 | return NULL; |
| 397 | } |
Elliott Hughes | 26c5e15 | 2012-07-11 11:47:22 -0700 | [diff] [blame] | 398 | if (!caller_class->CanAccessMember(init->GetDeclaringClass(), init->GetAccessFlags())) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 399 | ts.Self()->ThrowNewExceptionF("Ljava/lang/IllegalAccessException;", |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 400 | "%s is not accessible from class %s", |
| 401 | PrettyMethod(init).c_str(), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 402 | PrettyDescriptor(caller_ch.GetDescriptor()).c_str()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | Object* new_obj = c->AllocObject(); |
| 407 | if (new_obj == NULL) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 408 | DCHECK(ts.Self()->IsExceptionPending()); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | // invoke constructor; unlike reflection calls, we don't wrap exceptions |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 413 | jclass java_class = ts.AddLocalReference<jclass>(c); |
| 414 | jmethodID mid = ts.EncodeMethod(init); |
Elliott Hughes | 1521693 | 2012-03-21 21:53:06 -0700 | [diff] [blame] | 415 | return env->NewObject(java_class, mid); |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 418 | static JNINativeMethod gMethods[] = { |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 419 | 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] | 420 | NATIVE_METHOD(Class, getAnnotationDirectoryOffset, "()I"), |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 421 | NATIVE_METHOD(Class, getDeclaredConstructorOrMethod, "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Member;"), |
| 422 | NATIVE_METHOD(Class, getDeclaredConstructors, "(Z)[Ljava/lang/reflect/Constructor;"), |
| 423 | NATIVE_METHOD(Class, getDeclaredFieldNative, "(Ljava/lang/String;)Ljava/lang/reflect/Field;"), |
| 424 | NATIVE_METHOD(Class, getDeclaredFields, "(Z)[Ljava/lang/reflect/Field;"), |
| 425 | NATIVE_METHOD(Class, getDeclaredMethods, "(Z)[Ljava/lang/reflect/Method;"), |
Jesse Wilson | 6bf1915 | 2011-09-29 13:12:33 -0400 | [diff] [blame] | 426 | NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"), |
Elliott Hughes | 6bdc3b2 | 2011-09-16 19:24:10 -0700 | [diff] [blame] | 427 | NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"), |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 428 | NATIVE_METHOD(Class, getProxyInterfaces, "()[Ljava/lang/Class;"), |
Elliott Hughes | dd8df69 | 2011-09-23 14:42:41 -0700 | [diff] [blame] | 429 | NATIVE_METHOD(Class, isAssignableFrom, "(Ljava/lang/Class;)Z"), |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 430 | NATIVE_METHOD(Class, newInstanceImpl, "()Ljava/lang/Object;"), |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 431 | }; |
| 432 | |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 433 | void register_java_lang_Class(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 434 | REGISTER_NATIVE_METHODS("java/lang/Class"); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | } // namespace art |