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