blob: cd44ebcb65a3dacba3cac653c43c460a6af0170c [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_MIRROR_CLASS_INL_H_
18#define ART_RUNTIME_MIRROR_CLASS_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
20#include "class.h"
21
Brian Carlstromea46f952013-07-30 01:26:50 -070022#include "art_field.h"
23#include "art_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "class_loader.h"
Vladimir Marko23a28212014-01-09 19:24:37 +000025#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_cache.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070027#include "gc/heap-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "iftable.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080029#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "runtime.h"
31#include "string.h"
32
33namespace art {
34namespace mirror {
35
36inline size_t Class::GetObjectSize() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037 DCHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Mathieu Chartier79b4f382013-10-23 15:21:37 -070039 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040}
41
42inline Class* Class::GetSuperClass() const {
43 // Can only get super class for loaded classes (hack for when runtime is
44 // initializing)
45 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
46 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
47}
48
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070049inline ClassLoader* Class::GetClassLoader() const {
50 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
51}
52
53inline DexCache* Class::GetDexCache() const {
54 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
55}
56
Brian Carlstromea46f952013-07-30 01:26:50 -070057inline ObjectArray<ArtMethod>* Class::GetDirectMethods() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromea46f952013-07-30 01:26:50 -070059 return GetFieldObject<ObjectArray<ArtMethod>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
61}
62
Brian Carlstromea46f952013-07-30 01:26:50 -070063inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070065 DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
67 DCHECK_NE(0, new_direct_methods->GetLength());
68 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
69 new_direct_methods, false);
70}
71
Brian Carlstromea46f952013-07-30 01:26:50 -070072inline ArtMethod* Class::GetDirectMethod(int32_t i) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 return GetDirectMethods()->Get(i);
75}
76
Brian Carlstromea46f952013-07-30 01:26:50 -070077inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070078 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070079 ObjectArray<ArtMethod>* direct_methods =
80 GetFieldObject<ObjectArray<ArtMethod>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
82 direct_methods->Set(i, f);
83}
84
85// Returns the number of static, private, and constructor methods.
86inline size_t Class::NumDirectMethods() const {
87 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
88}
89
Brian Carlstromea46f952013-07-30 01:26:50 -070090inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromea46f952013-07-30 01:26:50 -070092 return GetFieldObject<ObjectArray<ArtMethod>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
94}
95
Brian Carlstromea46f952013-07-30 01:26:50 -070096inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 // TODO: we reassign virtual methods to grow the table for miranda
98 // methods.. they should really just be assigned once
99 DCHECK_NE(0, new_virtual_methods->GetLength());
100 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
101 new_virtual_methods, false);
102}
103
104inline size_t Class::NumVirtualMethods() const {
105 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
106}
107
Brian Carlstromea46f952013-07-30 01:26:50 -0700108inline ArtMethod* Class::GetVirtualMethod(uint32_t i) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
110 DCHECK(IsResolved() || IsErroneous());
111 return GetVirtualMethods()->Get(i);
112}
113
Brian Carlstromea46f952013-07-30 01:26:50 -0700114inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
116 DCHECK(IsLoaded() || IsErroneous());
117 return GetVirtualMethods()->Get(i);
118}
119
Brian Carlstromea46f952013-07-30 01:26:50 -0700120inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700122 ObjectArray<ArtMethod>* virtual_methods =
123 GetFieldObject<ObjectArray<ArtMethod>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
125 virtual_methods->Set(i, f);
126}
127
Brian Carlstromea46f952013-07-30 01:26:50 -0700128inline ObjectArray<ArtMethod>* Class::GetVTable() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 DCHECK(IsResolved() || IsErroneous());
Brian Carlstromea46f952013-07-30 01:26:50 -0700130 return GetFieldObject<ObjectArray<ArtMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131}
132
Brian Carlstromea46f952013-07-30 01:26:50 -0700133inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromea46f952013-07-30 01:26:50 -0700135 return GetFieldObject<ObjectArray<ArtMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136}
137
Brian Carlstromea46f952013-07-30 01:26:50 -0700138inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
140 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
141}
142
Jeff Hao88474b42013-10-23 16:24:40 -0700143inline ObjectArray<ArtMethod>* Class::GetImTable() const {
144 return GetFieldObject<ObjectArray<ArtMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), false);
145}
146
147inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) {
148 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable, false);
149}
150
Ian Rogers693ff612013-02-01 10:56:12 -0800151inline bool Class::Implements(const Class* klass) const {
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(const Class* src) const {
187 DCHECK(IsArrayClass()) << PrettyClass(this);
188 DCHECK(src->IsArrayClass()) << PrettyClass(src);
189 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
190}
191
192inline bool Class::IsAssignableFromArray(const Class* src) const {
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);
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700200 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Ian Rogers693ff612013-02-01 10:56:12 -0800201 return this == java_lang_Object;
202 }
203 return IsArrayAssignableFromArray(src);
204}
205
Vladimir Marko23a28212014-01-09 19:24:37 +0000206template <bool throw_on_failure>
207inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
208 uint32_t field_idx) {
209 if (UNLIKELY(!this->CanAccess(access_to))) {
210 // The referrer class can't access the field's declaring class but may still be able
211 // to access the field if the FieldId specifies an accessible subclass of the declaring
212 // class rather than the declaring class itself.
213 DexCache* referrer_dex_cache = this->GetDexCache();
214 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
215 // The referenced class has already been resolved with the field, get it from the dex cache.
216 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
217 DCHECK(dex_access_to != nullptr);
218 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
219 if (throw_on_failure) {
220 ThrowIllegalAccessErrorClass(this, dex_access_to);
221 }
222 return false;
223 }
224 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()),
225 this->CanAccessMember(dex_access_to, field->GetAccessFlags()));
226 }
227 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
228 return true;
229 }
230 if (throw_on_failure) {
231 ThrowIllegalAccessErrorField(this, field);
232 }
233 return false;
234}
235
236template <bool throw_on_failure, InvokeType throw_invoke_type>
237inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
238 uint32_t method_idx) {
239 COMPILE_ASSERT(throw_on_failure || throw_invoke_type == kStatic, non_default_throw_invoke_type);
240 if (UNLIKELY(!this->CanAccess(access_to))) {
241 // The referrer class can't access the method's declaring class but may still be able
242 // to access the method if the MethodId specifies an accessible subclass of the declaring
243 // class rather than the declaring class itself.
244 DexCache* referrer_dex_cache = this->GetDexCache();
245 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
246 // The referenced class has already been resolved with the method, get it from the dex cache.
247 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
248 DCHECK(dex_access_to != nullptr);
249 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
250 if (throw_on_failure) {
251 ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to,
252 method, throw_invoke_type);
253 }
254 return false;
255 }
256 DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()),
257 this->CanAccessMember(dex_access_to, method->GetAccessFlags()));
258 }
259 if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) {
260 return true;
261 }
262 if (throw_on_failure) {
263 ThrowIllegalAccessErrorMethod(this, method);
264 }
265 return false;
266}
267
Ian Rogers693ff612013-02-01 10:56:12 -0800268inline bool Class::IsSubClass(const Class* klass) const {
269 DCHECK(!IsInterface()) << PrettyClass(this);
270 DCHECK(!IsArrayClass()) << PrettyClass(this);
271 const Class* current = this;
272 do {
273 if (current == klass) {
274 return true;
275 }
276 current = current->GetSuperClass();
277 } while (current != NULL);
278 return false;
279}
280
Brian Carlstromea46f952013-07-30 01:26:50 -0700281inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) const {
Ian Rogers693ff612013-02-01 10:56:12 -0800282 Class* declaring_class = method->GetDeclaringClass();
283 DCHECK(declaring_class != NULL) << PrettyClass(this);
284 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
285 // TODO cache to improve lookup speed
286 int32_t iftable_count = GetIfTableCount();
287 IfTable* iftable = GetIfTable();
288 for (int32_t i = 0; i < iftable_count; i++) {
289 if (iftable->GetInterface(i) == declaring_class) {
290 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
291 }
292 }
293 return NULL;
294}
295
Brian Carlstromea46f952013-07-30 01:26:50 -0700296inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700298 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 // The argument method may from a super class.
300 // Use the index to a potentially overridden one for this instance's class.
301 return GetVTable()->Get(method->GetMethodIndex());
302}
303
Brian Carlstromea46f952013-07-30 01:26:50 -0700304inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) const
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
306 DCHECK(!method->GetDeclaringClass()->IsInterface());
307 return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex());
308}
309
Brian Carlstromea46f952013-07-30 01:26:50 -0700310inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 if (method->IsDirect()) {
312 return method;
313 }
Jeff Hao201803f2013-11-20 18:11:39 -0800314 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 return FindVirtualMethodForInterface(method);
316 }
317 return FindVirtualMethodForVirtual(method);
318}
319
320inline IfTable* Class::GetIfTable() const {
321 return GetFieldObject<IfTable*>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
322}
323
324inline int32_t Class::GetIfTableCount() const {
325 IfTable* iftable = GetIfTable();
326 if (iftable == NULL) {
327 return 0;
328 }
329 return iftable->Count();
330}
331
332inline void Class::SetIfTable(IfTable* new_iftable) {
333 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
334}
335
Brian Carlstromea46f952013-07-30 01:26:50 -0700336inline ObjectArray<ArtField>* Class::GetIFields() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromea46f952013-07-30 01:26:50 -0700338 return GetFieldObject<ObjectArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339}
340
Brian Carlstromea46f952013-07-30 01:26:50 -0700341inline void Class::SetIFields(ObjectArray<ArtField>* new_ifields)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700343 DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
345 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
346}
347
Brian Carlstromea46f952013-07-30 01:26:50 -0700348inline ObjectArray<ArtField>* Class::GetSFields() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromea46f952013-07-30 01:26:50 -0700350 return GetFieldObject<ObjectArray<ArtField>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351}
352
Brian Carlstromea46f952013-07-30 01:26:50 -0700353inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700355 DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
357 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
358}
359
360inline size_t Class::NumStaticFields() const {
361 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
362}
363
Brian Carlstromea46f952013-07-30 01:26:50 -0700364inline ArtField* Class::GetStaticField(uint32_t i) const // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
366 return GetSFields()->Get(i);
367}
368
Brian Carlstromea46f952013-07-30 01:26:50 -0700369inline void Class::SetStaticField(uint32_t i, ArtField* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700371 ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
373 sfields->Set(i, f);
374}
375
376inline size_t Class::NumInstanceFields() const {
377 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
378}
379
Brian Carlstromea46f952013-07-30 01:26:50 -0700380inline ArtField* Class::GetInstanceField(uint32_t i) const // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 DCHECK_NE(NumInstanceFields(), 0U);
383 return GetIFields()->Get(i);
384}
385
Brian Carlstromea46f952013-07-30 01:26:50 -0700386inline void Class::SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700387 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700388 ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField>*>(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
390 ifields->Set(i, f);
391}
392
393inline void Class::SetVerifyErrorClass(Class* klass) {
394 CHECK(klass != NULL) << PrettyClass(this);
395 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
396}
397
398inline uint32_t Class::GetAccessFlags() const {
399 // Check class is loaded or this is java.lang.String that has a
400 // circularity issue during loading the names of its members
401 DCHECK(IsLoaded() || IsErroneous() ||
402 this == String::GetJavaLangString() ||
Brian Carlstromea46f952013-07-30 01:26:50 -0700403 this == ArtField::GetJavaLangReflectArtField() ||
404 this == ArtMethod::GetJavaLangReflectArtMethod());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
406}
407
408inline String* Class::GetName() const {
409 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
410}
411inline void Class::SetName(String* name) {
412 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
413}
414
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700415inline void Class::CheckObjectAlloc() {
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700416 DCHECK(!IsArrayClass()) << PrettyClass(this);
417 DCHECK(IsInstantiable()) << PrettyClass(this);
418 // TODO: decide whether we want this check. It currently fails during bootstrap.
419 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
420 DCHECK_GE(this->object_size_, sizeof(Object));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700421}
422
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800423template <bool kIsInstrumented>
424inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700425 CheckObjectAlloc();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700426 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800427 return heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_,
428 allocator_type);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800429}
430
431inline Object* Class::AllocObject(Thread* self) {
432 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
433}
434
435inline Object* Class::AllocNonMovableObject(Thread* self) {
436 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700437}
438
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439} // namespace mirror
440} // namespace art
441
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700442#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_