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