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