blob: 5b72e5a033d4bacc1364236ea18a6b1b6c64b5e7 [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"
Mathieu Chartierf8322842014-05-16 10:59:25 -070024#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "class_loader.h"
Vladimir Marko23a28212014-01-09 19:24:37 +000026#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_cache.h"
Mathieu Chartierf8322842014-05-16 10:59:25 -070028#include "dex_file.h"
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070029#include "gc/heap-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "iftable.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080031#include "object_array-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070032#include "read_barrier-inl.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070033#include "reference-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "runtime.h"
35#include "string.h"
36
37namespace art {
38namespace mirror {
39
Hiroshi Yamauchi25023c72014-05-09 11:45:53 -070040template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
Ian Rogersef7d42f2014-01-06 12:55:46 -080041inline uint32_t Class::GetObjectSize() {
Hiroshi Yamauchi25023c72014-05-09 11:45:53 -070042 if (kIsDebugBuild) {
43 // Use a local variable as (D)CHECK can't handle the space between
44 // the two template params.
45 bool is_variable_size = IsVariableSize<kVerifyFlags, kReadBarrierOption>();
46 CHECK(!is_variable_size) << " class=" << PrettyTypeOf(this);
47 }
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -070048 return GetField32(ObjectSizeOffset());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049}
50
Ian Rogersef7d42f2014-01-06 12:55:46 -080051inline Class* Class::GetSuperClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052 // Can only get super class for loaded classes (hack for when runtime is
53 // initializing)
Brian Carlstrom073278c2014-02-19 15:21:21 -080054 DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded();
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070055 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056}
57
Ian Rogersef7d42f2014-01-06 12:55:46 -080058inline ClassLoader* Class::GetClassLoader() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070059 return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070060}
61
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080062template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080063inline DexCache* Class::GetDexCache() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070064 return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070065}
66
Ian Rogersef7d42f2014-01-06 12:55:46 -080067inline ObjectArray<ArtMethod>* Class::GetDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070069 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070}
71
Brian Carlstromea46f952013-07-30 01:26:50 -070072inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -070074 DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070075 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 DCHECK_NE(0, new_direct_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070077 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), new_direct_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078}
79
Ian Rogersef7d42f2014-01-06 12:55:46 -080080inline ArtMethod* Class::GetDirectMethod(int32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 return GetDirectMethods()->Get(i);
82}
83
Brian Carlstromea46f952013-07-30 01:26:50 -070084inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070085 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070086 ObjectArray<ArtMethod>* direct_methods =
Ian Rogers700a4022014-05-19 16:49:03 -070087 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010088 direct_methods->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089}
90
91// Returns the number of static, private, and constructor methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -080092inline uint32_t Class::NumDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
94}
95
Mathieu Chartier4e305412014-02-19 10:54:44 -080096template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080097inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070099 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100}
101
Brian Carlstromea46f952013-07-30 01:26:50 -0700102inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103 // TODO: we reassign virtual methods to grow the table for miranda
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104 // methods.. they should really just be assigned once.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 DCHECK_NE(0, new_virtual_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700106 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), new_virtual_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107}
108
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109inline uint32_t Class::NumVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
111}
112
Mathieu Chartier4e305412014-02-19 10:54:44 -0800113template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114inline ArtMethod* Class::GetVirtualMethod(uint32_t i) {
Hiroshi Yamauchif4c15a12014-10-20 16:56:58 -0700115 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>())
116 << PrettyClass(this) << " status=" << GetStatus();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700117 return GetVirtualMethods()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118}
119
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 DCHECK(IsLoaded() || IsErroneous());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700122 return GetVirtualMethods()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123}
124
Brian Carlstromea46f952013-07-30 01:26:50 -0700125inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700127 ObjectArray<ArtMethod>* virtual_methods =
Ian Rogers700a4022014-05-19 16:49:03 -0700128 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700129 virtual_methods->SetWithoutChecks<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130}
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132inline ObjectArray<ArtMethod>* Class::GetVTable() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 DCHECK(IsResolved() || 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 ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -0700139 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140}
141
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700143 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144}
145
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700146inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i) {
147 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
148 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset));
149}
150
151inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method) {
152 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
153 SetFieldObject<false>(MemberOffset(offset), method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700154}
155
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700156inline bool Class::HasVTable() {
157 return (GetVTable() != nullptr) || ShouldHaveEmbeddedImtAndVTable();
158}
159
160inline int32_t Class::GetVTableLength() {
161 if (ShouldHaveEmbeddedImtAndVTable()) {
162 return GetEmbeddedVTableLength();
163 }
164 return (GetVTable() != nullptr) ? GetVTable()->GetLength() : 0;
165}
166
167inline ArtMethod* Class::GetVTableEntry(uint32_t i) {
168 if (ShouldHaveEmbeddedImtAndVTable()) {
169 return GetEmbeddedVTableEntry(i);
170 }
171 return (GetVTable() != nullptr) ? GetVTable()->Get(i) : nullptr;
172}
173
174inline int32_t Class::GetEmbeddedVTableLength() {
175 return GetField32(EmbeddedVTableLengthOffset());
176}
177
178inline void Class::SetEmbeddedVTableLength(int32_t len) {
179 SetField32<false>(EmbeddedVTableLengthOffset(), len);
180}
181
182inline ArtMethod* Class::GetEmbeddedVTableEntry(uint32_t i) {
183 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry);
184 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset));
185}
186
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700187inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method) {
188 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry);
189 SetFieldObject<false>(MemberOffset(offset), method);
190 CHECK(method == GetVTableDuringLinking()->Get(i));
191}
192
Ian Rogersef7d42f2014-01-06 12:55:46 -0800193inline bool Class::Implements(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800194 DCHECK(klass != NULL);
195 DCHECK(klass->IsInterface()) << PrettyClass(this);
196 // All interfaces implemented directly and by our superclass, and
197 // recursively all super-interfaces of those interfaces, are listed
198 // in iftable_, so we can just do a linear scan through that.
199 int32_t iftable_count = GetIfTableCount();
200 IfTable* iftable = GetIfTable();
201 for (int32_t i = 0; i < iftable_count; i++) {
202 if (iftable->GetInterface(i) == klass) {
203 return true;
204 }
205 }
206 return false;
207}
208
209// Determine whether "this" is assignable from "src", where both of these
210// are array classes.
211//
212// Consider an array class, e.g. Y[][], where Y is a subclass of X.
213// Y[][] = Y[][] --> true (identity)
214// X[][] = Y[][] --> true (element superclass)
215// Y = Y[][] --> false
216// Y[] = Y[][] --> false
217// Object = Y[][] --> true (everything is an object)
218// Object[] = Y[][] --> true
219// Object[][] = Y[][] --> true
220// Object[][][] = Y[][] --> false (too many []s)
221// Serializable = Y[][] --> true (all arrays are Serializable)
222// Serializable[] = Y[][] --> true
223// Serializable[][] = Y[][] --> false (unless Y is Serializable)
224//
225// Don't forget about primitive types.
226// Object[] = int[] --> false
227//
Ian Rogersef7d42f2014-01-06 12:55:46 -0800228inline bool Class::IsArrayAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800229 DCHECK(IsArrayClass()) << PrettyClass(this);
230 DCHECK(src->IsArrayClass()) << PrettyClass(src);
231 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
232}
233
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234inline bool Class::IsAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800235 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
236 DCHECK(src->IsArrayClass()) << PrettyClass(src);
237 if (!IsArrayClass()) {
238 // If "this" is not also an array, it must be Object.
239 // src's super should be java_lang_Object, since it is an array.
240 Class* java_lang_Object = src->GetSuperClass();
241 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700242 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Ian Rogers693ff612013-02-01 10:56:12 -0800243 return this == java_lang_Object;
244 }
245 return IsArrayAssignableFromArray(src);
246}
247
Vladimir Marko89786432014-01-31 15:03:55 +0000248template <bool throw_on_failure, bool use_referrers_cache>
249inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250 uint32_t field_idx, DexCache* dex_cache) {
Vladimir Marko89786432014-01-31 15:03:55 +0000251 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000252 if (UNLIKELY(!this->CanAccess(access_to))) {
253 // The referrer class can't access the field's declaring class but may still be able
254 // to access the field if the FieldId specifies an accessible subclass of the declaring
255 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800256 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000257 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
258 // The referenced class has already been resolved with the field, get it from the dex cache.
259 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
260 DCHECK(dex_access_to != nullptr);
261 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
262 if (throw_on_failure) {
263 ThrowIllegalAccessErrorClass(this, dex_access_to);
264 }
265 return false;
266 }
267 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()),
268 this->CanAccessMember(dex_access_to, field->GetAccessFlags()));
269 }
270 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
271 return true;
272 }
273 if (throw_on_failure) {
274 ThrowIllegalAccessErrorField(this, field);
275 }
276 return false;
277}
278
Vladimir Marko89786432014-01-31 15:03:55 +0000279template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type>
280inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800281 uint32_t method_idx, DexCache* dex_cache) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800282 static_assert(throw_on_failure || throw_invoke_type == kStatic, "Non-default throw invoke type");
Vladimir Marko89786432014-01-31 15:03:55 +0000283 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000284 if (UNLIKELY(!this->CanAccess(access_to))) {
285 // The referrer class can't access the method's declaring class but may still be able
286 // to access the method if the MethodId specifies an accessible subclass of the declaring
287 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800288 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000289 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
290 // The referenced class has already been resolved with the method, get it from the dex cache.
291 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
292 DCHECK(dex_access_to != nullptr);
293 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
294 if (throw_on_failure) {
295 ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to,
296 method, throw_invoke_type);
297 }
298 return false;
299 }
300 DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()),
301 this->CanAccessMember(dex_access_to, method->GetAccessFlags()));
302 }
303 if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) {
304 return true;
305 }
306 if (throw_on_failure) {
307 ThrowIllegalAccessErrorMethod(this, method);
308 }
309 return false;
310}
311
Vladimir Marko89786432014-01-31 15:03:55 +0000312inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800313 DexCache* dex_cache, uint32_t field_idx) {
314 return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000315}
316
317inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field,
318 uint32_t field_idx) {
319 return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr);
320}
321
322inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800323 DexCache* dex_cache, uint32_t method_idx) {
324 return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000325}
326
327template <InvokeType throw_invoke_type>
328inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method,
329 uint32_t method_idx) {
330 return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx,
331 nullptr);
332}
333
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334inline bool Class::IsSubClass(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800335 DCHECK(!IsInterface()) << PrettyClass(this);
336 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800337 Class* current = this;
Ian Rogers693ff612013-02-01 10:56:12 -0800338 do {
339 if (current == klass) {
340 return true;
341 }
342 current = current->GetSuperClass();
343 } while (current != NULL);
344 return false;
345}
346
Ian Rogersef7d42f2014-01-06 12:55:46 -0800347inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) {
Ian Rogers693ff612013-02-01 10:56:12 -0800348 Class* declaring_class = method->GetDeclaringClass();
349 DCHECK(declaring_class != NULL) << PrettyClass(this);
350 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
351 // TODO cache to improve lookup speed
352 int32_t iftable_count = GetIfTableCount();
353 IfTable* iftable = GetIfTable();
354 for (int32_t i = 0; i < iftable_count; i++) {
355 if (iftable->GetInterface(i) == declaring_class) {
356 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
357 }
358 }
359 return NULL;
360}
361
Ian Rogersef7d42f2014-01-06 12:55:46 -0800362inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700363 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 // The argument method may from a super class.
365 // Use the index to a potentially overridden one for this instance's class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700366 return GetVTableEntry(method->GetMethodIndex());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367}
368
Ian Rogersef7d42f2014-01-06 12:55:46 -0800369inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 DCHECK(!method->GetDeclaringClass()->IsInterface());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700371 return GetSuperClass()->GetVTableEntry(method->GetMethodIndex());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372}
373
Ian Rogersef7d42f2014-01-06 12:55:46 -0800374inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 if (method->IsDirect()) {
376 return method;
377 }
Jeff Hao201803f2013-11-20 18:11:39 -0800378 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 return FindVirtualMethodForInterface(method);
380 }
381 return FindVirtualMethodForVirtual(method);
382}
383
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384inline IfTable* Class::GetIfTable() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700385 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386}
387
Ian Rogersef7d42f2014-01-06 12:55:46 -0800388inline int32_t Class::GetIfTableCount() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 IfTable* iftable = GetIfTable();
390 if (iftable == NULL) {
391 return 0;
392 }
393 return iftable->Count();
394}
395
396inline void Class::SetIfTable(IfTable* new_iftable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700397 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398}
399
Mathieu Chartierc7853442015-03-27 14:35:38 -0700400inline ArtField* Class::GetIFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 DCHECK(IsLoaded() || IsErroneous());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700402 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403}
404
Vladimir Marko76649e82014-11-10 18:32:59 +0000405inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() {
406 Class* super_class = GetSuperClass();
407 return (super_class != nullptr)
408 ? MemberOffset(RoundUp(super_class->GetObjectSize(),
409 sizeof(mirror::HeapReference<mirror::Object>)))
410 : ClassOffset();
411}
412
413inline MemberOffset Class::GetFirstReferenceStaticFieldOffset() {
414 DCHECK(IsResolved());
415 uint32_t base = sizeof(mirror::Class); // Static fields come after the class.
416 if (ShouldHaveEmbeddedImtAndVTable()) {
417 // Static fields come after the embedded tables.
418 base = mirror::Class::ComputeClassSize(true, GetEmbeddedVTableLength(),
419 0, 0, 0, 0, 0);
420 }
421 return MemberOffset(base);
422}
423
424inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking() {
425 DCHECK(IsLoaded());
426 uint32_t base = sizeof(mirror::Class); // Static fields come after the class.
427 if (ShouldHaveEmbeddedImtAndVTable()) {
428 // Static fields come after the embedded tables.
429 base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(),
430 0, 0, 0, 0, 0);
431 }
432 return MemberOffset(base);
433}
434
Mathieu Chartierc7853442015-03-27 14:35:38 -0700435inline void Class::SetIFields(ArtField* new_ifields) {
436 DCHECK(GetIFieldsUnchecked() == nullptr);
437 return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438}
439
Mathieu Chartierc7853442015-03-27 14:35:38 -0700440inline void Class::SetIFieldsUnchecked(ArtField* new_ifields) {
441 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
442}
443
444inline ArtField* Class::GetSFieldsUnchecked() {
445 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
446}
447
448inline ArtField* Class::GetIFieldsUnchecked() {
449 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
450}
451
452inline ArtField* Class::GetSFields() {
Mathieu Chartier987ca8b2015-03-15 14:19:14 -0700453 DCHECK(IsLoaded() || IsErroneous()) << GetStatus();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700454 return GetSFieldsUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455}
456
Mathieu Chartierc7853442015-03-27 14:35:38 -0700457inline void Class::SetSFields(ArtField* new_sfields) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700458 DCHECK((IsRetired() && new_sfields == nullptr) ||
Mathieu Chartierc7853442015-03-27 14:35:38 -0700459 GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr);
460 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461}
462
Mathieu Chartierc7853442015-03-27 14:35:38 -0700463inline void Class::SetSFieldsUnchecked(ArtField* new_sfields) {
464 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465}
466
Mathieu Chartierc7853442015-03-27 14:35:38 -0700467inline ArtField* Class::GetStaticField(uint32_t i) {
468 DCHECK_LT(i, NumStaticFields());
469 return &GetSFields()[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470}
471
Mathieu Chartierc7853442015-03-27 14:35:38 -0700472inline ArtField* Class::GetInstanceField(uint32_t i) {
473 DCHECK_LT(i, NumInstanceFields());
474 return &GetIFields()[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800475}
476
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700477template<VerifyObjectFlags kVerifyFlags>
478inline uint32_t Class::GetReferenceInstanceOffsets() {
479 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
480 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_));
481}
482
483inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) {
484 if (Runtime::Current()->IsActiveTransaction()) {
485 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
486 } else {
487 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
488 }
489}
490
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491inline void Class::SetVerifyErrorClass(Class* klass) {
492 CHECK(klass != NULL) << PrettyClass(this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100493 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700494 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100495 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700496 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100497 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498}
499
Mathieu Chartier4e305412014-02-19 10:54:44 -0800500template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800501inline uint32_t Class::GetAccessFlags() {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700502 // Check class is loaded/retired or this is java.lang.String that has a
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800503 // circularity issue during loading the names of its members
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700504 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
Mathieu Chartier4e305412014-02-19 10:54:44 -0800505 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800506 this == String::GetJavaLangString() ||
Andreas Gampea6928662014-12-12 11:06:00 -0800507 this == ArtMethod::GetJavaLangReflectArtMethod())
508 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
509 << " IsRetired=" << IsRetired<kVerifyFlags>()
510 << " IsErroneous=" <<
511 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
512 << " IsString=" << (this == String::GetJavaLangString())
Andreas Gampea6928662014-12-12 11:06:00 -0800513 << " IsArtMethod=" << (this == ArtMethod::GetJavaLangReflectArtMethod())
514 << " descriptor=" << PrettyDescriptor(this);
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -0700515 return GetField32<kVerifyFlags>(AccessFlagsOffset());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516}
517
Ian Rogersef7d42f2014-01-06 12:55:46 -0800518inline String* Class::GetName() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700519 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520}
521inline void Class::SetName(String* name) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100522 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700523 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100524 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700525 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100526 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527}
528
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700529template<VerifyObjectFlags kVerifyFlags>
530inline Primitive::Type Class::GetPrimitiveType() {
531 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700532 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
533 Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF);
534 DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type));
535 return type;
536}
537
538template<VerifyObjectFlags kVerifyFlags>
539inline size_t Class::GetPrimitiveTypeSizeShift() {
540 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
541 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
542 size_t size_shift = static_cast<Primitive::Type>(v32 >> 16);
543 DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF)));
544 return size_shift;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700545}
546
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700547inline void Class::CheckObjectAlloc() {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700548 DCHECK(!IsArrayClass())
549 << PrettyClass(this)
550 << "A array shouldn't be allocated through this "
551 << "as it requires a pre-fence visitor that sets the class size.";
552 DCHECK(!IsClassClass())
553 << PrettyClass(this)
554 << "A class object shouldn't be allocated through this "
555 << "as it requires a pre-fence visitor that sets the class size.";
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700556 DCHECK(IsInstantiable()) << PrettyClass(this);
557 // TODO: decide whether we want this check. It currently fails during bootstrap.
558 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
559 DCHECK_GE(this->object_size_, sizeof(Object));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700560}
561
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700562template<bool kIsInstrumented, bool kCheckAddFinalizer>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800563inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700564 CheckObjectAlloc();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700565 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700566 const bool add_finalizer = kCheckAddFinalizer && IsFinalizable();
567 if (!kCheckAddFinalizer) {
568 DCHECK(!IsFinalizable());
569 }
570 mirror::Object* obj =
571 heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_,
572 allocator_type, VoidFunctor());
573 if (add_finalizer && LIKELY(obj != nullptr)) {
574 heap->AddFinalizerReference(self, &obj);
Pavel Vyssotski3ac90da2014-12-02 19:54:50 +0600575 if (UNLIKELY(self->IsExceptionPending())) {
576 // Failed to allocate finalizer reference, it means that the whole allocation failed.
577 obj = nullptr;
578 }
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700579 }
580 return obj;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800581}
582
583inline Object* Class::AllocObject(Thread* self) {
584 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
585}
586
587inline Object* Class::AllocNonMovableObject(Thread* self) {
588 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700589}
590
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700591inline uint32_t Class::ComputeClassSize(bool has_embedded_tables,
592 uint32_t num_vtable_entries,
Fred Shih37f05ef2014-07-16 18:38:08 -0700593 uint32_t num_8bit_static_fields,
594 uint32_t num_16bit_static_fields,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700595 uint32_t num_32bit_static_fields,
596 uint32_t num_64bit_static_fields,
597 uint32_t num_ref_static_fields) {
598 // Space used by java.lang.Class and its instance fields.
599 uint32_t size = sizeof(Class);
600 // Space used by embedded tables.
601 if (has_embedded_tables) {
602 uint32_t embedded_imt_size = kImtSize * sizeof(ImTableEntry);
603 uint32_t embedded_vtable_size = num_vtable_entries * sizeof(VTableEntry);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700604 size += embedded_imt_size +
605 sizeof(int32_t) /* vtable len */ +
606 embedded_vtable_size;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700607 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700608
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700609 // Space used by reference statics.
610 size += num_ref_static_fields * sizeof(HeapReference<Object>);
Fred Shih37f05ef2014-07-16 18:38:08 -0700611 if (!IsAligned<8>(size) && num_64bit_static_fields > 0) {
612 uint32_t gap = 8 - (size & 0x7);
613 size += gap; // will be padded
614 // Shuffle 4-byte fields forward.
615 while (gap >= sizeof(uint32_t) && num_32bit_static_fields != 0) {
616 --num_32bit_static_fields;
617 gap -= sizeof(uint32_t);
618 }
619 // Shuffle 2-byte fields forward.
620 while (gap >= sizeof(uint16_t) && num_16bit_static_fields != 0) {
621 --num_16bit_static_fields;
622 gap -= sizeof(uint16_t);
623 }
624 // Shuffle byte fields forward.
625 while (gap >= sizeof(uint8_t) && num_8bit_static_fields != 0) {
626 --num_8bit_static_fields;
627 gap -= sizeof(uint8_t);
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700628 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700629 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700630 // Guaranteed to be at least 4 byte aligned. No need for further alignments.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700631 // Space used for primitive static fields.
Fred Shih37f05ef2014-07-16 18:38:08 -0700632 size += (num_8bit_static_fields * sizeof(uint8_t)) +
633 (num_16bit_static_fields * sizeof(uint16_t)) +
634 (num_32bit_static_fields * sizeof(uint32_t)) +
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700635 (num_64bit_static_fields * sizeof(uint64_t));
636 return size;
637}
638
Mathieu Chartier407f7022014-02-18 14:37:05 -0800639template <bool kVisitClass, typename Visitor>
640inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700641 VisitInstanceFieldsReferences<kVisitClass>(klass, visitor);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800642 // Right after a class is allocated, but not yet loaded
643 // (kStatusNotReady, see ClassLinkder::LoadClass()), GC may find it
644 // and scan it. IsTemp() may call Class::GetAccessFlags() but may
645 // fail in the DCHECK in Class::GetAccessFlags() because the class
646 // status is kStatusNotReady. To avoid it, rely on IsResolved()
647 // only. This is fine because a temp class never goes into the
648 // kStatusResolved state.
649 if (IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700650 // Temp classes don't ever populate imt/vtable or static fields and they are not even
Hiroshi Yamauchif4c15a12014-10-20 16:56:58 -0700651 // allocated with the right size for those. Also, unresolved classes don't have fields
652 // linked yet.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700653 VisitStaticFieldsReferences<kVisitClass>(this, visitor);
654 if (ShouldHaveEmbeddedImtAndVTable()) {
655 VisitEmbeddedImtAndVTable(visitor);
656 }
657 }
658}
659
660template<typename Visitor>
661inline void Class::VisitEmbeddedImtAndVTable(const Visitor& visitor) {
662 uint32_t pos = sizeof(mirror::Class);
663
664 size_t count = kImtSize;
665 for (size_t i = 0; i < count; ++i) {
666 MemberOffset offset = MemberOffset(pos);
667 visitor(this, offset, true);
668 pos += sizeof(ImTableEntry);
669 }
670
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700671 // Skip vtable length.
672 pos += sizeof(int32_t);
673
674 count = GetEmbeddedVTableLength();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700675 for (size_t i = 0; i < count; ++i) {
676 MemberOffset offset = MemberOffset(pos);
677 visitor(this, offset, true);
678 pos += sizeof(VTableEntry);
679 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800680}
681
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700682template<ReadBarrierOption kReadBarrierOption>
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700683inline bool Class::IsArtMethodClass() const {
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700684 return this == ArtMethod::GetJavaLangReflectArtMethod<kReadBarrierOption>();
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -0700685}
686
Fred Shih4ee7a662014-07-11 09:59:27 -0700687template<ReadBarrierOption kReadBarrierOption>
688inline bool Class::IsReferenceClass() const {
689 return this == Reference::GetJavaLangRefReference<kReadBarrierOption>();
690}
691
Hiroshi Yamauchi25023c72014-05-09 11:45:53 -0700692template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
693inline bool Class::IsClassClass() {
694 Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()->
695 template GetClass<kVerifyFlags, kReadBarrierOption>();
696 return this == java_lang_Class;
697}
698
Mathieu Chartierf8322842014-05-16 10:59:25 -0700699inline const DexFile& Class::GetDexFile() {
700 return *GetDexCache()->GetDexFile();
701}
702
703inline bool Class::DescriptorEquals(const char* match) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700704 if (IsArrayClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700705 return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700706 } else if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700707 return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700708 } else if (IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700709 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
710 } else {
711 const DexFile& dex_file = GetDexFile();
712 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
713 return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0;
714 }
715}
716
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200717inline void Class::AssertInitializedOrInitializingInThread(Thread* self) {
718 if (kIsDebugBuild && !IsInitialized()) {
719 CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus();
720 CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this)
721 << " is initializing in a different thread";
722 }
723}
724
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700725inline ObjectArray<Class>* Class::GetInterfaces() {
726 CHECK(IsProxyClass());
727 // First static field.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700728 auto* field = GetStaticField(0);
729 DCHECK_STREQ(field->GetName(), "interfaces");
730 MemberOffset field_offset = field->GetOffset();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700731 return GetFieldObject<ObjectArray<Class>>(field_offset);
732}
733
734inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() {
735 CHECK(IsProxyClass());
736 // Second static field.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700737 auto* field = GetStaticField(1);
738 DCHECK_STREQ(field->GetName(), "throws");
739 MemberOffset field_offset = field->GetOffset();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700740 return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset);
741}
742
Fred Shih4ee7a662014-07-11 09:59:27 -0700743inline MemberOffset Class::GetDisableIntrinsicFlagOffset() {
744 CHECK(IsReferenceClass());
745 // First static field
Mathieu Chartierc7853442015-03-27 14:35:38 -0700746 auto* field = GetStaticField(0);
747 DCHECK_STREQ(field->GetName(), "disableIntrinsic");
748 return field->GetOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700749}
750
751inline MemberOffset Class::GetSlowPathFlagOffset() {
752 CHECK(IsReferenceClass());
753 // Second static field
Mathieu Chartierc7853442015-03-27 14:35:38 -0700754 auto* field = GetStaticField(1);
755 DCHECK_STREQ(field->GetName(), "slowPathEnabled");
756 return field->GetOffset();
Fred Shih4ee7a662014-07-11 09:59:27 -0700757}
758
759inline bool Class::GetSlowPathEnabled() {
Fred Shih37f05ef2014-07-16 18:38:08 -0700760 return GetFieldBoolean(GetSlowPathFlagOffset());
Fred Shih4ee7a662014-07-11 09:59:27 -0700761}
762
763inline void Class::SetSlowPath(bool enabled) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700764 SetFieldBoolean<false>(GetSlowPathFlagOffset(), enabled);
Fred Shih4ee7a662014-07-11 09:59:27 -0700765}
766
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700767inline void Class::InitializeClassVisitor::operator()(
768 mirror::Object* obj, size_t usable_size) const {
769 DCHECK_LE(class_size_, usable_size);
770 // Avoid AsClass as object is not yet in live bitmap or allocation stack.
771 mirror::Class* klass = down_cast<mirror::Class*>(obj);
772 // DCHECK(klass->IsClass());
773 klass->SetClassSize(class_size_);
774 klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive.
775 klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index.
776 klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index.
777}
778
Andreas Gampe48498592014-09-10 19:48:05 -0700779inline void Class::SetAccessFlags(uint32_t new_access_flags) {
780 // Called inside a transaction when setting pre-verified flag during boot image compilation.
781 if (Runtime::Current()->IsActiveTransaction()) {
782 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags);
783 } else {
784 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags);
785 }
786}
787
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700788inline uint32_t Class::NumDirectInterfaces() {
789 if (IsPrimitive()) {
790 return 0;
791 } else if (IsArrayClass()) {
792 return 2;
793 } else if (IsProxyClass()) {
794 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
795 return interfaces != nullptr ? interfaces->GetLength() : 0;
796 } else {
797 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
798 if (interfaces == nullptr) {
799 return 0;
800 } else {
801 return interfaces->Size();
802 }
803 }
804}
805
Mathieu Chartiereace4582014-11-24 18:29:54 -0800806inline void Class::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
807 SetFieldObject<false>(DexCacheStringsOffset(), new_dex_cache_strings);
808}
809
810inline ObjectArray<String>* Class::GetDexCacheStrings() {
811 return GetFieldObject<ObjectArray<String>>(DexCacheStringsOffset());
812}
813
Mathieu Chartierc7853442015-03-27 14:35:38 -0700814template<class Visitor>
815void mirror::Class::VisitFieldRoots(Visitor& visitor) {
816 ArtField* const sfields = GetSFieldsUnchecked();
817 for (size_t i = 0, count = NumStaticFields(); i < count; ++i) {
818 if (kIsDebugBuild && GetStatus() != kStatusRetired) {
819 CHECK_EQ(sfields[i].GetDeclaringClass(), this);
820 }
821 visitor.VisitRoot(sfields[i].DeclaringClassRoot().AddressWithoutBarrier());
822 }
823 ArtField* const ifields = GetIFieldsUnchecked();
824 for (size_t i = 0, count = NumInstanceFields(); i < count; ++i) {
825 if (kIsDebugBuild && GetStatus() != kStatusRetired) {
826 CHECK_EQ(ifields[i].GetDeclaringClass(), this);
827 }
828 visitor.VisitRoot(ifields[i].DeclaringClassRoot().AddressWithoutBarrier());
829 }
830}
831
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800832} // namespace mirror
833} // namespace art
834
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700835#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_