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