blob: 62740bed2e5fda78ba9f29945a8956cf503d8971 [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
17#ifndef ART_SRC_MIRROR_CLASS_INL_H_
18#define ART_SRC_MIRROR_CLASS_INL_H_
19
20#include "class.h"
21
22#include "abstract_method.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "class_loader.h"
24#include "dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "field.h"
26#include "iftable.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080027#include "object_array-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "runtime.h"
29#include "string.h"
30
31namespace art {
32namespace mirror {
33
34inline size_t Class::GetObjectSize() const {
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035 DCHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
37 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070038 DCHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039 return result;
40}
41
42inline Class* Class::GetSuperClass() const {
43 // Can only get super class for loaded classes (hack for when runtime is
44 // initializing)
45 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
46 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
47}
48
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070049inline ClassLoader* Class::GetClassLoader() const {
50 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
51}
52
53inline DexCache* Class::GetDexCache() const {
54 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
55}
56
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057inline ObjectArray<AbstractMethod>* Class::GetDirectMethods() const {
58 DCHECK(IsLoaded() || IsErroneous());
59 return GetFieldObject<ObjectArray<AbstractMethod>*>(
60 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
61}
62
63inline void Class::SetDirectMethods(ObjectArray<AbstractMethod>* new_direct_methods)
64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
65 DCHECK(NULL == GetFieldObject<ObjectArray<AbstractMethod>*>(
66 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
67 DCHECK_NE(0, new_direct_methods->GetLength());
68 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
69 new_direct_methods, false);
70}
71
72inline AbstractMethod* Class::GetDirectMethod(int32_t i) const
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 return GetDirectMethods()->Get(i);
75}
76
77inline void Class::SetDirectMethod(uint32_t i, AbstractMethod* f) // TODO: uint16_t
78 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
79 ObjectArray<AbstractMethod>* direct_methods =
80 GetFieldObject<ObjectArray<AbstractMethod>*>(
81 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
82 direct_methods->Set(i, f);
83}
84
85// Returns the number of static, private, and constructor methods.
86inline size_t Class::NumDirectMethods() const {
87 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
88}
89
90inline ObjectArray<AbstractMethod>* Class::GetVirtualMethods() const {
91 DCHECK(IsLoaded() || IsErroneous());
92 return GetFieldObject<ObjectArray<AbstractMethod>*>(
93 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
94}
95
96inline void Class::SetVirtualMethods(ObjectArray<AbstractMethod>* new_virtual_methods) {
97 // TODO: we reassign virtual methods to grow the table for miranda
98 // methods.. they should really just be assigned once
99 DCHECK_NE(0, new_virtual_methods->GetLength());
100 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
101 new_virtual_methods, false);
102}
103
104inline size_t Class::NumVirtualMethods() const {
105 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
106}
107
108inline AbstractMethod* Class::GetVirtualMethod(uint32_t i) const
109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
110 DCHECK(IsResolved() || IsErroneous());
111 return GetVirtualMethods()->Get(i);
112}
113
114inline AbstractMethod* Class::GetVirtualMethodDuringLinking(uint32_t i) const
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
116 DCHECK(IsLoaded() || IsErroneous());
117 return GetVirtualMethods()->Get(i);
118}
119
120inline void Class::SetVirtualMethod(uint32_t i, AbstractMethod* f) // TODO: uint16_t
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
122 ObjectArray<AbstractMethod>* virtual_methods =
123 GetFieldObject<ObjectArray<AbstractMethod>*>(
124 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
125 virtual_methods->Set(i, f);
126}
127
128inline ObjectArray<AbstractMethod>* Class::GetVTable() const {
129 DCHECK(IsResolved() || IsErroneous());
130 return GetFieldObject<ObjectArray<AbstractMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
131}
132
133inline ObjectArray<AbstractMethod>* Class::GetVTableDuringLinking() const {
134 DCHECK(IsLoaded() || IsErroneous());
135 return GetFieldObject<ObjectArray<AbstractMethod>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
136}
137
138inline void Class::SetVTable(ObjectArray<AbstractMethod>* new_vtable)
139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
140 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
141}
142
Ian Rogers693ff612013-02-01 10:56:12 -0800143inline bool Class::Implements(const Class* klass) const {
144 DCHECK(klass != NULL);
145 DCHECK(klass->IsInterface()) << PrettyClass(this);
146 // All interfaces implemented directly and by our superclass, and
147 // recursively all super-interfaces of those interfaces, are listed
148 // in iftable_, so we can just do a linear scan through that.
149 int32_t iftable_count = GetIfTableCount();
150 IfTable* iftable = GetIfTable();
151 for (int32_t i = 0; i < iftable_count; i++) {
152 if (iftable->GetInterface(i) == klass) {
153 return true;
154 }
155 }
156 return false;
157}
158
159// Determine whether "this" is assignable from "src", where both of these
160// are array classes.
161//
162// Consider an array class, e.g. Y[][], where Y is a subclass of X.
163// Y[][] = Y[][] --> true (identity)
164// X[][] = Y[][] --> true (element superclass)
165// Y = Y[][] --> false
166// Y[] = Y[][] --> false
167// Object = Y[][] --> true (everything is an object)
168// Object[] = Y[][] --> true
169// Object[][] = Y[][] --> true
170// Object[][][] = Y[][] --> false (too many []s)
171// Serializable = Y[][] --> true (all arrays are Serializable)
172// Serializable[] = Y[][] --> true
173// Serializable[][] = Y[][] --> false (unless Y is Serializable)
174//
175// Don't forget about primitive types.
176// Object[] = int[] --> false
177//
178inline bool Class::IsArrayAssignableFromArray(const Class* src) const {
179 DCHECK(IsArrayClass()) << PrettyClass(this);
180 DCHECK(src->IsArrayClass()) << PrettyClass(src);
181 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
182}
183
184inline bool Class::IsAssignableFromArray(const Class* src) const {
185 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
186 DCHECK(src->IsArrayClass()) << PrettyClass(src);
187 if (!IsArrayClass()) {
188 // If "this" is not also an array, it must be Object.
189 // src's super should be java_lang_Object, since it is an array.
190 Class* java_lang_Object = src->GetSuperClass();
191 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
192 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
193 return this == java_lang_Object;
194 }
195 return IsArrayAssignableFromArray(src);
196}
197
198inline bool Class::IsSubClass(const Class* klass) const {
199 DCHECK(!IsInterface()) << PrettyClass(this);
200 DCHECK(!IsArrayClass()) << PrettyClass(this);
201 const Class* current = this;
202 do {
203 if (current == klass) {
204 return true;
205 }
206 current = current->GetSuperClass();
207 } while (current != NULL);
208 return false;
209}
210
211inline AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) const {
212 Class* declaring_class = method->GetDeclaringClass();
213 DCHECK(declaring_class != NULL) << PrettyClass(this);
214 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
215 // TODO cache to improve lookup speed
216 int32_t iftable_count = GetIfTableCount();
217 IfTable* iftable = GetIfTable();
218 for (int32_t i = 0; i < iftable_count; i++) {
219 if (iftable->GetInterface(i) == declaring_class) {
220 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
221 }
222 }
223 return NULL;
224}
225
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226inline AbstractMethod* Class::FindVirtualMethodForVirtual(AbstractMethod* method) const
227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700228 DCHECK(!method->GetDeclaringClass()->IsInterface() || method->IsMiranda());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 // The argument method may from a super class.
230 // Use the index to a potentially overridden one for this instance's class.
231 return GetVTable()->Get(method->GetMethodIndex());
232}
233
234inline AbstractMethod* Class::FindVirtualMethodForSuper(AbstractMethod* method) const
235 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
236 DCHECK(!method->GetDeclaringClass()->IsInterface());
237 return GetSuperClass()->GetVTable()->Get(method->GetMethodIndex());
238}
239
240inline AbstractMethod* Class::FindVirtualMethodForVirtualOrInterface(AbstractMethod* method) const {
241 if (method->IsDirect()) {
242 return method;
243 }
244 if (method->GetDeclaringClass()->IsInterface()) {
245 return FindVirtualMethodForInterface(method);
246 }
247 return FindVirtualMethodForVirtual(method);
248}
249
250inline IfTable* Class::GetIfTable() const {
251 return GetFieldObject<IfTable*>(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
252}
253
254inline int32_t Class::GetIfTableCount() const {
255 IfTable* iftable = GetIfTable();
256 if (iftable == NULL) {
257 return 0;
258 }
259 return iftable->Count();
260}
261
262inline void Class::SetIfTable(IfTable* new_iftable) {
263 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
264}
265
266inline ObjectArray<Field>* Class::GetIFields() const {
267 DCHECK(IsLoaded() || IsErroneous());
268 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
269}
270
271inline void Class::SetIFields(ObjectArray<Field>* new_ifields)
272 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
273 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
274 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
275 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
276}
277
278inline ObjectArray<Field>* Class::GetSFields() const {
279 DCHECK(IsLoaded() || IsErroneous());
280 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
281}
282
283inline void Class::SetSFields(ObjectArray<Field>* new_sfields)
284 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
285 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
286 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
287 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
288}
289
290inline size_t Class::NumStaticFields() const {
291 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
292}
293
294inline Field* Class::GetStaticField(uint32_t i) const // TODO: uint16_t
295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
296 return GetSFields()->Get(i);
297}
298
299inline void Class::SetStaticField(uint32_t i, Field* f) // TODO: uint16_t
300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
301 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
302 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
303 sfields->Set(i, f);
304}
305
306inline size_t Class::NumInstanceFields() const {
307 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
308}
309
310inline Field* Class::GetInstanceField(uint32_t i) const // TODO: uint16_t
311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
312 DCHECK_NE(NumInstanceFields(), 0U);
313 return GetIFields()->Get(i);
314}
315
316inline void Class::SetInstanceField(uint32_t i, Field* f) // TODO: uint16_t
317 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
318 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
319 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
320 ifields->Set(i, f);
321}
322
323inline void Class::SetVerifyErrorClass(Class* klass) {
324 CHECK(klass != NULL) << PrettyClass(this);
325 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
326}
327
328inline uint32_t Class::GetAccessFlags() const {
329 // Check class is loaded or this is java.lang.String that has a
330 // circularity issue during loading the names of its members
331 DCHECK(IsLoaded() || IsErroneous() ||
332 this == String::GetJavaLangString() ||
333 this == Field::GetJavaLangReflectField() ||
334 this == AbstractMethod::GetConstructorClass() ||
335 this == AbstractMethod::GetMethodClass());
336 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
337}
338
339inline String* Class::GetName() const {
340 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
341}
342inline void Class::SetName(String* name) {
343 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
344}
345
346} // namespace mirror
347} // namespace art
348
349#endif // ART_SRC_MIRROR_CLASS_INL_H_