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