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