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