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