blob: 025e62a5cb5eb6625518bfb085d0050f814394c3 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080036inline uint32_t Class::GetObjectSize() {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037 DCHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Mathieu Chartier79b4f382013-10-23 15:21:37 -070038 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039}
40
Ian Rogersef7d42f2014-01-06 12:55:46 -080041inline Class* Class::GetSuperClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042 // Can only get super class for loaded classes (hack for when runtime is
43 // initializing)
Brian Carlstrom073278c2014-02-19 15:21:21 -080044 DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded();
Ian Rogersef7d42f2014-01-06 12:55:46 -080045 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046}
47
Ian Rogersef7d42f2014-01-06 12:55:46 -080048inline ClassLoader* Class::GetClassLoader() {
49 return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070050}
51
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080052template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080053inline DexCache* Class::GetDexCache() {
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080054 return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070055}
56
Ian Rogersef7d42f2014-01-06 12:55:46 -080057inline ObjectArray<ArtMethod>* Class::GetDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 DCHECK(IsLoaded() || IsErroneous());
Ian Rogersef7d42f2014-01-06 12:55:46 -080059 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_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080065 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());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010068 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
69 new_direct_methods, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070}
71
Ian Rogersef7d42f2014-01-06 12:55:46 -080072inline ArtMethod* Class::GetDirectMethod(int32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 return GetDirectMethods()->Get(i);
74}
75
Brian Carlstromea46f952013-07-30 01:26:50 -070076inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070077 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070078 ObjectArray<ArtMethod>* direct_methods =
Ian Rogersef7d42f2014-01-06 12:55:46 -080079 GetFieldObject<ObjectArray<ArtMethod> >(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010081 direct_methods->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082}
83
84// Returns the number of static, private, and constructor methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -080085inline uint32_t Class::NumDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
87}
88
Mathieu Chartier4e305412014-02-19 10:54:44 -080089template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080090inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 DCHECK(IsLoaded() || IsErroneous());
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 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
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010098 // methods.. they should really just be assigned once.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 DCHECK_NE(0, new_virtual_methods->GetLength());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100100 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
101 new_virtual_methods, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102}
103
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104inline uint32_t Class::NumVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
106}
107
Mathieu Chartier4e305412014-02-19 10:54:44 -0800108template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109inline ArtMethod* Class::GetVirtualMethod(uint32_t i) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800110 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 return GetVirtualMethods()->Get(i);
112}
113
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 DCHECK(IsLoaded() || IsErroneous());
116 return GetVirtualMethods()->Get(i);
117}
118
Brian Carlstromea46f952013-07-30 01:26:50 -0700119inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700121 ObjectArray<ArtMethod>* virtual_methods =
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122 GetFieldObject<ObjectArray<ArtMethod> >(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100124 virtual_methods->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125}
126
Ian Rogersef7d42f2014-01-06 12:55:46 -0800127inline ObjectArray<ArtMethod>* Class::GetVTable() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 DCHECK(IsResolved() || IsErroneous());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130}
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 DCHECK(IsLoaded() || IsErroneous());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135}
136
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139}
140
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141inline ObjectArray<ArtMethod>* Class::GetImTable() {
142 return GetFieldObject<ObjectArray<ArtMethod> >(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), false);
Jeff Hao88474b42013-10-23 16:24:40 -0700143}
144
145inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable, false);
Jeff Hao88474b42013-10-23 16:24:40 -0700147}
148
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149inline bool Class::Implements(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800150 DCHECK(klass != NULL);
151 DCHECK(klass->IsInterface()) << PrettyClass(this);
152 // All interfaces implemented directly and by our superclass, and
153 // recursively all super-interfaces of those interfaces, are listed
154 // in iftable_, so we can just do a linear scan through that.
155 int32_t iftable_count = GetIfTableCount();
156 IfTable* iftable = GetIfTable();
157 for (int32_t i = 0; i < iftable_count; i++) {
158 if (iftable->GetInterface(i) == klass) {
159 return true;
160 }
161 }
162 return false;
163}
164
165// Determine whether "this" is assignable from "src", where both of these
166// are array classes.
167//
168// Consider an array class, e.g. Y[][], where Y is a subclass of X.
169// Y[][] = Y[][] --> true (identity)
170// X[][] = Y[][] --> true (element superclass)
171// Y = Y[][] --> false
172// Y[] = Y[][] --> false
173// Object = Y[][] --> true (everything is an object)
174// Object[] = Y[][] --> true
175// Object[][] = Y[][] --> true
176// Object[][][] = Y[][] --> false (too many []s)
177// Serializable = Y[][] --> true (all arrays are Serializable)
178// Serializable[] = Y[][] --> true
179// Serializable[][] = Y[][] --> false (unless Y is Serializable)
180//
181// Don't forget about primitive types.
182// Object[] = int[] --> false
183//
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184inline bool Class::IsArrayAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800185 DCHECK(IsArrayClass()) << PrettyClass(this);
186 DCHECK(src->IsArrayClass()) << PrettyClass(src);
187 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
188}
189
Ian Rogersef7d42f2014-01-06 12:55:46 -0800190inline bool Class::IsAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800191 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
192 DCHECK(src->IsArrayClass()) << PrettyClass(src);
193 if (!IsArrayClass()) {
194 // If "this" is not also an array, it must be Object.
195 // src's super should be java_lang_Object, since it is an array.
196 Class* java_lang_Object = src->GetSuperClass();
197 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700198 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Ian Rogers693ff612013-02-01 10:56:12 -0800199 return this == java_lang_Object;
200 }
201 return IsArrayAssignableFromArray(src);
202}
203
Vladimir Marko89786432014-01-31 15:03:55 +0000204template <bool throw_on_failure, bool use_referrers_cache>
205inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206 uint32_t field_idx, DexCache* dex_cache) {
Vladimir Marko89786432014-01-31 15:03:55 +0000207 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000208 if (UNLIKELY(!this->CanAccess(access_to))) {
209 // The referrer class can't access the field's declaring class but may still be able
210 // to access the field if the FieldId specifies an accessible subclass of the declaring
211 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800212 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000213 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
214 // The referenced class has already been resolved with the field, get it from the dex cache.
215 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
216 DCHECK(dex_access_to != nullptr);
217 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
218 if (throw_on_failure) {
219 ThrowIllegalAccessErrorClass(this, dex_access_to);
220 }
221 return false;
222 }
223 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()),
224 this->CanAccessMember(dex_access_to, field->GetAccessFlags()));
225 }
226 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
227 return true;
228 }
229 if (throw_on_failure) {
230 ThrowIllegalAccessErrorField(this, field);
231 }
232 return false;
233}
234
Vladimir Marko89786432014-01-31 15:03:55 +0000235template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type>
236inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800237 uint32_t method_idx, DexCache* dex_cache) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000238 COMPILE_ASSERT(throw_on_failure || throw_invoke_type == kStatic, non_default_throw_invoke_type);
Vladimir Marko89786432014-01-31 15:03:55 +0000239 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000240 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.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800244 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000245 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
Vladimir Marko89786432014-01-31 15:03:55 +0000268inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 DexCache* dex_cache, uint32_t field_idx) {
270 return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000271}
272
273inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field,
274 uint32_t field_idx) {
275 return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr);
276}
277
278inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800279 DexCache* dex_cache, uint32_t method_idx) {
280 return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000281}
282
283template <InvokeType throw_invoke_type>
284inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method,
285 uint32_t method_idx) {
286 return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx,
287 nullptr);
288}
289
Ian Rogersef7d42f2014-01-06 12:55:46 -0800290inline bool Class::IsSubClass(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800291 DCHECK(!IsInterface()) << PrettyClass(this);
292 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800293 Class* current = this;
Ian Rogers693ff612013-02-01 10:56:12 -0800294 do {
295 if (current == klass) {
296 return true;
297 }
298 current = current->GetSuperClass();
299 } while (current != NULL);
300 return false;
301}
302
Ian Rogersef7d42f2014-01-06 12:55:46 -0800303inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) {
Ian Rogers693ff612013-02-01 10:56:12 -0800304 Class* declaring_class = method->GetDeclaringClass();
305 DCHECK(declaring_class != NULL) << PrettyClass(this);
306 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
307 // TODO cache to improve lookup speed
308 int32_t iftable_count = GetIfTableCount();
309 IfTable* iftable = GetIfTable();
310 for (int32_t i = 0; i < iftable_count; i++) {
311 if (iftable->GetInterface(i) == declaring_class) {
312 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
313 }
314 }
315 return NULL;
316}
317
Ian Rogersef7d42f2014-01-06 12:55:46 -0800318inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700319 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 // The argument method may from a super class.
321 // Use the index to a potentially overridden one for this instance's class.
322 return GetVTable()->Get(method->GetMethodIndex());
323}
324
Ian Rogersef7d42f2014-01-06 12:55:46 -0800325inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 DCHECK(!method->GetDeclaringClass()->IsInterface());
327 return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex());
328}
329
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 if (method->IsDirect()) {
332 return method;
333 }
Jeff Hao201803f2013-11-20 18:11:39 -0800334 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335 return FindVirtualMethodForInterface(method);
336 }
337 return FindVirtualMethodForVirtual(method);
338}
339
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340inline IfTable* Class::GetIfTable() {
341 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342}
343
Ian Rogersef7d42f2014-01-06 12:55:46 -0800344inline int32_t Class::GetIfTableCount() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 IfTable* iftable = GetIfTable();
346 if (iftable == NULL) {
347 return 0;
348 }
349 return iftable->Count();
350}
351
352inline void Class::SetIfTable(IfTable* new_iftable) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100353 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354}
355
Ian Rogersef7d42f2014-01-06 12:55:46 -0800356inline ObjectArray<ArtField>* Class::GetIFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 DCHECK(IsLoaded() || IsErroneous());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800358 return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359}
360
Brian Carlstromea46f952013-07-30 01:26:50 -0700361inline void Class::SetIFields(ObjectArray<ArtField>* new_ifields)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800363 DCHECK(NULL == GetFieldObject<ObjectArray<ArtField> >(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100365 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366}
367
Ian Rogersef7d42f2014-01-06 12:55:46 -0800368inline ObjectArray<ArtField>* Class::GetSFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 DCHECK(IsLoaded() || IsErroneous());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370 return GetFieldObject<ObjectArray<ArtField> >(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371}
372
Brian Carlstromea46f952013-07-30 01:26:50 -0700373inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800375 DCHECK(NULL == GetFieldObject<ObjectArray<ArtField> >(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100377 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378}
379
Ian Rogersef7d42f2014-01-06 12:55:46 -0800380inline uint32_t Class::NumStaticFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
382}
383
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384inline ArtField* Class::GetStaticField(uint32_t i) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
386 return GetSFields()->Get(i);
387}
388
Brian Carlstromea46f952013-07-30 01:26:50 -0700389inline void Class::SetStaticField(uint32_t i, ArtField* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800391 ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField> >(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100393 sfields->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394}
395
Ian Rogersef7d42f2014-01-06 12:55:46 -0800396inline uint32_t Class::NumInstanceFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
398}
399
Ian Rogersef7d42f2014-01-06 12:55:46 -0800400inline ArtField* Class::GetInstanceField(uint32_t i) { // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 DCHECK_NE(NumInstanceFields(), 0U);
402 return GetIFields()->Get(i);
403}
404
Brian Carlstromea46f952013-07-30 01:26:50 -0700405inline void Class::SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700406 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800407 ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField> >(
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100409 ifields->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410}
411
412inline void Class::SetVerifyErrorClass(Class* klass) {
413 CHECK(klass != NULL) << PrettyClass(this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100414 if (Runtime::Current()->IsActiveTransaction()) {
415 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
416 } else {
417 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
418 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419}
420
Mathieu Chartier4e305412014-02-19 10:54:44 -0800421template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800422inline uint32_t Class::GetAccessFlags() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 // Check class is loaded or this is java.lang.String that has a
424 // circularity issue during loading the names of its members
Mathieu Chartier4e305412014-02-19 10:54:44 -0800425 DCHECK(IsLoaded<kVerifyFlags>() ||
426 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427 this == String::GetJavaLangString() ||
Brian Carlstromea46f952013-07-30 01:26:50 -0700428 this == ArtField::GetJavaLangReflectArtField() ||
429 this == ArtMethod::GetJavaLangReflectArtMethod());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800430 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431}
432
Ian Rogersef7d42f2014-01-06 12:55:46 -0800433inline String* Class::GetName() {
434 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435}
436inline void Class::SetName(String* name) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100437 if (Runtime::Current()->IsActiveTransaction()) {
438 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
439 } else {
440 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
441 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442}
443
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700444inline void Class::CheckObjectAlloc() {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700445 DCHECK(!IsArrayClass())
446 << PrettyClass(this)
447 << "A array shouldn't be allocated through this "
448 << "as it requires a pre-fence visitor that sets the class size.";
449 DCHECK(!IsClassClass())
450 << PrettyClass(this)
451 << "A class object shouldn't be allocated through this "
452 << "as it requires a pre-fence visitor that sets the class size.";
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700453 DCHECK(IsInstantiable()) << PrettyClass(this);
454 // TODO: decide whether we want this check. It currently fails during bootstrap.
455 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
456 DCHECK_GE(this->object_size_, sizeof(Object));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700457}
458
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800459template <bool kIsInstrumented>
460inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700461 CheckObjectAlloc();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700462 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800463 return heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_,
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700464 allocator_type, VoidFunctor());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800465}
466
467inline Object* Class::AllocObject(Thread* self) {
468 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
469}
470
471inline Object* Class::AllocNonMovableObject(Thread* self) {
472 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700473}
474
Mathieu Chartier407f7022014-02-18 14:37:05 -0800475template <bool kVisitClass, typename Visitor>
476inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
477 VisitInstanceFieldsReferences<kVisitClass>(klass, visitor);
478 VisitStaticFieldsReferences<kVisitClass>(this, visitor);
479}
480
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481} // namespace mirror
482} // namespace art
483
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700484#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_