Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_CLASS_INL_H_ |
| 18 | #define ART_RUNTIME_MIRROR_CLASS_INL_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | |
| 20 | #include "class.h" |
| 21 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 22 | #include "art_field.h" |
| 23 | #include "art_method.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 24 | #include "class_loader.h" |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 25 | #include "common_throws.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "dex_cache.h" |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 27 | #include "gc/heap-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "iftable.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 29 | #include "object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "runtime.h" |
| 31 | #include "string.h" |
| 32 | |
| 33 | namespace art { |
| 34 | namespace mirror { |
| 35 | |
| 36 | inline size_t Class::GetObjectSize() const { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 37 | DCHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 39 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | inline Class* Class::GetSuperClass() const { |
| 43 | // Can only get super class for loaded classes (hack for when runtime is |
| 44 | // initializing) |
| 45 | DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded(); |
| 46 | return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false); |
| 47 | } |
| 48 | |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 49 | inline ClassLoader* Class::GetClassLoader() const { |
| 50 | return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
| 51 | } |
| 52 | |
| 53 | inline DexCache* Class::GetDexCache() const { |
| 54 | return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
| 55 | } |
| 56 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 57 | inline ObjectArray<ArtMethod>* Class::GetDirectMethods() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | DCHECK(IsLoaded() || IsErroneous()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 59 | return GetFieldObject<ObjectArray<ArtMethod>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false); |
| 61 | } |
| 62 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 63 | inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 65 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 66 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false)); |
| 67 | DCHECK_NE(0, new_direct_methods->GetLength()); |
| 68 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), |
| 69 | new_direct_methods, false); |
| 70 | } |
| 71 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 72 | inline ArtMethod* Class::GetDirectMethod(int32_t i) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 73 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 74 | return GetDirectMethods()->Get(i); |
| 75 | } |
| 76 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 77 | inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 78 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 79 | ObjectArray<ArtMethod>* direct_methods = |
| 80 | GetFieldObject<ObjectArray<ArtMethod>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 81 | OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false); |
| 82 | direct_methods->Set(i, f); |
| 83 | } |
| 84 | |
| 85 | // Returns the number of static, private, and constructor methods. |
| 86 | inline size_t Class::NumDirectMethods() const { |
| 87 | return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0; |
| 88 | } |
| 89 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 90 | inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | DCHECK(IsLoaded() || IsErroneous()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 92 | return GetFieldObject<ObjectArray<ArtMethod>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false); |
| 94 | } |
| 95 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 96 | inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | // TODO: we reassign virtual methods to grow the table for miranda |
| 98 | // methods.. they should really just be assigned once |
| 99 | DCHECK_NE(0, new_virtual_methods->GetLength()); |
| 100 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), |
| 101 | new_virtual_methods, false); |
| 102 | } |
| 103 | |
| 104 | inline size_t Class::NumVirtualMethods() const { |
| 105 | return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0; |
| 106 | } |
| 107 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 108 | inline ArtMethod* Class::GetVirtualMethod(uint32_t i) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 110 | DCHECK(IsResolved() || IsErroneous()); |
| 111 | return GetVirtualMethods()->Get(i); |
| 112 | } |
| 113 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 114 | inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 116 | DCHECK(IsLoaded() || IsErroneous()); |
| 117 | return GetVirtualMethods()->Get(i); |
| 118 | } |
| 119 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 120 | inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 122 | ObjectArray<ArtMethod>* virtual_methods = |
| 123 | GetFieldObject<ObjectArray<ArtMethod>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 124 | OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false); |
| 125 | virtual_methods->Set(i, f); |
| 126 | } |
| 127 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 128 | inline ObjectArray<ArtMethod>* Class::GetVTable() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | DCHECK(IsResolved() || IsErroneous()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 130 | return GetFieldObject<ObjectArray<ArtMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 133 | inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 134 | DCHECK(IsLoaded() || IsErroneous()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 135 | return GetFieldObject<ObjectArray<ArtMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 138 | inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 139 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 140 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false); |
| 141 | } |
| 142 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 143 | inline ObjectArray<ArtMethod>* Class::GetImTable() const { |
| 144 | return GetFieldObject<ObjectArray<ArtMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), false); |
| 145 | } |
| 146 | |
| 147 | inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) { |
| 148 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable, false); |
| 149 | } |
| 150 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 151 | inline bool Class::Implements(const Class* klass) const { |
| 152 | DCHECK(klass != NULL); |
| 153 | DCHECK(klass->IsInterface()) << PrettyClass(this); |
| 154 | // All interfaces implemented directly and by our superclass, and |
| 155 | // recursively all super-interfaces of those interfaces, are listed |
| 156 | // in iftable_, so we can just do a linear scan through that. |
| 157 | int32_t iftable_count = GetIfTableCount(); |
| 158 | IfTable* iftable = GetIfTable(); |
| 159 | for (int32_t i = 0; i < iftable_count; i++) { |
| 160 | if (iftable->GetInterface(i) == klass) { |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // Determine whether "this" is assignable from "src", where both of these |
| 168 | // are array classes. |
| 169 | // |
| 170 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 171 | // Y[][] = Y[][] --> true (identity) |
| 172 | // X[][] = Y[][] --> true (element superclass) |
| 173 | // Y = Y[][] --> false |
| 174 | // Y[] = Y[][] --> false |
| 175 | // Object = Y[][] --> true (everything is an object) |
| 176 | // Object[] = Y[][] --> true |
| 177 | // Object[][] = Y[][] --> true |
| 178 | // Object[][][] = Y[][] --> false (too many []s) |
| 179 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 180 | // Serializable[] = Y[][] --> true |
| 181 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 182 | // |
| 183 | // Don't forget about primitive types. |
| 184 | // Object[] = int[] --> false |
| 185 | // |
| 186 | inline bool Class::IsArrayAssignableFromArray(const Class* src) const { |
| 187 | DCHECK(IsArrayClass()) << PrettyClass(this); |
| 188 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 189 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
| 190 | } |
| 191 | |
| 192 | inline bool Class::IsAssignableFromArray(const Class* src) const { |
| 193 | DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom |
| 194 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
| 195 | if (!IsArrayClass()) { |
| 196 | // If "this" is not also an array, it must be Object. |
| 197 | // src's super should be java_lang_Object, since it is an array. |
| 198 | Class* java_lang_Object = src->GetSuperClass(); |
| 199 | DCHECK(java_lang_Object != NULL) << PrettyClass(src); |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 200 | DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src); |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 201 | return this == java_lang_Object; |
| 202 | } |
| 203 | return IsArrayAssignableFromArray(src); |
| 204 | } |
| 205 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame^] | 206 | template <bool throw_on_failure, bool use_referrers_cache> |
| 207 | inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field, |
| 208 | uint32_t field_idx, DexCache* dex_cache) { |
| 209 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 210 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 211 | // The referrer class can't access the field's declaring class but may still be able |
| 212 | // to access the field if the FieldId specifies an accessible subclass of the declaring |
| 213 | // class rather than the declaring class itself. |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame^] | 214 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 215 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_; |
| 216 | // The referenced class has already been resolved with the field, get it from the dex cache. |
| 217 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 218 | DCHECK(dex_access_to != nullptr); |
| 219 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 220 | if (throw_on_failure) { |
| 221 | ThrowIllegalAccessErrorClass(this, dex_access_to); |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()), |
| 226 | this->CanAccessMember(dex_access_to, field->GetAccessFlags())); |
| 227 | } |
| 228 | if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) { |
| 229 | return true; |
| 230 | } |
| 231 | if (throw_on_failure) { |
| 232 | ThrowIllegalAccessErrorField(this, field); |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame^] | 237 | template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type> |
| 238 | inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method, |
| 239 | uint32_t method_idx, DexCache* dex_cache) { |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 240 | COMPILE_ASSERT(throw_on_failure || throw_invoke_type == kStatic, non_default_throw_invoke_type); |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame^] | 241 | DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 242 | if (UNLIKELY(!this->CanAccess(access_to))) { |
| 243 | // The referrer class can't access the method's declaring class but may still be able |
| 244 | // to access the method if the MethodId specifies an accessible subclass of the declaring |
| 245 | // class rather than the declaring class itself. |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame^] | 246 | DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; |
Vladimir Marko | 23a2821 | 2014-01-09 19:24:37 +0000 | [diff] [blame] | 247 | uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_; |
| 248 | // The referenced class has already been resolved with the method, get it from the dex cache. |
| 249 | Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); |
| 250 | DCHECK(dex_access_to != nullptr); |
| 251 | if (UNLIKELY(!this->CanAccess(dex_access_to))) { |
| 252 | if (throw_on_failure) { |
| 253 | ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to, |
| 254 | method, throw_invoke_type); |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()), |
| 259 | this->CanAccessMember(dex_access_to, method->GetAccessFlags())); |
| 260 | } |
| 261 | if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) { |
| 262 | return true; |
| 263 | } |
| 264 | if (throw_on_failure) { |
| 265 | ThrowIllegalAccessErrorMethod(this, method); |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | |
Vladimir Marko | 8978643 | 2014-01-31 15:03:55 +0000 | [diff] [blame^] | 270 | inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field, |
| 271 | DexCache& dex_cache, uint32_t field_idx) { |
| 272 | return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, &dex_cache); |
| 273 | } |
| 274 | |
| 275 | inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field, |
| 276 | uint32_t field_idx) { |
| 277 | return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr); |
| 278 | } |
| 279 | |
| 280 | inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method, |
| 281 | DexCache& dex_cache, uint32_t method_idx) { |
| 282 | return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, &dex_cache); |
| 283 | } |
| 284 | |
| 285 | template <InvokeType throw_invoke_type> |
| 286 | inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method, |
| 287 | uint32_t method_idx) { |
| 288 | return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx, |
| 289 | nullptr); |
| 290 | } |
| 291 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 292 | inline bool Class::IsSubClass(const Class* klass) const { |
| 293 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 294 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
| 295 | const Class* current = this; |
| 296 | do { |
| 297 | if (current == klass) { |
| 298 | return true; |
| 299 | } |
| 300 | current = current->GetSuperClass(); |
| 301 | } while (current != NULL); |
| 302 | return false; |
| 303 | } |
| 304 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 305 | inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) const { |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 306 | Class* declaring_class = method->GetDeclaringClass(); |
| 307 | DCHECK(declaring_class != NULL) << PrettyClass(this); |
| 308 | DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); |
| 309 | // TODO cache to improve lookup speed |
| 310 | int32_t iftable_count = GetIfTableCount(); |
| 311 | IfTable* iftable = GetIfTable(); |
| 312 | for (int32_t i = 0; i < iftable_count; i++) { |
| 313 | if (iftable->GetInterface(i) == declaring_class) { |
| 314 | return iftable->GetMethodArray(i)->Get(method->GetMethodIndex()); |
| 315 | } |
| 316 | } |
| 317 | return NULL; |
| 318 | } |
| 319 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 320 | inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 321 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 322 | DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 323 | // The argument method may from a super class. |
| 324 | // Use the index to a potentially overridden one for this instance's class. |
| 325 | return GetVTable()->Get(method->GetMethodIndex()); |
| 326 | } |
| 327 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 328 | inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 329 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 330 | DCHECK(!method->GetDeclaringClass()->IsInterface()); |
| 331 | return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex()); |
| 332 | } |
| 333 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 334 | inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 335 | if (method->IsDirect()) { |
| 336 | return method; |
| 337 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 338 | if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 339 | return FindVirtualMethodForInterface(method); |
| 340 | } |
| 341 | return FindVirtualMethodForVirtual(method); |
| 342 | } |
| 343 | |
| 344 | inline IfTable* Class::GetIfTable() const { |
| 345 | return GetFieldObject<IfTable*>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false); |
| 346 | } |
| 347 | |
| 348 | inline int32_t Class::GetIfTableCount() const { |
| 349 | IfTable* iftable = GetIfTable(); |
| 350 | if (iftable == NULL) { |
| 351 | return 0; |
| 352 | } |
| 353 | return iftable->Count(); |
| 354 | } |
| 355 | |
| 356 | inline void Class::SetIfTable(IfTable* new_iftable) { |
| 357 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false); |
| 358 | } |
| 359 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 360 | inline ObjectArray<ArtField>* Class::GetIFields() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 361 | DCHECK(IsLoaded() || IsErroneous()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 362 | return GetFieldObject<ObjectArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 365 | inline void Class::SetIFields(ObjectArray<ArtField>* new_ifields) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 366 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 367 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 368 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false)); |
| 369 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false); |
| 370 | } |
| 371 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 372 | inline ObjectArray<ArtField>* Class::GetSFields() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 373 | DCHECK(IsLoaded() || IsErroneous()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 374 | return GetFieldObject<ObjectArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 377 | inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 378 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 379 | DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 380 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false)); |
| 381 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false); |
| 382 | } |
| 383 | |
| 384 | inline size_t Class::NumStaticFields() const { |
| 385 | return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0; |
| 386 | } |
| 387 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 388 | inline ArtField* Class::GetStaticField(uint32_t i) const // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 389 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 390 | return GetSFields()->Get(i); |
| 391 | } |
| 392 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 393 | inline void Class::SetStaticField(uint32_t i, ArtField* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 394 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 395 | ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 396 | OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false); |
| 397 | sfields->Set(i, f); |
| 398 | } |
| 399 | |
| 400 | inline size_t Class::NumInstanceFields() const { |
| 401 | return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0; |
| 402 | } |
| 403 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 404 | inline ArtField* Class::GetInstanceField(uint32_t i) const // TODO: uint16_t |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 405 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 406 | DCHECK_NE(NumInstanceFields(), 0U); |
| 407 | return GetIFields()->Get(i); |
| 408 | } |
| 409 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 410 | inline void Class::SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 411 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 412 | ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField>*>( |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 413 | OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false); |
| 414 | ifields->Set(i, f); |
| 415 | } |
| 416 | |
| 417 | inline void Class::SetVerifyErrorClass(Class* klass) { |
| 418 | CHECK(klass != NULL) << PrettyClass(this); |
| 419 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false); |
| 420 | } |
| 421 | |
| 422 | inline uint32_t Class::GetAccessFlags() const { |
| 423 | // Check class is loaded or this is java.lang.String that has a |
| 424 | // circularity issue during loading the names of its members |
| 425 | DCHECK(IsLoaded() || IsErroneous() || |
| 426 | this == String::GetJavaLangString() || |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 427 | this == ArtField::GetJavaLangReflectArtField() || |
| 428 | this == ArtMethod::GetJavaLangReflectArtMethod()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 429 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false); |
| 430 | } |
| 431 | |
| 432 | inline String* Class::GetName() const { |
| 433 | return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false); |
| 434 | } |
| 435 | inline void Class::SetName(String* name) { |
| 436 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false); |
| 437 | } |
| 438 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 439 | inline void Class::CheckObjectAlloc() { |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 440 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
| 441 | DCHECK(IsInstantiable()) << PrettyClass(this); |
| 442 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 443 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
| 444 | DCHECK_GE(this->object_size_, sizeof(Object)); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 447 | template <bool kIsInstrumented> |
| 448 | inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 449 | CheckObjectAlloc(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 450 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 451 | return heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, |
| 452 | allocator_type); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | inline Object* Class::AllocObject(Thread* self) { |
| 456 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); |
| 457 | } |
| 458 | |
| 459 | inline Object* Class::AllocNonMovableObject(Thread* self) { |
| 460 | return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); |
Hiroshi Yamauchi | 967a0ad | 2013-09-10 16:24:21 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 463 | } // namespace mirror |
| 464 | } // namespace art |
| 465 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 466 | #endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ |