class-inl.h revision 98d1cc8033251c93786e2fa8c59a2e555a9493be
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 17#ifndef ART_RUNTIME_MIRROR_CLASS_INL_H_ 18#define ART_RUNTIME_MIRROR_CLASS_INL_H_ 19 20#include "class.h" 21 22#include "art_field-inl.h" 23#include "art_method-inl.h" 24#include "class_linker-inl.h" 25#include "class_loader.h" 26#include "common_throws.h" 27#include "dex_cache.h" 28#include "dex_file.h" 29#include "gc/heap-inl.h" 30#include "iftable.h" 31#include "object_array-inl.h" 32#include "read_barrier-inl.h" 33#include "runtime.h" 34#include "string.h" 35 36namespace art { 37namespace mirror { 38 39template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 40inline uint32_t Class::GetObjectSize() { 41 if (kIsDebugBuild) { 42 // Use a local variable as (D)CHECK can't handle the space between 43 // the two template params. 44 bool is_variable_size = IsVariableSize<kVerifyFlags, kReadBarrierOption>(); 45 CHECK(!is_variable_size) << " class=" << PrettyTypeOf(this); 46 } 47 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_)); 48} 49 50inline Class* Class::GetSuperClass() { 51 // Can only get super class for loaded classes (hack for when runtime is 52 // initializing) 53 DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded(); 54 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_)); 55} 56 57inline ClassLoader* Class::GetClassLoader() { 58 return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_)); 59} 60 61template<VerifyObjectFlags kVerifyFlags> 62inline DexCache* Class::GetDexCache() { 63 return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_)); 64} 65 66inline ObjectArray<ArtMethod>* Class::GetDirectMethods() { 67 DCHECK(IsLoaded() || IsErroneous()); 68 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)); 69} 70 71inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods) 72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 73 DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod>>( 74 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_))); 75 DCHECK_NE(0, new_direct_methods->GetLength()); 76 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), new_direct_methods); 77} 78 79inline ArtMethod* Class::GetDirectMethod(int32_t i) { 80 return GetDirectMethods()->Get(i); 81} 82 83inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t 84 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 85 ObjectArray<ArtMethod>* direct_methods = 86 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)); 87 direct_methods->Set<false>(i, f); 88} 89 90// Returns the number of static, private, and constructor methods. 91inline uint32_t Class::NumDirectMethods() { 92 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0; 93} 94 95template<VerifyObjectFlags kVerifyFlags> 96inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() { 97 DCHECK(IsLoaded() || IsErroneous()); 98 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_)); 99} 100 101inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) { 102 // TODO: we reassign virtual methods to grow the table for miranda 103 // methods.. they should really just be assigned once. 104 DCHECK_NE(0, new_virtual_methods->GetLength()); 105 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), new_virtual_methods); 106} 107 108inline uint32_t Class::NumVirtualMethods() { 109 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0; 110} 111 112template<VerifyObjectFlags kVerifyFlags> 113inline ArtMethod* Class::GetVirtualMethod(uint32_t i) { 114 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); 115 return GetVirtualMethods()->Get(i); 116} 117 118inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) { 119 DCHECK(IsLoaded() || IsErroneous()); 120 return GetVirtualMethods()->Get(i); 121} 122 123inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t 124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 125 ObjectArray<ArtMethod>* virtual_methods = 126 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_)); 127 virtual_methods->Set<false>(i, f); 128} 129 130inline ObjectArray<ArtMethod>* Class::GetVTable() { 131 DCHECK(IsResolved() || IsErroneous()); 132 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); 133} 134 135inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() { 136 DCHECK(IsLoaded() || IsErroneous()); 137 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_)); 138} 139 140inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) { 141 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable); 142} 143 144inline ObjectArray<ArtMethod>* Class::GetImTable() { 145 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_)); 146} 147 148inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) { 149 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable); 150} 151 152inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i) { 153 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry); 154 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset)); 155} 156 157inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method) { 158 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry); 159 SetFieldObject<false>(MemberOffset(offset), method); 160 CHECK(method == GetImTable()->Get(i)); 161} 162 163inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method) { 164 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry); 165 SetFieldObject<false>(MemberOffset(offset), method); 166 CHECK(method == GetVTableDuringLinking()->Get(i)); 167} 168 169inline bool Class::Implements(Class* klass) { 170 DCHECK(klass != NULL); 171 DCHECK(klass->IsInterface()) << PrettyClass(this); 172 // All interfaces implemented directly and by our superclass, and 173 // recursively all super-interfaces of those interfaces, are listed 174 // in iftable_, so we can just do a linear scan through that. 175 int32_t iftable_count = GetIfTableCount(); 176 IfTable* iftable = GetIfTable(); 177 for (int32_t i = 0; i < iftable_count; i++) { 178 if (iftable->GetInterface(i) == klass) { 179 return true; 180 } 181 } 182 return false; 183} 184 185// Determine whether "this" is assignable from "src", where both of these 186// are array classes. 187// 188// Consider an array class, e.g. Y[][], where Y is a subclass of X. 189// Y[][] = Y[][] --> true (identity) 190// X[][] = Y[][] --> true (element superclass) 191// Y = Y[][] --> false 192// Y[] = Y[][] --> false 193// Object = Y[][] --> true (everything is an object) 194// Object[] = Y[][] --> true 195// Object[][] = Y[][] --> true 196// Object[][][] = Y[][] --> false (too many []s) 197// Serializable = Y[][] --> true (all arrays are Serializable) 198// Serializable[] = Y[][] --> true 199// Serializable[][] = Y[][] --> false (unless Y is Serializable) 200// 201// Don't forget about primitive types. 202// Object[] = int[] --> false 203// 204inline bool Class::IsArrayAssignableFromArray(Class* src) { 205 DCHECK(IsArrayClass()) << PrettyClass(this); 206 DCHECK(src->IsArrayClass()) << PrettyClass(src); 207 return GetComponentType()->IsAssignableFrom(src->GetComponentType()); 208} 209 210inline bool Class::IsAssignableFromArray(Class* src) { 211 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom 212 DCHECK(src->IsArrayClass()) << PrettyClass(src); 213 if (!IsArrayClass()) { 214 // If "this" is not also an array, it must be Object. 215 // src's super should be java_lang_Object, since it is an array. 216 Class* java_lang_Object = src->GetSuperClass(); 217 DCHECK(java_lang_Object != NULL) << PrettyClass(src); 218 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src); 219 return this == java_lang_Object; 220 } 221 return IsArrayAssignableFromArray(src); 222} 223 224template <bool throw_on_failure, bool use_referrers_cache> 225inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field, 226 uint32_t field_idx, DexCache* dex_cache) { 227 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); 228 if (UNLIKELY(!this->CanAccess(access_to))) { 229 // The referrer class can't access the field's declaring class but may still be able 230 // to access the field if the FieldId specifies an accessible subclass of the declaring 231 // class rather than the declaring class itself. 232 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; 233 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_; 234 // The referenced class has already been resolved with the field, get it from the dex cache. 235 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); 236 DCHECK(dex_access_to != nullptr); 237 if (UNLIKELY(!this->CanAccess(dex_access_to))) { 238 if (throw_on_failure) { 239 ThrowIllegalAccessErrorClass(this, dex_access_to); 240 } 241 return false; 242 } 243 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()), 244 this->CanAccessMember(dex_access_to, field->GetAccessFlags())); 245 } 246 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) { 247 return true; 248 } 249 if (throw_on_failure) { 250 ThrowIllegalAccessErrorField(this, field); 251 } 252 return false; 253} 254 255template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type> 256inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method, 257 uint32_t method_idx, DexCache* dex_cache) { 258 COMPILE_ASSERT(throw_on_failure || throw_invoke_type == kStatic, non_default_throw_invoke_type); 259 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr); 260 if (UNLIKELY(!this->CanAccess(access_to))) { 261 // The referrer class can't access the method's declaring class but may still be able 262 // to access the method if the MethodId specifies an accessible subclass of the declaring 263 // class rather than the declaring class itself. 264 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache; 265 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_; 266 // The referenced class has already been resolved with the method, get it from the dex cache. 267 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx); 268 DCHECK(dex_access_to != nullptr); 269 if (UNLIKELY(!this->CanAccess(dex_access_to))) { 270 if (throw_on_failure) { 271 ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to, 272 method, throw_invoke_type); 273 } 274 return false; 275 } 276 DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()), 277 this->CanAccessMember(dex_access_to, method->GetAccessFlags())); 278 } 279 if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) { 280 return true; 281 } 282 if (throw_on_failure) { 283 ThrowIllegalAccessErrorMethod(this, method); 284 } 285 return false; 286} 287 288inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field, 289 DexCache* dex_cache, uint32_t field_idx) { 290 return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache); 291} 292 293inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field, 294 uint32_t field_idx) { 295 return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr); 296} 297 298inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method, 299 DexCache* dex_cache, uint32_t method_idx) { 300 return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache); 301} 302 303template <InvokeType throw_invoke_type> 304inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method, 305 uint32_t method_idx) { 306 return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx, 307 nullptr); 308} 309 310inline bool Class::IsSubClass(Class* klass) { 311 DCHECK(!IsInterface()) << PrettyClass(this); 312 DCHECK(!IsArrayClass()) << PrettyClass(this); 313 Class* current = this; 314 do { 315 if (current == klass) { 316 return true; 317 } 318 current = current->GetSuperClass(); 319 } while (current != NULL); 320 return false; 321} 322 323inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) { 324 Class* declaring_class = method->GetDeclaringClass(); 325 DCHECK(declaring_class != NULL) << PrettyClass(this); 326 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); 327 // TODO cache to improve lookup speed 328 int32_t iftable_count = GetIfTableCount(); 329 IfTable* iftable = GetIfTable(); 330 for (int32_t i = 0; i < iftable_count; i++) { 331 if (iftable->GetInterface(i) == declaring_class) { 332 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex()); 333 } 334 } 335 return NULL; 336} 337 338inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) { 339 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda()); 340 // The argument method may from a super class. 341 // Use the index to a potentially overridden one for this instance's class. 342 return GetVTable()->Get(method->GetMethodIndex()); 343} 344 345inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) { 346 DCHECK(!method->GetDeclaringClass()->IsInterface()); 347 return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex()); 348} 349 350inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) { 351 if (method->IsDirect()) { 352 return method; 353 } 354 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) { 355 return FindVirtualMethodForInterface(method); 356 } 357 return FindVirtualMethodForVirtual(method); 358} 359 360inline IfTable* Class::GetIfTable() { 361 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_)); 362} 363 364inline int32_t Class::GetIfTableCount() { 365 IfTable* iftable = GetIfTable(); 366 if (iftable == NULL) { 367 return 0; 368 } 369 return iftable->Count(); 370} 371 372inline void Class::SetIfTable(IfTable* new_iftable) { 373 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable); 374} 375 376inline ObjectArray<ArtField>* Class::GetIFields() { 377 DCHECK(IsLoaded() || IsErroneous()); 378 return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); 379} 380 381inline void Class::SetIFields(ObjectArray<ArtField>* new_ifields) 382 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 383 DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_))); 384 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields); 385} 386 387inline ObjectArray<ArtField>* Class::GetSFields() { 388 DCHECK(IsLoaded() || IsErroneous()); 389 return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); 390} 391 392inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields) 393 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 394 DCHECK((IsRetired() && new_sfields == nullptr) || 395 (NULL == GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)))); 396 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields); 397} 398 399inline uint32_t Class::NumStaticFields() { 400 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0; 401} 402 403 404inline ArtField* Class::GetStaticField(uint32_t i) // TODO: uint16_t 405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 406 return GetSFields()->GetWithoutChecks(i); 407} 408 409inline void Class::SetStaticField(uint32_t i, ArtField* f) // TODO: uint16_t 410 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 411 ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField>>( 412 OFFSET_OF_OBJECT_MEMBER(Class, sfields_)); 413 sfields->Set<false>(i, f); 414} 415 416inline uint32_t Class::NumInstanceFields() { 417 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0; 418} 419 420inline ArtField* Class::GetInstanceField(uint32_t i) { // TODO: uint16_t 421 DCHECK_NE(NumInstanceFields(), 0U); 422 return GetIFields()->GetWithoutChecks(i); 423} 424 425inline void Class::SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t 426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 427 ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField>>( 428 OFFSET_OF_OBJECT_MEMBER(Class, ifields_)); 429 ifields->Set<false>(i, f); 430} 431 432template<VerifyObjectFlags kVerifyFlags> 433inline uint32_t Class::GetReferenceInstanceOffsets() { 434 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>()); 435 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_)); 436} 437 438inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) { 439 if (Runtime::Current()->IsActiveTransaction()) { 440 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); 441 } else { 442 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id); 443 } 444} 445 446inline void Class::SetVerifyErrorClass(Class* klass) { 447 CHECK(klass != NULL) << PrettyClass(this); 448 if (Runtime::Current()->IsActiveTransaction()) { 449 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass); 450 } else { 451 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass); 452 } 453} 454 455template<VerifyObjectFlags kVerifyFlags> 456inline uint32_t Class::GetAccessFlags() { 457 // Check class is loaded/retired or this is java.lang.String that has a 458 // circularity issue during loading the names of its members 459 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || 460 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || 461 this == String::GetJavaLangString() || 462 this == ArtField::GetJavaLangReflectArtField() || 463 this == ArtMethod::GetJavaLangReflectArtMethod()); 464 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_)); 465} 466 467inline String* Class::GetName() { 468 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_)); 469} 470inline void Class::SetName(String* name) { 471 if (Runtime::Current()->IsActiveTransaction()) { 472 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); 473 } else { 474 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name); 475 } 476} 477 478template<VerifyObjectFlags kVerifyFlags> 479inline Primitive::Type Class::GetPrimitiveType() { 480 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t)); 481 return static_cast<Primitive::Type>( 482 GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_))); 483} 484 485inline void Class::CheckObjectAlloc() { 486 DCHECK(!IsArrayClass()) 487 << PrettyClass(this) 488 << "A array shouldn't be allocated through this " 489 << "as it requires a pre-fence visitor that sets the class size."; 490 DCHECK(!IsClassClass()) 491 << PrettyClass(this) 492 << "A class object shouldn't be allocated through this " 493 << "as it requires a pre-fence visitor that sets the class size."; 494 DCHECK(IsInstantiable()) << PrettyClass(this); 495 // TODO: decide whether we want this check. It currently fails during bootstrap. 496 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); 497 DCHECK_GE(this->object_size_, sizeof(Object)); 498} 499 500template<bool kIsInstrumented, bool kCheckAddFinalizer> 501inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) { 502 CheckObjectAlloc(); 503 gc::Heap* heap = Runtime::Current()->GetHeap(); 504 const bool add_finalizer = kCheckAddFinalizer && IsFinalizable(); 505 if (!kCheckAddFinalizer) { 506 DCHECK(!IsFinalizable()); 507 } 508 mirror::Object* obj = 509 heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_, 510 allocator_type, VoidFunctor()); 511 if (add_finalizer && LIKELY(obj != nullptr)) { 512 heap->AddFinalizerReference(self, &obj); 513 } 514 return obj; 515} 516 517inline Object* Class::AllocObject(Thread* self) { 518 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator()); 519} 520 521inline Object* Class::AllocNonMovableObject(Thread* self) { 522 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator()); 523} 524 525inline uint32_t Class::ComputeClassSize(bool has_embedded_tables, 526 uint32_t num_vtable_entries, 527 uint32_t num_32bit_static_fields, 528 uint32_t num_64bit_static_fields, 529 uint32_t num_ref_static_fields) { 530 // Space used by java.lang.Class and its instance fields. 531 uint32_t size = sizeof(Class); 532 // Space used by embedded tables. 533 if (has_embedded_tables) { 534 uint32_t embedded_imt_size = kImtSize * sizeof(ImTableEntry); 535 uint32_t embedded_vtable_size = num_vtable_entries * sizeof(VTableEntry); 536 size += embedded_imt_size + embedded_vtable_size; 537 } 538 // Space used by reference statics. 539 size += num_ref_static_fields * sizeof(HeapReference<Object>); 540 // Possible pad for alignment. 541 if (((size & 7) != 0) && (num_64bit_static_fields > 0) && (num_32bit_static_fields == 0)) { 542 size += sizeof(uint32_t); 543 } 544 // Space used for primitive static fields. 545 size += (num_32bit_static_fields * sizeof(uint32_t)) + 546 (num_64bit_static_fields * sizeof(uint64_t)); 547 return size; 548} 549 550template <bool kVisitClass, typename Visitor> 551inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) { 552 // Visit the static fields first so that we don't overwrite the SFields / IFields instance 553 // fields. 554 VisitInstanceFieldsReferences<kVisitClass>(klass, visitor); 555 if (!IsTemp()) { 556 // Temp classes don't ever populate imt/vtable or static fields and they are not even 557 // allocated with the right size for those. 558 VisitStaticFieldsReferences<kVisitClass>(this, visitor); 559 if (ShouldHaveEmbeddedImtAndVTable()) { 560 VisitEmbeddedImtAndVTable(visitor); 561 } 562 } 563} 564 565template<typename Visitor> 566inline void Class::VisitEmbeddedImtAndVTable(const Visitor& visitor) { 567 uint32_t pos = sizeof(mirror::Class); 568 569 size_t count = kImtSize; 570 for (size_t i = 0; i < count; ++i) { 571 MemberOffset offset = MemberOffset(pos); 572 visitor(this, offset, true); 573 pos += sizeof(ImTableEntry); 574 } 575 576 count = ((GetVTable() != NULL) ? GetVTable()->GetLength() : 0); 577 for (size_t i = 0; i < count; ++i) { 578 MemberOffset offset = MemberOffset(pos); 579 visitor(this, offset, true); 580 pos += sizeof(VTableEntry); 581 } 582} 583 584template<ReadBarrierOption kReadBarrierOption> 585inline bool Class::IsArtFieldClass() const { 586 return this == ArtField::GetJavaLangReflectArtField<kReadBarrierOption>(); 587} 588 589template<ReadBarrierOption kReadBarrierOption> 590inline bool Class::IsArtMethodClass() const { 591 return this == ArtMethod::GetJavaLangReflectArtMethod<kReadBarrierOption>(); 592} 593 594template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption> 595inline bool Class::IsClassClass() { 596 Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()-> 597 template GetClass<kVerifyFlags, kReadBarrierOption>(); 598 return this == java_lang_Class; 599} 600 601inline const DexFile& Class::GetDexFile() { 602 return *GetDexCache()->GetDexFile(); 603} 604 605inline bool Class::DescriptorEquals(const char* match) { 606 if (UNLIKELY(IsArrayClass())) { 607 return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1); 608 } else if (UNLIKELY(IsPrimitive())) { 609 return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0; 610 } else if (UNLIKELY(IsProxyClass())) { 611 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match; 612 } else { 613 const DexFile& dex_file = GetDexFile(); 614 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); 615 return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0; 616 } 617} 618 619inline void Class::AssertInitializedOrInitializingInThread(Thread* self) { 620 if (kIsDebugBuild && !IsInitialized()) { 621 CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus(); 622 CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this) 623 << " is initializing in a different thread"; 624 } 625} 626 627inline ObjectArray<Class>* Class::GetInterfaces() { 628 CHECK(IsProxyClass()); 629 // First static field. 630 DCHECK(GetSFields()->Get(0)->IsArtField()); 631 DCHECK_STREQ(GetSFields()->Get(0)->GetName(), "interfaces"); 632 MemberOffset field_offset = GetSFields()->Get(0)->GetOffset(); 633 return GetFieldObject<ObjectArray<Class>>(field_offset); 634} 635 636inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() { 637 CHECK(IsProxyClass()); 638 // Second static field. 639 DCHECK(GetSFields()->Get(1)->IsArtField()); 640 DCHECK_STREQ(GetSFields()->Get(1)->GetName(), "throws"); 641 MemberOffset field_offset = GetSFields()->Get(1)->GetOffset(); 642 return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset); 643} 644 645inline void Class::InitializeClassVisitor::operator()( 646 mirror::Object* obj, size_t usable_size) const { 647 DCHECK_LE(class_size_, usable_size); 648 // Avoid AsClass as object is not yet in live bitmap or allocation stack. 649 mirror::Class* klass = down_cast<mirror::Class*>(obj); 650 // DCHECK(klass->IsClass()); 651 klass->SetClassSize(class_size_); 652 klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive. 653 klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index. 654 klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index. 655} 656 657} // namespace mirror 658} // namespace art 659 660#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_ 661