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