class-inl.h revision 52e4b43d62896b56f8c2bd041e528472bb4a0d8d
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_INL_H_
18#define ART_RUNTIME_MIRROR_CLASS_INL_H_
19
20#include "class.h"
21
22#include "art_field.h"
23#include "art_method.h"
24#include "class_linker-inl.h"
25#include "class_loader.h"
26#include "common_throws.h"
27#include "dex_cache.h"
28#include "dex_file.h"
29#include "gc/heap-inl.h"
30#include "iftable.h"
31#include "object_array-inl.h"
32#include "runtime.h"
33#include "string.h"
34
35namespace art {
36namespace mirror {
37
38template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
39inline uint32_t Class::GetObjectSize() {
40  if (kIsDebugBuild) {
41    // Use a local variable as (D)CHECK can't handle the space between
42    // the two template params.
43    bool is_variable_size = IsVariableSize<kVerifyFlags, kReadBarrierOption>();
44    CHECK(!is_variable_size) << " class=" << PrettyTypeOf(this);
45  }
46  return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_));
47}
48
49inline Class* Class::GetSuperClass() {
50  // Can only get super class for loaded classes (hack for when runtime is
51  // initializing)
52  DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded();
53  return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_));
54}
55
56inline ClassLoader* Class::GetClassLoader() {
57  return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_));
58}
59
60template<VerifyObjectFlags kVerifyFlags>
61inline DexCache* Class::GetDexCache() {
62  return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_));
63}
64
65inline ObjectArray<ArtMethod>* Class::GetDirectMethods() {
66  DCHECK(IsLoaded() || IsErroneous());
67  return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
68}
69
70inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods)
71    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
72  DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod>>(
73      OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)));
74  DCHECK_NE(0, new_direct_methods->GetLength());
75  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), new_direct_methods);
76}
77
78inline ArtMethod* Class::GetDirectMethod(int32_t i) {
79  return GetDirectMethods()->Get(i);
80}
81
82inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f)  // TODO: uint16_t
83    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
84  ObjectArray<ArtMethod>* direct_methods =
85      GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
86  direct_methods->Set<false>(i, f);
87}
88
89// Returns the number of static, private, and constructor methods.
90inline uint32_t Class::NumDirectMethods() {
91  return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
92}
93
94template<VerifyObjectFlags kVerifyFlags>
95inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() {
96  DCHECK(IsLoaded() || IsErroneous());
97  return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
98}
99
100inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) {
101  // TODO: we reassign virtual methods to grow the table for miranda
102  // methods.. they should really just be assigned once.
103  DCHECK_NE(0, new_virtual_methods->GetLength());
104  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), new_virtual_methods);
105}
106
107inline uint32_t Class::NumVirtualMethods() {
108  return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
109}
110
111template<VerifyObjectFlags kVerifyFlags>
112inline ArtMethod* Class::GetVirtualMethod(uint32_t i) {
113  DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
114  return GetVirtualMethods()->Get(i);
115}
116
117inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) {
118  DCHECK(IsLoaded() || IsErroneous());
119  return GetVirtualMethods()->Get(i);
120}
121
122inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f)  // TODO: uint16_t
123    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
124  ObjectArray<ArtMethod>* virtual_methods =
125      GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
126  virtual_methods->Set<false>(i, f);
127}
128
129inline ObjectArray<ArtMethod>* Class::GetVTable() {
130  DCHECK(IsResolved() || IsErroneous());
131  return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
132}
133
134inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() {
135  DCHECK(IsLoaded() || IsErroneous());
136  return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
137}
138
139inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) {
140  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable);
141}
142
143inline ObjectArray<ArtMethod>* Class::GetImTable() {
144  return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_));
145}
146
147inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) {
148  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable);
149}
150
151inline bool Class::Implements(Class* klass) {
152  DCHECK(klass != NULL);
153  DCHECK(klass->IsInterface()) << PrettyClass(this);
154  // All interfaces implemented directly and by our superclass, and
155  // recursively all super-interfaces of those interfaces, are listed
156  // in iftable_, so we can just do a linear scan through that.
157  int32_t iftable_count = GetIfTableCount();
158  IfTable* iftable = GetIfTable();
159  for (int32_t i = 0; i < iftable_count; i++) {
160    if (iftable->GetInterface(i) == klass) {
161      return true;
162    }
163  }
164  return false;
165}
166
167// Determine whether "this" is assignable from "src", where both of these
168// are array classes.
169//
170// Consider an array class, e.g. Y[][], where Y is a subclass of X.
171//   Y[][]            = Y[][] --> true (identity)
172//   X[][]            = Y[][] --> true (element superclass)
173//   Y                = Y[][] --> false
174//   Y[]              = Y[][] --> false
175//   Object           = Y[][] --> true (everything is an object)
176//   Object[]         = Y[][] --> true
177//   Object[][]       = Y[][] --> true
178//   Object[][][]     = Y[][] --> false (too many []s)
179//   Serializable     = Y[][] --> true (all arrays are Serializable)
180//   Serializable[]   = Y[][] --> true
181//   Serializable[][] = Y[][] --> false (unless Y is Serializable)
182//
183// Don't forget about primitive types.
184//   Object[]         = int[] --> false
185//
186inline bool Class::IsArrayAssignableFromArray(Class* src) {
187  DCHECK(IsArrayClass())  << PrettyClass(this);
188  DCHECK(src->IsArrayClass()) << PrettyClass(src);
189  return GetComponentType()->IsAssignableFrom(src->GetComponentType());
190}
191
192inline bool Class::IsAssignableFromArray(Class* src) {
193  DCHECK(!IsInterface()) << PrettyClass(this);  // handled first in IsAssignableFrom
194  DCHECK(src->IsArrayClass()) << PrettyClass(src);
195  if (!IsArrayClass()) {
196    // If "this" is not also an array, it must be Object.
197    // src's super should be java_lang_Object, since it is an array.
198    Class* java_lang_Object = src->GetSuperClass();
199    DCHECK(java_lang_Object != NULL) << PrettyClass(src);
200    DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
201    return this == java_lang_Object;
202  }
203  return IsArrayAssignableFromArray(src);
204}
205
206template <bool throw_on_failure, bool use_referrers_cache>
207inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field,
208                                           uint32_t field_idx, DexCache* dex_cache) {
209  DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
210  if (UNLIKELY(!this->CanAccess(access_to))) {
211    // The referrer class can't access the field's declaring class but may still be able
212    // to access the field if the FieldId specifies an accessible subclass of the declaring
213    // class rather than the declaring class itself.
214    DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
215    uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
216    // The referenced class has already been resolved with the field, get it from the dex cache.
217    Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
218    DCHECK(dex_access_to != nullptr);
219    if (UNLIKELY(!this->CanAccess(dex_access_to))) {
220      if (throw_on_failure) {
221        ThrowIllegalAccessErrorClass(this, dex_access_to);
222      }
223      return false;
224    }
225    DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()),
226              this->CanAccessMember(dex_access_to, field->GetAccessFlags()));
227  }
228  if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
229    return true;
230  }
231  if (throw_on_failure) {
232    ThrowIllegalAccessErrorField(this, field);
233  }
234  return false;
235}
236
237template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type>
238inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method,
239                                            uint32_t method_idx, DexCache* dex_cache) {
240  COMPILE_ASSERT(throw_on_failure || throw_invoke_type == kStatic, non_default_throw_invoke_type);
241  DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
242  if (UNLIKELY(!this->CanAccess(access_to))) {
243    // The referrer class can't access the method's declaring class but may still be able
244    // to access the method if the MethodId specifies an accessible subclass of the declaring
245    // class rather than the declaring class itself.
246    DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
247    uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
248    // The referenced class has already been resolved with the method, get it from the dex cache.
249    Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
250    DCHECK(dex_access_to != nullptr);
251    if (UNLIKELY(!this->CanAccess(dex_access_to))) {
252      if (throw_on_failure) {
253        ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to,
254                                                      method, throw_invoke_type);
255      }
256      return false;
257    }
258    DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()),
259              this->CanAccessMember(dex_access_to, method->GetAccessFlags()));
260  }
261  if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) {
262    return true;
263  }
264  if (throw_on_failure) {
265    ThrowIllegalAccessErrorMethod(this, method);
266  }
267  return false;
268}
269
270inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
271                                          DexCache* dex_cache, uint32_t field_idx) {
272  return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache);
273}
274
275inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field,
276                                            uint32_t field_idx) {
277  return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr);
278}
279
280inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
281                                           DexCache* dex_cache, uint32_t method_idx) {
282  return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache);
283}
284
285template <InvokeType throw_invoke_type>
286inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method,
287                                             uint32_t method_idx) {
288  return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx,
289                                                                 nullptr);
290}
291
292inline bool Class::IsSubClass(Class* klass) {
293  DCHECK(!IsInterface()) << PrettyClass(this);
294  DCHECK(!IsArrayClass()) << PrettyClass(this);
295  Class* current = this;
296  do {
297    if (current == klass) {
298      return true;
299    }
300    current = current->GetSuperClass();
301  } while (current != NULL);
302  return false;
303}
304
305inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) {
306  Class* declaring_class = method->GetDeclaringClass();
307  DCHECK(declaring_class != NULL) << PrettyClass(this);
308  DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
309  // TODO cache to improve lookup speed
310  int32_t iftable_count = GetIfTableCount();
311  IfTable* iftable = GetIfTable();
312  for (int32_t i = 0; i < iftable_count; i++) {
313    if (iftable->GetInterface(i) == declaring_class) {
314      return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
315    }
316  }
317  return NULL;
318}
319
320inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) {
321  DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
322  // The argument method may from a super class.
323  // Use the index to a potentially overridden one for this instance's class.
324  return GetVTable()->Get(method->GetMethodIndex());
325}
326
327inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) {
328  DCHECK(!method->GetDeclaringClass()->IsInterface());
329  return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex());
330}
331
332inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) {
333  if (method->IsDirect()) {
334    return method;
335  }
336  if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) {
337    return FindVirtualMethodForInterface(method);
338  }
339  return FindVirtualMethodForVirtual(method);
340}
341
342inline IfTable* Class::GetIfTable() {
343  return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_));
344}
345
346inline int32_t Class::GetIfTableCount() {
347  IfTable* iftable = GetIfTable();
348  if (iftable == NULL) {
349    return 0;
350  }
351  return iftable->Count();
352}
353
354inline void Class::SetIfTable(IfTable* new_iftable) {
355  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable);
356}
357
358inline ObjectArray<ArtField>* Class::GetIFields() {
359  DCHECK(IsLoaded() || IsErroneous());
360  return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
361}
362
363inline void Class::SetIFields(ObjectArray<ArtField>* new_ifields)
364    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
365  DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)));
366  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
367}
368
369inline ObjectArray<ArtField>* Class::GetSFields() {
370  DCHECK(IsLoaded() || IsErroneous());
371  return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
372}
373
374inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields)
375    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
376  DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)));
377  SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
378}
379
380inline uint32_t Class::NumStaticFields() {
381  return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
382}
383
384
385inline ArtField* Class::GetStaticField(uint32_t i)  // TODO: uint16_t
386    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
387  return GetSFields()->GetWithoutChecks(i);
388}
389
390inline void Class::SetStaticField(uint32_t i, ArtField* f)  // TODO: uint16_t
391    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
392  ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField>>(
393      OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
394  sfields->Set<false>(i, f);
395}
396
397inline uint32_t Class::NumInstanceFields() {
398  return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
399}
400
401inline ArtField* Class::GetInstanceField(uint32_t i) {  // TODO: uint16_t
402  DCHECK_NE(NumInstanceFields(), 0U);
403  return GetIFields()->GetWithoutChecks(i);
404}
405
406inline void Class::SetInstanceField(uint32_t i, ArtField* f)  // TODO: uint16_t
407    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
408  ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField>>(
409      OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
410  ifields->Set<false>(i, f);
411}
412
413template<VerifyObjectFlags kVerifyFlags>
414inline uint32_t Class::GetReferenceInstanceOffsets() {
415  DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
416  return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_));
417}
418
419inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) {
420  if (Runtime::Current()->IsActiveTransaction()) {
421    SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
422  } else {
423    SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
424  }
425}
426
427inline void Class::SetVerifyErrorClass(Class* klass) {
428  CHECK(klass != NULL) << PrettyClass(this);
429  if (Runtime::Current()->IsActiveTransaction()) {
430    SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
431  } else {
432    SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
433  }
434}
435
436template<VerifyObjectFlags kVerifyFlags>
437inline uint32_t Class::GetAccessFlags() {
438  // Check class is loaded or this is java.lang.String that has a
439  // circularity issue during loading the names of its members
440  DCHECK(IsLoaded<kVerifyFlags>() ||
441         IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
442         this == String::GetJavaLangString() ||
443         this == ArtField::GetJavaLangReflectArtField() ||
444         this == ArtMethod::GetJavaLangReflectArtMethod());
445  return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_));
446}
447
448inline String* Class::GetName() {
449  return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_));
450}
451inline void Class::SetName(String* name) {
452  if (Runtime::Current()->IsActiveTransaction()) {
453    SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
454  } else {
455    SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
456  }
457}
458
459template<VerifyObjectFlags kVerifyFlags>
460inline Primitive::Type Class::GetPrimitiveType() {
461  DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
462  return static_cast<Primitive::Type>(
463      GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)));
464}
465
466inline void Class::CheckObjectAlloc() {
467  DCHECK(!IsArrayClass())
468      << PrettyClass(this)
469      << "A array shouldn't be allocated through this "
470      << "as it requires a pre-fence visitor that sets the class size.";
471  DCHECK(!IsClassClass())
472      << PrettyClass(this)
473      << "A class object shouldn't be allocated through this "
474      << "as it requires a pre-fence visitor that sets the class size.";
475  DCHECK(IsInstantiable()) << PrettyClass(this);
476  // TODO: decide whether we want this check. It currently fails during bootstrap.
477  // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
478  DCHECK_GE(this->object_size_, sizeof(Object));
479}
480
481template<bool kIsInstrumented, bool kCheckAddFinalizer>
482inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
483  CheckObjectAlloc();
484  gc::Heap* heap = Runtime::Current()->GetHeap();
485  const bool add_finalizer = kCheckAddFinalizer && IsFinalizable();
486  if (!kCheckAddFinalizer) {
487    DCHECK(!IsFinalizable());
488  }
489  mirror::Object* obj =
490      heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_,
491                                                             allocator_type, VoidFunctor());
492  if (add_finalizer && LIKELY(obj != nullptr)) {
493    heap->AddFinalizerReference(self, &obj);
494  }
495  return obj;
496}
497
498inline Object* Class::AllocObject(Thread* self) {
499  return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
500}
501
502inline Object* Class::AllocNonMovableObject(Thread* self) {
503  return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
504}
505
506template <bool kVisitClass, typename Visitor>
507inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
508  // Visit the static fields first so that we don't overwrite the SFields / IFields instance
509  // fields.
510  VisitStaticFieldsReferences<kVisitClass>(this, visitor);
511  VisitInstanceFieldsReferences<kVisitClass>(klass, visitor);
512}
513
514inline bool Class::IsArtFieldClass() const {
515  return this == ArtField::GetJavaLangReflectArtField();
516}
517
518inline bool Class::IsArtMethodClass() const {
519  return this == ArtMethod::GetJavaLangReflectArtMethod();
520}
521
522template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
523inline bool Class::IsClassClass() {
524  Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()->
525      template GetClass<kVerifyFlags, kReadBarrierOption>();
526  return this == java_lang_Class;
527}
528
529inline const DexFile& Class::GetDexFile() {
530  return *GetDexCache()->GetDexFile();
531}
532
533inline bool Class::DescriptorEquals(const char* match) {
534  if (UNLIKELY(IsArrayClass())) {
535    return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1);
536  } else if (UNLIKELY(IsPrimitive())) {
537    return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0;
538  } else if (UNLIKELY(IsProxyClass())) {
539    return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
540  } else {
541    const DexFile& dex_file = GetDexFile();
542    const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
543    return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0;
544  }
545}
546
547}  // namespace mirror
548}  // namespace art
549
550#endif  // ART_RUNTIME_MIRROR_CLASS_INL_H_
551