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