blob: f4656ec78b6d4a3c5b302206732c125f2daffb2c [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
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "art_field-inl.h"
23#include "art_method-inl.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"
Mathieu Chartierf8322842014-05-16 10:59:25 -070027#include "dex_file.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070028#include "gc/heap-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "iftable.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080030#include "object_array-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "read_barrier-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070032#include "reference-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "runtime.h"
34#include "string.h"
35
36namespace art {
37namespace mirror {
38
Hiroshi Yamauchi25023c72014-05-09 11:45:53 -070039template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080040inline uint32_t Class::GetObjectSize() {
Vladimir Marko3481ba22015-04-13 12:22:36 +010041 // Note: Extra parentheses to avoid the comma being interpreted as macro parameter separator.
42 DCHECK((!IsVariableSize<kVerifyFlags, kReadBarrierOption>())) << " class=" << PrettyTypeOf(this);
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -070043 return GetField32(ObjectSizeOffset());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044}
45
Ian Rogersef7d42f2014-01-06 12:55:46 -080046inline Class* Class::GetSuperClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 // Can only get super class for loaded classes (hack for when runtime is
48 // initializing)
Brian Carlstrom073278c2014-02-19 15:21:21 -080049 DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded();
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070050 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051}
52
Ian Rogersef7d42f2014-01-06 12:55:46 -080053inline ClassLoader* Class::GetClassLoader() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070054 return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070055}
56
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080057template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080058inline DexCache* Class::GetDexCache() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059 return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070060}
61
Ian Rogersef7d42f2014-01-06 12:55:46 -080062inline ObjectArray<ArtMethod>* Class::GetDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070064 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065}
66
Brian Carlstromea46f952013-07-30 01:26:50 -070067inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -070069 DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070070 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 DCHECK_NE(0, new_direct_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070072 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), new_direct_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073}
74
Ian Rogersef7d42f2014-01-06 12:55:46 -080075inline ArtMethod* Class::GetDirectMethod(int32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 return GetDirectMethods()->Get(i);
77}
78
Brian Carlstromea46f952013-07-30 01:26:50 -070079inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070080 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070081 ObjectArray<ArtMethod>* direct_methods =
Ian Rogers700a4022014-05-19 16:49:03 -070082 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010083 direct_methods->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084}
85
86// Returns the number of static, private, and constructor methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -080087inline uint32_t Class::NumDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
89}
90
Mathieu Chartier4e305412014-02-19 10:54:44 -080091template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080092inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070094 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095}
96
Brian Carlstromea46f952013-07-30 01:26:50 -070097inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 // TODO: we reassign virtual methods to grow the table for miranda
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010099 // methods.. they should really just be assigned once.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 DCHECK_NE(0, new_virtual_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700101 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), new_virtual_methods);
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) {
Hiroshi Yamauchif4c15a12014-10-20 16:56:58 -0700110 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>())
111 << PrettyClass(this) << " status=" << GetStatus();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700112 return GetVirtualMethods()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113}
114
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 DCHECK(IsLoaded() || IsErroneous());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700117 return GetVirtualMethods()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118}
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 =
Ian Rogers700a4022014-05-19 16:49:03 -0700123 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700124 virtual_methods->SetWithoutChecks<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 Rogers700a4022014-05-19 16:49:03 -0700129 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
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 Rogers700a4022014-05-19 16:49:03 -0700134 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135}
136
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700138 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139}
140
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700141inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i) {
142 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
143 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset));
144}
145
146inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method) {
147 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
148 SetFieldObject<false>(MemberOffset(offset), method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700149}
150
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700151inline bool Class::HasVTable() {
152 return (GetVTable() != nullptr) || ShouldHaveEmbeddedImtAndVTable();
153}
154
155inline int32_t Class::GetVTableLength() {
156 if (ShouldHaveEmbeddedImtAndVTable()) {
157 return GetEmbeddedVTableLength();
158 }
159 return (GetVTable() != nullptr) ? GetVTable()->GetLength() : 0;
160}
161
162inline ArtMethod* Class::GetVTableEntry(uint32_t i) {
163 if (ShouldHaveEmbeddedImtAndVTable()) {
164 return GetEmbeddedVTableEntry(i);
165 }
166 return (GetVTable() != nullptr) ? GetVTable()->Get(i) : nullptr;
167}
168
169inline int32_t Class::GetEmbeddedVTableLength() {
170 return GetField32(EmbeddedVTableLengthOffset());
171}
172
173inline void Class::SetEmbeddedVTableLength(int32_t len) {
174 SetField32<false>(EmbeddedVTableLengthOffset(), len);
175}
176
177inline ArtMethod* Class::GetEmbeddedVTableEntry(uint32_t i) {
178 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry);
179 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset));
180}
181
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700182inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method) {
183 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry);
184 SetFieldObject<false>(MemberOffset(offset), method);
185 CHECK(method == GetVTableDuringLinking()->Get(i));
186}
187
Ian Rogersef7d42f2014-01-06 12:55:46 -0800188inline bool Class::Implements(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800189 DCHECK(klass != NULL);
190 DCHECK(klass->IsInterface()) << PrettyClass(this);
191 // All interfaces implemented directly and by our superclass, and
192 // recursively all super-interfaces of those interfaces, are listed
193 // in iftable_, so we can just do a linear scan through that.
194 int32_t iftable_count = GetIfTableCount();
195 IfTable* iftable = GetIfTable();
196 for (int32_t i = 0; i < iftable_count; i++) {
197 if (iftable->GetInterface(i) == klass) {
198 return true;
199 }
200 }
201 return false;
202}
203
204// Determine whether "this" is assignable from "src", where both of these
205// are array classes.
206//
207// Consider an array class, e.g. Y[][], where Y is a subclass of X.
208// Y[][] = Y[][] --> true (identity)
209// X[][] = Y[][] --> true (element superclass)
210// Y = Y[][] --> false
211// Y[] = Y[][] --> false
212// Object = Y[][] --> true (everything is an object)
213// Object[] = Y[][] --> true
214// Object[][] = Y[][] --> true
215// Object[][][] = Y[][] --> false (too many []s)
216// Serializable = Y[][] --> true (all arrays are Serializable)
217// Serializable[] = Y[][] --> true
218// Serializable[][] = Y[][] --> false (unless Y is Serializable)
219//
220// Don't forget about primitive types.
221// Object[] = int[] --> false
222//
Ian Rogersef7d42f2014-01-06 12:55:46 -0800223inline bool Class::IsArrayAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800224 DCHECK(IsArrayClass()) << PrettyClass(this);
225 DCHECK(src->IsArrayClass()) << PrettyClass(src);
226 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
227}
228
Ian Rogersef7d42f2014-01-06 12:55:46 -0800229inline bool Class::IsAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800230 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
231 DCHECK(src->IsArrayClass()) << PrettyClass(src);
232 if (!IsArrayClass()) {
233 // If "this" is not also an array, it must be Object.
234 // src's super should be java_lang_Object, since it is an array.
235 Class* java_lang_Object = src->GetSuperClass();
236 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700237 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Ian Rogers693ff612013-02-01 10:56:12 -0800238 return this == java_lang_Object;
239 }
240 return IsArrayAssignableFromArray(src);
241}
242
Vladimir Marko89786432014-01-31 15:03:55 +0000243template <bool throw_on_failure, bool use_referrers_cache>
244inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800245 uint32_t field_idx, DexCache* dex_cache) {
Vladimir Marko89786432014-01-31 15:03:55 +0000246 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000247 if (UNLIKELY(!this->CanAccess(access_to))) {
248 // The referrer class can't access the field's declaring class but may still be able
249 // to access the field if the FieldId specifies an accessible subclass of the declaring
250 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800251 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000252 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
253 // The referenced class has already been resolved with the field, get it from the dex cache.
254 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
255 DCHECK(dex_access_to != nullptr);
256 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
257 if (throw_on_failure) {
258 ThrowIllegalAccessErrorClass(this, dex_access_to);
259 }
260 return false;
261 }
262 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()),
263 this->CanAccessMember(dex_access_to, field->GetAccessFlags()));
264 }
265 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
266 return true;
267 }
268 if (throw_on_failure) {
269 ThrowIllegalAccessErrorField(this, field);
270 }
271 return false;
272}
273
Vladimir Marko89786432014-01-31 15:03:55 +0000274template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type>
275inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800276 uint32_t method_idx, DexCache* dex_cache) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800277 static_assert(throw_on_failure || throw_invoke_type == kStatic, "Non-default throw invoke type");
Vladimir Marko89786432014-01-31 15:03:55 +0000278 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000279 if (UNLIKELY(!this->CanAccess(access_to))) {
280 // The referrer class can't access the method's declaring class but may still be able
281 // to access the method if the MethodId specifies an accessible subclass of the declaring
282 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800283 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000284 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
285 // The referenced class has already been resolved with the method, get it from the dex cache.
286 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
287 DCHECK(dex_access_to != nullptr);
288 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
289 if (throw_on_failure) {
290 ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to,
291 method, throw_invoke_type);
292 }
293 return false;
294 }
295 DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()),
296 this->CanAccessMember(dex_access_to, method->GetAccessFlags()));
297 }
298 if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) {
299 return true;
300 }
301 if (throw_on_failure) {
302 ThrowIllegalAccessErrorMethod(this, method);
303 }
304 return false;
305}
306
Vladimir Marko89786432014-01-31 15:03:55 +0000307inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800308 DexCache* dex_cache, uint32_t field_idx) {
309 return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000310}
311
312inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field,
313 uint32_t field_idx) {
314 return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr);
315}
316
317inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800318 DexCache* dex_cache, uint32_t method_idx) {
319 return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000320}
321
322template <InvokeType throw_invoke_type>
323inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method,
324 uint32_t method_idx) {
325 return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx,
326 nullptr);
327}
328
Ian Rogersef7d42f2014-01-06 12:55:46 -0800329inline bool Class::IsSubClass(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800330 DCHECK(!IsInterface()) << PrettyClass(this);
331 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332 Class* current = this;
Ian Rogers693ff612013-02-01 10:56:12 -0800333 do {
334 if (current == klass) {
335 return true;
336 }
337 current = current->GetSuperClass();
338 } while (current != NULL);
339 return false;
340}
341
Ian Rogersef7d42f2014-01-06 12:55:46 -0800342inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) {
Ian Rogers693ff612013-02-01 10:56:12 -0800343 Class* declaring_class = method->GetDeclaringClass();
344 DCHECK(declaring_class != NULL) << PrettyClass(this);
345 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
346 // TODO cache to improve lookup speed
347 int32_t iftable_count = GetIfTableCount();
348 IfTable* iftable = GetIfTable();
349 for (int32_t i = 0; i < iftable_count; i++) {
350 if (iftable->GetInterface(i) == declaring_class) {
351 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
352 }
353 }
354 return NULL;
355}
356
Ian Rogersef7d42f2014-01-06 12:55:46 -0800357inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700358 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 // The argument method may from a super class.
360 // Use the index to a potentially overridden one for this instance's class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700361 return GetVTableEntry(method->GetMethodIndex());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362}
363
Ian Rogersef7d42f2014-01-06 12:55:46 -0800364inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 DCHECK(!method->GetDeclaringClass()->IsInterface());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700366 return GetSuperClass()->GetVTableEntry(method->GetMethodIndex());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367}
368
Ian Rogersef7d42f2014-01-06 12:55:46 -0800369inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 if (method->IsDirect()) {
371 return method;
372 }
Jeff Hao201803f2013-11-20 18:11:39 -0800373 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 return FindVirtualMethodForInterface(method);
375 }
376 return FindVirtualMethodForVirtual(method);
377}
378
Ian Rogersef7d42f2014-01-06 12:55:46 -0800379inline IfTable* Class::GetIfTable() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700380 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381}
382
Ian Rogersef7d42f2014-01-06 12:55:46 -0800383inline int32_t Class::GetIfTableCount() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384 IfTable* iftable = GetIfTable();
385 if (iftable == NULL) {
386 return 0;
387 }
388 return iftable->Count();
389}
390
391inline void Class::SetIfTable(IfTable* new_iftable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700392 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393}
394
Mathieu Chartierc7853442015-03-27 14:35:38 -0700395inline ArtField* Class::GetIFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 DCHECK(IsLoaded() || IsErroneous());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700397 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398}
399
Vladimir Marko76649e82014-11-10 18:32:59 +0000400inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() {
401 Class* super_class = GetSuperClass();
402 return (super_class != nullptr)
403 ? MemberOffset(RoundUp(super_class->GetObjectSize(),
404 sizeof(mirror::HeapReference<mirror::Object>)))
405 : ClassOffset();
406}
407
408inline MemberOffset Class::GetFirstReferenceStaticFieldOffset() {
409 DCHECK(IsResolved());
410 uint32_t base = sizeof(mirror::Class); // Static fields come after the class.
411 if (ShouldHaveEmbeddedImtAndVTable()) {
412 // Static fields come after the embedded tables.
413 base = mirror::Class::ComputeClassSize(true, GetEmbeddedVTableLength(),
414 0, 0, 0, 0, 0);
415 }
416 return MemberOffset(base);
417}
418
419inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking() {
420 DCHECK(IsLoaded());
421 uint32_t base = sizeof(mirror::Class); // Static fields come after the class.
422 if (ShouldHaveEmbeddedImtAndVTable()) {
423 // Static fields come after the embedded tables.
424 base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(),
425 0, 0, 0, 0, 0);
426 }
427 return MemberOffset(base);
428}
429
Mathieu Chartierc7853442015-03-27 14:35:38 -0700430inline void Class::SetIFields(ArtField* new_ifields) {
431 DCHECK(GetIFieldsUnchecked() == nullptr);
432 return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433}
434
Mathieu Chartierc7853442015-03-27 14:35:38 -0700435inline void Class::SetIFieldsUnchecked(ArtField* new_ifields) {
436 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
437}
438
439inline ArtField* Class::GetSFieldsUnchecked() {
440 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
441}
442
443inline ArtField* Class::GetIFieldsUnchecked() {
444 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
445}
446
447inline ArtField* Class::GetSFields() {
Mathieu Chartier987ca8b2015-03-15 14:19:14 -0700448 DCHECK(IsLoaded() || IsErroneous()) << GetStatus();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700449 return GetSFieldsUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450}
451
Mathieu Chartierc7853442015-03-27 14:35:38 -0700452inline void Class::SetSFields(ArtField* new_sfields) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700453 DCHECK((IsRetired() && new_sfields == nullptr) ||
Mathieu Chartierc7853442015-03-27 14:35:38 -0700454 GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr);
455 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456}
457
Mathieu Chartierc7853442015-03-27 14:35:38 -0700458inline void Class::SetSFieldsUnchecked(ArtField* new_sfields) {
459 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800460}
461
Mathieu Chartierc7853442015-03-27 14:35:38 -0700462inline ArtField* Class::GetStaticField(uint32_t i) {
463 DCHECK_LT(i, NumStaticFields());
464 return &GetSFields()[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465}
466
Mathieu Chartierc7853442015-03-27 14:35:38 -0700467inline ArtField* Class::GetInstanceField(uint32_t i) {
468 DCHECK_LT(i, NumInstanceFields());
469 return &GetIFields()[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470}
471
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700472template<VerifyObjectFlags kVerifyFlags>
473inline uint32_t Class::GetReferenceInstanceOffsets() {
474 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
475 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_));
476}
477
478inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) {
479 if (Runtime::Current()->IsActiveTransaction()) {
480 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
481 } else {
482 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
483 }
484}
485
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486inline void Class::SetVerifyErrorClass(Class* klass) {
487 CHECK(klass != NULL) << PrettyClass(this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100488 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700489 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100490 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700491 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100492 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493}
494
Mathieu Chartier4e305412014-02-19 10:54:44 -0800495template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800496inline uint32_t Class::GetAccessFlags() {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700497 // Check class is loaded/retired or this is java.lang.String that has a
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498 // circularity issue during loading the names of its members
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700499 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
Mathieu Chartier4e305412014-02-19 10:54:44 -0800500 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800501 this == String::GetJavaLangString() ||
Andreas Gampea6928662014-12-12 11:06:00 -0800502 this == ArtMethod::GetJavaLangReflectArtMethod())
503 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
504 << " IsRetired=" << IsRetired<kVerifyFlags>()
505 << " IsErroneous=" <<
506 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
507 << " IsString=" << (this == String::GetJavaLangString())
Andreas Gampea6928662014-12-12 11:06:00 -0800508 << " IsArtMethod=" << (this == ArtMethod::GetJavaLangReflectArtMethod())
509 << " descriptor=" << PrettyDescriptor(this);
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -0700510 return GetField32<kVerifyFlags>(AccessFlagsOffset());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511}
512
Ian Rogersef7d42f2014-01-06 12:55:46 -0800513inline String* Class::GetName() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700514 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515}
516inline void Class::SetName(String* name) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100517 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700518 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100519 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700520 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100521 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522}
523
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700524template<VerifyObjectFlags kVerifyFlags>
525inline Primitive::Type Class::GetPrimitiveType() {
526 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700527 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
528 Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF);
529 DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type));
530 return type;
531}
532
533template<VerifyObjectFlags kVerifyFlags>
534inline size_t Class::GetPrimitiveTypeSizeShift() {
535 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
536 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
537 size_t size_shift = static_cast<Primitive::Type>(v32 >> 16);
538 DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF)));
539 return size_shift;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700540}
541
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700542inline void Class::CheckObjectAlloc() {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700543 DCHECK(!IsArrayClass())
544 << PrettyClass(this)
545 << "A array shouldn't be allocated through this "
546 << "as it requires a pre-fence visitor that sets the class size.";
547 DCHECK(!IsClassClass())
548 << PrettyClass(this)
549 << "A class object shouldn't be allocated through this "
550 << "as it requires a pre-fence visitor that sets the class size.";
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700551 DCHECK(IsInstantiable()) << PrettyClass(this);
552 // TODO: decide whether we want this check. It currently fails during bootstrap.
553 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
554 DCHECK_GE(this->object_size_, sizeof(Object));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700555}
556
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700557template<bool kIsInstrumented, bool kCheckAddFinalizer>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800558inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700559 CheckObjectAlloc();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700560 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700561 const bool add_finalizer = kCheckAddFinalizer && IsFinalizable();
562 if (!kCheckAddFinalizer) {
563 DCHECK(!IsFinalizable());
564 }
565 mirror::Object* obj =
566 heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_,
567 allocator_type, VoidFunctor());
568 if (add_finalizer && LIKELY(obj != nullptr)) {
569 heap->AddFinalizerReference(self, &obj);
Pavel Vyssotski3ac90da2014-12-02 19:54:50 +0600570 if (UNLIKELY(self->IsExceptionPending())) {
571 // Failed to allocate finalizer reference, it means that the whole allocation failed.
572 obj = nullptr;
573 }
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700574 }
575 return obj;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800576}
577
578inline Object* Class::AllocObject(Thread* self) {
579 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
580}
581
582inline Object* Class::AllocNonMovableObject(Thread* self) {
583 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700584}
585
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700586inline uint32_t Class::ComputeClassSize(bool has_embedded_tables,
587 uint32_t num_vtable_entries,
Fred Shih37f05ef2014-07-16 18:38:08 -0700588 uint32_t num_8bit_static_fields,
589 uint32_t num_16bit_static_fields,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700590 uint32_t num_32bit_static_fields,
591 uint32_t num_64bit_static_fields,
592 uint32_t num_ref_static_fields) {
593 // Space used by java.lang.Class and its instance fields.
594 uint32_t size = sizeof(Class);
595 // Space used by embedded tables.
596 if (has_embedded_tables) {
597 uint32_t embedded_imt_size = kImtSize * sizeof(ImTableEntry);
598 uint32_t embedded_vtable_size = num_vtable_entries * sizeof(VTableEntry);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700599 size += embedded_imt_size +
600 sizeof(int32_t) /* vtable len */ +
601 embedded_vtable_size;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700602 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700603
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700604 // Space used by reference statics.
605 size += num_ref_static_fields * sizeof(HeapReference<Object>);
Fred Shih37f05ef2014-07-16 18:38:08 -0700606 if (!IsAligned<8>(size) && num_64bit_static_fields > 0) {
607 uint32_t gap = 8 - (size & 0x7);
608 size += gap; // will be padded
609 // Shuffle 4-byte fields forward.
610 while (gap >= sizeof(uint32_t) && num_32bit_static_fields != 0) {
611 --num_32bit_static_fields;
612 gap -= sizeof(uint32_t);
613 }
614 // Shuffle 2-byte fields forward.
615 while (gap >= sizeof(uint16_t) && num_16bit_static_fields != 0) {
616 --num_16bit_static_fields;
617 gap -= sizeof(uint16_t);
618 }
619 // Shuffle byte fields forward.
620 while (gap >= sizeof(uint8_t) && num_8bit_static_fields != 0) {
621 --num_8bit_static_fields;
622 gap -= sizeof(uint8_t);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700623 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700624 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700625 // Guaranteed to be at least 4 byte aligned. No need for further alignments.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700626 // Space used for primitive static fields.
Fred Shih37f05ef2014-07-16 18:38:08 -0700627 size += (num_8bit_static_fields * sizeof(uint8_t)) +
628 (num_16bit_static_fields * sizeof(uint16_t)) +
629 (num_32bit_static_fields * sizeof(uint32_t)) +
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700630 (num_64bit_static_fields * sizeof(uint64_t));
631 return size;
632}
633
Mathieu Chartier407f7022014-02-18 14:37:05 -0800634template <bool kVisitClass, typename Visitor>
635inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700636 VisitInstanceFieldsReferences<kVisitClass>(klass, visitor);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800637 // Right after a class is allocated, but not yet loaded
638 // (kStatusNotReady, see ClassLinkder::LoadClass()), GC may find it
639 // and scan it. IsTemp() may call Class::GetAccessFlags() but may
640 // fail in the DCHECK in Class::GetAccessFlags() because the class
641 // status is kStatusNotReady. To avoid it, rely on IsResolved()
642 // only. This is fine because a temp class never goes into the
643 // kStatusResolved state.
644 if (IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700645 // Temp classes don't ever populate imt/vtable or static fields and they are not even
Hiroshi Yamauchif4c15a12014-10-20 16:56:58 -0700646 // allocated with the right size for those. Also, unresolved classes don't have fields
647 // linked yet.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700648 VisitStaticFieldsReferences<kVisitClass>(this, visitor);
649 if (ShouldHaveEmbeddedImtAndVTable()) {
650 VisitEmbeddedImtAndVTable(visitor);
651 }
652 }
653}
654
655template<typename Visitor>
656inline void Class::VisitEmbeddedImtAndVTable(const Visitor& visitor) {
657 uint32_t pos = sizeof(mirror::Class);
658
659 size_t count = kImtSize;
660 for (size_t i = 0; i < count; ++i) {
661 MemberOffset offset = MemberOffset(pos);
662 visitor(this, offset, true);
663 pos += sizeof(ImTableEntry);
664 }
665
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700666 // Skip vtable length.
667 pos += sizeof(int32_t);
668
669 count = GetEmbeddedVTableLength();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700670 for (size_t i = 0; i < count; ++i) {
671 MemberOffset offset = MemberOffset(pos);
672 visitor(this, offset, true);
673 pos += sizeof(VTableEntry);
674 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800675}
676
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700677template<ReadBarrierOption kReadBarrierOption>
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700678inline bool Class::IsArtMethodClass() const {
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700679 return this == ArtMethod::GetJavaLangReflectArtMethod<kReadBarrierOption>();
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -0700680}
681
Fred Shih4ee7a662014-07-11 09:59:27 -0700682template<ReadBarrierOption kReadBarrierOption>
683inline bool Class::IsReferenceClass() const {
684 return this == Reference::GetJavaLangRefReference<kReadBarrierOption>();
685}
686
Hiroshi Yamauchi25023c72014-05-09 11:45:53 -0700687template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
688inline bool Class::IsClassClass() {
689 Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()->
690 template GetClass<kVerifyFlags, kReadBarrierOption>();
691 return this == java_lang_Class;
692}
693
Mathieu Chartierf8322842014-05-16 10:59:25 -0700694inline const DexFile& Class::GetDexFile() {
695 return *GetDexCache()->GetDexFile();
696}
697
698inline bool Class::DescriptorEquals(const char* match) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700699 if (IsArrayClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700700 return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700701 } else if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700702 return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700703 } else if (IsProxyClass()) {
Vladimir Marko3481ba22015-04-13 12:22:36 +0100704 return ProxyDescriptorEquals(match);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700705 } else {
706 const DexFile& dex_file = GetDexFile();
707 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
708 return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0;
709 }
710}
711
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200712inline void Class::AssertInitializedOrInitializingInThread(Thread* self) {
713 if (kIsDebugBuild && !IsInitialized()) {
714 CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus();
715 CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this)
716 << " is initializing in a different thread";
717 }
718}
719
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700720inline ObjectArray<Class>* Class::GetInterfaces() {
721 CHECK(IsProxyClass());
722 // First static field.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700723 auto* field = GetStaticField(0);
724 DCHECK_STREQ(field->GetName(), "interfaces");
725 MemberOffset field_offset = field->GetOffset();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700726 return GetFieldObject<ObjectArray<Class>>(field_offset);
727}
728
729inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() {
730 CHECK(IsProxyClass());
731 // Second static field.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700732 auto* field = GetStaticField(1);
733 DCHECK_STREQ(field->GetName(), "throws");
734 MemberOffset field_offset = field->GetOffset();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700735 return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset);
736}
737
Fred Shih4ee7a662014-07-11 09:59:27 -0700738inline MemberOffset Class::GetDisableIntrinsicFlagOffset() {
739 CHECK(IsReferenceClass());
740 // First static field
Mathieu Chartierc7853442015-03-27 14:35:38 -0700741 auto* field = GetStaticField(0);
742 DCHECK_STREQ(field->GetName(), "disableIntrinsic");
743 return field->GetOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700744}
745
746inline MemberOffset Class::GetSlowPathFlagOffset() {
747 CHECK(IsReferenceClass());
748 // Second static field
Mathieu Chartierc7853442015-03-27 14:35:38 -0700749 auto* field = GetStaticField(1);
750 DCHECK_STREQ(field->GetName(), "slowPathEnabled");
751 return field->GetOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700752}
753
754inline bool Class::GetSlowPathEnabled() {
Fred Shih37f05ef2014-07-16 18:38:08 -0700755 return GetFieldBoolean(GetSlowPathFlagOffset());
Fred Shih4ee7a662014-07-11 09:59:27 -0700756}
757
758inline void Class::SetSlowPath(bool enabled) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700759 SetFieldBoolean<false>(GetSlowPathFlagOffset(), enabled);
Fred Shih4ee7a662014-07-11 09:59:27 -0700760}
761
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700762inline void Class::InitializeClassVisitor::operator()(
763 mirror::Object* obj, size_t usable_size) const {
764 DCHECK_LE(class_size_, usable_size);
765 // Avoid AsClass as object is not yet in live bitmap or allocation stack.
766 mirror::Class* klass = down_cast<mirror::Class*>(obj);
767 // DCHECK(klass->IsClass());
768 klass->SetClassSize(class_size_);
769 klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive.
770 klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index.
771 klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index.
772}
773
Andreas Gampe48498592014-09-10 19:48:05 -0700774inline void Class::SetAccessFlags(uint32_t new_access_flags) {
775 // Called inside a transaction when setting pre-verified flag during boot image compilation.
776 if (Runtime::Current()->IsActiveTransaction()) {
777 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags);
778 } else {
779 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags);
780 }
781}
782
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700783inline uint32_t Class::NumDirectInterfaces() {
784 if (IsPrimitive()) {
785 return 0;
786 } else if (IsArrayClass()) {
787 return 2;
788 } else if (IsProxyClass()) {
789 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
790 return interfaces != nullptr ? interfaces->GetLength() : 0;
791 } else {
792 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
793 if (interfaces == nullptr) {
794 return 0;
795 } else {
796 return interfaces->Size();
797 }
798 }
799}
800
Mathieu Chartiereace4582014-11-24 18:29:54 -0800801inline void Class::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
802 SetFieldObject<false>(DexCacheStringsOffset(), new_dex_cache_strings);
803}
804
805inline ObjectArray<String>* Class::GetDexCacheStrings() {
806 return GetFieldObject<ObjectArray<String>>(DexCacheStringsOffset());
807}
808
Mathieu Chartierc7853442015-03-27 14:35:38 -0700809template<class Visitor>
810void mirror::Class::VisitFieldRoots(Visitor& visitor) {
811 ArtField* const sfields = GetSFieldsUnchecked();
812 for (size_t i = 0, count = NumStaticFields(); i < count; ++i) {
813 if (kIsDebugBuild && GetStatus() != kStatusRetired) {
814 CHECK_EQ(sfields[i].GetDeclaringClass(), this);
815 }
816 visitor.VisitRoot(sfields[i].DeclaringClassRoot().AddressWithoutBarrier());
817 }
818 ArtField* const ifields = GetIFieldsUnchecked();
819 for (size_t i = 0, count = NumInstanceFields(); i < count; ++i) {
820 if (kIsDebugBuild && GetStatus() != kStatusRetired) {
821 CHECK_EQ(ifields[i].GetDeclaringClass(), this);
822 }
823 visitor.VisitRoot(ifields[i].DeclaringClassRoot().AddressWithoutBarrier());
824 }
825}
826
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800827} // namespace mirror
828} // namespace art
829
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700830#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_