class.h revision 5ddb4104ac605d66693b55b79f26f8b8a5505e63
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_H_ 18#define ART_RUNTIME_MIRROR_CLASS_H_ 19 20#include "gc/heap.h" 21#include "modifiers.h" 22#include "object.h" 23#include "primitive.h" 24 25/* 26 * A magic value for refOffsets. Ignore the bits and walk the super 27 * chain when this is the value. 28 * [This is an unlikely "natural" value, since it would be 30 non-ref instance 29 * fields followed by 2 ref instance fields.] 30 */ 31#define CLASS_WALK_SUPER 3U 32#define CLASS_BITS_PER_WORD (sizeof(uint32_t) * 8) 33#define CLASS_OFFSET_ALIGNMENT 4 34#define CLASS_HIGH_BIT (1U << (CLASS_BITS_PER_WORD - 1)) 35/* 36 * Given an offset, return the bit number which would encode that offset. 37 * Local use only. 38 */ 39#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \ 40 ((unsigned int)(byteOffset) / \ 41 CLASS_OFFSET_ALIGNMENT) 42/* 43 * Is the given offset too large to be encoded? 44 */ 45#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \ 46 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD) 47/* 48 * Return a single bit, encoding the offset. 49 * Undefined if the offset is too large, as defined above. 50 */ 51#define CLASS_BIT_FROM_OFFSET(byteOffset) \ 52 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset)) 53/* 54 * Return an offset, given a bit number as returned from CLZ. 55 */ 56#define CLASS_OFFSET_FROM_CLZ(rshift) \ 57 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT)) 58 59namespace art { 60 61struct ClassClassOffsets; 62struct ClassOffsets; 63class Signature; 64class StringPiece; 65 66namespace mirror { 67 68class ArtField; 69class ClassLoader; 70class DexCache; 71class IfTable; 72 73// C++ mirror of java.lang.Class 74class MANAGED Class : public Object { 75 public: 76 // Class Status 77 // 78 // kStatusNotReady: If a Class cannot be found in the class table by 79 // FindClass, it allocates an new one with AllocClass in the 80 // kStatusNotReady and calls LoadClass. Note if it does find a 81 // class, it may not be kStatusResolved and it will try to push it 82 // forward toward kStatusResolved. 83 // 84 // kStatusIdx: LoadClass populates with Class with information from 85 // the DexFile, moving the status to kStatusIdx, indicating that the 86 // Class value in super_class_ has not been populated. The new Class 87 // can then be inserted into the classes table. 88 // 89 // kStatusLoaded: After taking a lock on Class, the ClassLinker will 90 // attempt to move a kStatusIdx class forward to kStatusLoaded by 91 // using ResolveClass to initialize the super_class_ and ensuring the 92 // interfaces are resolved. 93 // 94 // kStatusResolved: Still holding the lock on Class, the ClassLinker 95 // shows linking is complete and fields of the Class populated by making 96 // it kStatusResolved. Java allows circularities of the form where a super 97 // class has a field that is of the type of the sub class. We need to be able 98 // to fully resolve super classes while resolving types for fields. 99 // 100 // kStatusRetryVerificationAtRuntime: The verifier sets a class to 101 // this state if it encounters a soft failure at compile time. This 102 // often happens when there are unresolved classes in other dex 103 // files, and this status marks a class as needing to be verified 104 // again at runtime. 105 // 106 // TODO: Explain the other states 107 enum Status { 108 kStatusError = -1, 109 kStatusNotReady = 0, 110 kStatusIdx = 1, // Loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_. 111 kStatusLoaded = 2, // DEX idx values resolved. 112 kStatusResolved = 3, // Part of linking. 113 kStatusVerifying = 4, // In the process of being verified. 114 kStatusRetryVerificationAtRuntime = 5, // Compile time verification failed, retry at runtime. 115 kStatusVerifyingAtRuntime = 6, // Retrying verification at runtime. 116 kStatusVerified = 7, // Logically part of linking; done pre-init. 117 kStatusInitializing = 8, // Class init in progress. 118 kStatusInitialized = 9, // Ready to go. 119 kStatusMax = 10, 120 }; 121 122 Status GetStatus() const { 123 DCHECK_EQ(sizeof(Status), sizeof(uint32_t)); 124 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), true)); 125 } 126 127 void SetStatus(Status new_status, Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 128 129 static MemberOffset StatusOffset() { 130 return OFFSET_OF_OBJECT_MEMBER(Class, status_); 131 } 132 133 // Returns true if the class has failed to link. 134 bool IsErroneous() const { 135 return GetStatus() == kStatusError; 136 } 137 138 // Returns true if the class has been loaded. 139 bool IsIdxLoaded() const { 140 return GetStatus() >= kStatusIdx; 141 } 142 143 // Returns true if the class has been loaded. 144 bool IsLoaded() const { 145 return GetStatus() >= kStatusLoaded; 146 } 147 148 // Returns true if the class has been linked. 149 bool IsResolved() const { 150 return GetStatus() >= kStatusResolved; 151 } 152 153 // Returns true if the class was compile-time verified. 154 bool IsCompileTimeVerified() const { 155 return GetStatus() >= kStatusRetryVerificationAtRuntime; 156 } 157 158 // Returns true if the class has been verified. 159 bool IsVerified() const { 160 return GetStatus() >= kStatusVerified; 161 } 162 163 // Returns true if the class is initializing. 164 bool IsInitializing() const { 165 return GetStatus() >= kStatusInitializing; 166 } 167 168 // Returns true if the class is initialized. 169 bool IsInitialized() const { 170 return GetStatus() == kStatusInitialized; 171 } 172 173 uint32_t GetAccessFlags() const; 174 175 void SetAccessFlags(uint32_t new_access_flags) { 176 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false); 177 } 178 179 // Returns true if the class is an interface. 180 bool IsInterface() const { 181 return (GetAccessFlags() & kAccInterface) != 0; 182 } 183 184 // Returns true if the class is declared public. 185 bool IsPublic() const { 186 return (GetAccessFlags() & kAccPublic) != 0; 187 } 188 189 // Returns true if the class is declared final. 190 bool IsFinal() const { 191 return (GetAccessFlags() & kAccFinal) != 0; 192 } 193 194 bool IsFinalizable() const { 195 return (GetAccessFlags() & kAccClassIsFinalizable) != 0; 196 } 197 198 void SetFinalizable() { 199 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false); 200 SetAccessFlags(flags | kAccClassIsFinalizable); 201 } 202 203 // Returns true if the class is abstract. 204 bool IsAbstract() const { 205 return (GetAccessFlags() & kAccAbstract) != 0; 206 } 207 208 // Returns true if the class is an annotation. 209 bool IsAnnotation() const { 210 return (GetAccessFlags() & kAccAnnotation) != 0; 211 } 212 213 // Returns true if the class is synthetic. 214 bool IsSynthetic() const { 215 return (GetAccessFlags() & kAccSynthetic) != 0; 216 } 217 218 bool IsReferenceClass() const { 219 return (GetAccessFlags() & kAccClassIsReference) != 0; 220 } 221 222 bool IsWeakReferenceClass() const { 223 return (GetAccessFlags() & kAccClassIsWeakReference) != 0; 224 } 225 226 bool IsSoftReferenceClass() const { 227 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference; 228 } 229 230 bool IsFinalizerReferenceClass() const { 231 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0; 232 } 233 234 bool IsPhantomReferenceClass() const { 235 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0; 236 } 237 238 // Can references of this type be assigned to by things of another type? For non-array types 239 // this is a matter of whether sub-classes may exist - which they can't if the type is final. 240 // For array classes, where all the classes are final due to there being no sub-classes, an 241 // Object[] may be assigned to by a String[] but a String[] may not be assigned to by other 242 // types as the component is final. 243 bool CannotBeAssignedFromOtherTypes() const { 244 if (!IsArrayClass()) { 245 return IsFinal(); 246 } else { 247 Class* component = GetComponentType(); 248 if (component->IsPrimitive()) { 249 return true; 250 } else { 251 return component->CannotBeAssignedFromOtherTypes(); 252 } 253 } 254 } 255 256 String* GetName() const; // Returns the cached name. 257 void SetName(String* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); // Sets the cached name. 258 // Computes the name, then sets the cached value. 259 String* ComputeName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 260 261 bool IsProxyClass() const { 262 // Read access flags without using getter as whether something is a proxy can be check in 263 // any loaded state 264 // TODO: switch to a check if the super class is java.lang.reflect.Proxy? 265 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false); 266 return (access_flags & kAccClassIsProxy) != 0; 267 } 268 269 Primitive::Type GetPrimitiveType() const { 270 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t)); 271 return static_cast<Primitive::Type>( 272 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false)); 273 } 274 275 void SetPrimitiveType(Primitive::Type new_type) { 276 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t)); 277 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false); 278 } 279 280 // Returns true if the class is a primitive type. 281 bool IsPrimitive() const { 282 return GetPrimitiveType() != Primitive::kPrimNot; 283 } 284 285 bool IsPrimitiveBoolean() const { 286 return GetPrimitiveType() == Primitive::kPrimBoolean; 287 } 288 289 bool IsPrimitiveByte() const { 290 return GetPrimitiveType() == Primitive::kPrimByte; 291 } 292 293 bool IsPrimitiveChar() const { 294 return GetPrimitiveType() == Primitive::kPrimChar; 295 } 296 297 bool IsPrimitiveShort() const { 298 return GetPrimitiveType() == Primitive::kPrimShort; 299 } 300 301 bool IsPrimitiveInt() const { 302 return GetPrimitiveType() == Primitive::kPrimInt; 303 } 304 305 bool IsPrimitiveLong() const { 306 return GetPrimitiveType() == Primitive::kPrimLong; 307 } 308 309 bool IsPrimitiveFloat() const { 310 return GetPrimitiveType() == Primitive::kPrimFloat; 311 } 312 313 bool IsPrimitiveDouble() const { 314 return GetPrimitiveType() == Primitive::kPrimDouble; 315 } 316 317 bool IsPrimitiveVoid() const { 318 return GetPrimitiveType() == Primitive::kPrimVoid; 319 } 320 321 bool IsPrimitiveArray() const { 322 return IsArrayClass() && GetComponentType()->IsPrimitive(); 323 } 324 325 // Depth of class from java.lang.Object 326 size_t Depth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 327 size_t depth = 0; 328 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) { 329 depth++; 330 } 331 return depth; 332 } 333 334 bool IsArrayClass() const { 335 return GetComponentType() != NULL; 336 } 337 338 bool IsClassClass() const; 339 340 bool IsStringClass() const; 341 342 bool IsThrowableClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 343 344 bool IsArtFieldClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 345 346 bool IsArtMethodClass() const; 347 348 static MemberOffset ComponentTypeOffset() { 349 return OFFSET_OF_OBJECT_MEMBER(Class, component_type_); 350 } 351 352 Class* GetComponentType() const { 353 return GetFieldObject<Class*>(ComponentTypeOffset(), false); 354 } 355 356 void SetComponentType(Class* new_component_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 357 DCHECK(GetComponentType() == NULL); 358 DCHECK(new_component_type != NULL); 359 SetFieldObject(ComponentTypeOffset(), new_component_type, false); 360 } 361 362 size_t GetComponentSize() const { 363 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType()); 364 } 365 366 bool IsObjectClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 367 return !IsPrimitive() && GetSuperClass() == NULL; 368 } 369 bool IsInstantiable() const { 370 return (!IsPrimitive() && !IsInterface() && !IsAbstract()) || ((IsAbstract()) && IsArrayClass()); 371 } 372 373 bool IsObjectArrayClass() const { 374 return GetComponentType() != NULL && !GetComponentType()->IsPrimitive(); 375 } 376 377 // Creates a raw object instance but does not invoke the default constructor. 378 template <bool kIsInstrumented> 379 ALWAYS_INLINE Object* Alloc(Thread* self, gc::AllocatorType allocator_type) 380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 381 382 Object* AllocObject(Thread* self) 383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 384 Object* AllocNonMovableObject(Thread* self) 385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 386 387 bool IsVariableSize() const { 388 // Classes and arrays vary in size, and so the object_size_ field cannot 389 // be used to get their instance size 390 return IsClassClass() || IsArrayClass(); 391 } 392 393 size_t SizeOf() const { 394 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 395 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false); 396 } 397 398 size_t GetClassSize() const { 399 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t)); 400 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false); 401 } 402 403 void SetClassSize(size_t new_class_size) 404 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 405 406 size_t GetObjectSize() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 407 408 void SetObjectSize(size_t new_object_size) { 409 DCHECK(!IsVariableSize()); 410 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 411 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false); 412 } 413 414 // Returns true if this class is in the same packages as that class. 415 bool IsInSamePackage(const Class* that) const 416 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 417 418 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2); 419 420 // Returns true if this class can access that class. 421 bool CanAccess(Class* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 422 return that->IsPublic() || this->IsInSamePackage(that); 423 } 424 425 // Can this class access a member in the provided class with the provided member access flags? 426 // Note that access to the class isn't checked in case the declaring class is protected and the 427 // method has been exposed by a public sub-class 428 bool CanAccessMember(Class* access_to, uint32_t member_flags) const 429 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 430 // Classes can access all of their own members 431 if (this == access_to) { 432 return true; 433 } 434 // Public members are trivially accessible 435 if (member_flags & kAccPublic) { 436 return true; 437 } 438 // Private members are trivially not accessible 439 if (member_flags & kAccPrivate) { 440 return false; 441 } 442 // Check for protected access from a sub-class, which may or may not be in the same package. 443 if (member_flags & kAccProtected) { 444 if (this->IsSubClass(access_to)) { 445 return true; 446 } 447 } 448 // Allow protected access from other classes in the same package. 449 return this->IsInSamePackage(access_to); 450 } 451 452 bool IsSubClass(const Class* klass) const 453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 454 455 // Can src be assigned to this class? For example, String can be assigned to Object (by an 456 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing 457 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface 458 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign 459 // to themselves. Classes for primitive types may not assign to each other. 460 inline bool IsAssignableFrom(const Class* src) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 461 DCHECK(src != NULL); 462 if (this == src) { 463 // Can always assign to things of the same type. 464 return true; 465 } else if (IsObjectClass()) { 466 // Can assign any reference to java.lang.Object. 467 return !src->IsPrimitive(); 468 } else if (IsInterface()) { 469 return src->Implements(this); 470 } else if (src->IsArrayClass()) { 471 return IsAssignableFromArray(src); 472 } else { 473 return !src->IsInterface() && src->IsSubClass(this); 474 } 475 } 476 477 Class* GetSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 478 479 void SetSuperClass(Class *new_super_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 480 // super class is assigned once, except during class linker initialization 481 Class* old_super_class = GetFieldObject<Class*>( 482 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false); 483 DCHECK(old_super_class == NULL || old_super_class == new_super_class); 484 DCHECK(new_super_class != NULL); 485 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false); 486 } 487 488 bool HasSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 489 return GetSuperClass() != NULL; 490 } 491 492 static MemberOffset SuperClassOffset() { 493 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_)); 494 } 495 496 ClassLoader* GetClassLoader() const; 497 498 void SetClassLoader(ClassLoader* new_cl) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 499 500 static MemberOffset DexCacheOffset() { 501 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_)); 502 } 503 504 enum { 505 kDumpClassFullDetail = 1, 506 kDumpClassClassLoader = (1 << 1), 507 kDumpClassInitialized = (1 << 2), 508 }; 509 510 void DumpClass(std::ostream& os, int flags) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 511 512 DexCache* GetDexCache() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 513 514 void SetDexCache(DexCache* new_dex_cache) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 515 516 ObjectArray<ArtMethod>* GetDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 517 518 void SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods) 519 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 520 521 ArtMethod* GetDirectMethod(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 522 523 void SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t 524 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 525 526 // Returns the number of static, private, and constructor methods. 527 size_t NumDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 528 529 ObjectArray<ArtMethod>* GetVirtualMethods() const 530 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 531 532 void SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) 533 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 534 535 // Returns the number of non-inherited virtual methods. 536 size_t NumVirtualMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 537 538 ArtMethod* GetVirtualMethod(uint32_t i) const 539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 540 541 ArtMethod* GetVirtualMethodDuringLinking(uint32_t i) const 542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 543 544 void SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t 545 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 546 547 ObjectArray<ArtMethod>* GetVTable() const; 548 549 ObjectArray<ArtMethod>* GetVTableDuringLinking() const; 550 551 void SetVTable(ObjectArray<ArtMethod>* new_vtable) 552 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 553 554 static MemberOffset VTableOffset() { 555 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_); 556 } 557 558 ObjectArray<ArtMethod>* GetImTable() const; 559 560 void SetImTable(ObjectArray<ArtMethod>* new_imtable) 561 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 562 563 static MemberOffset ImTableOffset() { 564 return OFFSET_OF_OBJECT_MEMBER(Class, imtable_); 565 } 566 567 // Given a method implemented by this class but potentially from a super class, return the 568 // specific implementation method for this class. 569 ArtMethod* FindVirtualMethodForVirtual(ArtMethod* method) const 570 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 571 572 // Given a method implemented by this class' super class, return the specific implementation 573 // method for this class. 574 ArtMethod* FindVirtualMethodForSuper(ArtMethod* method) const 575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 576 577 // Given a method implemented by this class, but potentially from a 578 // super class or interface, return the specific implementation 579 // method for this class. 580 ArtMethod* FindVirtualMethodForInterface(ArtMethod* method) const 581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE; 582 583 ArtMethod* FindVirtualMethodForVirtualOrInterface(ArtMethod* method) const 584 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 585 586 ArtMethod* FindInterfaceMethod(const StringPiece& name, const Signature& signature) const 587 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 588 589 ArtMethod* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const 590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 591 592 ArtMethod* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const 593 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 594 595 ArtMethod* FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const 596 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 597 598 ArtMethod* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const 599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 600 601 ArtMethod* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const 602 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 603 604 ArtMethod* FindDirectMethod(const StringPiece& name, const Signature& signature) const 605 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 606 607 ArtMethod* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const 608 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 609 610 ArtMethod* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const 611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 612 613 ArtMethod* FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) const 614 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 615 616 ArtMethod* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const 617 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 618 619 ArtMethod* FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const 620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 621 622 ArtMethod* FindVirtualMethod(const StringPiece& name, const Signature& signature) const 623 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 624 625 ArtMethod* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const 626 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 627 628 ArtMethod* FindClassInitializer() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 629 630 int32_t GetIfTableCount() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 631 632 IfTable* GetIfTable() const; 633 634 void SetIfTable(IfTable* new_iftable) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 635 636 // Get instance fields of the class (See also GetSFields). 637 ObjectArray<ArtField>* GetIFields() const; 638 639 void SetIFields(ObjectArray<ArtField>* new_ifields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 640 641 size_t NumInstanceFields() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 642 643 ArtField* GetInstanceField(uint32_t i) const // TODO: uint16_t 644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 645 646 void SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t 647 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 648 649 // Returns the number of instance fields containing reference types. 650 size_t NumReferenceInstanceFields() const { 651 DCHECK(IsResolved() || IsErroneous()); 652 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 653 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false); 654 } 655 656 size_t NumReferenceInstanceFieldsDuringLinking() const { 657 DCHECK(IsLoaded() || IsErroneous()); 658 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 659 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false); 660 } 661 662 void SetNumReferenceInstanceFields(size_t new_num) { 663 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 664 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false); 665 } 666 667 uint32_t GetReferenceInstanceOffsets() const { 668 DCHECK(IsResolved() || IsErroneous()); 669 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false); 670 } 671 672 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) 673 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 674 675 // Beginning of static field data 676 static MemberOffset FieldsOffset() { 677 return OFFSET_OF_OBJECT_MEMBER(Class, fields_); 678 } 679 680 // Returns the number of static fields containing reference types. 681 size_t NumReferenceStaticFields() const { 682 DCHECK(IsResolved() || IsErroneous()); 683 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 684 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false); 685 } 686 687 size_t NumReferenceStaticFieldsDuringLinking() const { 688 DCHECK(IsLoaded() || IsErroneous()); 689 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 690 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false); 691 } 692 693 void SetNumReferenceStaticFields(size_t new_num) { 694 DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); 695 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false); 696 } 697 698 // Gets the static fields of the class. 699 ObjectArray<ArtField>* GetSFields() const; 700 701 void SetSFields(ObjectArray<ArtField>* new_sfields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 702 703 size_t NumStaticFields() const; 704 705 ArtField* GetStaticField(uint32_t i) const; // TODO: uint16_t 706 707 void SetStaticField(uint32_t i, ArtField* f); // TODO: uint16_t 708 709 uint32_t GetReferenceStaticOffsets() const { 710 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false); 711 } 712 713 void SetReferenceStaticOffsets(uint32_t new_reference_offsets); 714 715 // Find a static or instance field using the JLS resolution order 716 ArtField* FindField(const StringPiece& name, const StringPiece& type) 717 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 718 719 // Finds the given instance field in this class or a superclass. 720 ArtField* FindInstanceField(const StringPiece& name, const StringPiece& type) 721 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 722 723 // Finds the given instance field in this class or a superclass, only searches classes that 724 // have the same dex cache. 725 ArtField* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) 726 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 727 728 ArtField* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) 729 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 730 731 ArtField* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) 732 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 733 734 // Finds the given static field in this class or a superclass. 735 ArtField* FindStaticField(const StringPiece& name, const StringPiece& type) 736 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 737 738 // Finds the given static field in this class or superclass, only searches classes that 739 // have the same dex cache. 740 ArtField* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) 741 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 742 743 ArtField* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) 744 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 745 746 ArtField* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) 747 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 748 749 pid_t GetClinitThreadId() const { 750 DCHECK(IsIdxLoaded() || IsErroneous()); 751 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false); 752 } 753 754 void SetClinitThreadId(pid_t new_clinit_thread_id) { 755 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false); 756 } 757 758 Class* GetVerifyErrorClass() const { 759 // DCHECK(IsErroneous()); 760 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false); 761 } 762 763 uint16_t GetDexClassDefIndex() const { 764 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_), false); 765 } 766 767 void SetDexClassDefIndex(uint16_t class_def_idx) { 768 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_class_def_idx_), class_def_idx, false); 769 } 770 771 uint16_t GetDexTypeIndex() const { 772 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false); 773 } 774 775 void SetDexTypeIndex(uint16_t type_idx) { 776 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false); 777 } 778 779 static Class* GetJavaLangClass() { 780 DCHECK(java_lang_Class_ != NULL); 781 return java_lang_Class_; 782 } 783 784 // Can't call this SetClass or else gets called instead of Object::SetClass in places. 785 static void SetClassClass(Class* java_lang_Class); 786 static void ResetClass(); 787 static void VisitRoots(RootVisitor* visitor, void* arg) 788 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 789 790 // When class is verified, set the kAccPreverified flag on each method. 791 void SetPreverifiedFlagOnAllMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 792 793 private: 794 void SetVerifyErrorClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 795 796 bool Implements(const Class* klass) const 797 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 798 bool IsArrayAssignableFromArray(const Class* klass) const 799 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 800 bool IsAssignableFromArray(const Class* klass) const 801 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 802 803 void CheckObjectAlloc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 804 805 // defining class loader, or NULL for the "bootstrap" system loader 806 ClassLoader* class_loader_; 807 808 // For array classes, the component class object for instanceof/checkcast 809 // (for String[][][], this will be String[][]). NULL for non-array classes. 810 Class* component_type_; 811 812 // DexCache of resolved constant pool entries (will be NULL for classes generated by the 813 // runtime such as arrays and primitive classes). 814 DexCache* dex_cache_; 815 816 // static, private, and <init> methods 817 ObjectArray<ArtMethod>* direct_methods_; 818 819 // instance fields 820 // 821 // These describe the layout of the contents of an Object. 822 // Note that only the fields directly declared by this class are 823 // listed in ifields; fields declared by a superclass are listed in 824 // the superclass's Class.ifields. 825 // 826 // All instance fields that refer to objects are guaranteed to be at 827 // the beginning of the field list. num_reference_instance_fields_ 828 // specifies the number of reference fields. 829 ObjectArray<ArtField>* ifields_; 830 831 // The interface table (iftable_) contains pairs of a interface class and an array of the 832 // interface methods. There is one pair per interface supported by this class. That means one 833 // pair for each interface we support directly, indirectly via superclass, or indirectly via a 834 // superinterface. This will be null if neither we nor our superclass implement any interfaces. 835 // 836 // Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". 837 // Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a 838 // single vtable. 839 // 840 // For every interface a concrete class implements, we create an array of the concrete vtable_ 841 // methods for the methods in the interface. 842 IfTable* iftable_; 843 844 // Interface method table (imt), for quick "invoke-interface". 845 ObjectArray<ArtMethod>* imtable_; 846 847 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName 848 String* name_; 849 850 // Static fields 851 ObjectArray<ArtField>* sfields_; 852 853 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type. 854 Class* super_class_; 855 856 // If class verify fails, we must return same error on subsequent tries. 857 Class* verify_error_class_; 858 859 // Virtual methods defined in this class; invoked through vtable. 860 ObjectArray<ArtMethod>* virtual_methods_; 861 862 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is 863 // copied in, and virtual methods from our class either replace those from the super or are 864 // appended. For abstract classes, methods may be created in the vtable that aren't in 865 // virtual_ methods_ for miranda methods. 866 ObjectArray<ArtMethod>* vtable_; 867 868 // Access flags; low 16 bits are defined by VM spec. 869 uint32_t access_flags_; 870 871 // Total size of the Class instance; used when allocating storage on gc heap. 872 // See also object_size_. 873 size_t class_size_; 874 875 // Tid used to check for recursive <clinit> invocation. 876 pid_t clinit_thread_id_; 877 878 // ClassDef index in dex file, -1 if no class definition such as an array. 879 // TODO: really 16bits 880 int32_t dex_class_def_idx_; 881 882 // Type index in dex file. 883 // TODO: really 16bits 884 int32_t dex_type_idx_; 885 886 // Number of instance fields that are object refs. 887 size_t num_reference_instance_fields_; 888 889 // Number of static fields that are object refs, 890 size_t num_reference_static_fields_; 891 892 // Total object size; used when allocating storage on gc heap. 893 // (For interfaces and abstract classes this will be zero.) 894 // See also class_size_. 895 size_t object_size_; 896 897 // Primitive type value, or Primitive::kPrimNot (0); set for generated primitive classes. 898 Primitive::Type primitive_type_; 899 900 // Bitmap of offsets of ifields. 901 uint32_t reference_instance_offsets_; 902 903 // Bitmap of offsets of sfields. 904 uint32_t reference_static_offsets_; 905 906 // State of class initialization. 907 Status status_; 908 909 // TODO: ? 910 // initiating class loader list 911 // NOTE: for classes with low serialNumber, these are unused, and the 912 // values are kept in a table in gDvm. 913 // InitiatingLoaderList initiating_loader_list_; 914 915 // Location of first static field. 916 uint32_t fields_[0]; 917 918 // java.lang.Class 919 static Class* java_lang_Class_; 920 921 friend struct art::ClassOffsets; // for verifying offset information 922 DISALLOW_IMPLICIT_CONSTRUCTORS(Class); 923}; 924 925std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); 926 927class MANAGED ClassClass : public Class { 928 private: 929 int32_t pad_; 930 int64_t serialVersionUID_; 931 friend struct art::ClassClassOffsets; // for verifying offset information 932 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass); 933}; 934 935} // namespace mirror 936} // namespace art 937 938#endif // ART_RUNTIME_MIRROR_CLASS_H_ 939