blob: 5752a15b8b9af4d88a0eb8e2e957be98c827d814 [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"
Vladimir Marko80afd022015-05-19 18:08:00 +010035#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036
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() {
Vladimir Marko3481ba22015-04-13 12:22:36 +010042 // Note: Extra parentheses to avoid the comma being interpreted as macro parameter separator.
43 DCHECK((!IsVariableSize<kVerifyFlags, kReadBarrierOption>())) << " class=" << PrettyTypeOf(this);
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -070044 return GetField32(ObjectSizeOffset());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045}
46
Ian Rogersef7d42f2014-01-06 12:55:46 -080047inline Class* Class::GetSuperClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048 // Can only get super class for loaded classes (hack for when runtime is
49 // initializing)
Brian Carlstrom073278c2014-02-19 15:21:21 -080050 DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded();
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070051 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
Ian Rogersef7d42f2014-01-06 12:55:46 -080054inline ClassLoader* Class::GetClassLoader() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070055 return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070056}
57
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080058template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080059inline DexCache* Class::GetDexCache() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070060 return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070061}
62
Ian Rogersef7d42f2014-01-06 12:55:46 -080063inline ObjectArray<ArtMethod>* Class::GetDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070065 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066}
67
Brian Carlstromea46f952013-07-30 01:26:50 -070068inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070070 DCHECK(nullptr == GetFieldObject<ObjectArray<ArtMethod>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070071 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 DCHECK_NE(0, new_direct_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070073 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), new_direct_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074}
75
Ian Rogersef7d42f2014-01-06 12:55:46 -080076inline ArtMethod* Class::GetDirectMethod(int32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 return GetDirectMethods()->Get(i);
78}
79
Brian Carlstromea46f952013-07-30 01:26:50 -070080inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070081 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070082 ObjectArray<ArtMethod>* direct_methods =
Ian Rogers700a4022014-05-19 16:49:03 -070083 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010084 direct_methods->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085}
86
87// Returns the number of static, private, and constructor methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -080088inline uint32_t Class::NumDirectMethods() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070089 return (GetDirectMethods() != nullptr) ? GetDirectMethods()->GetLength() : 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090}
91
Mathieu Chartier4e305412014-02-19 10:54:44 -080092template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080093inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070095 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096}
97
Brian Carlstromea46f952013-07-30 01:26:50 -070098inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 // TODO: we reassign virtual methods to grow the table for miranda
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100100 // methods.. they should really just be assigned once.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 DCHECK_NE(0, new_virtual_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700102 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), new_virtual_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103}
104
Ian Rogersef7d42f2014-01-06 12:55:46 -0800105inline uint32_t Class::NumVirtualMethods() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 return (GetVirtualMethods() != nullptr) ? GetVirtualMethods()->GetLength() : 0;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107}
108
Mathieu Chartier4e305412014-02-19 10:54:44 -0800109template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110inline ArtMethod* Class::GetVirtualMethod(uint32_t i) {
Hiroshi Yamauchif4c15a12014-10-20 16:56:58 -0700111 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>())
112 << PrettyClass(this) << " status=" << GetStatus();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700113 return GetVirtualMethods()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114}
115
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 DCHECK(IsLoaded() || IsErroneous());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700118 return GetVirtualMethods()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119}
120
Brian Carlstromea46f952013-07-30 01:26:50 -0700121inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700123 ObjectArray<ArtMethod>* virtual_methods =
Ian Rogers700a4022014-05-19 16:49:03 -0700124 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700125 virtual_methods->SetWithoutChecks<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126}
127
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128inline ObjectArray<ArtMethod>* Class::GetVTable() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 DCHECK(IsResolved() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -0700130 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131}
132
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -0700135 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136}
137
Ian Rogersef7d42f2014-01-06 12:55:46 -0800138inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700139 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140}
141
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700142inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i) {
143 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
144 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset));
145}
146
147inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method) {
148 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
149 SetFieldObject<false>(MemberOffset(offset), method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700150}
151
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700152inline bool Class::HasVTable() {
153 return (GetVTable() != nullptr) || ShouldHaveEmbeddedImtAndVTable();
154}
155
156inline int32_t Class::GetVTableLength() {
157 if (ShouldHaveEmbeddedImtAndVTable()) {
158 return GetEmbeddedVTableLength();
159 }
160 return (GetVTable() != nullptr) ? GetVTable()->GetLength() : 0;
161}
162
163inline ArtMethod* Class::GetVTableEntry(uint32_t i) {
164 if (ShouldHaveEmbeddedImtAndVTable()) {
165 return GetEmbeddedVTableEntry(i);
166 }
167 return (GetVTable() != nullptr) ? GetVTable()->Get(i) : nullptr;
168}
169
170inline int32_t Class::GetEmbeddedVTableLength() {
171 return GetField32(EmbeddedVTableLengthOffset());
172}
173
174inline void Class::SetEmbeddedVTableLength(int32_t len) {
175 SetField32<false>(EmbeddedVTableLengthOffset(), len);
176}
177
178inline ArtMethod* Class::GetEmbeddedVTableEntry(uint32_t i) {
179 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry);
180 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset));
181}
182
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700183inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method) {
184 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry);
185 SetFieldObject<false>(MemberOffset(offset), method);
186 CHECK(method == GetVTableDuringLinking()->Get(i));
187}
188
Ian Rogersef7d42f2014-01-06 12:55:46 -0800189inline bool Class::Implements(Class* klass) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700190 DCHECK(klass != nullptr);
Ian Rogers693ff612013-02-01 10:56:12 -0800191 DCHECK(klass->IsInterface()) << PrettyClass(this);
192 // All interfaces implemented directly and by our superclass, and
193 // recursively all super-interfaces of those interfaces, are listed
194 // in iftable_, so we can just do a linear scan through that.
195 int32_t iftable_count = GetIfTableCount();
196 IfTable* iftable = GetIfTable();
197 for (int32_t i = 0; i < iftable_count; i++) {
198 if (iftable->GetInterface(i) == klass) {
199 return true;
200 }
201 }
202 return false;
203}
204
205// Determine whether "this" is assignable from "src", where both of these
206// are array classes.
207//
208// Consider an array class, e.g. Y[][], where Y is a subclass of X.
209// Y[][] = Y[][] --> true (identity)
210// X[][] = Y[][] --> true (element superclass)
211// Y = Y[][] --> false
212// Y[] = Y[][] --> false
213// Object = Y[][] --> true (everything is an object)
214// Object[] = Y[][] --> true
215// Object[][] = Y[][] --> true
216// Object[][][] = Y[][] --> false (too many []s)
217// Serializable = Y[][] --> true (all arrays are Serializable)
218// Serializable[] = Y[][] --> true
219// Serializable[][] = Y[][] --> false (unless Y is Serializable)
220//
221// Don't forget about primitive types.
222// Object[] = int[] --> false
223//
Ian Rogersef7d42f2014-01-06 12:55:46 -0800224inline bool Class::IsArrayAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800225 DCHECK(IsArrayClass()) << PrettyClass(this);
226 DCHECK(src->IsArrayClass()) << PrettyClass(src);
227 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
228}
229
Ian Rogersef7d42f2014-01-06 12:55:46 -0800230inline bool Class::IsAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800231 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
232 DCHECK(src->IsArrayClass()) << PrettyClass(src);
233 if (!IsArrayClass()) {
234 // If "this" is not also an array, it must be Object.
235 // src's super should be java_lang_Object, since it is an array.
236 Class* java_lang_Object = src->GetSuperClass();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 DCHECK(java_lang_Object != nullptr) << PrettyClass(src);
238 DCHECK(java_lang_Object->GetSuperClass() == nullptr) << PrettyClass(src);
Ian Rogers693ff612013-02-01 10:56:12 -0800239 return this == java_lang_Object;
240 }
241 return IsArrayAssignableFromArray(src);
242}
243
Vladimir Marko89786432014-01-31 15:03:55 +0000244template <bool throw_on_failure, bool use_referrers_cache>
245inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246 uint32_t field_idx, DexCache* dex_cache) {
Vladimir Marko89786432014-01-31 15:03:55 +0000247 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000248 if (UNLIKELY(!this->CanAccess(access_to))) {
249 // The referrer class can't access the field's declaring class but may still be able
250 // to access the field if the FieldId specifies an accessible subclass of the declaring
251 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800252 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000253 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
254 // The referenced class has already been resolved with the field, get it from the dex cache.
255 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
256 DCHECK(dex_access_to != nullptr);
257 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
258 if (throw_on_failure) {
259 ThrowIllegalAccessErrorClass(this, dex_access_to);
260 }
261 return false;
262 }
263 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()),
264 this->CanAccessMember(dex_access_to, field->GetAccessFlags()));
265 }
266 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
267 return true;
268 }
269 if (throw_on_failure) {
270 ThrowIllegalAccessErrorField(this, field);
271 }
272 return false;
273}
274
Vladimir Marko89786432014-01-31 15:03:55 +0000275template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type>
276inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800277 uint32_t method_idx, DexCache* dex_cache) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800278 static_assert(throw_on_failure || throw_invoke_type == kStatic, "Non-default throw invoke type");
Vladimir Marko89786432014-01-31 15:03:55 +0000279 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000280 if (UNLIKELY(!this->CanAccess(access_to))) {
281 // The referrer class can't access the method's declaring class but may still be able
282 // to access the method if the MethodId specifies an accessible subclass of the declaring
283 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800284 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000285 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
286 // The referenced class has already been resolved with the method, get it from the dex cache.
287 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
288 DCHECK(dex_access_to != nullptr);
289 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
290 if (throw_on_failure) {
291 ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to,
292 method, throw_invoke_type);
293 }
294 return false;
295 }
296 DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()),
297 this->CanAccessMember(dex_access_to, method->GetAccessFlags()));
298 }
299 if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) {
300 return true;
301 }
302 if (throw_on_failure) {
303 ThrowIllegalAccessErrorMethod(this, method);
304 }
305 return false;
306}
307
Vladimir Marko89786432014-01-31 15:03:55 +0000308inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800309 DexCache* dex_cache, uint32_t field_idx) {
310 return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000311}
312
313inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field,
314 uint32_t field_idx) {
315 return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr);
316}
317
318inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800319 DexCache* dex_cache, uint32_t method_idx) {
320 return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000321}
322
323template <InvokeType throw_invoke_type>
324inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method,
325 uint32_t method_idx) {
326 return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx,
327 nullptr);
328}
329
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330inline bool Class::IsSubClass(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800331 DCHECK(!IsInterface()) << PrettyClass(this);
332 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800333 Class* current = this;
Ian Rogers693ff612013-02-01 10:56:12 -0800334 do {
335 if (current == klass) {
336 return true;
337 }
338 current = current->GetSuperClass();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700339 } while (current != nullptr);
Ian Rogers693ff612013-02-01 10:56:12 -0800340 return false;
341}
342
Ian Rogersef7d42f2014-01-06 12:55:46 -0800343inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) {
Ian Rogers693ff612013-02-01 10:56:12 -0800344 Class* declaring_class = method->GetDeclaringClass();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700345 DCHECK(declaring_class != nullptr) << PrettyClass(this);
Ian Rogers693ff612013-02-01 10:56:12 -0800346 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
347 // TODO cache to improve lookup speed
348 int32_t iftable_count = GetIfTableCount();
349 IfTable* iftable = GetIfTable();
350 for (int32_t i = 0; i < iftable_count; i++) {
351 if (iftable->GetInterface(i) == declaring_class) {
352 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
353 }
354 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700355 return nullptr;
Ian Rogers693ff612013-02-01 10:56:12 -0800356}
357
Ian Rogersef7d42f2014-01-06 12:55:46 -0800358inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700359 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 // The argument method may from a super class.
361 // Use the index to a potentially overridden one for this instance's class.
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700362 return GetVTableEntry(method->GetMethodIndex());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363}
364
Ian Rogersef7d42f2014-01-06 12:55:46 -0800365inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 DCHECK(!method->GetDeclaringClass()->IsInterface());
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700367 return GetSuperClass()->GetVTableEntry(method->GetMethodIndex());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368}
369
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 if (method->IsDirect()) {
372 return method;
373 }
Jeff Hao201803f2013-11-20 18:11:39 -0800374 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 return FindVirtualMethodForInterface(method);
376 }
377 return FindVirtualMethodForVirtual(method);
378}
379
Ian Rogersef7d42f2014-01-06 12:55:46 -0800380inline IfTable* Class::GetIfTable() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700381 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382}
383
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384inline int32_t Class::GetIfTableCount() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 IfTable* iftable = GetIfTable();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700386 if (iftable == nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800387 return 0;
388 }
389 return iftable->Count();
390}
391
392inline void Class::SetIfTable(IfTable* new_iftable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700393 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394}
395
Mathieu Chartierc7853442015-03-27 14:35:38 -0700396inline ArtField* Class::GetIFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 DCHECK(IsLoaded() || IsErroneous());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700398 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399}
400
Vladimir Marko76649e82014-11-10 18:32:59 +0000401inline MemberOffset Class::GetFirstReferenceInstanceFieldOffset() {
402 Class* super_class = GetSuperClass();
403 return (super_class != nullptr)
404 ? MemberOffset(RoundUp(super_class->GetObjectSize(),
405 sizeof(mirror::HeapReference<mirror::Object>)))
406 : ClassOffset();
407}
408
409inline MemberOffset Class::GetFirstReferenceStaticFieldOffset() {
410 DCHECK(IsResolved());
411 uint32_t base = sizeof(mirror::Class); // Static fields come after the class.
412 if (ShouldHaveEmbeddedImtAndVTable()) {
413 // Static fields come after the embedded tables.
414 base = mirror::Class::ComputeClassSize(true, GetEmbeddedVTableLength(),
415 0, 0, 0, 0, 0);
416 }
417 return MemberOffset(base);
418}
419
420inline MemberOffset Class::GetFirstReferenceStaticFieldOffsetDuringLinking() {
421 DCHECK(IsLoaded());
422 uint32_t base = sizeof(mirror::Class); // Static fields come after the class.
423 if (ShouldHaveEmbeddedImtAndVTable()) {
424 // Static fields come after the embedded tables.
425 base = mirror::Class::ComputeClassSize(true, GetVTableDuringLinking()->GetLength(),
426 0, 0, 0, 0, 0);
427 }
428 return MemberOffset(base);
429}
430
Mathieu Chartierc7853442015-03-27 14:35:38 -0700431inline void Class::SetIFields(ArtField* new_ifields) {
432 DCHECK(GetIFieldsUnchecked() == nullptr);
433 return SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434}
435
Mathieu Chartierc7853442015-03-27 14:35:38 -0700436inline void Class::SetIFieldsUnchecked(ArtField* new_ifields) {
437 SetFieldPtr<false, true, kVerifyNone>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
438}
439
440inline ArtField* Class::GetSFieldsUnchecked() {
441 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
442}
443
444inline ArtField* Class::GetIFieldsUnchecked() {
445 return GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
446}
447
448inline ArtField* Class::GetSFields() {
Mathieu Chartier987ca8b2015-03-15 14:19:14 -0700449 DCHECK(IsLoaded() || IsErroneous()) << GetStatus();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700450 return GetSFieldsUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800451}
452
Mathieu Chartierc7853442015-03-27 14:35:38 -0700453inline void Class::SetSFields(ArtField* new_sfields) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700454 DCHECK((IsRetired() && new_sfields == nullptr) ||
Mathieu Chartierc7853442015-03-27 14:35:38 -0700455 GetFieldPtr<ArtField*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_)) == nullptr);
456 SetFieldPtr<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457}
458
Mathieu Chartierc7853442015-03-27 14:35:38 -0700459inline void Class::SetSFieldsUnchecked(ArtField* new_sfields) {
460 SetFieldPtr<false, true, kVerifyNone>(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 ArtField* Class::GetStaticField(uint32_t i) {
464 DCHECK_LT(i, NumStaticFields());
465 return &GetSFields()[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466}
467
Mathieu Chartierc7853442015-03-27 14:35:38 -0700468inline ArtField* Class::GetInstanceField(uint32_t i) {
469 DCHECK_LT(i, NumInstanceFields());
470 return &GetIFields()[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471}
472
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700473template<VerifyObjectFlags kVerifyFlags>
474inline uint32_t Class::GetReferenceInstanceOffsets() {
475 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
476 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_));
477}
478
479inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) {
480 if (Runtime::Current()->IsActiveTransaction()) {
481 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
482 } else {
483 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
484 }
485}
486
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487inline void Class::SetVerifyErrorClass(Class* klass) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700488 CHECK(klass != nullptr) << PrettyClass(this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100489 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700490 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100491 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700492 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100493 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494}
495
Mathieu Chartier4e305412014-02-19 10:54:44 -0800496template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497inline uint32_t Class::GetAccessFlags() {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700498 // Check class is loaded/retired or this is java.lang.String that has a
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 // circularity issue during loading the names of its members
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700500 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
Mathieu Chartier4e305412014-02-19 10:54:44 -0800501 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502 this == String::GetJavaLangString() ||
Andreas Gampea6928662014-12-12 11:06:00 -0800503 this == ArtMethod::GetJavaLangReflectArtMethod())
504 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
505 << " IsRetired=" << IsRetired<kVerifyFlags>()
506 << " IsErroneous=" <<
507 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
508 << " IsString=" << (this == String::GetJavaLangString())
Andreas Gampea6928662014-12-12 11:06:00 -0800509 << " IsArtMethod=" << (this == ArtMethod::GetJavaLangReflectArtMethod())
510 << " descriptor=" << PrettyDescriptor(this);
Hiroshi Yamauchie01a5202015-03-19 12:35:04 -0700511 return GetField32<kVerifyFlags>(AccessFlagsOffset());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800512}
513
Ian Rogersef7d42f2014-01-06 12:55:46 -0800514inline String* Class::GetName() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700515 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516}
517inline void Class::SetName(String* name) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100518 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700519 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100520 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700521 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100522 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523}
524
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700525template<VerifyObjectFlags kVerifyFlags>
526inline Primitive::Type Class::GetPrimitiveType() {
527 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700528 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
529 Primitive::Type type = static_cast<Primitive::Type>(v32 & 0xFFFF);
530 DCHECK_EQ(static_cast<size_t>(v32 >> 16), Primitive::ComponentSizeShift(type));
531 return type;
532}
533
534template<VerifyObjectFlags kVerifyFlags>
535inline size_t Class::GetPrimitiveTypeSizeShift() {
536 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
537 int32_t v32 = GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_));
538 size_t size_shift = static_cast<Primitive::Type>(v32 >> 16);
539 DCHECK_EQ(size_shift, Primitive::ComponentSizeShift(static_cast<Primitive::Type>(v32 & 0xFFFF)));
540 return size_shift;
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700541}
542
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700543inline void Class::CheckObjectAlloc() {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700544 DCHECK(!IsArrayClass())
545 << PrettyClass(this)
546 << "A array shouldn't be allocated through this "
547 << "as it requires a pre-fence visitor that sets the class size.";
548 DCHECK(!IsClassClass())
549 << PrettyClass(this)
550 << "A class object shouldn't be allocated through this "
551 << "as it requires a pre-fence visitor that sets the class size.";
Jeff Hao848f70a2014-01-15 13:49:50 -0800552 DCHECK(!IsStringClass())
553 << PrettyClass(this)
554 << "A string 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()) {
Vladimir Marko3481ba22015-04-13 12:22:36 +0100709 return ProxyDescriptorEquals(match);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700710 } 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();
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700817 // Since we visit class roots while we may be writing these fields, check against null.
818 // TODO: Is this safe for concurrent compaction?
819 if (sfields != nullptr) {
820 for (size_t i = 0, count = NumStaticFields(); i < count; ++i) {
821 if (kIsDebugBuild && IsResolved()) {
822 CHECK_EQ(sfields[i].GetDeclaringClass(), this) << GetStatus();
823 }
824 visitor.VisitRoot(sfields[i].DeclaringClassRoot().AddressWithoutBarrier());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700825 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700826 }
827 ArtField* const ifields = GetIFieldsUnchecked();
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700828 if (ifields != nullptr) {
829 for (size_t i = 0, count = NumInstanceFields(); i < count; ++i) {
830 if (kIsDebugBuild && IsResolved()) {
831 CHECK_EQ(ifields[i].GetDeclaringClass(), this) << GetStatus();
832 }
833 visitor.VisitRoot(ifields[i].DeclaringClassRoot().AddressWithoutBarrier());
Mathieu Chartierc7853442015-03-27 14:35:38 -0700834 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700835 }
836}
837
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800838} // namespace mirror
839} // namespace art
840
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700841#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_