Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_MIRROR_CLASS_H_ |
| 18 | #define ART_RUNTIME_MIRROR_CLASS_H_ |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | */ |
Brian Carlstrom | fb6996f | 2013-07-18 18:21:14 -0700 | [diff] [blame^] | 30 | #define CLASS_WALK_SUPER 3U |
| 31 | #define CLASS_BITS_PER_WORD (sizeof(uint32_t) * 8) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #define CLASS_OFFSET_ALIGNMENT 4 |
Brian Carlstrom | fb6996f | 2013-07-18 18:21:14 -0700 | [diff] [blame^] | 33 | #define CLASS_HIGH_BIT (1U << (CLASS_BITS_PER_WORD - 1)) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
| 58 | namespace art { |
| 59 | |
| 60 | struct ClassClassOffsets; |
| 61 | struct ClassOffsets; |
| 62 | class StringPiece; |
| 63 | |
| 64 | namespace mirror { |
| 65 | |
| 66 | class ClassLoader; |
| 67 | class DexCache; |
| 68 | class Field; |
| 69 | class IfTable; |
| 70 | |
| 71 | // Type for the InitializedStaticStorage table. Currently the Class |
| 72 | // provides the static storage. However, this might change to an Array |
| 73 | // to improve image sharing, so we use this type to avoid assumptions |
| 74 | // on the current storage. |
| 75 | class MANAGED StaticStorageBase : public Object { |
| 76 | }; |
| 77 | |
| 78 | // C++ mirror of java.lang.Class |
| 79 | class MANAGED Class : public StaticStorageBase { |
| 80 | public: |
| 81 | // Class Status |
| 82 | // |
| 83 | // kStatusNotReady: If a Class cannot be found in the class table by |
| 84 | // FindClass, it allocates an new one with AllocClass in the |
| 85 | // kStatusNotReady and calls LoadClass. Note if it does find a |
| 86 | // class, it may not be kStatusResolved and it will try to push it |
| 87 | // forward toward kStatusResolved. |
| 88 | // |
| 89 | // kStatusIdx: LoadClass populates with Class with information from |
| 90 | // the DexFile, moving the status to kStatusIdx, indicating that the |
| 91 | // Class value in super_class_ has not been populated. The new Class |
| 92 | // can then be inserted into the classes table. |
| 93 | // |
| 94 | // kStatusLoaded: After taking a lock on Class, the ClassLinker will |
| 95 | // attempt to move a kStatusIdx class forward to kStatusLoaded by |
| 96 | // using ResolveClass to initialize the super_class_ and ensuring the |
| 97 | // interfaces are resolved. |
| 98 | // |
| 99 | // kStatusResolved: Still holding the lock on Class, the ClassLinker |
| 100 | // shows linking is complete and fields of the Class populated by making |
| 101 | // it kStatusResolved. Java allows circularities of the form where a super |
| 102 | // class has a field that is of the type of the sub class. We need to be able |
| 103 | // to fully resolve super classes while resolving types for fields. |
| 104 | // |
| 105 | // kStatusRetryVerificationAtRuntime: The verifier sets a class to |
| 106 | // this state if it encounters a soft failure at compile time. This |
| 107 | // often happens when there are unresolved classes in other dex |
| 108 | // files, and this status marks a class as needing to be verified |
| 109 | // again at runtime. |
| 110 | // |
| 111 | // TODO: Explain the other states |
| 112 | enum Status { |
| 113 | kStatusError = -1, |
| 114 | kStatusNotReady = 0, |
| 115 | kStatusIdx = 1, // Loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_. |
| 116 | kStatusLoaded = 2, // DEX idx values resolved. |
| 117 | kStatusResolved = 3, // Part of linking. |
| 118 | kStatusVerifying = 4, // In the process of being verified. |
| 119 | kStatusRetryVerificationAtRuntime = 5, // Compile time verification failed, retry at runtime. |
| 120 | kStatusVerifyingAtRuntime = 6, // Retrying verification at runtime. |
| 121 | kStatusVerified = 7, // Logically part of linking; done pre-init. |
| 122 | kStatusInitializing = 8, // Class init in progress. |
| 123 | kStatusInitialized = 9, // Ready to go. |
| 124 | }; |
| 125 | |
| 126 | Status GetStatus() const { |
| 127 | DCHECK_EQ(sizeof(Status), sizeof(uint32_t)); |
| 128 | return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false)); |
| 129 | } |
| 130 | |
| 131 | void SetStatus(Status new_status) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 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 | |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 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 false; |
| 250 | } else { |
| 251 | return component->CannotBeAssignedFromOtherTypes(); |
| 252 | } |
| 253 | } |
| 254 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 IsFieldClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 345 | |
| 346 | bool IsMethodClass() const; |
| 347 | |
| 348 | Class* GetComponentType() const { |
| 349 | return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false); |
| 350 | } |
| 351 | |
| 352 | void SetComponentType(Class* new_component_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 353 | DCHECK(GetComponentType() == NULL); |
| 354 | DCHECK(new_component_type != NULL); |
| 355 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false); |
| 356 | } |
| 357 | |
| 358 | size_t GetComponentSize() const { |
| 359 | return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType()); |
| 360 | } |
| 361 | |
| 362 | bool IsObjectClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 363 | return !IsPrimitive() && GetSuperClass() == NULL; |
| 364 | } |
| 365 | bool IsInstantiable() const { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 366 | return (!IsPrimitive() && !IsInterface() && !IsAbstract()) || ((IsAbstract()) && IsArrayClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | bool IsObjectArrayClass() const { |
| 370 | return GetComponentType() != NULL && !GetComponentType()->IsPrimitive(); |
| 371 | } |
| 372 | |
| 373 | // Creates a raw object instance but does not invoke the default constructor. |
| 374 | Object* AllocObject(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 375 | |
| 376 | bool IsVariableSize() const { |
| 377 | // Classes and arrays vary in size, and so the object_size_ field cannot |
| 378 | // be used to get their instance size |
| 379 | return IsClassClass() || IsArrayClass(); |
| 380 | } |
| 381 | |
| 382 | size_t SizeOf() const { |
| 383 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 384 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false); |
| 385 | } |
| 386 | |
| 387 | size_t GetClassSize() const { |
| 388 | DCHECK_EQ(sizeof(size_t), sizeof(uint32_t)); |
| 389 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false); |
| 390 | } |
| 391 | |
| 392 | void SetClassSize(size_t new_class_size) |
| 393 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 394 | |
| 395 | size_t GetObjectSize() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 396 | |
| 397 | void SetObjectSize(size_t new_object_size) { |
| 398 | DCHECK(!IsVariableSize()); |
| 399 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 400 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false); |
| 401 | } |
| 402 | |
| 403 | // Returns true if this class is in the same packages as that class. |
| 404 | bool IsInSamePackage(const Class* that) const |
| 405 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 406 | |
| 407 | static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2); |
| 408 | |
| 409 | // Returns true if this class can access that class. |
| 410 | bool CanAccess(Class* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 411 | return that->IsPublic() || this->IsInSamePackage(that); |
| 412 | } |
| 413 | |
| 414 | // Can this class access a member in the provided class with the provided member access flags? |
| 415 | // Note that access to the class isn't checked in case the declaring class is protected and the |
| 416 | // method has been exposed by a public sub-class |
| 417 | bool CanAccessMember(Class* access_to, uint32_t member_flags) const |
| 418 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 419 | // Classes can access all of their own members |
| 420 | if (this == access_to) { |
| 421 | return true; |
| 422 | } |
| 423 | // Public members are trivially accessible |
| 424 | if (member_flags & kAccPublic) { |
| 425 | return true; |
| 426 | } |
| 427 | // Private members are trivially not accessible |
| 428 | if (member_flags & kAccPrivate) { |
| 429 | return false; |
| 430 | } |
| 431 | // Check for protected access from a sub-class, which may or may not be in the same package. |
| 432 | if (member_flags & kAccProtected) { |
| 433 | if (this->IsSubClass(access_to)) { |
| 434 | return true; |
| 435 | } |
| 436 | } |
| 437 | // Allow protected access from other classes in the same package. |
| 438 | return this->IsInSamePackage(access_to); |
| 439 | } |
| 440 | |
| 441 | bool IsSubClass(const Class* klass) const |
| 442 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 443 | |
| 444 | // Can src be assigned to this class? For example, String can be assigned to Object (by an |
| 445 | // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing |
| 446 | // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface |
| 447 | // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign |
| 448 | // to themselves. Classes for primitive types may not assign to each other. |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 449 | inline bool IsAssignableFrom(const Class* src) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 450 | DCHECK(src != NULL); |
| 451 | if (this == src) { |
| 452 | // Can always assign to things of the same type. |
| 453 | return true; |
| 454 | } else if (IsObjectClass()) { |
| 455 | // Can assign any reference to java.lang.Object. |
| 456 | return !src->IsPrimitive(); |
| 457 | } else if (IsInterface()) { |
| 458 | return src->Implements(this); |
| 459 | } else if (src->IsArrayClass()) { |
| 460 | return IsAssignableFromArray(src); |
| 461 | } else { |
| 462 | return !src->IsInterface() && src->IsSubClass(this); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | Class* GetSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 467 | |
| 468 | void SetSuperClass(Class *new_super_class) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 469 | // super class is assigned once, except during class linker initialization |
| 470 | Class* old_super_class = GetFieldObject<Class*>( |
| 471 | OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false); |
| 472 | DCHECK(old_super_class == NULL || old_super_class == new_super_class); |
| 473 | DCHECK(new_super_class != NULL); |
| 474 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false); |
| 475 | } |
| 476 | |
| 477 | bool HasSuperClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 478 | return GetSuperClass() != NULL; |
| 479 | } |
| 480 | |
| 481 | static MemberOffset SuperClassOffset() { |
| 482 | return MemberOffset(OFFSETOF_MEMBER(Class, super_class_)); |
| 483 | } |
| 484 | |
| 485 | ClassLoader* GetClassLoader() const; |
| 486 | |
| 487 | void SetClassLoader(ClassLoader* new_cl) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 488 | |
| 489 | static MemberOffset DexCacheOffset() { |
| 490 | return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_)); |
| 491 | } |
| 492 | |
| 493 | enum { |
| 494 | kDumpClassFullDetail = 1, |
| 495 | kDumpClassClassLoader = (1 << 1), |
| 496 | kDumpClassInitialized = (1 << 2), |
| 497 | }; |
| 498 | |
| 499 | void DumpClass(std::ostream& os, int flags) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 500 | |
| 501 | DexCache* GetDexCache() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 502 | |
| 503 | void SetDexCache(DexCache* new_dex_cache) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 504 | |
| 505 | ObjectArray<AbstractMethod>* GetDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 506 | |
| 507 | void SetDirectMethods(ObjectArray<AbstractMethod>* new_direct_methods) |
| 508 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 509 | |
| 510 | AbstractMethod* GetDirectMethod(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 511 | |
| 512 | void SetDirectMethod(uint32_t i, AbstractMethod* f) // TODO: uint16_t |
| 513 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 514 | |
| 515 | // Returns the number of static, private, and constructor methods. |
| 516 | size_t NumDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 517 | |
| 518 | ObjectArray<AbstractMethod>* GetVirtualMethods() const |
| 519 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 520 | |
| 521 | void SetVirtualMethods(ObjectArray<AbstractMethod>* new_virtual_methods) |
| 522 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 523 | |
| 524 | // Returns the number of non-inherited virtual methods. |
| 525 | size_t NumVirtualMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 526 | |
| 527 | AbstractMethod* GetVirtualMethod(uint32_t i) const |
| 528 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 529 | |
| 530 | AbstractMethod* GetVirtualMethodDuringLinking(uint32_t i) const |
| 531 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 532 | |
| 533 | void SetVirtualMethod(uint32_t i, AbstractMethod* f) // TODO: uint16_t |
| 534 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 535 | |
| 536 | ObjectArray<AbstractMethod>* GetVTable() const; |
| 537 | |
| 538 | ObjectArray<AbstractMethod>* GetVTableDuringLinking() const; |
| 539 | |
| 540 | void SetVTable(ObjectArray<AbstractMethod>* new_vtable) |
| 541 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 542 | |
| 543 | static MemberOffset VTableOffset() { |
| 544 | return OFFSET_OF_OBJECT_MEMBER(Class, vtable_); |
| 545 | } |
| 546 | |
| 547 | // Given a method implemented by this class but potentially from a super class, return the |
| 548 | // specific implementation method for this class. |
| 549 | AbstractMethod* FindVirtualMethodForVirtual(AbstractMethod* method) const |
| 550 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 551 | |
| 552 | // Given a method implemented by this class' super class, return the specific implementation |
| 553 | // method for this class. |
| 554 | AbstractMethod* FindVirtualMethodForSuper(AbstractMethod* method) const |
| 555 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 556 | |
| 557 | // Given a method implemented by this class, but potentially from a |
| 558 | // super class or interface, return the specific implementation |
| 559 | // method for this class. |
| 560 | AbstractMethod* FindVirtualMethodForInterface(AbstractMethod* method) const |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 561 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 562 | |
| 563 | AbstractMethod* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const |
| 564 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 565 | |
| 566 | AbstractMethod* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
| 567 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 568 | |
| 569 | AbstractMethod* FindVirtualMethodForVirtualOrInterface(AbstractMethod* method) const |
| 570 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 571 | |
| 572 | AbstractMethod* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const |
| 573 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 574 | |
| 575 | AbstractMethod* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
| 576 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 577 | |
| 578 | AbstractMethod* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const |
| 579 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 580 | |
| 581 | AbstractMethod* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
| 582 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 583 | |
| 584 | AbstractMethod* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const |
| 585 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 586 | |
| 587 | AbstractMethod* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
| 588 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 589 | |
| 590 | AbstractMethod* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const |
| 591 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 592 | |
| 593 | AbstractMethod* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
| 594 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 595 | |
| 596 | int32_t GetIfTableCount() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 597 | |
| 598 | IfTable* GetIfTable() const; |
| 599 | |
| 600 | void SetIfTable(IfTable* new_iftable) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 601 | |
| 602 | // Get instance fields of the class (See also GetSFields). |
| 603 | ObjectArray<Field>* GetIFields() const; |
| 604 | |
| 605 | void SetIFields(ObjectArray<Field>* new_ifields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 606 | |
| 607 | size_t NumInstanceFields() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 608 | |
| 609 | Field* GetInstanceField(uint32_t i) const // TODO: uint16_t |
| 610 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 611 | |
| 612 | void SetInstanceField(uint32_t i, Field* f) // TODO: uint16_t |
| 613 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 614 | |
| 615 | // Returns the number of instance fields containing reference types. |
| 616 | size_t NumReferenceInstanceFields() const { |
| 617 | DCHECK(IsResolved() || IsErroneous()); |
| 618 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 619 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false); |
| 620 | } |
| 621 | |
| 622 | size_t NumReferenceInstanceFieldsDuringLinking() const { |
| 623 | DCHECK(IsLoaded() || IsErroneous()); |
| 624 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 625 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false); |
| 626 | } |
| 627 | |
| 628 | void SetNumReferenceInstanceFields(size_t new_num) { |
| 629 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 630 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false); |
| 631 | } |
| 632 | |
| 633 | uint32_t GetReferenceInstanceOffsets() const { |
| 634 | DCHECK(IsResolved() || IsErroneous()); |
| 635 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false); |
| 636 | } |
| 637 | |
| 638 | void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) |
| 639 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 640 | |
| 641 | // Beginning of static field data |
| 642 | static MemberOffset FieldsOffset() { |
| 643 | return OFFSET_OF_OBJECT_MEMBER(Class, fields_); |
| 644 | } |
| 645 | |
| 646 | // Returns the number of static fields containing reference types. |
| 647 | size_t NumReferenceStaticFields() const { |
| 648 | DCHECK(IsResolved() || IsErroneous()); |
| 649 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 650 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false); |
| 651 | } |
| 652 | |
| 653 | size_t NumReferenceStaticFieldsDuringLinking() const { |
| 654 | DCHECK(IsLoaded() || IsErroneous()); |
| 655 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 656 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false); |
| 657 | } |
| 658 | |
| 659 | void SetNumReferenceStaticFields(size_t new_num) { |
| 660 | DCHECK_EQ(sizeof(size_t), sizeof(int32_t)); |
| 661 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false); |
| 662 | } |
| 663 | |
| 664 | // Gets the static fields of the class. |
| 665 | ObjectArray<Field>* GetSFields() const; |
| 666 | |
| 667 | void SetSFields(ObjectArray<Field>* new_sfields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 668 | |
| 669 | size_t NumStaticFields() const; |
| 670 | |
| 671 | Field* GetStaticField(uint32_t i) const; // TODO: uint16_t |
| 672 | |
| 673 | void SetStaticField(uint32_t i, Field* f); // TODO: uint16_t |
| 674 | |
| 675 | uint32_t GetReferenceStaticOffsets() const { |
| 676 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false); |
| 677 | } |
| 678 | |
| 679 | void SetReferenceStaticOffsets(uint32_t new_reference_offsets); |
| 680 | |
| 681 | // Find a static or instance field using the JLS resolution order |
| 682 | Field* FindField(const StringPiece& name, const StringPiece& type) |
| 683 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 684 | |
| 685 | // Finds the given instance field in this class or a superclass. |
| 686 | Field* FindInstanceField(const StringPiece& name, const StringPiece& type) |
| 687 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 688 | |
| 689 | // Finds the given instance field in this class or a superclass, only searches classes that |
| 690 | // have the same dex cache. |
| 691 | Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) |
| 692 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 693 | |
| 694 | Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) |
| 695 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 696 | |
| 697 | Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) |
| 698 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 699 | |
| 700 | // Finds the given static field in this class or a superclass. |
| 701 | Field* FindStaticField(const StringPiece& name, const StringPiece& type) |
| 702 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 703 | |
| 704 | // Finds the given static field in this class or superclass, only searches classes that |
| 705 | // have the same dex cache. |
| 706 | Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) |
| 707 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 708 | |
| 709 | Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) |
| 710 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 711 | |
| 712 | Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) |
| 713 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 714 | |
| 715 | pid_t GetClinitThreadId() const { |
| 716 | DCHECK(IsIdxLoaded() || IsErroneous()); |
| 717 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false); |
| 718 | } |
| 719 | |
| 720 | void SetClinitThreadId(pid_t new_clinit_thread_id) { |
| 721 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false); |
| 722 | } |
| 723 | |
| 724 | Class* GetVerifyErrorClass() const { |
| 725 | // DCHECK(IsErroneous()); |
| 726 | return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false); |
| 727 | } |
| 728 | |
| 729 | uint16_t GetDexTypeIndex() const { |
| 730 | return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false); |
| 731 | } |
| 732 | |
| 733 | void SetDexTypeIndex(uint16_t type_idx) { |
| 734 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false); |
| 735 | } |
| 736 | |
| 737 | static Class* GetJavaLangClass() { |
| 738 | DCHECK(java_lang_Class_ != NULL); |
| 739 | return java_lang_Class_; |
| 740 | } |
| 741 | |
| 742 | // Can't call this SetClass or else gets called instead of Object::SetClass in places. |
| 743 | static void SetClassClass(Class* java_lang_Class); |
| 744 | static void ResetClass(); |
| 745 | |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 746 | // When class is verified, set the kAccPreverified flag on each method. |
| 747 | void SetPreverifiedFlagOnAllMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 748 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 749 | private: |
| 750 | void SetVerifyErrorClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 751 | |
| 752 | bool Implements(const Class* klass) const |
| 753 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 754 | bool IsArrayAssignableFromArray(const Class* klass) const |
| 755 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 756 | bool IsAssignableFromArray(const Class* klass) const |
| 757 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 758 | |
| 759 | // defining class loader, or NULL for the "bootstrap" system loader |
| 760 | ClassLoader* class_loader_; |
| 761 | |
| 762 | // For array classes, the component class object for instanceof/checkcast |
| 763 | // (for String[][][], this will be String[][]). NULL for non-array classes. |
| 764 | Class* component_type_; |
| 765 | |
| 766 | // DexCache of resolved constant pool entries (will be NULL for classes generated by the |
| 767 | // runtime such as arrays and primitive classes). |
| 768 | DexCache* dex_cache_; |
| 769 | |
| 770 | // static, private, and <init> methods |
| 771 | ObjectArray<AbstractMethod>* direct_methods_; |
| 772 | |
| 773 | // instance fields |
| 774 | // |
| 775 | // These describe the layout of the contents of an Object. |
| 776 | // Note that only the fields directly declared by this class are |
| 777 | // listed in ifields; fields declared by a superclass are listed in |
| 778 | // the superclass's Class.ifields. |
| 779 | // |
| 780 | // All instance fields that refer to objects are guaranteed to be at |
| 781 | // the beginning of the field list. num_reference_instance_fields_ |
| 782 | // specifies the number of reference fields. |
| 783 | ObjectArray<Field>* ifields_; |
| 784 | |
| 785 | // The interface table (iftable_) contains pairs of a interface class and an array of the |
| 786 | // interface methods. There is one pair per interface supported by this class. That means one |
| 787 | // pair for each interface we support directly, indirectly via superclass, or indirectly via a |
| 788 | // superinterface. This will be null if neither we nor our superclass implement any interfaces. |
| 789 | // |
| 790 | // Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". |
| 791 | // Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a |
| 792 | // single vtable. |
| 793 | // |
| 794 | // For every interface a concrete class implements, we create an array of the concrete vtable_ |
| 795 | // methods for the methods in the interface. |
| 796 | IfTable* iftable_; |
| 797 | |
| 798 | // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName |
| 799 | String* name_; |
| 800 | |
| 801 | // Static fields |
| 802 | ObjectArray<Field>* sfields_; |
| 803 | |
| 804 | // The superclass, or NULL if this is java.lang.Object, an interface or primitive type. |
| 805 | Class* super_class_; |
| 806 | |
| 807 | // If class verify fails, we must return same error on subsequent tries. |
| 808 | Class* verify_error_class_; |
| 809 | |
| 810 | // virtual methods defined in this class; invoked through vtable |
| 811 | ObjectArray<AbstractMethod>* virtual_methods_; |
| 812 | |
| 813 | // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is |
| 814 | // copied in, and virtual methods from our class either replace those from the super or are |
| 815 | // appended. For abstract classes, methods may be created in the vtable that aren't in |
| 816 | // virtual_ methods_ for miranda methods. |
| 817 | ObjectArray<AbstractMethod>* vtable_; |
| 818 | |
| 819 | // access flags; low 16 bits are defined by VM spec |
| 820 | uint32_t access_flags_; |
| 821 | |
| 822 | // Total size of the Class instance; used when allocating storage on gc heap. |
| 823 | // See also object_size_. |
| 824 | size_t class_size_; |
| 825 | |
| 826 | // tid used to check for recursive <clinit> invocation |
| 827 | pid_t clinit_thread_id_; |
| 828 | |
| 829 | // type index from dex file |
| 830 | // TODO: really 16bits |
| 831 | uint32_t dex_type_idx_; |
| 832 | |
| 833 | // number of instance fields that are object refs |
| 834 | size_t num_reference_instance_fields_; |
| 835 | |
| 836 | // number of static fields that are object refs |
| 837 | size_t num_reference_static_fields_; |
| 838 | |
| 839 | // Total object size; used when allocating storage on gc heap. |
| 840 | // (For interfaces and abstract classes this will be zero.) |
| 841 | // See also class_size_. |
| 842 | size_t object_size_; |
| 843 | |
| 844 | // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes |
| 845 | Primitive::Type primitive_type_; |
| 846 | |
| 847 | // Bitmap of offsets of ifields. |
| 848 | uint32_t reference_instance_offsets_; |
| 849 | |
| 850 | // Bitmap of offsets of sfields. |
| 851 | uint32_t reference_static_offsets_; |
| 852 | |
| 853 | // state of class initialization |
| 854 | Status status_; |
| 855 | |
| 856 | // TODO: ? |
| 857 | // initiating class loader list |
| 858 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 859 | // values are kept in a table in gDvm. |
| 860 | // InitiatingLoaderList initiating_loader_list_; |
| 861 | |
| 862 | // Location of first static field. |
| 863 | uint32_t fields_[0]; |
| 864 | |
| 865 | // java.lang.Class |
| 866 | static Class* java_lang_Class_; |
| 867 | |
| 868 | friend struct art::ClassOffsets; // for verifying offset information |
| 869 | DISALLOW_IMPLICIT_CONSTRUCTORS(Class); |
| 870 | }; |
| 871 | |
| 872 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); |
| 873 | |
| 874 | class MANAGED ClassClass : public Class { |
| 875 | private: |
| 876 | int32_t padding_; |
| 877 | int64_t serialVersionUID_; |
| 878 | friend struct art::ClassClassOffsets; // for verifying offset information |
| 879 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass); |
| 880 | }; |
| 881 | |
| 882 | } // namespace mirror |
| 883 | } // namespace art |
| 884 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 885 | #endif // ART_RUNTIME_MIRROR_CLASS_H_ |