blob: 349d4a34df2e8abc00339169ec5d3c9dfe1704a7 [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"
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() {
Hiroshi Yamauchi25023c72014-05-09 11:45:53 -070041 if (kIsDebugBuild) {
42 // Use a local variable as (D)CHECK can't handle the space between
43 // the two template params.
44 bool is_variable_size = IsVariableSize<kVerifyFlags, kReadBarrierOption>();
45 CHECK(!is_variable_size) << " class=" << PrettyTypeOf(this);
46 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070047 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048}
49
Ian Rogersef7d42f2014-01-06 12:55:46 -080050inline Class* Class::GetSuperClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051 // Can only get super class for loaded classes (hack for when runtime is
52 // initializing)
Brian Carlstrom073278c2014-02-19 15:21:21 -080053 DCHECK(IsLoaded() || IsErroneous() || !Runtime::Current()->IsStarted()) << IsLoaded();
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070054 return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055}
56
Ian Rogersef7d42f2014-01-06 12:55:46 -080057inline ClassLoader* Class::GetClassLoader() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070058 return GetFieldObject<ClassLoader>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070059}
60
Mathieu Chartierc2f4d022014-03-03 16:11:42 -080061template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080062inline DexCache* Class::GetDexCache() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070063 return GetFieldObject<DexCache, kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_));
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070064}
65
Ian Rogersef7d42f2014-01-06 12:55:46 -080066inline ObjectArray<ArtMethod>* Class::GetDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070068 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069}
70
Brian Carlstromea46f952013-07-30 01:26:50 -070071inline void Class::SetDirectMethods(ObjectArray<ArtMethod>* new_direct_methods)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -070073 DCHECK(NULL == GetFieldObject<ObjectArray<ArtMethod>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070074 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 DCHECK_NE(0, new_direct_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070076 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), new_direct_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077}
78
Ian Rogersef7d42f2014-01-06 12:55:46 -080079inline ArtMethod* Class::GetDirectMethod(int32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 return GetDirectMethods()->Get(i);
81}
82
Brian Carlstromea46f952013-07-30 01:26:50 -070083inline void Class::SetDirectMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070084 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -070085 ObjectArray<ArtMethod>* direct_methods =
Ian Rogers700a4022014-05-19 16:49:03 -070086 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010087 direct_methods->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088}
89
90// Returns the number of static, private, and constructor methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -080091inline uint32_t Class::NumDirectMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
93}
94
Mathieu Chartier4e305412014-02-19 10:54:44 -080095template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -080096inline ObjectArray<ArtMethod>* Class::GetVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -070098 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099}
100
Brian Carlstromea46f952013-07-30 01:26:50 -0700101inline void Class::SetVirtualMethods(ObjectArray<ArtMethod>* new_virtual_methods) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 // TODO: we reassign virtual methods to grow the table for miranda
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100103 // methods.. they should really just be assigned once.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 DCHECK_NE(0, new_virtual_methods->GetLength());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700105 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), new_virtual_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106}
107
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108inline uint32_t Class::NumVirtualMethods() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
110}
111
Mathieu Chartier4e305412014-02-19 10:54:44 -0800112template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113inline ArtMethod* Class::GetVirtualMethod(uint32_t i) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800114 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 return GetVirtualMethods()->Get(i);
116}
117
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118inline ArtMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 DCHECK(IsLoaded() || IsErroneous());
120 return GetVirtualMethods()->Get(i);
121}
122
Brian Carlstromea46f952013-07-30 01:26:50 -0700123inline void Class::SetVirtualMethod(uint32_t i, ArtMethod* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700125 ObjectArray<ArtMethod>* virtual_methods =
Ian Rogers700a4022014-05-19 16:49:03 -0700126 GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100127 virtual_methods->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128}
129
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130inline ObjectArray<ArtMethod>* Class::GetVTable() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 DCHECK(IsResolved() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -0700132 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133}
134
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135inline ObjectArray<ArtMethod>* Class::GetVTableDuringLinking() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -0700137 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138}
139
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140inline void Class::SetVTable(ObjectArray<ArtMethod>* new_vtable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700141 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142}
143
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144inline ObjectArray<ArtMethod>* Class::GetImTable() {
Ian Rogers700a4022014-05-19 16:49:03 -0700145 return GetFieldObject<ObjectArray<ArtMethod>>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_));
Jeff Hao88474b42013-10-23 16:24:40 -0700146}
147
148inline void Class::SetImTable(ObjectArray<ArtMethod>* new_imtable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700149 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, imtable_), new_imtable);
Jeff Hao88474b42013-10-23 16:24:40 -0700150}
151
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700152inline ArtMethod* Class::GetEmbeddedImTableEntry(uint32_t i) {
153 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
154 return GetFieldObject<mirror::ArtMethod>(MemberOffset(offset));
155}
156
157inline void Class::SetEmbeddedImTableEntry(uint32_t i, ArtMethod* method) {
158 uint32_t offset = EmbeddedImTableOffset().Uint32Value() + i * sizeof(ImTableEntry);
159 SetFieldObject<false>(MemberOffset(offset), method);
160 CHECK(method == GetImTable()->Get(i));
161}
162
163inline void Class::SetEmbeddedVTableEntry(uint32_t i, ArtMethod* method) {
164 uint32_t offset = EmbeddedVTableOffset().Uint32Value() + i * sizeof(VTableEntry);
165 SetFieldObject<false>(MemberOffset(offset), method);
166 CHECK(method == GetVTableDuringLinking()->Get(i));
167}
168
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169inline bool Class::Implements(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800170 DCHECK(klass != NULL);
171 DCHECK(klass->IsInterface()) << PrettyClass(this);
172 // All interfaces implemented directly and by our superclass, and
173 // recursively all super-interfaces of those interfaces, are listed
174 // in iftable_, so we can just do a linear scan through that.
175 int32_t iftable_count = GetIfTableCount();
176 IfTable* iftable = GetIfTable();
177 for (int32_t i = 0; i < iftable_count; i++) {
178 if (iftable->GetInterface(i) == klass) {
179 return true;
180 }
181 }
182 return false;
183}
184
185// Determine whether "this" is assignable from "src", where both of these
186// are array classes.
187//
188// Consider an array class, e.g. Y[][], where Y is a subclass of X.
189// Y[][] = Y[][] --> true (identity)
190// X[][] = Y[][] --> true (element superclass)
191// Y = Y[][] --> false
192// Y[] = Y[][] --> false
193// Object = Y[][] --> true (everything is an object)
194// Object[] = Y[][] --> true
195// Object[][] = Y[][] --> true
196// Object[][][] = Y[][] --> false (too many []s)
197// Serializable = Y[][] --> true (all arrays are Serializable)
198// Serializable[] = Y[][] --> true
199// Serializable[][] = Y[][] --> false (unless Y is Serializable)
200//
201// Don't forget about primitive types.
202// Object[] = int[] --> false
203//
Ian Rogersef7d42f2014-01-06 12:55:46 -0800204inline bool Class::IsArrayAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800205 DCHECK(IsArrayClass()) << PrettyClass(this);
206 DCHECK(src->IsArrayClass()) << PrettyClass(src);
207 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
208}
209
Ian Rogersef7d42f2014-01-06 12:55:46 -0800210inline bool Class::IsAssignableFromArray(Class* src) {
Ian Rogers693ff612013-02-01 10:56:12 -0800211 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
212 DCHECK(src->IsArrayClass()) << PrettyClass(src);
213 if (!IsArrayClass()) {
214 // If "this" is not also an array, it must be Object.
215 // src's super should be java_lang_Object, since it is an array.
216 Class* java_lang_Object = src->GetSuperClass();
217 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700218 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Ian Rogers693ff612013-02-01 10:56:12 -0800219 return this == java_lang_Object;
220 }
221 return IsArrayAssignableFromArray(src);
222}
223
Vladimir Marko89786432014-01-31 15:03:55 +0000224template <bool throw_on_failure, bool use_referrers_cache>
225inline bool Class::ResolvedFieldAccessTest(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800226 uint32_t field_idx, DexCache* dex_cache) {
Vladimir Marko89786432014-01-31 15:03:55 +0000227 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000228 if (UNLIKELY(!this->CanAccess(access_to))) {
229 // The referrer class can't access the field's declaring class but may still be able
230 // to access the field if the FieldId specifies an accessible subclass of the declaring
231 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800232 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000233 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetFieldId(field_idx).class_idx_;
234 // The referenced class has already been resolved with the field, get it from the dex cache.
235 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
236 DCHECK(dex_access_to != nullptr);
237 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
238 if (throw_on_failure) {
239 ThrowIllegalAccessErrorClass(this, dex_access_to);
240 }
241 return false;
242 }
243 DCHECK_EQ(this->CanAccessMember(access_to, field->GetAccessFlags()),
244 this->CanAccessMember(dex_access_to, field->GetAccessFlags()));
245 }
246 if (LIKELY(this->CanAccessMember(access_to, field->GetAccessFlags()))) {
247 return true;
248 }
249 if (throw_on_failure) {
250 ThrowIllegalAccessErrorField(this, field);
251 }
252 return false;
253}
254
Vladimir Marko89786432014-01-31 15:03:55 +0000255template <bool throw_on_failure, bool use_referrers_cache, InvokeType throw_invoke_type>
256inline bool Class::ResolvedMethodAccessTest(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800257 uint32_t method_idx, DexCache* dex_cache) {
Vladimir Marko23a28212014-01-09 19:24:37 +0000258 COMPILE_ASSERT(throw_on_failure || throw_invoke_type == kStatic, non_default_throw_invoke_type);
Vladimir Marko89786432014-01-31 15:03:55 +0000259 DCHECK_EQ(use_referrers_cache, dex_cache == nullptr);
Vladimir Marko23a28212014-01-09 19:24:37 +0000260 if (UNLIKELY(!this->CanAccess(access_to))) {
261 // The referrer class can't access the method's declaring class but may still be able
262 // to access the method if the MethodId specifies an accessible subclass of the declaring
263 // class rather than the declaring class itself.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800264 DexCache* referrer_dex_cache = use_referrers_cache ? this->GetDexCache() : dex_cache;
Vladimir Marko23a28212014-01-09 19:24:37 +0000265 uint32_t class_idx = referrer_dex_cache->GetDexFile()->GetMethodId(method_idx).class_idx_;
266 // The referenced class has already been resolved with the method, get it from the dex cache.
267 Class* dex_access_to = referrer_dex_cache->GetResolvedType(class_idx);
268 DCHECK(dex_access_to != nullptr);
269 if (UNLIKELY(!this->CanAccess(dex_access_to))) {
270 if (throw_on_failure) {
271 ThrowIllegalAccessErrorClassForMethodDispatch(this, dex_access_to,
272 method, throw_invoke_type);
273 }
274 return false;
275 }
276 DCHECK_EQ(this->CanAccessMember(access_to, method->GetAccessFlags()),
277 this->CanAccessMember(dex_access_to, method->GetAccessFlags()));
278 }
279 if (LIKELY(this->CanAccessMember(access_to, method->GetAccessFlags()))) {
280 return true;
281 }
282 if (throw_on_failure) {
283 ThrowIllegalAccessErrorMethod(this, method);
284 }
285 return false;
286}
287
Vladimir Marko89786432014-01-31 15:03:55 +0000288inline bool Class::CanAccessResolvedField(Class* access_to, ArtField* field,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800289 DexCache* dex_cache, uint32_t field_idx) {
290 return ResolvedFieldAccessTest<false, false>(access_to, field, field_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000291}
292
293inline bool Class::CheckResolvedFieldAccess(Class* access_to, ArtField* field,
294 uint32_t field_idx) {
295 return ResolvedFieldAccessTest<true, true>(access_to, field, field_idx, nullptr);
296}
297
298inline bool Class::CanAccessResolvedMethod(Class* access_to, ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299 DexCache* dex_cache, uint32_t method_idx) {
300 return ResolvedMethodAccessTest<false, false, kStatic>(access_to, method, method_idx, dex_cache);
Vladimir Marko89786432014-01-31 15:03:55 +0000301}
302
303template <InvokeType throw_invoke_type>
304inline bool Class::CheckResolvedMethodAccess(Class* access_to, ArtMethod* method,
305 uint32_t method_idx) {
306 return ResolvedMethodAccessTest<true, true, throw_invoke_type>(access_to, method, method_idx,
307 nullptr);
308}
309
Ian Rogersef7d42f2014-01-06 12:55:46 -0800310inline bool Class::IsSubClass(Class* klass) {
Ian Rogers693ff612013-02-01 10:56:12 -0800311 DCHECK(!IsInterface()) << PrettyClass(this);
312 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800313 Class* current = this;
Ian Rogers693ff612013-02-01 10:56:12 -0800314 do {
315 if (current == klass) {
316 return true;
317 }
318 current = current->GetSuperClass();
319 } while (current != NULL);
320 return false;
321}
322
Ian Rogersef7d42f2014-01-06 12:55:46 -0800323inline ArtMethod* Class::FindVirtualMethodForInterface(ArtMethod* method) {
Ian Rogers693ff612013-02-01 10:56:12 -0800324 Class* declaring_class = method->GetDeclaringClass();
325 DCHECK(declaring_class != NULL) << PrettyClass(this);
326 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
327 // TODO cache to improve lookup speed
328 int32_t iftable_count = GetIfTableCount();
329 IfTable* iftable = GetIfTable();
330 for (int32_t i = 0; i < iftable_count; i++) {
331 if (iftable->GetInterface(i) == declaring_class) {
332 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
333 }
334 }
335 return NULL;
336}
337
Ian Rogersef7d42f2014-01-06 12:55:46 -0800338inline ArtMethod* Class::FindVirtualMethodForVirtual(ArtMethod* method) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700339 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 // The argument method may from a super class.
341 // Use the index to a potentially overridden one for this instance's class.
342 return GetVTable()->Get(method->GetMethodIndex());
343}
344
Ian Rogersef7d42f2014-01-06 12:55:46 -0800345inline ArtMethod* Class::FindVirtualMethodForSuper(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346 DCHECK(!method->GetDeclaringClass()->IsInterface());
347 return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex());
348}
349
Ian Rogersef7d42f2014-01-06 12:55:46 -0800350inline ArtMethod* Class::FindVirtualMethodForVirtualOrInterface(ArtMethod* method) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 if (method->IsDirect()) {
352 return method;
353 }
Jeff Hao201803f2013-11-20 18:11:39 -0800354 if (method->GetDeclaringClass()->IsInterface() && !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 return FindVirtualMethodForInterface(method);
356 }
357 return FindVirtualMethodForVirtual(method);
358}
359
Ian Rogersef7d42f2014-01-06 12:55:46 -0800360inline IfTable* Class::GetIfTable() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700361 return GetFieldObject<IfTable>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362}
363
Ian Rogersef7d42f2014-01-06 12:55:46 -0800364inline int32_t Class::GetIfTableCount() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 IfTable* iftable = GetIfTable();
366 if (iftable == NULL) {
367 return 0;
368 }
369 return iftable->Count();
370}
371
372inline void Class::SetIfTable(IfTable* new_iftable) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700373 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374}
375
Ian Rogersef7d42f2014-01-06 12:55:46 -0800376inline ObjectArray<ArtField>* Class::GetIFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 DCHECK(IsLoaded() || IsErroneous());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700378 return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379}
380
Brian Carlstromea46f952013-07-30 01:26:50 -0700381inline void Class::SetIFields(ObjectArray<ArtField>* new_ifields)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -0700383 DCHECK(NULL == GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_)));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700384 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385}
386
Ian Rogersef7d42f2014-01-06 12:55:46 -0800387inline ObjectArray<ArtField>* Class::GetSFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers700a4022014-05-19 16:49:03 -0700389 return GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390}
391
Brian Carlstromea46f952013-07-30 01:26:50 -0700392inline void Class::SetSFields(ObjectArray<ArtField>* new_sfields)
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700394 DCHECK((IsRetired() && new_sfields == nullptr) ||
395 (NULL == GetFieldObject<ObjectArray<ArtField>>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_))));
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700396 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397}
398
Ian Rogersef7d42f2014-01-06 12:55:46 -0800399inline uint32_t Class::NumStaticFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
401}
402
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700403
Ian Rogersef7d42f2014-01-06 12:55:46 -0800404inline ArtField* Class::GetStaticField(uint32_t i) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700406 return GetSFields()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407}
408
Brian Carlstromea46f952013-07-30 01:26:50 -0700409inline void Class::SetStaticField(uint32_t i, ArtField* f) // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -0700411 ObjectArray<ArtField>* sfields= GetFieldObject<ObjectArray<ArtField>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700412 OFFSET_OF_OBJECT_MEMBER(Class, sfields_));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100413 sfields->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414}
415
Ian Rogersef7d42f2014-01-06 12:55:46 -0800416inline uint32_t Class::NumInstanceFields() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
418}
419
Ian Rogersef7d42f2014-01-06 12:55:46 -0800420inline ArtField* Class::GetInstanceField(uint32_t i) { // TODO: uint16_t
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 DCHECK_NE(NumInstanceFields(), 0U);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700422 return GetIFields()->GetWithoutChecks(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423}
424
Brian Carlstromea46f952013-07-30 01:26:50 -0700425inline void Class::SetInstanceField(uint32_t i, ArtField* f) // TODO: uint16_t
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700426 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers700a4022014-05-19 16:49:03 -0700427 ObjectArray<ArtField>* ifields= GetFieldObject<ObjectArray<ArtField>>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700428 OFFSET_OF_OBJECT_MEMBER(Class, ifields_));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100429 ifields->Set<false>(i, f);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430}
431
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700432template<VerifyObjectFlags kVerifyFlags>
433inline uint32_t Class::GetReferenceInstanceOffsets() {
434 DCHECK(IsResolved<kVerifyFlags>() || IsErroneous<kVerifyFlags>());
435 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_));
436}
437
438inline void Class::SetClinitThreadId(pid_t new_clinit_thread_id) {
439 if (Runtime::Current()->IsActiveTransaction()) {
440 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
441 } else {
442 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id);
443 }
444}
445
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446inline void Class::SetVerifyErrorClass(Class* klass) {
447 CHECK(klass != NULL) << PrettyClass(this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100448 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700449 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100450 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700451 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100452 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453}
454
Mathieu Chartier4e305412014-02-19 10:54:44 -0800455template<VerifyObjectFlags kVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800456inline uint32_t Class::GetAccessFlags() {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700457 // Check class is loaded/retired or this is java.lang.String that has a
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800458 // circularity issue during loading the names of its members
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700459 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
Mathieu Chartier4e305412014-02-19 10:54:44 -0800460 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 this == String::GetJavaLangString() ||
Brian Carlstromea46f952013-07-30 01:26:50 -0700462 this == ArtField::GetJavaLangReflectArtField() ||
463 this == ArtMethod::GetJavaLangReflectArtMethod());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700464 return GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465}
466
Ian Rogersef7d42f2014-01-06 12:55:46 -0800467inline String* Class::GetName() {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700468 return GetFieldObject<String>(OFFSET_OF_OBJECT_MEMBER(Class, name_));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469}
470inline void Class::SetName(String* name) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100471 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700472 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100473 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700474 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, name_), name);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100475 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476}
477
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700478template<VerifyObjectFlags kVerifyFlags>
479inline Primitive::Type Class::GetPrimitiveType() {
480 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
481 return static_cast<Primitive::Type>(
482 GetField32<kVerifyFlags>(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_)));
483}
484
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700485inline void Class::CheckObjectAlloc() {
Hiroshi Yamauchi4cd662e2014-04-03 16:28:10 -0700486 DCHECK(!IsArrayClass())
487 << PrettyClass(this)
488 << "A array shouldn't be allocated through this "
489 << "as it requires a pre-fence visitor that sets the class size.";
490 DCHECK(!IsClassClass())
491 << PrettyClass(this)
492 << "A class object shouldn't be allocated through this "
493 << "as it requires a pre-fence visitor that sets the class size.";
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700494 DCHECK(IsInstantiable()) << PrettyClass(this);
495 // TODO: decide whether we want this check. It currently fails during bootstrap.
496 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
497 DCHECK_GE(this->object_size_, sizeof(Object));
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700498}
499
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700500template<bool kIsInstrumented, bool kCheckAddFinalizer>
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800501inline Object* Class::Alloc(Thread* self, gc::AllocatorType allocator_type) {
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700502 CheckObjectAlloc();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700503 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier8668c3c2014-04-24 16:48:11 -0700504 const bool add_finalizer = kCheckAddFinalizer && IsFinalizable();
505 if (!kCheckAddFinalizer) {
506 DCHECK(!IsFinalizable());
507 }
508 mirror::Object* obj =
509 heap->AllocObjectWithAllocator<kIsInstrumented, false>(self, this, this->object_size_,
510 allocator_type, VoidFunctor());
511 if (add_finalizer && LIKELY(obj != nullptr)) {
512 heap->AddFinalizerReference(self, &obj);
513 }
514 return obj;
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800515}
516
517inline Object* Class::AllocObject(Thread* self) {
518 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
519}
520
521inline Object* Class::AllocNonMovableObject(Thread* self) {
522 return Alloc<true>(self, Runtime::Current()->GetHeap()->GetCurrentNonMovingAllocator());
Hiroshi Yamauchi967a0ad2013-09-10 16:24:21 -0700523}
524
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700525inline uint32_t Class::ComputeClassSize(bool has_embedded_tables,
526 uint32_t num_vtable_entries,
527 uint32_t num_32bit_static_fields,
528 uint32_t num_64bit_static_fields,
529 uint32_t num_ref_static_fields) {
530 // Space used by java.lang.Class and its instance fields.
531 uint32_t size = sizeof(Class);
532 // Space used by embedded tables.
533 if (has_embedded_tables) {
534 uint32_t embedded_imt_size = kImtSize * sizeof(ImTableEntry);
535 uint32_t embedded_vtable_size = num_vtable_entries * sizeof(VTableEntry);
536 size += embedded_imt_size + embedded_vtable_size;
537 }
538 // Space used by reference statics.
539 size += num_ref_static_fields * sizeof(HeapReference<Object>);
540 // Possible pad for alignment.
541 if (((size & 7) != 0) && (num_64bit_static_fields > 0) && (num_32bit_static_fields == 0)) {
542 size += sizeof(uint32_t);
543 }
544 // Space used for primitive static fields.
545 size += (num_32bit_static_fields * sizeof(uint32_t)) +
546 (num_64bit_static_fields * sizeof(uint64_t));
547 return size;
548}
549
Mathieu Chartier407f7022014-02-18 14:37:05 -0800550template <bool kVisitClass, typename Visitor>
551inline void Class::VisitReferences(mirror::Class* klass, const Visitor& visitor) {
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700552 // Visit the static fields first so that we don't overwrite the SFields / IFields instance
553 // fields.
Mathieu Chartier52e4b432014-06-10 11:22:31 -0700554 VisitInstanceFieldsReferences<kVisitClass>(klass, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700555 if (!IsTemp()) {
556 // Temp classes don't ever populate imt/vtable or static fields and they are not even
557 // allocated with the right size for those.
558 VisitStaticFieldsReferences<kVisitClass>(this, visitor);
559 if (ShouldHaveEmbeddedImtAndVTable()) {
560 VisitEmbeddedImtAndVTable(visitor);
561 }
562 }
563}
564
565template<typename Visitor>
566inline void Class::VisitEmbeddedImtAndVTable(const Visitor& visitor) {
567 uint32_t pos = sizeof(mirror::Class);
568
569 size_t count = kImtSize;
570 for (size_t i = 0; i < count; ++i) {
571 MemberOffset offset = MemberOffset(pos);
572 visitor(this, offset, true);
573 pos += sizeof(ImTableEntry);
574 }
575
576 count = ((GetVTable() != NULL) ? GetVTable()->GetLength() : 0);
577 for (size_t i = 0; i < count; ++i) {
578 MemberOffset offset = MemberOffset(pos);
579 visitor(this, offset, true);
580 pos += sizeof(VTableEntry);
581 }
Mathieu Chartier407f7022014-02-18 14:37:05 -0800582}
583
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700584template<ReadBarrierOption kReadBarrierOption>
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700585inline bool Class::IsArtFieldClass() const {
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700586 return this == ArtField::GetJavaLangReflectArtField<kReadBarrierOption>();
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -0700587}
588
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700589template<ReadBarrierOption kReadBarrierOption>
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700590inline bool Class::IsArtMethodClass() const {
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -0700591 return this == ArtMethod::GetJavaLangReflectArtMethod<kReadBarrierOption>();
Hiroshi Yamauchi9103c862014-04-22 13:51:07 -0700592}
593
Hiroshi Yamauchi25023c72014-05-09 11:45:53 -0700594template<VerifyObjectFlags kVerifyFlags, ReadBarrierOption kReadBarrierOption>
595inline bool Class::IsClassClass() {
596 Class* java_lang_Class = GetClass<kVerifyFlags, kReadBarrierOption>()->
597 template GetClass<kVerifyFlags, kReadBarrierOption>();
598 return this == java_lang_Class;
599}
600
Mathieu Chartierf8322842014-05-16 10:59:25 -0700601inline const DexFile& Class::GetDexFile() {
602 return *GetDexCache()->GetDexFile();
603}
604
605inline bool Class::DescriptorEquals(const char* match) {
606 if (UNLIKELY(IsArrayClass())) {
607 return match[0] == '[' && GetComponentType()->DescriptorEquals(match + 1);
608 } else if (UNLIKELY(IsPrimitive())) {
609 return strcmp(Primitive::Descriptor(GetPrimitiveType()), match) == 0;
610 } else if (UNLIKELY(IsProxyClass())) {
611 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
612 } else {
613 const DexFile& dex_file = GetDexFile();
614 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
615 return strcmp(dex_file.GetTypeDescriptor(type_id), match) == 0;
616 }
617}
618
Sebastien Hertz4e99b3d2014-06-24 14:35:40 +0200619inline void Class::AssertInitializedOrInitializingInThread(Thread* self) {
620 if (kIsDebugBuild && !IsInitialized()) {
621 CHECK(IsInitializing()) << PrettyClass(this) << " is not initializing: " << GetStatus();
622 CHECK_EQ(GetClinitThreadId(), self->GetTid()) << PrettyClass(this)
623 << " is initializing in a different thread";
624 }
625}
626
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700627inline ObjectArray<Class>* Class::GetInterfaces() {
628 CHECK(IsProxyClass());
629 // First static field.
630 DCHECK(GetSFields()->Get(0)->IsArtField());
631 DCHECK_STREQ(GetSFields()->Get(0)->GetName(), "interfaces");
632 MemberOffset field_offset = GetSFields()->Get(0)->GetOffset();
633 return GetFieldObject<ObjectArray<Class>>(field_offset);
634}
635
636inline ObjectArray<ObjectArray<Class>>* Class::GetThrows() {
637 CHECK(IsProxyClass());
638 // Second static field.
639 DCHECK(GetSFields()->Get(1)->IsArtField());
640 DCHECK_STREQ(GetSFields()->Get(1)->GetName(), "throws");
641 MemberOffset field_offset = GetSFields()->Get(1)->GetOffset();
642 return GetFieldObject<ObjectArray<ObjectArray<Class>>>(field_offset);
643}
644
645inline void Class::InitializeClassVisitor::operator()(
646 mirror::Object* obj, size_t usable_size) const {
647 DCHECK_LE(class_size_, usable_size);
648 // Avoid AsClass as object is not yet in live bitmap or allocation stack.
649 mirror::Class* klass = down_cast<mirror::Class*>(obj);
650 // DCHECK(klass->IsClass());
651 klass->SetClassSize(class_size_);
652 klass->SetPrimitiveType(Primitive::kPrimNot); // Default to not being primitive.
653 klass->SetDexClassDefIndex(DexFile::kDexNoIndex16); // Default to no valid class def index.
654 klass->SetDexTypeIndex(DexFile::kDexNoIndex16); // Default to no valid type index.
655}
656
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657} // namespace mirror
658} // namespace art
659
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700660#endif // ART_RUNTIME_MIRROR_CLASS_INL_H_