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