blob: 205d7c08f013e7622d6650f60b9d3c71587fded8 [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
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 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
63 uint8_t z;
64 int8_t b;
65 uint16_t c;
66 int16_t s;
67 int32_t i;
68 int64_t j;
69 float f;
70 double d;
71 Object* l;
72};
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
75static const uint32_t kAccPrivate = 0x0002; // field, method, ic
76static const uint32_t kAccProtected = 0x0004; // field, method, ic
77static const uint32_t kAccStatic = 0x0008; // field, method, ic
78static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
79static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
80static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
81static const uint32_t kAccVolatile = 0x0040; // field
82static const uint32_t kAccBridge = 0x0040; // method (1.5)
83static const uint32_t kAccTransient = 0x0080; // field
84static const uint32_t kAccVarargs = 0x0080; // method (1.5)
85static const uint32_t kAccNative = 0x0100; // method
86static const uint32_t kAccInterface = 0x0200; // class, ic
87static const uint32_t kAccAbstract = 0x0400; // class, method, ic
88static const uint32_t kAccStrict = 0x0800; // method
89static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
90static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
91static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093static const uint32_t kAccMiranda = 0x8000; // method
94
Brian Carlstroma331b3c2011-07-18 17:47:56 -070095static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
96
Brian Carlstrombe977852011-07-19 14:54:54 -070097static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
98static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070099static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700100
Brian Carlstrom1f870082011-08-23 16:02:11 -0700101static const uint32_t kAccClassFlagsMask = (kAccPublic
102 | kAccFinal
103 | kAccInterface
104 | kAccAbstract
105 | kAccSynthetic
106 | kAccAnnotation
107 | kAccEnum);
108static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
109 | kAccPrivate
110 | kAccProtected
111 | kAccStatic);
112static const uint32_t kAccFieldFlagsMask = (kAccPublic
113 | kAccPrivate
114 | kAccProtected
115 | kAccStatic
116 | kAccFinal
117 | kAccVolatile
118 | kAccTransient
119 | kAccSynthetic
120 | kAccEnum);
121static const uint32_t kAccMethodFlagsMask = (kAccPublic
122 | kAccPrivate
123 | kAccProtected
124 | kAccStatic
125 | kAccFinal
126 | kAccSynchronized
127 | kAccBridge
128 | kAccVarargs
129 | kAccNative
130 | kAccAbstract
131 | kAccStrict
132 | kAccSynthetic
133 | kAccConstructor
134 | kAccDeclaredSynchronized);
135
136// if only kAccClassIsReference is set, we have a soft reference
137static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
138static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
139static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
140static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
141
142static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
143 | kAccClassIsWeakReference
144 | kAccClassIsFinalizerReference
145 | kAccClassIsPhantomReference);
146
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700147/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700148 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149 */
150/*
151 * A magic value for refOffsets. Ignore the bits and walk the super
152 * chain when this is the value.
153 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
154 * fields followed by 2 ref instance fields.]
155 */
156#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
158#define CLASS_OFFSET_ALIGNMENT 4
159#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
160/*
161 * Given an offset, return the bit number which would encode that offset.
162 * Local use only.
163 */
164#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700165 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700166 CLASS_OFFSET_ALIGNMENT)
167/*
168 * Is the given offset too large to be encoded?
169 */
170#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
171 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
172/*
173 * Return a single bit, encoding the offset.
174 * Undefined if the offset is too large, as defined above.
175 */
176#define CLASS_BIT_FROM_OFFSET(byteOffset) \
177 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
178/*
179 * Return an offset, given a bit number as returned from CLZ.
180 */
181#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700182 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700183
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184#define OFFSET_OF_OBJECT_MEMBER(type, field) \
185 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700187// Classes shared with the managed side of the world need to be packed
188// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700189#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700190
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700192class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700193 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 static MemberOffset ClassOffset() {
195 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196 }
197
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700199 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 }
201
202 void SetClass(Class* new_klass);
203
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700204 bool InstanceOf(const Class* klass) const;
205
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700206 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700207
Elliott Hughes081be7f2011-09-18 16:50:26 -0700208 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700209
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 static MemberOffset MonitorOffset() {
211 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
212 }
213
Elliott Hughes5f791332011-09-15 17:45:30 -0700214 volatile int32_t* GetRawLockWordAddress() {
215 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
216 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
217 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 }
219
Elliott Hughes5f791332011-09-15 17:45:30 -0700220 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221
Elliott Hughes5f791332011-09-15 17:45:30 -0700222 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223
Ian Rogersff1ed472011-09-20 13:46:24 -0700224 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225
Elliott Hughes5f791332011-09-15 17:45:30 -0700226 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227
Elliott Hughes5f791332011-09-15 17:45:30 -0700228 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Elliott Hughes5f791332011-09-15 17:45:30 -0700230 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Elliott Hughes5f791332011-09-15 17:45:30 -0700232 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700233
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700234 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235
236 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700237 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700238 return down_cast<Class*>(this);
239 }
240
241 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700242 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700243 return down_cast<const Class*>(this);
244 }
245
Brian Carlstrom4873d462011-08-21 15:23:39 -0700246 bool IsClassClass() const;
247
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700248 bool IsObjectArray() const;
249
250 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700251 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700252
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700253 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700254 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700255
Brian Carlstromb63ec392011-08-27 17:38:27 -0700256 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700257
258 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700259 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700260 return down_cast<Array*>(this);
261 }
262
263 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700264 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700265 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700266 }
267
Brian Carlstroma663ea52011-08-19 23:33:41 -0700268 bool IsString() const;
269
270 String* AsString() {
271 DCHECK(IsString());
272 return down_cast<String*>(this);
273 }
274
275 bool IsMethod() const;
276
277 Method* AsMethod() {
278 DCHECK(IsMethod());
279 return down_cast<Method*>(this);
280 }
281
Brian Carlstrom4873d462011-08-21 15:23:39 -0700282 const Method* AsMethod() const {
283 DCHECK(IsMethod());
284 return down_cast<const Method*>(this);
285 }
286
Brian Carlstroma663ea52011-08-19 23:33:41 -0700287 bool IsField() const;
288
289 Field* AsField() {
290 DCHECK(IsField());
291 return down_cast<Field*>(this);
292 }
293
Brian Carlstrom4873d462011-08-21 15:23:39 -0700294 const Field* AsField() const {
295 DCHECK(IsField());
296 return down_cast<const Field*>(this);
297 }
298
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299 bool IsReferenceInstance() const;
300
301 bool IsWeakReferenceInstance() const;
302
303 bool IsSoftReferenceInstance() const;
304
305 bool IsFinalizerReferenceInstance() const;
306
307 bool IsPhantomReferenceInstance() const;
308
309 // Accessors for Java type fields
310 template<class T>
311 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700312 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
313 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700314 Heap::VerifyObject(result);
315 return result;
316 }
317
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700318 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700319 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700320 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
321 if (new_value != NULL) {
322 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700324 }
325
326 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
327 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700328 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
329 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700331 return android_atomic_acquire_load(word_addr);
332 } else {
333 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700334 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 }
336
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700337 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
338 if (this_is_valid) {
339 Heap::VerifyObject(this);
340 }
341 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700342 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700344 /*
345 * TODO: add an android_atomic_synchronization_store() function and
346 * use it in the 32-bit volatile set handlers. On some platforms we
347 * can use a fast atomic instruction and avoid the barriers.
348 */
349 ANDROID_MEMBAR_STORE();
350 *word_addr = new_value;
351 ANDROID_MEMBAR_FULL();
352 } else {
353 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700354 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 }
356
357 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
358 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700359 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700360 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700362 uint64_t result = QuasiAtomicRead64(addr);
363 ANDROID_MEMBAR_FULL();
364 return result;
365 } else {
366 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368 }
369
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700370 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700372 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
373 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700375 ANDROID_MEMBAR_STORE();
376 QuasiAtomicSwap64(new_value, addr);
377 // Post-store barrier not required due to use of atomic op or mutex.
378 } else {
379 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 }
382
383 protected:
384 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385 template<class T>
386 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700387 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 }
389
390 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700391 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
392 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 }
394
395 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700396 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397
Elliott Hughes5f791332011-09-15 17:45:30 -0700398 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700399
Elliott Hughes5f791332011-09-15 17:45:30 -0700400 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700401 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700402 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700403};
404
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700405// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700406class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400407 private:
408 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700409 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700410 friend struct AccessibleObjectOffsets; // for verifying offset information
411 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400412};
413
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700415class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700416 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700418
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700419 void SetDeclaringClass(Class *new_declaring_class);
420
421 const String* GetName() const;
422
423 void SetName(String* new_name);
424
425 uint32_t GetAccessFlags() const;
426
427 void SetAccessFlags(uint32_t new_access_flags) {
428 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags,
429 false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700430 }
431
Elliott Hughes80609252011-09-23 17:24:51 -0700432 bool IsPublic() const {
433 return (GetAccessFlags() & kAccPublic) != 0;
434 }
435
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700436 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700438 }
439
jeffhaobdb76512011-09-07 11:43:16 -0700440 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700441 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700442 }
443
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700444 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700445
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700446 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700447
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700448 // Gets type using type index and resolved types in the dex cache, may be null
449 // if type isn't yet resolved
450 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700451
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700452 // Performs full resolution, may return null and set exceptions if type cannot
453 // be resolved
454 Class* GetType() const;
455
456 // Offset to field within an Object
457 MemberOffset GetOffset() const;
458
buzbee34cd9e52011-09-08 14:31:52 -0700459 static MemberOffset OffsetOffset() {
460 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
461 }
462
Brian Carlstrom845490b2011-09-19 15:56:53 -0700463 static Field* FindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer);
464 static Field* FindStaticFieldFromCode(uint32_t field_idx, const Method* referrer);
465 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static);
buzbee34cd9e52011-09-08 14:31:52 -0700466
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700467 MemberOffset GetOffsetDuringLinking() const;
468
469 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700470
Brian Carlstrom4873d462011-08-21 15:23:39 -0700471 // field access, null object for static fields
472 bool GetBoolean(const Object* object) const;
473 void SetBoolean(Object* object, bool z) const;
474 int8_t GetByte(const Object* object) const;
475 void SetByte(Object* object, int8_t b) const;
476 uint16_t GetChar(const Object* object) const;
477 void SetChar(Object* object, uint16_t c) const;
478 uint16_t GetShort(const Object* object) const;
479 void SetShort(Object* object, uint16_t s) const;
480 int32_t GetInt(const Object* object) const;
481 void SetInt(Object* object, int32_t i) const;
482 int64_t GetLong(const Object* object) const;
483 void SetLong(Object* object, int64_t j) const;
484 float GetFloat(const Object* object) const;
485 void SetFloat(Object* object, float f) const;
486 double GetDouble(const Object* object) const;
487 void SetDouble(Object* object, double d) const;
488 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700489 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700490
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700491 // Slow path routines for static field access when field was unresolved at
492 // compile time
493 static uint32_t Get32StaticFromCode(uint32_t field_idx,
494 const Method* referrer);
495 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
496 uint32_t new_value);
497 static uint64_t Get64StaticFromCode(uint32_t field_idx,
498 const Method* referrer);
499 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
500 uint64_t new_value);
501 static Object* GetObjStaticFromCode(uint32_t field_idx,
502 const Method* referrer);
503 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
504 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700505
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700506 static Class* GetJavaLangReflectField() {
507 DCHECK(java_lang_reflect_Field_ != NULL);
508 return java_lang_reflect_Field_;
509 }
510
511 static void SetClass(Class* java_lang_reflect_Field);
512 static void ResetClass();
513
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700514 bool IsVolatile() const {
515 return (GetAccessFlags() & kAccVolatile) != 0;
516 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700517
Elliott Hughes80609252011-09-23 17:24:51 -0700518 void InitJavaFields();
519
buzbee1da522d2011-09-04 11:22:20 -0700520 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700521 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700522 uint32_t Get32(const Object* object) const;
523 void Set32(Object* object, uint32_t new_value) const;
524 uint64_t Get64(const Object* object) const;
525 void Set64(Object* object, uint64_t new_value) const;
526 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700527 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700528
Elliott Hughes80609252011-09-23 17:24:51 -0700529 void InitJavaFieldsLocked();
530
Jesse Wilson35baaab2011-08-10 16:18:03 -0400531 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700532
Jesse Wilson35baaab2011-08-10 16:18:03 -0400533 // The class in which this field is declared.
534 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700535
Jesse Wilson35baaab2011-08-10 16:18:03 -0400536 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537
Brian Carlstromdbc05252011-09-09 01:59:59 -0700538 const String* name_;
539
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700540 // Type of the field
Elliott Hughes80609252011-09-23 17:24:51 -0700541 mutable Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400542
Brian Carlstromdbc05252011-09-09 01:59:59 -0700543 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700544
Jesse Wilson35baaab2011-08-10 16:18:03 -0400545 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700546
547 // Offset of field within an instance or in the Class' static fields
548 uint32_t offset_;
549
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700550 // Dex cache index of resolved type
551 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400552
Brian Carlstrom693267a2011-09-06 09:25:34 -0700553 int32_t slot_;
554
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700555 static Class* java_lang_reflect_Field_;
556
Brian Carlstrom693267a2011-09-06 09:25:34 -0700557 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400558 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700559};
560
Jesse Wilson6bf19152011-09-29 13:12:33 -0400561// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700562class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700563 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700564 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700565 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700566 Object* obj,
567 Thread* thread,
568 byte* args,
569 JValue* result);
570
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700571 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700572
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700573 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700574
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700575 static MemberOffset DeclaringClassOffset() {
576 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700577 }
578
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700579 // Returns the method name, e.g. "<init>" or "eatLunch"
580 const String* GetName() const;
581
582 void SetName(String* new_name);
583
jeffhaoe23d93c2011-09-15 14:48:43 -0700584 ByteArray* GetRegisterMapData() const;
585
jeffhaoa0a764a2011-09-16 10:43:38 -0700586 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700587
588 ByteArray* GetRegisterMapHeader() const;
589
jeffhaoa0a764a2011-09-16 10:43:38 -0700590 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700591
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700592 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700593
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700594 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700595
596 const String* GetSignature() const;
597
598 void SetSignature(String* new_signature);
599
600 bool HasSameNameAndDescriptor(const Method* that) const;
601
602 uint32_t GetAccessFlags() const;
603
604 void SetAccessFlags(uint32_t new_access_flags) {
605 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags,
606 false);
607 }
608
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700609 // Returns true if the method is declared public.
610 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700611 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700612 }
613
614 // Returns true if the method is declared private.
615 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700616 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700617 }
618
619 // Returns true if the method is declared static.
620 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 }
623
Brian Carlstrom1f870082011-08-23 16:02:11 -0700624 // Returns true if the method is a constructor.
625 bool IsConstructor() const {
626 return (access_flags_ & kAccConstructor) != 0;
627 }
628
629 // Returns true if the method is static, private, or a constructor.
630 bool IsDirect() const {
631 return IsStatic() || IsPrivate() || IsConstructor();
632 }
633
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700634 // Returns true if the method is declared synchronized.
635 bool IsSynchronized() const {
636 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700637 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700638 }
639
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700640 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700641 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700642 }
643
Elliott Hughes80609252011-09-23 17:24:51 -0700644 bool IsMiranda() const {
645 return (GetAccessFlags() & kAccMiranda) != 0;
646 }
647
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700649 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700650 }
651
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700652 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700653 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700654 }
655
656 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700658 }
659
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700660 uint16_t GetMethodIndex() const;
661
662 size_t GetVtableIndex() const {
663 return GetMethodIndex();
664 }
665
666 void SetMethodIndex(uint16_t new_method_index) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700667 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700668 new_method_index, false);
669 }
670
671 static MemberOffset MethodIndexOffset() {
672 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
673 }
674
675 uint32_t GetCodeItemOffset() const {
676 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
677 }
678
679 void SetCodeItemOffset(uint32_t new_code_off) {
680 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
681 new_code_off, false);
682 }
683
684 // Number of 32bit registers that would be required to hold all the arguments
685 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700686
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700687 // Number of argument bytes required for densely packing the
688 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700689 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700690
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700691 uint16_t NumRegisters() const;
692
693 void SetNumRegisters(uint16_t new_num_registers) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700694 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700695 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700696 }
697
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700698 uint16_t NumIns() const;
699
700 void SetNumIns(uint16_t new_num_ins) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700701 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700702 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700703 }
704
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700705 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700706
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700707 void SetNumOuts(uint16_t new_num_outs) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700708 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700709 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700710 }
711
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700712 uint32_t GetProtoIdx() const;
713
714 void SetProtoIdx(uint32_t new_proto_idx) {
715 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700716 }
717
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700718 ObjectArray<String>* GetDexCacheStrings() const;
719 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
720
721 static MemberOffset DexCacheStringsOffset() {
722 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
723 }
724
buzbee2a475e72011-09-07 17:19:17 -0700725 static MemberOffset DexCacheResolvedTypesOffset() {
726 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
727 }
728
buzbee34cd9e52011-09-08 14:31:52 -0700729 static MemberOffset DexCacheResolvedFieldsOffset() {
730 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
731 }
732
buzbee1da522d2011-09-04 11:22:20 -0700733 static MemberOffset DexCacheInitializedStaticStorageOffset() {
734 return OFFSET_OF_OBJECT_MEMBER(Method,
735 dex_cache_initialized_static_storage_);
736 }
737
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700738 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
739 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
740
741 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
742 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
743
744 ObjectArray<Field>* GetDexCacheResolvedFields() const;
745 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
746
747 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
748 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
749
750 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
751 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
752
753 void SetReturnTypeIdx(uint32_t new_return_type_idx);
754
755 Class* GetReturnType() const;
756
757 bool IsReturnAReference() const;
758
759 bool IsReturnAFloat() const;
760
761 bool IsReturnADouble() const;
762
Ian Rogersb033c752011-07-20 12:22:35 -0700763 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700764 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700765 }
766
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700767 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700768
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700769 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700770
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700771 // "Args" may refer to any of the 3 levels of "Args."
772 // To avoid confusion, our code will denote which "Args" clearly:
773 // 1. UserArgs: Args that a user see.
774 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
775 // receiver.
776 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
777 // E.g., the first in Args is Method* for both static and non-static
778 // methods. And CConvArgs doesn't deal with the receiver because
779 // receiver is hardwired in an implicit register, so CConvArgs doesn't
780 // need to deal with it.
781 //
782 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700783 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700784
785 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700786 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700787 size_t NumReferenceArgs() const;
788
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700789 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700790 size_t NumLongOrDoubleArgs() const;
791
Ian Rogersb033c752011-07-20 12:22:35 -0700792 // Is the given method parameter a reference?
793 bool IsParamAReference(unsigned int param) const;
794
795 // Is the given method parameter a long or double?
796 bool IsParamALongOrDouble(unsigned int param) const;
797
Ian Rogersdf20fe02011-07-20 20:34:16 -0700798 // Size in bytes of the given parameter
799 size_t ParamSize(unsigned int param) const;
800
801 // Size in bytes of the return value
802 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700803
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700804 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
805
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700806 const ByteArray* GetCodeArray() const {
807 return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false);
808 }
809
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700810 const void* GetCode() const {
811 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700812 }
813
Brian Carlstrome24fa612011-09-29 00:53:55 -0700814 void SetCode(void* code) {
815 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
816 }
817
818 uint32_t GetOatCodeOffset() const {
819 CHECK(!Runtime::Current()->IsStarted());
820 return reinterpret_cast<uint32_t>(GetCode());
821 }
822
823 void SetOatCodeOffset(uint32_t code_offset) {
824 CHECK(!Runtime::Current()->IsStarted());
825 SetCode(reinterpret_cast<void*>(code_offset));
826 }
827
828 void SetCodeArray(ByteArray* code_array, InstructionSet instruction_set);
buzbeec143c552011-08-20 17:38:58 -0700829
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700830 static MemberOffset GetCodeOffset() {
831 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700832 }
833
Ian Rogersbdb03912011-09-14 00:55:44 -0700834 // Is the given PC within the code array?
835 bool IsWithinCode(uintptr_t pc) const;
836
837 IntArray* GetMappingTable() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700838 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
839 }
840
841 void SetMappingTable(IntArray* mapping_table) {
842 SetFieldPtr<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), mapping_table, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700843 }
844
buzbeec41e5b52011-09-23 12:46:19 -0700845 ShortArray* GetVMapTable() const {
Ian Rogersd6b1f612011-09-27 13:38:14 -0700846 return GetFieldObject<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700847 }
848
Brian Carlstrome24fa612011-09-29 00:53:55 -0700849 void SetVMapTable(ShortArray* vmap_table) {
850 SetFieldPtr<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
851 }
852
Shih-wei Liaod11af152011-08-23 16:02:11 -0700853 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 DCHECK(sizeof(size_t) == sizeof(uint32_t));
855 size_t result = GetField32(
856 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
857 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
858 return result;
859 }
860
861 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
862 DCHECK(sizeof(size_t) == sizeof(uint32_t));
863 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
864 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
865 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700866 }
867
Shih-wei Liaod11af152011-08-23 16:02:11 -0700868 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700869 DCHECK(sizeof(size_t) == sizeof(uint32_t));
870 return GetField32(
871 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700872 }
873
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700874 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
875 DCHECK(sizeof(size_t) == sizeof(uint32_t));
876 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
877 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
878 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700879 }
880
Brian Carlstrom16192862011-09-12 17:50:06 -0700881 bool IsRegistered();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700882
Brian Carlstrom16192862011-09-12 17:50:06 -0700883 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700884
Brian Carlstrom16192862011-09-12 17:50:06 -0700885 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700886
Ian Rogersb033c752011-07-20 12:22:35 -0700887 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700888 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700889 }
890
Brian Carlstrom78128a62011-09-15 17:21:19 -0700891 const void* GetNativeMethod() const {
892 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
893 }
894
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700895 ByteArray* GetInvokeStubArray() const {
896 ByteArray* result = GetFieldPtr<ByteArray*>(
897 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false);
898 // TODO: DCHECK(result != NULL); should be ahead of time compiled
899 return result;
900 }
901
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700902 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700903 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700904 InvokeStub* result = GetFieldPtr<InvokeStub*>(
905 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
906 // TODO: DCHECK(result != NULL); should be ahead of time compiled
907 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700908 }
909
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700910 static MemberOffset GetInvokeStubOffset() {
911 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700912 }
913
buzbee561227c2011-09-02 15:28:19 -0700914 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
915 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
916 }
917
918 static MemberOffset GetDexCacheResolvedMethodsOffset() {
919 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
920 }
921
922 static MemberOffset GetMethodIndexOffset() {
923 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
924 }
925
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700926 void SetInvokeStub(const ByteArray* invoke_stub_array);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700927
Ian Rogers90865722011-09-19 11:11:44 -0700928 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700929 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700930 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700931
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700932 void SetCoreSpillMask(uint32_t core_spill_mask) {
933 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700934 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700935 }
936
Ian Rogers90865722011-09-19 11:11:44 -0700937 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700938 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
939 }
940
941 void SetFpSpillMask(uint32_t fp_spill_mask) {
942 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700943 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700944 }
945
Ian Rogers90865722011-09-19 11:11:44 -0700946 // Is this a hand crafted method used for something like describing callee saves?
947 bool IsPhony() const {
Ian Rogersff1ed472011-09-20 13:46:24 -0700948 bool result = this == Runtime::Current()->GetCalleeSaveMethod();
Ian Rogers90865722011-09-19 11:11:44 -0700949 // Check that if we do think it is phony it looks like the callee save method
950 DCHECK(!result || GetCoreSpillMask() != 0);
951 return result;
952 }
953
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700954 // Converts a native PC to a dex PC. TODO: this is a no-op
955 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700956 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700957
958 // Converts a dex PC to a native PC. TODO: this is a no-op
959 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700960 uintptr_t ToNativePC(const uint32_t dex_pc) const;
961
962 // Find the catch block for the given exception type and dex_pc
963 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700964
Elliott Hughes80609252011-09-23 17:24:51 -0700965 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
966
967 static Class* GetConstructorClass() {
968 return java_lang_reflect_Constructor_;
969 }
970
971 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700972 return java_lang_reflect_Method_;
973 }
974
Elliott Hughes80609252011-09-23 17:24:51 -0700975 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700976
Elliott Hughes418d20f2011-09-22 14:00:39 -0700977 void InitJavaFields();
978
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700979 private:
980 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700981 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700982
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700983 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
984 // the class we are a part of
985 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700986 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700987 Object* java_formal_type_parameters_;
988 Object* java_generic_exception_types_;
989 Object* java_generic_parameter_types_;
990 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700991
Brian Carlstrom693267a2011-09-06 09:25:34 -0700992 String* name_;
993
Elliott Hughes418d20f2011-09-22 14:00:39 -0700994 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700995 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700996 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700997
Brian Carlstrom693267a2011-09-06 09:25:34 -0700998 // Storage for code_
999 const ByteArray* code_array_;
1000
1001 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -07001002 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1003
1004 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1005 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1006
1007 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1008 ObjectArray<Field>* dex_cache_resolved_fields_;
1009
1010 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1011 ObjectArray<Method>* dex_cache_resolved_methods_;
1012
Brian Carlstromdbc05252011-09-09 01:59:59 -07001013 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1014 ObjectArray<Class>* dex_cache_resolved_types_;
1015
1016 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1017 ObjectArray<String>* dex_cache_strings_;
1018
1019 // Storage for invoke_stub_
1020 const ByteArray* invoke_stub_array_;
1021
1022 // Storage for mapping_table_
Ian Rogersbdb03912011-09-14 00:55:44 -07001023 IntArray* mapping_table_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001024
jeffhaoe23d93c2011-09-15 14:48:43 -07001025 // Byte arrays that hold data for the register maps
1026 const ByteArray* register_map_data_;
1027 const ByteArray* register_map_header_;
1028
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001029 // The short-form method descriptor string.
1030 String* shorty_;
1031
Brian Carlstromdbc05252011-09-09 01:59:59 -07001032 // The method descriptor. This represents the parameters a method
1033 // takes and value it returns. This string is a list of the type
1034 // descriptors for the parameters enclosed in parenthesis followed
1035 // by the return type descriptor. For example, for the method
1036 //
1037 // Object mymethod(int i, double d, Thread t)
1038 //
1039 // the method descriptor would be
1040 //
1041 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1042 String* signature_;
1043
buzbeec41e5b52011-09-23 12:46:19 -07001044 // Storage for Dalvik virtual register mapping_table_
1045 ShortArray* vmap_table_;
1046
Brian Carlstromdbc05252011-09-09 01:59:59 -07001047 uint32_t java_generic_types_are_initialized_;
1048
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001049 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001050 uint32_t access_flags_;
1051
1052 // Compiled code associated with this method for callers from managed code.
1053 // May be compiled managed code or a bridge for invoking a native method.
1054 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001055
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001056 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001057 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001058
Brian Carlstrom693267a2011-09-06 09:25:34 -07001059 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001060 uint32_t core_spill_mask_;
1061
1062 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001063 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001064
Brian Carlstrom693267a2011-09-06 09:25:34 -07001065 // Total size in bytes of the frame
1066 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001067
Brian Carlstrom693267a2011-09-06 09:25:34 -07001068 // Native invocation stub entry point for calling from native to managed code.
1069 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001070
Brian Carlstrom693267a2011-09-06 09:25:34 -07001071 // Index of the return type
1072 uint32_t java_return_type_idx_;
1073
Brian Carlstrom693267a2011-09-06 09:25:34 -07001074 // For concrete virtual methods, this is the offset of the method
1075 // in Class::vtable_.
1076 //
1077 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001078 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001079 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001080
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001081 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001082 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001083
Brian Carlstrom693267a2011-09-06 09:25:34 -07001084 // Method bounds; not needed for an abstract method.
1085 //
1086 // For a native method, we compute the size of the argument list, and
1087 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001088 uint32_t num_ins_;
1089 uint32_t num_outs_;
1090 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001091
Brian Carlstrom693267a2011-09-06 09:25:34 -07001092 // Method prototype descriptor string (return and argument types).
1093 uint32_t proto_idx_;
1094
1095 // Offset of return PC within frame for compiled code (in bytes)
1096 size_t return_pc_offset_in_bytes_;
1097
Brian Carlstrom693267a2011-09-06 09:25:34 -07001098 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001099
Elliott Hughes80609252011-09-23 17:24:51 -07001100 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001101 static Class* java_lang_reflect_Method_;
1102
Brian Carlstrom693267a2011-09-06 09:25:34 -07001103 friend class ImageWriter; // for relocating code_ and invoke_stub_
1104 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001105 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001106};
1107
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001108class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001109 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001110 // A convenience for code that doesn't know the component size,
1111 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001112 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001113
Brian Carlstromb63ec392011-08-27 17:38:27 -07001114 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001115
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001116 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001117
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001118 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001119 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001120 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001121
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001123 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001124 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001125 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001126
buzbeec143c552011-08-20 17:38:58 -07001127 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001128 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001129 }
1130
1131 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001133 }
1134
Elliott Hughesbf86d042011-08-31 17:53:14 -07001135 void* GetRawData() {
1136 return reinterpret_cast<void*>(first_element_);
1137 }
1138
Elliott Hughes289da822011-08-16 10:11:20 -07001139 protected:
1140 bool IsValidIndex(int32_t index) const {
1141 if (index < 0 || index >= length_) {
Elliott Hughes80609252011-09-23 17:24:51 -07001142 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001143 }
1144 return true;
1145 }
1146
Elliott Hughes80609252011-09-23 17:24:51 -07001147 protected:
1148 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1149 bool ThrowArrayStoreException(Object* object) const;
1150
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001151 private:
1152 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001153 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001154 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1155 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001156 // Marker for the data (used by generated code)
1157 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001158
Carl Shapirof88c9522011-08-06 15:47:38 -07001159 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001160};
1161
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001162template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001163class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001164 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001165 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001166
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001167 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001168
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001169 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001170
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001171 // Set element without bound and element type checks, to be used in limited
1172 // circumstances, such as during boot image writing
1173 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001174
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001175 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001176 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001177 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001178
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001179 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001180
1181 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001182 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001183};
1184
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001185template<class T>
1186ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1187 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1188}
1189
1190template<class T>
1191T* ObjectArray<T>::Get(int32_t i) const {
1192 if (!IsValidIndex(i)) {
1193 return NULL;
1194 }
1195 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1196 return GetFieldObject<T*>(data_offset, false);
1197}
1198
1199template<class T>
1200ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1201 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1202 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1203 return new_array;
1204}
1205
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001206// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001207// provides the static storage. However, this might change to an Array
1208// to improve image sharing, so we use this type to avoid assumptions
1209// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001210class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001211
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001212// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001213class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001214 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001215
1216 // Class Status
1217 //
1218 // kStatusNotReady: If a Class cannot be found in the class table by
1219 // FindClass, it allocates an new one with AllocClass in the
1220 // kStatusNotReady and calls LoadClass. Note if it does find a
1221 // class, it may not be kStatusResolved and it will try to push it
1222 // forward toward kStatusResolved.
1223 //
1224 // kStatusIdx: LoadClass populates with Class with information from
1225 // the DexFile, moving the status to kStatusIdx, indicating that the
1226 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001227 // populated based on super_class_type_idx_ and
1228 // interfaces_type_idx_. The new Class can then be inserted into the
1229 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001230 //
1231 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1232 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1233 // using ResolveClass to initialize the super_class_ and interfaces_.
1234 //
1235 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001236 // shows linking is complete and fields of the Class populated by making
1237 // it kStatusResolved. Java allows circularities of the form where a super
1238 // class has a field that is of the type of the sub class. We need to be able
1239 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001240
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001241 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001242 kStatusError = -1,
1243 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001244 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001245 kStatusLoaded = 2, // DEX idx values resolved
1246 kStatusResolved = 3, // part of linking
1247 kStatusVerifying = 4, // in the process of being verified
1248 kStatusVerified = 5, // logically part of linking; done pre-init
1249 kStatusInitializing = 6, // class init in progress
1250 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001251 };
1252
1253 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001254 kPrimNot = 0,
1255 kPrimBoolean,
1256 kPrimByte,
1257 kPrimChar,
1258 kPrimShort,
1259 kPrimInt,
1260 kPrimLong,
1261 kPrimFloat,
1262 kPrimDouble,
1263 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001264 };
1265
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001266 Status GetStatus() const {
1267 CHECK(sizeof(Status) == sizeof(uint32_t));
1268 return static_cast<Status>(
1269 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1270 }
1271
1272 void SetStatus(Status new_status);
1273
1274 // Returns true if the class has failed to link.
1275 bool IsErroneous() const {
1276 return GetStatus() == kStatusError;
1277 }
1278
1279 // Returns true if the class has been loaded.
1280 bool IsIdxLoaded() const {
1281 return GetStatus() >= kStatusIdx;
1282 }
1283
1284 // Returns true if the class has been loaded.
1285 bool IsLoaded() const {
1286 return GetStatus() >= kStatusLoaded;
1287 }
1288
1289 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001290 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001291 return GetStatus() >= kStatusResolved;
1292 }
1293
1294 // Returns true if the class has been verified.
1295 bool IsVerified() const {
1296 return GetStatus() >= kStatusVerified;
1297 }
1298
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001299 // Returns true if the class is initializing.
1300 bool IsInitializing() const {
1301 return GetStatus() >= kStatusInitializing;
1302 }
1303
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001304 // Returns true if the class is initialized.
1305 bool IsInitialized() const {
1306 return GetStatus() == kStatusInitialized;
1307 }
1308
1309 uint32_t GetAccessFlags() const;
1310
1311 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001312 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001313 }
1314
1315 // Returns true if the class is an interface.
1316 bool IsInterface() const {
1317 return (GetAccessFlags() & kAccInterface) != 0;
1318 }
1319
1320 // Returns true if the class is declared public.
1321 bool IsPublic() const {
1322 return (GetAccessFlags() & kAccPublic) != 0;
1323 }
1324
1325 // Returns true if the class is declared final.
1326 bool IsFinal() const {
1327 return (GetAccessFlags() & kAccFinal) != 0;
1328 }
1329
1330 // Returns true if the class is abstract.
1331 bool IsAbstract() const {
1332 return (GetAccessFlags() & kAccAbstract) != 0;
1333 }
1334
1335 // Returns true if the class is an annotation.
1336 bool IsAnnotation() const {
1337 return (GetAccessFlags() & kAccAnnotation) != 0;
1338 }
1339
1340 // Returns true if the class is synthetic.
1341 bool IsSynthetic() const {
1342 return (GetAccessFlags() & kAccSynthetic) != 0;
1343 }
1344
1345 bool IsReferenceClass() const {
1346 return (GetAccessFlags() & kAccClassIsReference) != 0;
1347 }
1348
1349 bool IsWeakReferenceClass() const {
1350 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1351 }
1352
1353 bool IsSoftReferenceClass() const {
1354 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1355 }
1356
1357 bool IsFinalizerReferenceClass() const {
1358 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1359 }
1360
1361 bool IsPhantomReferenceClass() const {
1362 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1363 }
1364
1365 PrimitiveType GetPrimitiveType() const {
1366 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1367 return static_cast<PrimitiveType>(
1368 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1369 }
1370
1371 void SetPrimitiveType(PrimitiveType new_type) {
1372 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001373 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 }
1375
1376 // Returns true if the class is a primitive type.
1377 bool IsPrimitive() const {
1378 return GetPrimitiveType() != kPrimNot;
1379 }
1380
1381 bool IsPrimitiveBoolean() const {
1382 return GetPrimitiveType() == kPrimBoolean;
1383 }
1384
1385 bool IsPrimitiveByte() const {
1386 return GetPrimitiveType() == kPrimByte;
1387 }
1388
1389 bool IsPrimitiveChar() const {
1390 return GetPrimitiveType() == kPrimChar;
1391 }
1392
1393 bool IsPrimitiveShort() const {
1394 return GetPrimitiveType() == kPrimShort;
1395 }
1396
1397 bool IsPrimitiveInt() const {
1398 return GetPrimitiveType() == kPrimInt;
1399 }
1400
1401 bool IsPrimitiveLong() const {
1402 return GetPrimitiveType() == kPrimLong;
1403 }
1404
1405 bool IsPrimitiveFloat() const {
1406 return GetPrimitiveType() == kPrimFloat;
1407 }
1408
1409 bool IsPrimitiveDouble() const {
1410 return GetPrimitiveType() == kPrimDouble;
1411 }
1412
1413 bool IsPrimitiveVoid() const {
1414 return GetPrimitiveType() == kPrimVoid;
1415 }
1416
1417 size_t PrimitiveSize() const;
1418
1419 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001420 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001421 }
1422
1423 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001424 return GetFieldObject<Class*>(
1425 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1426 }
1427
1428 void SetComponentType(Class* new_component_type) {
1429 DCHECK(GetComponentType() == NULL);
1430 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001431 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001432 }
1433
1434 size_t GetComponentSize() const {
1435 return GetTypeSize(GetComponentType()->GetDescriptor());
1436 }
1437
1438 bool IsObjectClass() const {
1439 return !IsPrimitive() && GetSuperClass() == NULL;
1440 }
1441
Brian Carlstrom1f870082011-08-23 16:02:11 -07001442 // Creates a raw object instance but does not invoke the default constructor.
1443 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001444
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001445 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001446
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001447 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001448 const String* result = GetFieldObject<const String*>(
1449 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1450 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1451 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1452 return result;
1453 }
1454
1455 void SetDescriptor(String* new_descriptor);
1456
1457 bool IsVariableSize() const {
1458 // Classes and arrays vary in size, and so the object_size_ field cannot
1459 // be used to get their instance size
1460 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001461 }
1462
Brian Carlstrom4873d462011-08-21 15:23:39 -07001463 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001464 CHECK(sizeof(size_t) == sizeof(int32_t));
1465 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001466 }
1467
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001468 size_t GetClassSize() const {
1469 CHECK(sizeof(size_t) == sizeof(uint32_t));
1470 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471 }
1472
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001473 void SetClassSize(size_t new_class_size) {
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001474 DCHECK(new_class_size >= GetClassSize())
Elliott Hughes54e7df12011-09-16 11:47:04 -07001475 << " class=" << PrettyTypeOf(this)
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001476 << " new_class_size=" << new_class_size
1477 << " GetClassSize=" << GetClassSize();
Elliott Hughes54e7df12011-09-16 11:47:04 -07001478 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001479 }
1480
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001481 size_t GetObjectSize() const {
1482 CHECK(!IsVariableSize());
1483 CHECK(sizeof(size_t) == sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001484 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001485 CHECK_GE(result, sizeof(Object));
1486 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001487 }
1488
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001489 void SetObjectSize(size_t new_object_size) {
1490 DCHECK(!IsVariableSize());
1491 CHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001492 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001493 }
1494
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001495 // Returns true if this class is in the same packages as that class.
1496 bool IsInSamePackage(const Class* that) const;
1497
Brian Carlstrome24fa612011-09-29 00:53:55 -07001498 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001499
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001500 // Returns true if this class can access that class.
1501 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001502 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001503 }
1504
jeffhao4a801a42011-09-23 13:53:40 -07001505 // Validate method/field access.
1506 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1507 // quick accept for public access
1508 if (member_flags & kAccPublic) {
1509 return true;
1510 }
1511
1512 // quick accept for access from same class
1513 if (this == access_to) {
1514 return true;
1515 }
1516
1517 // quick reject for private access from another class
1518 if (member_flags & kAccPrivate) {
1519 return false;
1520 }
1521
1522 // Semi-quick test for protected access from a sub-class, which may or
1523 // may not be in the same package.
1524 if (member_flags & kAccProtected) {
1525 if (this->IsSubClass(access_to)) {
1526 return true;
1527 }
1528 }
1529
1530 // Allow protected and private access from other classes in the same package.
1531 return this->IsInSamePackage(access_to);
1532 }
1533
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001534 bool IsSubClass(const Class* klass) const;
1535
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001536 bool IsAssignableFrom(const Class* src) const {
1537 DCHECK(src != NULL);
1538 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001539 // Can always assign to things of the same type
1540 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001541 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001542 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001543 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001544 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001545 return src->Implements(this);
1546 } else if (src->IsArrayClass()) {
1547 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001548 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001549 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001550 }
1551 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001552
buzbee991e3ac2011-09-29 15:44:22 -07001553 // Assignable test for code, won't throw. Null and equality tests already performed
1554 static uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class)
1555 {
1556 DCHECK(klass != NULL);
1557 DCHECK(ref_class != NULL);
1558 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1559 }
1560
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001561 Class* GetSuperClass() const {
1562 // Can only get super class for loaded classes (hack for when runtime is
1563 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001564 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001565 return GetFieldObject<Class*>(
1566 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1567 }
1568
buzbee4a3164f2011-09-03 11:25:10 -07001569 static MemberOffset SuperClassOffset() {
1570 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1571 }
1572
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001573 void SetSuperClass(Class *new_super_class) {
1574 // super class is assigned once, except during class linker initialization
1575 Class* old_super_class = GetFieldObject<Class*>(
1576 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1577 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1578 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001579 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001580 }
1581
1582 bool HasSuperClass() const {
1583 return GetSuperClass() != NULL;
1584 }
1585
1586 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001587 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001588 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1589 false);
1590 }
1591
1592 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001593 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001594 }
1595
1596 const ClassLoader* GetClassLoader() const;
1597
1598 void SetClassLoader(const ClassLoader* new_cl);
1599
1600 static MemberOffset DexCacheOffset() {
1601 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1602 }
1603
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001604 enum {
1605 kDumpClassFullDetail = 1,
1606 kDumpClassClassLoader = (1 << 1),
1607 kDumpClassInitialized = (1 << 2),
1608 };
1609
Elliott Hughes4681c802011-09-25 18:04:37 -07001610 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001611
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001612 DexCache* GetDexCache() const;
1613
1614 void SetDexCache(DexCache* new_dex_cache);
1615
1616 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001617 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001618 return GetFieldObject<ObjectArray<Method>*>(
1619 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1620 }
1621
1622 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1623 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1624 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1625 DCHECK_NE(0, new_direct_methods->GetLength());
1626 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1627 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001628 }
1629
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001630 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001631 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001632 }
1633
1634 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001635 ObjectArray<Method>* direct_methods =
1636 GetFieldObject<ObjectArray<Method>*>(
1637 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1638 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001639 }
1640
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001641 // Returns the number of static, private, and constructor methods.
1642 size_t NumDirectMethods() const {
1643 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1644 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001645
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001646 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001647 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001648 return GetFieldObject<ObjectArray<Method>*>(
1649 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1650 }
1651
1652 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1653 // TODO: we reassign virtual methods to grow the table for miranda
1654 // methods.. they should really just be assigned once
1655 DCHECK_NE(0, new_virtual_methods->GetLength());
1656 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1657 new_virtual_methods, false);
1658 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001659
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001660 // Returns the number of non-inherited virtual methods.
1661 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001662 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001663 }
1664
1665 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001666 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001667 return GetVirtualMethods()->Get(i);
1668 }
1669
1670 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001671 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001672 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001673 }
1674
1675 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001676 ObjectArray<Method>* virtual_methods =
1677 GetFieldObject<ObjectArray<Method>*>(
1678 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1679 virtual_methods->Set(i, f);
1680 }
1681
1682 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001683 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001684 return GetFieldObject<ObjectArray<Method>*>(
1685 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1686 }
1687
1688 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001689 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001690 return GetFieldObject<ObjectArray<Method>*>(
1691 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1692 }
1693
1694 void SetVTable(ObjectArray<Method>* new_vtable) {
1695 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1696 }
1697
1698 static MemberOffset VTableOffset() {
1699 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001700 }
1701
Brian Carlstrom30b94452011-08-25 21:35:26 -07001702 // Given a method implemented by this class but potentially from a
1703 // super class, return the specific implementation
1704 // method for this class.
1705 Method* FindVirtualMethodForVirtual(Method* method) {
1706 DCHECK(!method->GetDeclaringClass()->IsInterface());
1707 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001708 // Use the index to a potentially overridden one for this instance's class.
1709 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001710 }
1711
1712 // Given a method implemented by this class, but potentially from a
1713 // super class or interface, return the specific implementation
1714 // method for this class.
1715 Method* FindVirtualMethodForInterface(Method* method);
1716
jeffhaobdb76512011-09-07 11:43:16 -07001717 Method* FindInterfaceMethod(const StringPiece& name,
1718 const StringPiece& descriptor);
1719
Brian Carlstrom30b94452011-08-25 21:35:26 -07001720 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001721 if (method->IsDirect()) {
1722 return method;
1723 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001724 if (method->GetDeclaringClass()->IsInterface()) {
1725 return FindVirtualMethodForInterface(method);
1726 }
1727 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001728 }
1729
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001730 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001731 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001732
1733 Method* FindVirtualMethod(const StringPiece& name,
1734 const StringPiece& descriptor);
1735
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001736 Method* FindDeclaredDirectMethod(const StringPiece& name,
1737 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001738
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001739 Method* FindDirectMethod(const StringPiece& name,
1740 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001741
Carl Shapiro69759ea2011-07-21 18:13:35 -07001742 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001743 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001744 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1745 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1746 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001747 }
1748
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001749 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001750 CHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001751 return GetFieldObject<IntArray*>(
1752 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1753 }
1754
1755 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1756
1757 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001758 CHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001759 return GetFieldObject<ObjectArray<Class>*>(
1760 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1761 }
1762
1763 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1764 DCHECK(NULL == GetFieldObject<Object*>(
1765 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001766 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001767 }
1768
1769 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001770 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001771 ObjectArray<Class>* interfaces =
1772 GetFieldObject<ObjectArray<Class>*>(
1773 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1774 interfaces->Set(i, f);
1775 }
1776
1777 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001778 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001779 return GetInterfaces()->Get(i);
1780 }
1781
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001782 int32_t GetIfTableCount() const {
1783 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1784 if (iftable == NULL) {
1785 return 0;
1786 }
1787 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001788 }
1789
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001790 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001791 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001792 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001793 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1794 }
1795
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001796 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001797 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001798 }
1799
1800 // Get instance fields
1801 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001802 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001803 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804 }
1805
1806 void SetIFields(ObjectArray<Field>* new_ifields) {
1807 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1808 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001809 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001810 }
1811
1812 size_t NumInstanceFields() const {
1813 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1814 }
1815
1816 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1817 DCHECK_NE(NumInstanceFields(), 0U);
1818 return GetIFields()->Get(i);
1819 }
1820
1821 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1822 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1823 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1824 ifields->Set(i, f);
1825 }
1826
1827 // Returns the number of instance fields containing reference types.
1828 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001829 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001830 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001831 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001832 }
1833
1834 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001835 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001836 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001837 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001838 }
1839
1840 void SetNumReferenceInstanceFields(size_t new_num) {
1841 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001842 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001843 }
1844
1845 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001846 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001847 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001848 }
1849
1850 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1851
1852 // Beginning of static field data
1853 static MemberOffset FieldsOffset() {
1854 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1855 }
1856
1857 // Returns the number of static fields containing reference types.
1858 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001859 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001860 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001861 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001862 }
1863
1864 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001865 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001866 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001867 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001868 }
1869
1870 void SetNumReferenceStaticFields(size_t new_num) {
1871 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001872 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001873 }
1874
1875 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001876 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001877 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001878 }
1879
1880 void SetSFields(ObjectArray<Field>* new_sfields) {
1881 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1882 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001883 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001884 }
1885
1886 size_t NumStaticFields() const {
1887 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1888 }
1889
1890 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1891 return GetSFields()->Get(i);
1892 }
1893
1894 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1895 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1896 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1897 sfields->Set(i, f);
1898 }
1899
1900 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001901 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001902 }
1903
1904 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1905
1906 // Finds the given instance field in this class or a superclass.
1907 Field* FindInstanceField(const StringPiece& name, Class* type);
1908
1909 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1910
1911 // Finds the given static field in this class or a superclass.
1912 Field* FindStaticField(const StringPiece& name, Class* type);
1913
1914 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1915
Elliott Hughesdcc24742011-09-07 14:02:44 -07001916 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001917 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001918 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1919 }
1920
Elliott Hughesdcc24742011-09-07 14:02:44 -07001921 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001922 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001923 }
1924
1925 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001926 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001927 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001928 }
1929
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001930 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001931 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001932 }
1933
Brian Carlstromc74255f2011-09-11 22:47:39 -07001934 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001935
Brian Carlstromc74255f2011-09-11 22:47:39 -07001936 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001937
jeffhaobdb76512011-09-07 11:43:16 -07001938 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001939 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001940 bool IsArrayAssignableFromArray(const Class* klass) const;
1941 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001942
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001943 // descriptor for the class such as "java.lang.Class" or "[C"
1944 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001945
Brian Carlstrom693267a2011-09-06 09:25:34 -07001946 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001947 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001948
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001949 // For array classes, the component class object for instanceof/checkcast
1950 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001951 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001952
Brian Carlstrom693267a2011-09-06 09:25:34 -07001953 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1954 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001955
Brian Carlstrom693267a2011-09-06 09:25:34 -07001956 // DexCache of resolved constant pool entries
1957 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1958 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001959
1960 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001961 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001962
Brian Carlstrom693267a2011-09-06 09:25:34 -07001963 // instance fields
1964 //
1965 // These describe the layout of the contents of an Object.
1966 // Note that only the fields directly declared by this class are
1967 // listed in ifields; fields declared by a superclass are listed in
1968 // the superclass's Class.ifields.
1969 //
1970 // All instance fields that refer to objects are guaranteed to be at
1971 // the beginning of the field list. num_reference_instance_fields_
1972 // specifies the number of reference fields.
1973 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001974
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001975 // Interface table (iftable_), one entry per interface supported by
1976 // this class. That means one entry for each interface we support
1977 // directly, indirectly via superclass, or indirectly via
1978 // superinterface. This will be null if neither we nor our
1979 // superclass implement any interfaces.
1980 //
1981 // Why we need this: given "class Foo implements Face", declare
1982 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1983 // is part of the Face interface. We can't easily use a single
1984 // vtable.
1985 //
1986 // For every interface a concrete class implements, we create an array
1987 // of the concrete vtable_ methods for the methods in the interface.
1988 ObjectArray<InterfaceEntry>* iftable_;
1989
Brian Carlstromdbc05252011-09-09 01:59:59 -07001990 // array of interfaces this class implements directly
1991 // see also interfaces_type_idx_
1992 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001993
1994 // array of type_idx's for interfaces this class implements directly
1995 // see also interfaces_
1996 IntArray* interfaces_type_idx_;
1997
Brian Carlstromdbc05252011-09-09 01:59:59 -07001998 // Static fields
1999 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002000
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002001 // source file name, if known. Otherwise, NULL.
2002 String* source_file_;
2003
Brian Carlstromdbc05252011-09-09 01:59:59 -07002004 // The superclass, or NULL if this is java.lang.Object or a
2005 // primitive type.
2006 // see also super_class_type_idx_;
2007 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002008
Brian Carlstromdbc05252011-09-09 01:59:59 -07002009 // If class verify fails, we must return same error on subsequent tries.
2010 // Update with SetVerifyErrorClass to ensure a write barrier is used.
2011 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002012
Brian Carlstromdbc05252011-09-09 01:59:59 -07002013 // virtual methods defined in this class; invoked through vtable
2014 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002015
Brian Carlstromdbc05252011-09-09 01:59:59 -07002016 // Virtual method table (vtable), for use by "invoke-virtual". The
2017 // vtable from the superclass is copied in, and virtual methods from
2018 // our class either replace those from the super or are appended.
2019 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002020
Brian Carlstromdbc05252011-09-09 01:59:59 -07002021 // access flags; low 16 bits are defined by VM spec
2022 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002023
Jesse Wilson6bf19152011-09-29 13:12:33 -04002024 // Total size of the Class instance; used when allocating storage on gc heap.
2025 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002026 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002027
Elliott Hughes5f791332011-09-15 17:45:30 -07002028 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002029 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002030
Brian Carlstromdbc05252011-09-09 01:59:59 -07002031 // number of instance fields that are object refs
2032 size_t num_reference_instance_fields_;
2033
2034 // number of static fields that are object refs
2035 size_t num_reference_static_fields_;
2036
2037 // Total object size; used when allocating storage on gc heap.
2038 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002039 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002040 size_t object_size_;
2041
2042 // primitive type index, or kPrimNot (0); set for generated prim classes
2043 PrimitiveType primitive_type_;
2044
2045 // Bitmap of offsets of ifields.
2046 uint32_t reference_instance_offsets_;
2047
2048 // Bitmap of offsets of sfields.
2049 uint32_t reference_static_offsets_;
2050
Brian Carlstrom693267a2011-09-06 09:25:34 -07002051 // state of class initialization
2052 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002053
Brian Carlstrom693267a2011-09-06 09:25:34 -07002054 // Set in LoadClass, used to LinkClass
2055 // see also super_class_
2056 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002057
Brian Carlstrom693267a2011-09-06 09:25:34 -07002058 // TODO: ?
2059 // initiating class loader list
2060 // NOTE: for classes with low serialNumber, these are unused, and the
2061 // values are kept in a table in gDvm.
2062 // InitiatingLoaderList initiating_loader_list_;
2063
Brian Carlstrom4873d462011-08-21 15:23:39 -07002064 // Location of first static field.
2065 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002066
Brian Carlstrom693267a2011-09-06 09:25:34 -07002067 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002068 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002069};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002070
Elliott Hughes1f359b02011-07-17 14:27:17 -07002071std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002072
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002073inline void Object::SetClass(Class* new_klass) {
2074 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002075 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002076}
2077
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002078inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002079 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002080 DCHECK(GetClass() != NULL);
2081 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002082}
2083
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002084inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002085 Class* java_lang_Class = GetClass()->GetClass();
2086 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002087}
2088
Brian Carlstrom4873d462011-08-21 15:23:39 -07002089inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002090 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002091 return this == java_lang_Class;
2092}
2093
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002094inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002095 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002096}
2097
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002098template<class T>
2099inline ObjectArray<T>* Object::AsObjectArray() {
2100 DCHECK(IsObjectArray());
2101 return down_cast<ObjectArray<T>*>(this);
2102}
2103
2104template<class T>
2105inline const ObjectArray<T>* Object::AsObjectArray() const {
2106 DCHECK(IsObjectArray());
2107 return down_cast<const ObjectArray<T>*>(this);
2108}
2109
Brian Carlstromb63ec392011-08-27 17:38:27 -07002110inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002111 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002112}
2113
Brian Carlstroma663ea52011-08-19 23:33:41 -07002114inline bool Object::IsField() const {
2115 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002116 Class* java_lang_reflect_Field =
2117 java_lang_Class->GetInstanceField(0)->GetClass();
2118 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002119}
2120
2121inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002122 Class* c = GetClass();
2123 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002124}
2125
2126inline bool Object::IsReferenceInstance() const {
2127 return GetClass()->IsReferenceClass();
2128}
2129
2130inline bool Object::IsWeakReferenceInstance() const {
2131 return GetClass()->IsWeakReferenceClass();
2132}
2133
2134inline bool Object::IsSoftReferenceInstance() const {
2135 return GetClass()->IsSoftReferenceClass();
2136}
2137
2138inline bool Object::IsFinalizerReferenceInstance() const {
2139 return GetClass()->IsFinalizerReferenceClass();
2140}
2141
2142inline bool Object::IsPhantomReferenceInstance() const {
2143 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002144}
2145
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002146inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002147 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002148 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002149 result = AsArray()->SizeOf();
2150 } else if (IsClass()) {
2151 result = AsClass()->SizeOf();
2152 } else {
2153 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002154 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002155 DCHECK(!IsField() || result == sizeof(Field));
2156 DCHECK(!IsMethod() || result == sizeof(Method));
2157 return result;
2158}
2159
2160inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002161 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002162 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002163 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2164 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002165 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002166 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2167 num_bytes.Uint32Value(), false);
2168}
2169
2170inline Class* Field::GetDeclaringClass() const {
2171 Class* result = GetFieldObject<Class*>(
2172 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2173 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002174 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002175 return result;
2176}
2177
2178inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2179 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2180 new_declaring_class, false);
2181}
2182
2183inline Class* Method::GetDeclaringClass() const {
2184 Class* result =
2185 GetFieldObject<Class*>(
2186 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2187 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002188 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002189 return result;
2190}
2191
2192inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2193 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2194 new_declaring_class, false);
2195}
2196
2197inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002198 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002199 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2200 false);
2201}
2202
2203inline bool Method::IsReturnAReference() const {
2204 return !GetReturnType()->IsPrimitive();
2205}
2206
2207inline bool Method::IsReturnAFloat() const {
2208 return GetReturnType()->IsPrimitiveFloat();
2209}
2210
2211inline bool Method::IsReturnADouble() const {
2212 return GetReturnType()->IsPrimitiveDouble();
2213}
2214
2215inline bool Method::IsReturnALong() const {
2216 return GetReturnType()->IsPrimitiveLong();
2217}
2218
2219inline bool Method::IsReturnVoid() const {
2220 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002221}
2222
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002223inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002224 // This is safe from overflow because the array was already allocated, so we know it's sane.
2225 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002226}
2227
2228template<class T>
2229void ObjectArray<T>::Set(int32_t i, T* object) {
2230 if (IsValidIndex(i)) {
2231 if (object != NULL) {
2232 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002233 if (!object->InstanceOf(element_class)) {
2234 ThrowArrayStoreException(object);
2235 return;
2236 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002237 }
2238 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2239 SetFieldObject(data_offset, object, false);
2240 }
2241}
2242
2243template<class T>
2244void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2245 DCHECK(IsValidIndex(i));
2246 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2247 SetFieldObject(data_offset, object, false);
2248}
2249
2250template<class T>
2251void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2252 ObjectArray<T>* dst, int dst_pos,
2253 size_t length) {
2254 if (src->IsValidIndex(src_pos) &&
2255 src->IsValidIndex(src_pos+length-1) &&
2256 dst->IsValidIndex(dst_pos) &&
2257 dst->IsValidIndex(dst_pos+length-1)) {
2258 MemberOffset src_offset(DataOffset().Int32Value() +
2259 src_pos * sizeof(Object*));
2260 MemberOffset dst_offset(DataOffset().Int32Value() +
2261 dst_pos * sizeof(Object*));
2262 Class* array_class = dst->GetClass();
2263 if (array_class == src->GetClass()) {
2264 // No need for array store checks if arrays are of the same type
2265 for (size_t i=0; i < length; i++) {
2266 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2267 dst->SetFieldObject(dst_offset, object, false);
2268 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2269 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2270 }
2271 } else {
2272 Class* element_class = array_class->GetComponentType();
2273 CHECK(!element_class->IsPrimitive());
2274 for (size_t i=0; i < length; i++) {
2275 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002276 if (object != NULL && !object->InstanceOf(element_class)) {
2277 dst->ThrowArrayStoreException(object);
2278 return;
2279 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002280 dst->SetFieldObject(dst_offset, object, false);
2281 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2282 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2283 }
2284 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002285 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002286 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002287}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002288
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002289class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002290 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002291 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002292 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002293 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002294 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2295};
2296
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002297class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002298 private:
2299 CharArray* ASCII_;
2300 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002301 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002302 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002303 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002304 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2305};
2306
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002307class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002308 private:
2309 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2310 uint32_t TYPE_BOOLEAN_;
2311 uint32_t TYPE_BYTE_;
2312 uint32_t TYPE_CHAR_;
2313 uint32_t TYPE_DOUBLE_;
2314 uint32_t TYPE_FLOAT_;
2315 uint32_t TYPE_INTEGER_;
2316 uint32_t TYPE_LONG_;
2317 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002318 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002319 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2320};
2321
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002322class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002323 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002324 Object* ORDER_BY_SIGNATURE_;
2325 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002326 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2327};
2328
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002329template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002330class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002331 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002332 typedef T ElementType;
2333
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002334 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002335
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002336 const T* GetData() const {
2337 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002338 }
2339
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002340 T* GetData() {
2341 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002342 }
2343
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002344 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002345 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002346 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002347 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002348 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002349 }
2350
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002351 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002352 // TODO: ArrayStoreException
2353 if (IsValidIndex(i)) {
2354 GetData()[i] = value;
2355 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002356 }
2357
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002358 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002359 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002360 CHECK(array_class != NULL);
2361 array_class_ = array_class;
2362 }
2363
Brian Carlstroma663ea52011-08-19 23:33:41 -07002364 static void ResetArrayClass() {
2365 CHECK(array_class_ != NULL);
2366 array_class_ = NULL;
2367 }
2368
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002369 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002370 // Location of first element.
2371 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002372
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002373 static Class* array_class_;
2374
Carl Shapirof88c9522011-08-06 15:47:38 -07002375 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002376};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002377
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002378inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2379 DCHECK(NULL == GetFieldObject<IntArray*>(
2380 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2381 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2382 new_interfaces_idx, false);
2383}
2384
2385// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002386class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002387 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002388 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002389 const CharArray* result = GetFieldObject<const CharArray*>(
2390 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2391 DCHECK(result != NULL);
2392 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002393 }
2394
Elliott Hughes814e4032011-08-23 12:07:56 -07002395 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002396 int32_t result = GetField32(
2397 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2398 DCHECK_LE(0, result);
2399 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002400 }
2401
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002402 int32_t GetLength() const;
2403
Brian Carlstrom395520e2011-09-25 19:35:00 -07002404 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002405
2406 void ComputeHashCode() {
2407 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002408 }
2409
Elliott Hughes814e4032011-08-23 12:07:56 -07002410 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002411 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002412 }
2413
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002414 uint16_t CharAt(int32_t index) const;
2415
Brian Carlstromc74255f2011-09-11 22:47:39 -07002416 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002417
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002418 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002419 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002420 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002421
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002422 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002423
Jesse Wilson8989d992011-08-02 13:39:42 -07002424 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002425 const char* utf8_data_in);
2426
2427 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2428
2429 static String* Alloc(Class* java_lang_String, CharArray* array);
2430
2431 bool Equals(const char* modified_utf8) const;
2432
2433 // TODO: do we need this overload? give it a more intention-revealing name.
2434 bool Equals(const StringPiece& modified_utf8) const;
2435
2436 bool Equals(const String* that) const;
2437
2438 // TODO: do we need this overload? give it a more intention-revealing name.
2439 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2440 int32_t that_length) const;
2441
2442 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2443 std::string ToModifiedUtf8() const;
2444
2445 static Class* GetJavaLangString() {
2446 DCHECK(java_lang_String_ != NULL);
2447 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002448 }
2449
Brian Carlstroma663ea52011-08-19 23:33:41 -07002450 static void SetClass(Class* java_lang_String);
2451 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002452
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002453 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002454 void SetHashCode(int32_t new_hash_code) {
2455 DCHECK_EQ(0u,
2456 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2457 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2458 new_hash_code, false);
2459 }
2460
2461 void SetCount(int32_t new_count) {
2462 DCHECK_LE(0, new_count);
2463 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2464 }
2465
2466 void SetOffset(int32_t new_offset) {
2467 DCHECK_LE(0, new_offset);
2468 DCHECK_GE(GetLength(), new_offset);
2469 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2470 }
2471
2472 void SetArray(CharArray* new_array) {
2473 DCHECK(new_array != NULL);
2474 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2475 }
2476
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002477 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2478 CharArray* array_;
2479
Brian Carlstromdbc05252011-09-09 01:59:59 -07002480 int32_t count_;
2481
Carl Shapirof88c9522011-08-06 15:47:38 -07002482 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002483
Elliott Hughes289da822011-08-16 10:11:20 -07002484 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002485
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002486 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002487
Brian Carlstrom693267a2011-09-06 09:25:34 -07002488 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002489 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002490};
2491
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002492inline const String* Field::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002493 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002494 String* result =
2495 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2496 DCHECK(result != NULL);
2497 return result;
2498}
2499
2500inline void Field::SetName(String* new_name) {
2501 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2502 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002503}
2504
2505inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002506 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002507 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2508}
2509
2510inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002511 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002512 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2513}
2514
2515inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002516 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002517 return MemberOffset(
2518 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2519}
2520
2521inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002522 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002523 return MemberOffset(
2524 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2525}
2526
2527inline const String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002528 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002529 const String* result =
2530 GetFieldObject<const String*>(
2531 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2532 DCHECK(result != NULL);
2533 return result;
2534}
2535
2536inline void Method::SetName(String* new_name) {
2537 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2538 new_name, false);
2539
2540}
2541
jeffhaoe23d93c2011-09-15 14:48:43 -07002542inline ByteArray* Method::GetRegisterMapData() const {
2543 return GetFieldObject<ByteArray*>(
2544 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2545}
2546
jeffhaoa0a764a2011-09-16 10:43:38 -07002547inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002548 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002549 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002550}
2551
2552inline ByteArray* Method::GetRegisterMapHeader() const {
2553 return GetFieldObject<ByteArray*>(
2554 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2555}
2556
jeffhaoa0a764a2011-09-16 10:43:38 -07002557inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002558 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002559 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002560}
2561
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002562inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002563 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002564 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002565 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2566}
2567
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002568inline void Method::SetShorty(String* new_shorty) {
2569 DCHECK(NULL == GetFieldObject<String*>(
2570 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2571 DCHECK_LE(1, new_shorty->GetLength());
2572 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2573}
2574
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002575inline const String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002576 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002577 const String* result =
2578 GetFieldObject<const String*>(
2579 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2580 DCHECK(result != NULL);
2581 return result;
2582}
2583
2584inline void Method::SetSignature(String* new_signature) {
2585 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2586 new_signature, false);
2587}
2588
2589inline uint32_t Class::GetAccessFlags() const {
2590 // Check class is loaded or this is java.lang.String that has a
2591 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002592 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002593 this == String::GetJavaLangString() ||
2594 this == Field::GetJavaLangReflectField() ||
2595 this == Method::GetConstructorClass() ||
2596 this == Method::GetMethodClass()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002597 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2598}
2599
2600inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002601 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002602 DCHECK(new_descriptor != NULL);
2603 DCHECK_NE(0, new_descriptor->GetLength());
2604 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2605 new_descriptor, false);
2606}
2607
Brian Carlstromc74255f2011-09-11 22:47:39 -07002608inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002609 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002610 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2611}
2612
2613inline void Class::SetSourceFile(String* new_source_file) {
2614 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2615}
2616
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002617inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002618 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002619 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2620}
2621
2622inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002623 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002624 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002625}
2626
2627inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002628 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002629 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002630}
2631
2632inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002633 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002634 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002635}
2636
2637inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002638 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002639 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002640}
2641
2642inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002643 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002644 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2645}
2646
2647// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002648class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002649 public:
2650 void SetDetailMessage(String* new_detail_message) {
2651 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2652 new_detail_message, false);
2653 }
2654
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002655 private:
2656 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2657 Throwable* cause_;
2658 String* detail_message_;
2659 Object* stack_state_; // Note this is Java volatile:
2660 Object* stack_trace_;
2661 Object* suppressed_exceptions_;
2662
Brian Carlstrom693267a2011-09-06 09:25:34 -07002663 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002664 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2665};
2666
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002667// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002668class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002669 public:
2670 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002671 return GetFieldObject<const String*>(
2672 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002673 }
2674
2675 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002676 return GetFieldObject<const String*>(
2677 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002678 }
2679
2680 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002681 return GetFieldObject<const String*>(
2682 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002683 }
2684
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002685 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002686 return GetField32(
2687 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002688 }
2689
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002690 static StackTraceElement* Alloc(const String* declaring_class,
2691 const String* method_name,
2692 const String* file_name,
2693 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002694
2695 static void SetClass(Class* java_lang_StackTraceElement);
2696
2697 static void ResetClass();
2698
2699 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002700 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002701 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002702 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002703 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002704 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002705
2706 static Class* GetStackTraceElement() {
2707 DCHECK(java_lang_StackTraceElement_ != NULL);
2708 return java_lang_StackTraceElement_;
2709 }
2710
2711 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002712
2713 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002714 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2715};
2716
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002717class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002718 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002719 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002720 Class* interface = Get(kInterface)->AsClass();
2721 DCHECK(interface != NULL);
2722 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002723 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002724
Brian Carlstrom30b94452011-08-25 21:35:26 -07002725 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002726 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002727 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002728 DCHECK(Get(kInterface) == NULL);
2729 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002730 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002731
Brian Carlstrom86927212011-09-15 11:31:11 -07002732 size_t GetMethodArrayCount() const {
2733 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2734 if (method_array == 0) {
2735 return 0;
2736 }
2737 return method_array->GetLength();
2738 }
2739
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002740 ObjectArray<Method>* GetMethodArray() const {
2741 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2742 DCHECK(method_array != NULL);
2743 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002744 }
2745
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002746 void SetMethodArray(ObjectArray<Method>* new_ma) {
2747 DCHECK(new_ma != NULL);
2748 DCHECK(Get(kMethodArray) == NULL);
2749 Set(kMethodArray, new_ma);
2750 }
2751
2752 static size_t LengthAsArray() {
2753 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002754 }
2755
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002756 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002757
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002758 enum ArrayIndex {
2759 // Points to the interface class.
2760 kInterface = 0,
2761 // Method pointers into the vtable, allow fast map from interface
2762 // method index to concrete instance method.
2763 kMethodArray = 1,
2764 kMax = 2,
2765 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002766
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002767 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002768};
2769
2770} // namespace art
2771
2772#endif // ART_SRC_OBJECT_H_