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; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 62 | class Signature; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 63 | class StringPiece; |
| 64 | |
| 65 | namespace mirror { |
| 66 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 67 | class ArtField; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 68 | class ClassLoader; |
| 69 | class DexCache; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 70 | class 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. |
| 76 | class MANAGED StaticStorageBase : public Object { |
| 77 | }; |
| 78 | |
| 79 | // C++ mirror of java.lang.Class |
| 80 | class 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. |
Brian Carlstrom | ba150c3 | 2013-08-27 17:31:03 -0700 | [diff] [blame] | 125 | kStatusMax = 10, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | Status GetStatus() const { |
| 129 | DCHECK_EQ(sizeof(Status), sizeof(uint32_t)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 130 | return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), true)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 133 | void SetStatus(Status new_status, Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 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()) { |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 251 | return true; |
Ian Rogers | 04f94f4 | 2013-06-10 15:09:26 -0700 | [diff] [blame] | 252 | } else { |
| 253 | return component->CannotBeAssignedFromOtherTypes(); |
| 254 | } |
| 255 | } |
| 256 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 346 | bool IsArtFieldClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 347 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 348 | bool IsArtMethodClass() const; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 349 | |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 350 | static MemberOffset ComponentTypeOffset() { |
| 351 | return OFFSET_OF_OBJECT_MEMBER(Class, component_type_); |
| 352 | } |
| 353 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 354 | Class* GetComponentType() const { |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 355 | return GetFieldObject<Class*>(ComponentTypeOffset(), false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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); |
Ian Rogers | a9a8254 | 2013-10-04 11:17:26 -0700 | [diff] [blame] | 361 | SetFieldObject(ComponentTypeOffset(), new_component_type, false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 { |
Sameer Abu Asal | 02c4223 | 2013-04-30 12:09:45 -0700 | [diff] [blame] | 372 | return (!IsPrimitive() && !IsInterface() && !IsAbstract()) || ((IsAbstract()) && IsArrayClass()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 380 | Object* AllocObject(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 381 | return AllocObjectInstrumented(self); |
| 382 | } |
| 383 | |
| 384 | Object* AllocObjectUninstrumented(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 385 | Object* AllocObjectInstrumented(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Ian Rogers | fa46d3e | 2013-05-15 00:16:04 -0700 | [diff] [blame] | 460 | 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] | 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 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 516 | ObjectArray<ArtMethod>* GetDirectMethods() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 517 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 518 | void SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 519 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 520 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 521 | ArtMethod* GetDirectMethod(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 522 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 523 | void SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 529 | ObjectArray<ArtMethod>* GetVirtualMethods() const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 530 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 531 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 532 | void SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 538 | ArtMethod* GetVirtualMethod(uint32_t i) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 539 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 540 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 541 | ArtMethod* GetVirtualMethodDuringLinking(uint32_t i) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 542 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 543 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 544 | void SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 545 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 546 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 547 | ObjectArray<ArtMethod>* GetVTable() const; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 548 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 549 | ObjectArray<ArtMethod>* GetVTableDuringLinking() const; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 550 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 551 | void SetVTable(ObjectArray<ArtMethod>* new_vtable) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 552 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 553 | |
| 554 | static MemberOffset VTableOffset() { |
| 555 | return OFFSET_OF_OBJECT_MEMBER(Class, vtable_); |
| 556 | } |
| 557 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame^] | 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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 567 | // Given a method implemented by this class but potentially from a super class, return the |
| 568 | // specific implementation method for this class. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 569 | ArtMethod* FindVirtualMethodForVirtual(ArtMethod* method) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 574 | ArtMethod* FindVirtualMethodForSuper(ArtMethod* method) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 580 | ArtMethod* FindVirtualMethodForInterface(ArtMethod* method) const |
Ian Rogers | 1ffa32f | 2013-02-05 18:29:08 -0800 | [diff] [blame] | 581 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) ALWAYS_INLINE; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 582 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 583 | ArtMethod* FindVirtualMethodForVirtualOrInterface(ArtMethod* method) const |
| 584 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 585 | |
| 586 | ArtMethod* FindInterfaceMethod(const StringPiece& name, const Signature& signature) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 587 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 588 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 589 | ArtMethod* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 590 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 591 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 592 | ArtMethod* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 593 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 594 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 595 | ArtMethod* FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const |
| 596 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 597 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 598 | ArtMethod* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 599 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 600 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 601 | ArtMethod* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 602 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 603 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 604 | ArtMethod* FindDirectMethod(const StringPiece& name, const Signature& signature) const |
| 605 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 606 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 607 | ArtMethod* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 608 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 609 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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). |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 637 | ObjectArray<ArtField>* GetIFields() const; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 638 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 639 | void SetIFields(ObjectArray<ArtField>* new_ifields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 640 | |
| 641 | size_t NumInstanceFields() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 642 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 643 | ArtField* GetInstanceField(uint32_t i) const // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 644 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 645 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 646 | void SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 699 | ObjectArray<ArtField>* GetSFields() const; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 700 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 701 | void SetSFields(ObjectArray<ArtField>* new_sfields) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 702 | |
| 703 | size_t NumStaticFields() const; |
| 704 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 705 | ArtField* GetStaticField(uint32_t i) const; // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 706 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 707 | void SetStaticField(uint32_t i, ArtField* f); // TODO: uint16_t |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 716 | ArtField* FindField(const StringPiece& name, const StringPiece& type) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 717 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 718 | |
| 719 | // Finds the given instance field in this class or a superclass. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 720 | ArtField* FindInstanceField(const StringPiece& name, const StringPiece& type) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 725 | ArtField* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 726 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 727 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 728 | ArtField* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 729 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 730 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 731 | ArtField* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 732 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 733 | |
| 734 | // Finds the given static field in this class or a superclass. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 735 | ArtField* FindStaticField(const StringPiece& name, const StringPiece& type) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 740 | ArtField* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 741 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 742 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 743 | ArtField* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 744 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 745 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 746 | ArtField* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 788 | // When class is verified, set the kAccPreverified flag on each method. |
| 789 | void SetPreverifiedFlagOnAllMethods() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 790 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 791 | private: |
| 792 | void SetVerifyErrorClass(Class* klass) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 793 | |
| 794 | bool Implements(const Class* klass) const |
| 795 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 796 | bool IsArrayAssignableFromArray(const Class* klass) const |
| 797 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 798 | bool IsAssignableFromArray(const Class* klass) const |
| 799 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 800 | |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 801 | void CheckObjectAlloc() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 802 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 803 | // defining class loader, or NULL for the "bootstrap" system loader |
| 804 | ClassLoader* class_loader_; |
| 805 | |
| 806 | // For array classes, the component class object for instanceof/checkcast |
| 807 | // (for String[][][], this will be String[][]). NULL for non-array classes. |
| 808 | Class* component_type_; |
| 809 | |
| 810 | // DexCache of resolved constant pool entries (will be NULL for classes generated by the |
| 811 | // runtime such as arrays and primitive classes). |
| 812 | DexCache* dex_cache_; |
| 813 | |
| 814 | // static, private, and <init> methods |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 815 | ObjectArray<ArtMethod>* direct_methods_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 816 | |
| 817 | // instance fields |
| 818 | // |
| 819 | // These describe the layout of the contents of an Object. |
| 820 | // Note that only the fields directly declared by this class are |
| 821 | // listed in ifields; fields declared by a superclass are listed in |
| 822 | // the superclass's Class.ifields. |
| 823 | // |
| 824 | // All instance fields that refer to objects are guaranteed to be at |
| 825 | // the beginning of the field list. num_reference_instance_fields_ |
| 826 | // specifies the number of reference fields. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 827 | ObjectArray<ArtField>* ifields_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 828 | |
| 829 | // The interface table (iftable_) contains pairs of a interface class and an array of the |
| 830 | // interface methods. There is one pair per interface supported by this class. That means one |
| 831 | // pair for each interface we support directly, indirectly via superclass, or indirectly via a |
| 832 | // superinterface. This will be null if neither we nor our superclass implement any interfaces. |
| 833 | // |
| 834 | // Why we need this: given "class Foo implements Face", declare "Face faceObj = new Foo()". |
| 835 | // Invoke faceObj.blah(), where "blah" is part of the Face interface. We can't easily use a |
| 836 | // single vtable. |
| 837 | // |
| 838 | // For every interface a concrete class implements, we create an array of the concrete vtable_ |
| 839 | // methods for the methods in the interface. |
| 840 | IfTable* iftable_; |
| 841 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame^] | 842 | // Interface method table (imt), for quick "invoke-interface". |
| 843 | ObjectArray<ArtMethod>* imtable_; |
| 844 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 845 | // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName |
| 846 | String* name_; |
| 847 | |
| 848 | // Static fields |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 849 | ObjectArray<ArtField>* sfields_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 850 | |
| 851 | // The superclass, or NULL if this is java.lang.Object, an interface or primitive type. |
| 852 | Class* super_class_; |
| 853 | |
| 854 | // If class verify fails, we must return same error on subsequent tries. |
| 855 | Class* verify_error_class_; |
| 856 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 857 | // Virtual methods defined in this class; invoked through vtable. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 858 | ObjectArray<ArtMethod>* virtual_methods_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 859 | |
| 860 | // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is |
| 861 | // copied in, and virtual methods from our class either replace those from the super or are |
| 862 | // appended. For abstract classes, methods may be created in the vtable that aren't in |
| 863 | // virtual_ methods_ for miranda methods. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 864 | ObjectArray<ArtMethod>* vtable_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 865 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 866 | // Access flags; low 16 bits are defined by VM spec. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 867 | uint32_t access_flags_; |
| 868 | |
| 869 | // Total size of the Class instance; used when allocating storage on gc heap. |
| 870 | // See also object_size_. |
| 871 | size_t class_size_; |
| 872 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 873 | // Tid used to check for recursive <clinit> invocation. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 874 | pid_t clinit_thread_id_; |
| 875 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 876 | // ClassDef index in dex file, -1 if no class definition such as an array. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 877 | // TODO: really 16bits |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 878 | int32_t dex_class_def_idx_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 879 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 880 | // Type index in dex file. |
| 881 | // TODO: really 16bits |
| 882 | int32_t dex_type_idx_; |
| 883 | |
| 884 | // Number of instance fields that are object refs. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 885 | size_t num_reference_instance_fields_; |
| 886 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 887 | // Number of static fields that are object refs, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 888 | size_t num_reference_static_fields_; |
| 889 | |
| 890 | // Total object size; used when allocating storage on gc heap. |
| 891 | // (For interfaces and abstract classes this will be zero.) |
| 892 | // See also class_size_. |
| 893 | size_t object_size_; |
| 894 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 895 | // Primitive type value, or Primitive::kPrimNot (0); set for generated primitive classes. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 896 | Primitive::Type primitive_type_; |
| 897 | |
| 898 | // Bitmap of offsets of ifields. |
| 899 | uint32_t reference_instance_offsets_; |
| 900 | |
| 901 | // Bitmap of offsets of sfields. |
| 902 | uint32_t reference_static_offsets_; |
| 903 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 904 | // State of class initialization. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 905 | Status status_; |
| 906 | |
| 907 | // TODO: ? |
| 908 | // initiating class loader list |
| 909 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 910 | // values are kept in a table in gDvm. |
| 911 | // InitiatingLoaderList initiating_loader_list_; |
| 912 | |
| 913 | // Location of first static field. |
| 914 | uint32_t fields_[0]; |
| 915 | |
| 916 | // java.lang.Class |
| 917 | static Class* java_lang_Class_; |
| 918 | |
| 919 | friend struct art::ClassOffsets; // for verifying offset information |
| 920 | DISALLOW_IMPLICIT_CONSTRUCTORS(Class); |
| 921 | }; |
| 922 | |
| 923 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); |
| 924 | |
| 925 | class MANAGED ClassClass : public Class { |
| 926 | private: |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame^] | 927 | int32_t pad_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 928 | int64_t serialVersionUID_; |
| 929 | friend struct art::ClassClassOffsets; // for verifying offset information |
| 930 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass); |
| 931 | }; |
| 932 | |
| 933 | } // namespace mirror |
| 934 | } // namespace art |
| 935 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 936 | #endif // ART_RUNTIME_MIRROR_CLASS_H_ |