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