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