blob: 3ece5356f182a9e400cbc23194b501e5970365e9 [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"
26#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
29#include "macros.h"
30#include "offsets.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070031#include "primitive.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
Logan Chienfca7e872011-12-20 20:08:22 +080074#if defined(ART_USE_LLVM_COMPILER)
75namespace compiler_llvm {
76 class InferredRegCategoryMap;
77}
78#endif
79
Brian Carlstrombe977852011-07-19 14:54:54 -070080static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
81static const uint32_t kAccPrivate = 0x0002; // field, method, ic
82static const uint32_t kAccProtected = 0x0004; // field, method, ic
83static const uint32_t kAccStatic = 0x0008; // field, method, ic
84static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
85static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -070086static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -070087static const uint32_t kAccVolatile = 0x0040; // field
88static const uint32_t kAccBridge = 0x0040; // method (1.5)
89static const uint32_t kAccTransient = 0x0080; // field
90static const uint32_t kAccVarargs = 0x0080; // method (1.5)
91static const uint32_t kAccNative = 0x0100; // method
92static const uint32_t kAccInterface = 0x0200; // class, ic
93static const uint32_t kAccAbstract = 0x0400; // class, method, ic
94static const uint32_t kAccStrict = 0x0800; // method
95static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
96static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
97static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070098
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070099static const uint32_t kAccMiranda = 0x8000; // method
100
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700101static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
102
Elliott Hughes582a7d12011-10-10 18:38:42 -0700103static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
104static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800105static const uint32_t kAccClassIsProxy = 0x00040000; // class (dex only)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700106static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700107
Elliott Hughes20cde902011-10-04 17:37:27 -0700108// Special runtime-only flags.
109// Note: if only kAccClassIsReference is set, we have a soft reference.
110static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
111static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
112static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
113static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
114static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700115
116static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
117 | kAccClassIsWeakReference
118 | kAccClassIsFinalizerReference
119 | kAccClassIsPhantomReference);
120
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700121/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700122 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700123 */
124/*
125 * A magic value for refOffsets. Ignore the bits and walk the super
126 * chain when this is the value.
127 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
128 * fields followed by 2 ref instance fields.]
129 */
130#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700131#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
132#define CLASS_OFFSET_ALIGNMENT 4
133#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
134/*
135 * Given an offset, return the bit number which would encode that offset.
136 * Local use only.
137 */
138#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700139 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700140 CLASS_OFFSET_ALIGNMENT)
141/*
142 * Is the given offset too large to be encoded?
143 */
144#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
145 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
146/*
147 * Return a single bit, encoding the offset.
148 * Undefined if the offset is too large, as defined above.
149 */
150#define CLASS_BIT_FROM_OFFSET(byteOffset) \
151 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
152/*
153 * Return an offset, given a bit number as returned from CLZ.
154 */
155#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700156 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700158#define OFFSET_OF_OBJECT_MEMBER(type, field) \
159 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700160
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700161// Classes shared with the managed side of the world need to be packed
162// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700163#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700164
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700165// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700166class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700167 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168 static MemberOffset ClassOffset() {
169 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700170 }
171
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700172 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700173 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700174 }
175
176 void SetClass(Class* new_klass);
177
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700178 bool InstanceOf(const Class* klass) const;
179
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700180 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700181
Elliott Hughes081be7f2011-09-18 16:50:26 -0700182 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700183
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184 static MemberOffset MonitorOffset() {
185 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
186 }
187
Elliott Hughes5f791332011-09-15 17:45:30 -0700188 volatile int32_t* GetRawLockWordAddress() {
189 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
190 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
191 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 }
193
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700194 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195
Elliott Hughes5f791332011-09-15 17:45:30 -0700196 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700197
Ian Rogersff1ed472011-09-20 13:46:24 -0700198 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700199
Elliott Hughes5f791332011-09-15 17:45:30 -0700200 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700201
Elliott Hughes5f791332011-09-15 17:45:30 -0700202 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700203
Elliott Hughes5f791332011-09-15 17:45:30 -0700204 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205
Elliott Hughes5f791332011-09-15 17:45:30 -0700206 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700207
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700208 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700209
210 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700211 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 return down_cast<Class*>(this);
213 }
214
215 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700217 return down_cast<const Class*>(this);
218 }
219
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700220 bool IsObjectArray() const;
221
222 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700223 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700224
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700225 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700226 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227
Brian Carlstromb63ec392011-08-27 17:38:27 -0700228 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700229
230 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700231 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700232 return down_cast<Array*>(this);
233 }
234
235 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700236 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700237 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700238 }
239
Elliott Hughesdbb40792011-11-18 17:05:22 -0800240 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700241
242 bool IsMethod() const;
243
244 Method* AsMethod() {
245 DCHECK(IsMethod());
246 return down_cast<Method*>(this);
247 }
248
Brian Carlstrom4873d462011-08-21 15:23:39 -0700249 const Method* AsMethod() const {
250 DCHECK(IsMethod());
251 return down_cast<const Method*>(this);
252 }
253
Brian Carlstroma663ea52011-08-19 23:33:41 -0700254 bool IsField() const;
255
256 Field* AsField() {
257 DCHECK(IsField());
258 return down_cast<Field*>(this);
259 }
260
Brian Carlstrom4873d462011-08-21 15:23:39 -0700261 const Field* AsField() const {
262 DCHECK(IsField());
263 return down_cast<const Field*>(this);
264 }
265
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700266 bool IsReferenceInstance() const;
267
268 bool IsWeakReferenceInstance() const;
269
270 bool IsSoftReferenceInstance() const;
271
272 bool IsFinalizerReferenceInstance() const;
273
274 bool IsPhantomReferenceInstance() const;
275
276 // Accessors for Java type fields
277 template<class T>
278 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700279 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
280 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800281 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700282 return result;
283 }
284
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700285 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800286 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700287 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
288 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800289 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700290 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 }
292
293 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800294 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700295 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
296 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700297 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700298 return android_atomic_acquire_load(word_addr);
299 } else {
300 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 }
303
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700304 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
305 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800306 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700307 }
308 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700309 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700310 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700311 /*
312 * TODO: add an android_atomic_synchronization_store() function and
313 * use it in the 32-bit volatile set handlers. On some platforms we
314 * can use a fast atomic instruction and avoid the barriers.
315 */
316 ANDROID_MEMBAR_STORE();
317 *word_addr = new_value;
318 ANDROID_MEMBAR_FULL();
319 } else {
320 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 }
323
324 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800325 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700326 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700327 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700328 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700329 uint64_t result = QuasiAtomicRead64(addr);
330 ANDROID_MEMBAR_FULL();
331 return result;
332 } else {
333 return *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 SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800338 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700339 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
340 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700341 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700342 ANDROID_MEMBAR_STORE();
343 QuasiAtomicSwap64(new_value, addr);
344 // Post-store barrier not required due to use of atomic op or mutex.
345 } else {
346 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
349
350 protected:
351 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 template<class T>
353 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700354 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 }
356
357 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800358 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
359 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700360 }
361
362 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700363 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364
Elliott Hughes5f791332011-09-15 17:45:30 -0700365 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700366
Elliott Hughes5f791332011-09-15 17:45:30 -0700367 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700368 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700369 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700370};
371
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372struct ObjectIdentityHash {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800373 size_t operator()(const Object* const& obj) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700374#ifdef MOVING_GARBAGE_COLLECTOR
375 // TODO: we'll need to use the Object's internal concept of identity
376 UNIMPLEMENTED(FATAL);
377#endif
378 return reinterpret_cast<size_t>(obj);
379 }
380};
381
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500383class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700384 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700386
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 void SetDeclaringClass(Class *new_declaring_class);
388
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389 uint32_t GetAccessFlags() const;
390
391 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700392 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700393 }
394
Elliott Hughes80609252011-09-23 17:24:51 -0700395 bool IsPublic() const {
396 return (GetAccessFlags() & kAccPublic) != 0;
397 }
398
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700399 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700400 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700401 }
402
jeffhaobdb76512011-09-07 11:43:16 -0700403 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700404 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700405 }
406
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800407 uint32_t GetDexFieldIndex() const {
408 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
409 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700410
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800411 void SetDexFieldIndex(uint32_t new_idx) {
412 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
413 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414
415 // Offset to field within an Object
416 MemberOffset GetOffset() const;
417
buzbee34cd9e52011-09-08 14:31:52 -0700418 static MemberOffset OffsetOffset() {
419 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
420 }
421
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 MemberOffset GetOffsetDuringLinking() const;
423
424 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700425
Brian Carlstrom4873d462011-08-21 15:23:39 -0700426 // field access, null object for static fields
427 bool GetBoolean(const Object* object) const;
428 void SetBoolean(Object* object, bool z) const;
429 int8_t GetByte(const Object* object) const;
430 void SetByte(Object* object, int8_t b) const;
431 uint16_t GetChar(const Object* object) const;
432 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700433 int16_t GetShort(const Object* object) const;
434 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700435 int32_t GetInt(const Object* object) const;
436 void SetInt(Object* object, int32_t i) const;
437 int64_t GetLong(const Object* object) const;
438 void SetLong(Object* object, int64_t j) const;
439 float GetFloat(const Object* object) const;
440 void SetFloat(Object* object, float f) const;
441 double GetDouble(const Object* object) const;
442 void SetDouble(Object* object, double d) const;
443 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700444 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700445
Ian Rogersce9eca62011-10-07 17:11:03 -0700446 // raw field accesses
447 uint32_t Get32(const Object* object) const;
448 void Set32(Object* object, uint32_t new_value) const;
449 uint64_t Get64(const Object* object) const;
450 void Set64(Object* object, uint64_t new_value) const;
451 Object* GetObj(const Object* object) const;
452 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700453
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700454 static Class* GetJavaLangReflectField() {
455 DCHECK(java_lang_reflect_Field_ != NULL);
456 return java_lang_reflect_Field_;
457 }
458
459 static void SetClass(Class* java_lang_reflect_Field);
460 static void ResetClass();
461
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700462 bool IsVolatile() const {
463 return (GetAccessFlags() & kAccVolatile) != 0;
464 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700465
buzbee1da522d2011-09-04 11:22:20 -0700466 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400467 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800468 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400469 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700470
Jesse Wilson35baaab2011-08-10 16:18:03 -0400471 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700472
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800473 // Dex cache index of field id
474 uint32_t field_dex_idx_;
475
Brian Carlstrom693267a2011-09-06 09:25:34 -0700476 // Offset of field within an instance or in the Class' static fields
477 uint32_t offset_;
478
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700479 static Class* java_lang_reflect_Field_;
480
Brian Carlstrom693267a2011-09-06 09:25:34 -0700481 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400482 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700483};
484
Jesse Wilson6bf19152011-09-29 13:12:33 -0400485// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500486class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700487 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700488 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700489 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700490 Object* obj,
491 Thread* thread,
Elliott Hughes77405792012-03-15 15:22:12 -0700492 JValue* args,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700493 JValue* result);
494
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700495 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700496
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700498
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700499 static MemberOffset DeclaringClassOffset() {
500 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700501 }
502
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700503 uint32_t GetAccessFlags() const;
504
505 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700506 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700507 }
508
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700509 // Returns true if the method is declared public.
510 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700511 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 }
513
514 // Returns true if the method is declared private.
515 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700516 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700517 }
518
519 // Returns true if the method is declared static.
520 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700521 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700522 }
523
Brian Carlstrom1f870082011-08-23 16:02:11 -0700524 // Returns true if the method is a constructor.
525 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700526 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700527 }
528
529 // Returns true if the method is static, private, or a constructor.
530 bool IsDirect() const {
Brian Carlstromf5822582012-03-19 22:34:31 -0700531 return IsDirect(GetAccessFlags());
532 }
533
534 static bool IsDirect(uint32_t access_flags) {
535 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700536 }
537
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700538 // Returns true if the method is declared synchronized.
539 bool IsSynchronized() const {
540 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700541 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700542 }
543
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700544 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700545 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700546 }
547
Elliott Hughes80609252011-09-23 17:24:51 -0700548 bool IsMiranda() const {
549 return (GetAccessFlags() & kAccMiranda) != 0;
550 }
551
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700552 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700553 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700554 }
555
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700556 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700557 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700558 }
559
560 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700561 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700562 }
563
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800564 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700565
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700566 uint16_t GetMethodIndex() const;
567
568 size_t GetVtableIndex() const {
569 return GetMethodIndex();
570 }
571
572 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700573 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700574 }
575
576 static MemberOffset MethodIndexOffset() {
577 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
578 }
579
580 uint32_t GetCodeItemOffset() const {
581 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
582 }
583
584 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700585 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700586 }
587
588 // Number of 32bit registers that would be required to hold all the arguments
589 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700590
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800591 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700592
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800593 void SetDexMethodIndex(uint32_t new_idx) {
594 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700595 }
596
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700597 ObjectArray<String>* GetDexCacheStrings() const;
598 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
599
600 static MemberOffset DexCacheStringsOffset() {
601 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
602 }
603
Ian Rogers19846512012-02-24 11:42:47 -0800604 static MemberOffset DexCacheResolvedMethodsOffset() {
605 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
606 }
607
buzbee2a475e72011-09-07 17:19:17 -0700608 static MemberOffset DexCacheResolvedTypesOffset() {
609 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
610 }
611
buzbee1da522d2011-09-04 11:22:20 -0700612 static MemberOffset DexCacheInitializedStaticStorageOffset() {
613 return OFFSET_OF_OBJECT_MEMBER(Method,
614 dex_cache_initialized_static_storage_);
615 }
616
Ian Rogers19846512012-02-24 11:42:47 -0800617 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
618 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
619
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700620 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
621 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
622
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700623 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
624 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
625
Ian Rogers466bb252011-10-14 03:29:56 -0700626 // Find the method that this method overrides
627 Method* FindOverriddenMethod() const;
628
Elliott Hughes77405792012-03-15 15:22:12 -0700629 void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700630
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631 const void* GetCode() const {
632 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700633 }
634
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700635 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700636 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
637 }
638
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700639 uint32_t GetCodeSize() const {
640 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
641 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
642 if (code == 0) {
643 return 0;
644 }
645 // TODO: make this Thumb2 specific
646 code &= ~0x1;
647 return reinterpret_cast<uint32_t*>(code)[-1];
648 }
649
650 bool IsWithinCode(uintptr_t pc) const {
651 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
652 if (code == 0) {
653 return pc == 0;
654 }
655 return (code <= pc && pc < code + GetCodeSize());
656 }
657
658 void AssertPcIsWithinCode(uintptr_t pc) const;
659
Brian Carlstrome24fa612011-09-29 00:53:55 -0700660 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700661 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700662 return reinterpret_cast<uint32_t>(GetCode());
663 }
664
665 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700666 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700667 SetCode(reinterpret_cast<void*>(code_offset));
668 }
669
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700670 static MemberOffset GetCodeOffset() {
671 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700672 }
673
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700674 const uint32_t* GetMappingTable() const {
675 const uint32_t* map = GetMappingTableRaw();
676 if (map == NULL) {
677 return map;
678 }
679 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700680 }
681
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700682 uint32_t GetMappingTableLength() const {
683 const uint32_t* map = GetMappingTableRaw();
684 if (map == NULL) {
685 return 0;
686 }
687 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700688 }
689
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700690 const uint32_t* GetMappingTableRaw() const {
691 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700692 }
693
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700694 void SetMappingTable(const uint32_t* mapping_table) {
695 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
696 mapping_table, false);
697 }
698
699 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700700 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700701 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
702 }
703
704 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700705 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700706 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
707 }
708
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800709 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700710 const uint16_t* GetVmapTableRaw() const {
711 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
712 }
713
714 void SetVmapTable(const uint16_t* vmap_table) {
715 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
716 }
717
718 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700719 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700720 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
721 }
722
723 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700724 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700725 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700726 }
727
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800728 const uint8_t* GetGcMap() const {
729 const uint8_t* gc_map_raw = GetGcMapRaw();
730 if (gc_map_raw == NULL) {
731 return gc_map_raw;
732 }
733 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 }
735
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800736 uint32_t GetGcMapLength() const {
737 const uint8_t* gc_map_raw = GetGcMapRaw();
738 if (gc_map_raw == NULL) {
739 return 0;
740 }
741 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
742 (gc_map_raw[1] << 16) |
743 (gc_map_raw[2] << 8) |
744 (gc_map_raw[3] << 0));
745 }
746
747 const uint8_t* GetGcMapRaw() const {
748 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
749 }
750 void SetGcMap(const uint8_t* data) {
751 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
752 }
753
754 uint32_t GetOatGcMapOffset() const {
755 DCHECK(!Runtime::Current()->IsStarted());
756 return reinterpret_cast<uint32_t>(GetGcMapRaw());
757 }
758 void SetOatGcMapOffset(uint32_t gc_map_offset) {
759 DCHECK(!Runtime::Current()->IsStarted());
760 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 }
762
Shih-wei Liaod11af152011-08-23 16:02:11 -0700763 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700764 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
765 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
TDYa12728f1a142012-03-15 21:51:52 -0700766#if !defined(ART_USE_LLVM_COMPILER) // LLVM uses shadow stack instead.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700767 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
TDYa12728f1a142012-03-15 21:51:52 -0700768#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700769 return result;
770 }
771
772 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700773 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700774 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
775 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700776 }
777
Shih-wei Liaod11af152011-08-23 16:02:11 -0700778 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700780 }
781
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700782 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700783
Ian Rogers60db5ab2012-02-20 17:02:00 -0800784 void RegisterNative(Thread* self, const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700785
Ian Rogers19846512012-02-24 11:42:47 -0800786 void UnregisterNative(Thread* self);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700787
Ian Rogersb033c752011-07-20 12:22:35 -0700788 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700789 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700790 }
791
Brian Carlstrom78128a62011-09-15 17:21:19 -0700792 const void* GetNativeMethod() const {
793 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
794 }
795
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700796 // Native to managed invocation stub entry point
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700797 const InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700798 InvokeStub* result = GetFieldPtr<InvokeStub*>(
799 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
800 // TODO: DCHECK(result != NULL); should be ahead of time compiled
801 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700802 }
803
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700804 void SetInvokeStub(InvokeStub* invoke_stub) {
805 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
806 invoke_stub, false);
807 }
808
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700809 uint32_t GetInvokeStubSize() const {
810 const uint32_t* invoke_stub = reinterpret_cast<const uint32_t*>(GetInvokeStub());
811 if (invoke_stub == NULL) {
812 return 0;
813 }
814 return invoke_stub[-1];
815 }
816
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700817 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700818 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700819 return reinterpret_cast<uint32_t>(GetInvokeStub());
820 }
821
822 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700823 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700824 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
825 }
826
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700827 static MemberOffset GetInvokeStubOffset() {
828 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700829 }
830
buzbee561227c2011-09-02 15:28:19 -0700831 static MemberOffset GetMethodIndexOffset() {
832 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
833 }
834
Ian Rogers90865722011-09-19 11:11:44 -0700835 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700836 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700837 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700838
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700839 void SetCoreSpillMask(uint32_t core_spill_mask) {
840 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700841 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700842 }
843
Ian Rogers90865722011-09-19 11:11:44 -0700844 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700845 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
846 }
847
848 void SetFpSpillMask(uint32_t fp_spill_mask) {
849 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700850 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700851 }
852
Ian Rogers57b86d42012-03-27 16:05:41 -0700853 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
854 // conventions for a method of managed code.
855 bool IsRuntimeMethod() const {
856 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
857 }
858
Ian Rogers90865722011-09-19 11:11:44 -0700859 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700860 bool IsCalleeSaveMethod() const {
Ian Rogers57b86d42012-03-27 16:05:41 -0700861 if (!IsRuntimeMethod()) {
862 return false;
863 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700864 Runtime* runtime = Runtime::Current();
865 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700866 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700867 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
868 result = true;
869 break;
870 }
871 }
Ian Rogers90865722011-09-19 11:11:44 -0700872 return result;
873 }
874
Ian Rogers19846512012-02-24 11:42:47 -0800875 bool IsResolutionMethod() const {
876 bool result = this == Runtime::Current()->GetResolutionMethod();
877 // Check that if we do think it is phony it looks like the resolution method
878 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
879 return result;
880 }
881
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700882 // Converts a native PC to a dex PC. TODO: this is a no-op
883 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700884 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700885
886 // Converts a dex PC to a native PC. TODO: this is a no-op
887 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700888 uintptr_t ToNativePC(const uint32_t dex_pc) const;
889
890 // Find the catch block for the given exception type and dex_pc
891 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700892
Elliott Hughes80609252011-09-23 17:24:51 -0700893 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
894
895 static Class* GetConstructorClass() {
896 return java_lang_reflect_Constructor_;
897 }
898
899 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700900 return java_lang_reflect_Method_;
901 }
902
Elliott Hughes80609252011-09-23 17:24:51 -0700903 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700904
905 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700906 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800907 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700908 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700909
Brian Carlstrom693267a2011-09-06 09:25:34 -0700910 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800911 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700912
913 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800914 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700915
916 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700917 ObjectArray<Class>* dex_cache_resolved_types_;
918
919 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
920 ObjectArray<String>* dex_cache_strings_;
921
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700922 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700923 uint32_t access_flags_;
924
925 // Compiled code associated with this method for callers from managed code.
926 // May be compiled managed code or a bridge for invoking a native method.
927 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700928
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700929 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700930 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700931
Brian Carlstrom693267a2011-09-06 09:25:34 -0700932 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700933 uint32_t core_spill_mask_;
934
935 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700936 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700937
Brian Carlstrom693267a2011-09-06 09:25:34 -0700938 // Total size in bytes of the frame
939 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700940
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800941 // Garbage collection map
942 const uint8_t* gc_map_;
943
Brian Carlstrom693267a2011-09-06 09:25:34 -0700944 // Native invocation stub entry point for calling from native to managed code.
945 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700946
Ian Rogersd81871c2011-10-03 13:57:23 -0700947 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700948 const uint32_t* mapping_table_;
949
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800950 // Index into method_ids of the dex file associated with this method
951 uint32_t method_dex_index_;
952
Ian Rogers466bb252011-10-14 03:29:56 -0700953 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700954 //
Ian Rogers466bb252011-10-14 03:29:56 -0700955 // For abstract methods in an interface class, this is the offset of the method in
956 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -0800957 //
958 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700959 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700960
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700961 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -0700962 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700963
Ian Rogersd81871c2011-10-03 13:57:23 -0700964 // When a register is promoted into a register, the spill mask holds which registers hold dex
965 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
966 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700967 const uint16_t* vmap_table_;
968
Elliott Hughes80609252011-09-23 17:24:51 -0700969 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700970 static Class* java_lang_reflect_Method_;
971
Brian Carlstrom693267a2011-09-06 09:25:34 -0700972 friend class ImageWriter; // for relocating code_ and invoke_stub_
973 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700974 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700975};
976
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700977class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700978 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700979 // A convenience for code that doesn't know the component size,
980 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700981 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700982
Brian Carlstromb63ec392011-08-27 17:38:27 -0700983 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700984
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700985 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700986
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700987 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700988 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700989 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700990
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700991 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700992 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700993 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700994 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700995
buzbeec143c552011-08-20 17:38:58 -0700996 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700997 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -0700998 }
999
Ian Rogersa15e67d2012-02-28 13:51:55 -08001000 static MemberOffset DataOffset(size_t component_size) {
1001 if (component_size != sizeof(int64_t)) {
1002 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
1003 } else {
1004 // Align longs and doubles.
1005 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
1006 }
buzbeec143c552011-08-20 17:38:58 -07001007 }
1008
Ian Rogersa15e67d2012-02-28 13:51:55 -08001009 void* GetRawData(size_t component_size) {
1010 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1011 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001012 }
1013
Elliott Hughes289da822011-08-16 10:11:20 -07001014 protected:
1015 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001016 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001017 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001018 }
1019 return true;
1020 }
1021
Elliott Hughes80609252011-09-23 17:24:51 -07001022 protected:
1023 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1024 bool ThrowArrayStoreException(Object* object) const;
1025
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001026 private:
1027 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001028 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001029 // Marker for the data (used by generated code)
1030 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001031
Carl Shapirof88c9522011-08-06 15:47:38 -07001032 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001033};
1034
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001035template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001036class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001037 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001038 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001039
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001040 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001041
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001042 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001043
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001044 // Set element without bound and element type checks, to be used in limited
1045 // circumstances, such as during boot image writing
1046 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001047
Ian Rogers5d76c432011-10-31 21:42:49 -07001048 T* GetWithoutChecks(int32_t i) const;
1049
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001050 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001051 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001052 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001053
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001054 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001055
1056 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001057 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001058};
1059
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001060template<class T>
1061ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001062 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001063 if (UNLIKELY(array == NULL)) {
1064 return NULL;
1065 } else {
1066 return array->AsObjectArray<T>();
1067 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001068}
1069
1070template<class T>
1071T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001072 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001073 return NULL;
1074 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001075 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001076 return GetFieldObject<T*>(data_offset, false);
1077}
1078
1079template<class T>
1080ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1081 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1082 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1083 return new_array;
1084}
1085
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001086// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001087// provides the static storage. However, this might change to an Array
1088// to improve image sharing, so we use this type to avoid assumptions
1089// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001090class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001091
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001092// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001093class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001094 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001095
1096 // Class Status
1097 //
1098 // kStatusNotReady: If a Class cannot be found in the class table by
1099 // FindClass, it allocates an new one with AllocClass in the
1100 // kStatusNotReady and calls LoadClass. Note if it does find a
1101 // class, it may not be kStatusResolved and it will try to push it
1102 // forward toward kStatusResolved.
1103 //
1104 // kStatusIdx: LoadClass populates with Class with information from
1105 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001106 // Class value in super_class_ has not been populated. The new Class
1107 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001108 //
1109 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1110 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001111 // using ResolveClass to initialize the super_class_ and ensuring the
1112 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001113 //
1114 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001115 // shows linking is complete and fields of the Class populated by making
1116 // it kStatusResolved. Java allows circularities of the form where a super
1117 // class has a field that is of the type of the sub class. We need to be able
1118 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001119
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001120 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001121 kStatusError = -1,
1122 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001123 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001124 kStatusLoaded = 2, // DEX idx values resolved
1125 kStatusResolved = 3, // part of linking
1126 kStatusVerifying = 4, // in the process of being verified
1127 kStatusVerified = 5, // logically part of linking; done pre-init
1128 kStatusInitializing = 6, // class init in progress
1129 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001130 };
1131
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001133 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1134 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001135 }
1136
1137 void SetStatus(Status new_status);
1138
1139 // Returns true if the class has failed to link.
1140 bool IsErroneous() const {
1141 return GetStatus() == kStatusError;
1142 }
1143
1144 // Returns true if the class has been loaded.
1145 bool IsIdxLoaded() const {
1146 return GetStatus() >= kStatusIdx;
1147 }
1148
1149 // Returns true if the class has been loaded.
1150 bool IsLoaded() const {
1151 return GetStatus() >= kStatusLoaded;
1152 }
1153
1154 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001155 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 return GetStatus() >= kStatusResolved;
1157 }
1158
1159 // Returns true if the class has been verified.
1160 bool IsVerified() const {
1161 return GetStatus() >= kStatusVerified;
1162 }
1163
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001164 // Returns true if the class is initializing.
1165 bool IsInitializing() const {
1166 return GetStatus() >= kStatusInitializing;
1167 }
1168
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001169 // Returns true if the class is initialized.
1170 bool IsInitialized() const {
1171 return GetStatus() == kStatusInitialized;
1172 }
1173
1174 uint32_t GetAccessFlags() const;
1175
1176 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001177 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001178 }
1179
1180 // Returns true if the class is an interface.
1181 bool IsInterface() const {
1182 return (GetAccessFlags() & kAccInterface) != 0;
1183 }
1184
1185 // Returns true if the class is declared public.
1186 bool IsPublic() const {
1187 return (GetAccessFlags() & kAccPublic) != 0;
1188 }
1189
1190 // Returns true if the class is declared final.
1191 bool IsFinal() const {
1192 return (GetAccessFlags() & kAccFinal) != 0;
1193 }
1194
Elliott Hughes20cde902011-10-04 17:37:27 -07001195 bool IsFinalizable() const {
1196 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1197 }
1198
1199 void SetFinalizable() {
1200 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1201 SetAccessFlags(flags | kAccClassIsFinalizable);
1202 }
1203
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001204 // Returns true if the class is abstract.
1205 bool IsAbstract() const {
1206 return (GetAccessFlags() & kAccAbstract) != 0;
1207 }
1208
1209 // Returns true if the class is an annotation.
1210 bool IsAnnotation() const {
1211 return (GetAccessFlags() & kAccAnnotation) != 0;
1212 }
1213
1214 // Returns true if the class is synthetic.
1215 bool IsSynthetic() const {
1216 return (GetAccessFlags() & kAccSynthetic) != 0;
1217 }
1218
1219 bool IsReferenceClass() const {
1220 return (GetAccessFlags() & kAccClassIsReference) != 0;
1221 }
1222
1223 bool IsWeakReferenceClass() const {
1224 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1225 }
1226
1227 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001228 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001229 }
1230
1231 bool IsFinalizerReferenceClass() const {
1232 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1233 }
1234
1235 bool IsPhantomReferenceClass() const {
1236 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1237 }
1238
Ian Rogersd418eda2012-01-30 12:14:28 -08001239
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001240 String* GetName() const; // Returns the cached name
Ian Rogersd418eda2012-01-30 12:14:28 -08001241 void SetName(String* name); // Sets the cached name
1242 String* ComputeName(); // Computes the name, then sets the cached value
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001243
1244 bool IsProxyClass() const {
1245 // Read access flags without using getter as whether something is a proxy can be check in
1246 // any loaded state
1247 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1248 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1249 return (access_flags & kAccClassIsProxy) != 0;
1250 }
1251
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001252 Primitive::Type GetPrimitiveType() const {
1253 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1254 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001255 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1256 }
1257
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001258 void SetPrimitiveType(Primitive::Type new_type) {
1259 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001260 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001261 }
1262
1263 // Returns true if the class is a primitive type.
1264 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001265 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001266 }
1267
1268 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001269 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001270 }
1271
1272 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001273 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001274 }
1275
1276 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001277 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001278 }
1279
1280 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001281 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001282 }
1283
1284 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001285 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001286 }
1287
1288 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001289 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001290 }
1291
1292 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001293 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001294 }
1295
1296 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001297 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001298 }
1299
1300 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001301 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001302 }
1303
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 // Depth of class from java.lang.Object
1305 size_t Depth() {
1306 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001307 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 depth++;
1309 }
1310 return depth;
1311 }
1312
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001313 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001314 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001315 }
1316
Elliott Hughesdbb40792011-11-18 17:05:22 -08001317 bool IsClassClass() const;
1318
1319 bool IsStringClass() const;
1320
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001321 bool IsThrowableClass() const;
1322
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001323 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001324 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001325 }
1326
1327 void SetComponentType(Class* new_component_type) {
1328 DCHECK(GetComponentType() == NULL);
1329 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001330 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001331 }
1332
1333 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001334 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001335 }
1336
1337 bool IsObjectClass() const {
1338 return !IsPrimitive() && GetSuperClass() == NULL;
1339 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001340 bool IsInstantiable() const {
1341 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1342 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001343
Brian Carlstrom1f870082011-08-23 16:02:11 -07001344 // Creates a raw object instance but does not invoke the default constructor.
1345 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001346
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001347 bool IsVariableSize() const {
1348 // Classes and arrays vary in size, and so the object_size_ field cannot
1349 // be used to get their instance size
1350 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 }
1352
Brian Carlstrom4873d462011-08-21 15:23:39 -07001353 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001354 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001355 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001356 }
1357
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001358 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001359 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001360 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001361 }
1362
Ian Rogers0571d352011-11-03 19:51:38 -07001363 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001365 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001366 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001367 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001368 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001369 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001370 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001371 }
1372
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001373 void SetObjectSize(size_t new_object_size) {
1374 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001375 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001376 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001377 }
1378
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001379 // Returns true if this class is in the same packages as that class.
1380 bool IsInSamePackage(const Class* that) const;
1381
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001382 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001383
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001384 // Returns true if this class can access that class.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001385 bool CanAccess(Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001386 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001387 }
1388
Ian Rogersf2391652011-12-14 12:50:52 -08001389 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001390 // Note that access to the class isn't checked in case the declaring class is protected and the
1391 // method has been exposed by a public sub-class
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001392 bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
Ian Rogersf2391652011-12-14 12:50:52 -08001393 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001394 if (this == access_to) {
1395 return true;
1396 }
Ian Rogersf2391652011-12-14 12:50:52 -08001397 // Public members are trivially accessible
1398 if (member_flags & kAccPublic) {
1399 return true;
1400 }
1401 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001402 if (member_flags & kAccPrivate) {
1403 return false;
1404 }
Ian Rogersf2391652011-12-14 12:50:52 -08001405 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001406 if (member_flags & kAccProtected) {
1407 if (this->IsSubClass(access_to)) {
1408 return true;
1409 }
1410 }
Ian Rogersf2391652011-12-14 12:50:52 -08001411 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001412 return this->IsInSamePackage(access_to);
1413 }
1414
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001415 bool IsSubClass(const Class* klass) const;
1416
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1418 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1419 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1420 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1421 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001422 bool IsAssignableFrom(const Class* src) const {
1423 DCHECK(src != NULL);
1424 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001425 // Can always assign to things of the same type
1426 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001427 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001428 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001429 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001430 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001431 return src->Implements(this);
1432 } else if (src->IsArrayClass()) {
1433 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001434 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001436 }
1437 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001438
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001439 Class* GetSuperClass() const {
1440 // Can only get super class for loaded classes (hack for when runtime is
1441 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001442 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001443 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001444 }
1445
1446 void SetSuperClass(Class *new_super_class) {
1447 // super class is assigned once, except during class linker initialization
1448 Class* old_super_class = GetFieldObject<Class*>(
1449 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1450 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1451 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001452 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001453 }
1454
1455 bool HasSuperClass() const {
1456 return GetSuperClass() != NULL;
1457 }
1458
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001459 static MemberOffset SuperClassOffset() {
1460 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001461 }
1462
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001463 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001464
1465 void SetClassLoader(const ClassLoader* new_cl);
1466
1467 static MemberOffset DexCacheOffset() {
1468 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1469 }
1470
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001471 enum {
1472 kDumpClassFullDetail = 1,
1473 kDumpClassClassLoader = (1 << 1),
1474 kDumpClassInitialized = (1 << 2),
1475 };
1476
Elliott Hughes4681c802011-09-25 18:04:37 -07001477 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001478
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001479 DexCache* GetDexCache() const;
1480
1481 void SetDexCache(DexCache* new_dex_cache);
1482
1483 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001484 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001485 return GetFieldObject<ObjectArray<Method>*>(
1486 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1487 }
1488
1489 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1490 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1491 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1492 DCHECK_NE(0, new_direct_methods->GetLength());
1493 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1494 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001495 }
1496
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001497 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001498 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001499 }
1500
1501 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001502 ObjectArray<Method>* direct_methods =
1503 GetFieldObject<ObjectArray<Method>*>(
1504 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1505 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001506 }
1507
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001508 // Returns the number of static, private, and constructor methods.
1509 size_t NumDirectMethods() const {
1510 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1511 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001512
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001513 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001514 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001515 return GetFieldObject<ObjectArray<Method>*>(
1516 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1517 }
1518
1519 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1520 // TODO: we reassign virtual methods to grow the table for miranda
1521 // methods.. they should really just be assigned once
1522 DCHECK_NE(0, new_virtual_methods->GetLength());
1523 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1524 new_virtual_methods, false);
1525 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001526
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527 // Returns the number of non-inherited virtual methods.
1528 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001529 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001530 }
1531
1532 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001533 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001534 return GetVirtualMethods()->Get(i);
1535 }
1536
1537 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001538 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001539 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001540 }
1541
1542 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001543 ObjectArray<Method>* virtual_methods =
1544 GetFieldObject<ObjectArray<Method>*>(
1545 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1546 virtual_methods->Set(i, f);
1547 }
1548
1549 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001550 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001551 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001552 }
1553
1554 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001555 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001556 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001557 }
1558
1559 void SetVTable(ObjectArray<Method>* new_vtable) {
1560 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1561 }
1562
1563 static MemberOffset VTableOffset() {
1564 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001565 }
1566
Brian Carlstrom30b94452011-08-25 21:35:26 -07001567 // Given a method implemented by this class but potentially from a
1568 // super class, return the specific implementation
1569 // method for this class.
1570 Method* FindVirtualMethodForVirtual(Method* method) {
1571 DCHECK(!method->GetDeclaringClass()->IsInterface());
1572 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001573 // Use the index to a potentially overridden one for this instance's class.
1574 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001575 }
1576
1577 // Given a method implemented by this class, but potentially from a
1578 // super class or interface, return the specific implementation
1579 // method for this class.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001580 Method* FindVirtualMethodForInterface(Method* method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001581
Ian Rogers466bb252011-10-14 03:29:56 -07001582 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001583
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001584 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1585
Brian Carlstrom30b94452011-08-25 21:35:26 -07001586 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001587 if (method->IsDirect()) {
1588 return method;
1589 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001590 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001591 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001592 }
1593 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001594 }
1595
Ian Rogers466bb252011-10-14 03:29:56 -07001596 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001597
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001598 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1599
Ian Rogers466bb252011-10-14 03:29:56 -07001600 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001601
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001602 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001603
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001604 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1605
1606 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1607
1608 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1609
1610 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001611
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001612 int32_t GetIfTableCount() const {
1613 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1614 if (iftable == NULL) {
1615 return 0;
1616 }
1617 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001618 }
1619
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001620 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001621 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001622 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001623 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1624 }
1625
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001626 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001627 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001628 }
1629
1630 // Get instance fields
1631 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001632 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001633 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001634 }
1635
1636 void SetIFields(ObjectArray<Field>* new_ifields) {
1637 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1638 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001639 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001640 }
1641
1642 size_t NumInstanceFields() const {
1643 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1644 }
1645
1646 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1647 DCHECK_NE(NumInstanceFields(), 0U);
1648 return GetIFields()->Get(i);
1649 }
1650
1651 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1652 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1653 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1654 ifields->Set(i, f);
1655 }
1656
1657 // Returns the number of instance fields containing reference types.
1658 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001659 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001660 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001661 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001662 }
1663
1664 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001665 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001666 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001667 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001668 }
1669
1670 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001671 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001672 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001673 }
1674
1675 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001676 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001677 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001678 }
1679
1680 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1681
1682 // Beginning of static field data
1683 static MemberOffset FieldsOffset() {
1684 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1685 }
1686
1687 // Returns the number of static fields containing reference types.
1688 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001689 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001690 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001691 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001692 }
1693
1694 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001695 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001696 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001697 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001698 }
1699
1700 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001701 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001702 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001703 }
1704
1705 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001706 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001707 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001708 }
1709
1710 void SetSFields(ObjectArray<Field>* new_sfields) {
1711 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1712 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001713 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001714 }
1715
1716 size_t NumStaticFields() const {
1717 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1718 }
1719
1720 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1721 return GetSFields()->Get(i);
1722 }
1723
1724 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1725 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1726 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1727 sfields->Set(i, f);
1728 }
1729
1730 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001731 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001732 }
1733
1734 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1735
Ian Rogersb067ac22011-12-13 18:05:09 -08001736 // Find a static or instance field using the JLS resolution order
1737 Field* FindField(const StringPiece& name, const StringPiece& type);
1738
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001739 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001740 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001741
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001742 // Finds the given instance field in this class or a superclass, only searches classes that
1743 // have the same dex cache.
1744 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1745
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001746 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001747
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001748 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1749
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001750 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001751 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001752
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001753 // Finds the given static field in this class or superclass, only searches classes that
1754 // have the same dex cache.
1755 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1756
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001757 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001759 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1760
Elliott Hughesdcc24742011-09-07 14:02:44 -07001761 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001762 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001763 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1764 }
1765
Elliott Hughesdcc24742011-09-07 14:02:44 -07001766 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001767 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001768 }
1769
1770 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001771 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001772 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001773 }
1774
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001775 uint16_t GetDexTypeIndex() const {
1776 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001777 }
1778
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001779 void SetDexTypeIndex(uint16_t type_idx) {
1780 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001781 }
1782
jeffhaobdb76512011-09-07 11:43:16 -07001783 private:
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001784 void SetVerifyErrorClass(Class* klass) {
1785 CHECK(klass != NULL) << PrettyClass(this);
1786 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1787 }
1788
jeffhao5dbddee2011-09-07 16:38:26 -07001789 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001790 bool IsArrayAssignableFromArray(const Class* klass) const;
1791 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001792
Brian Carlstrom693267a2011-09-06 09:25:34 -07001793 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001794 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001795
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001796 // For array classes, the component class object for instanceof/checkcast
1797 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001798 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001799
Elliott Hughes81ff3182012-03-23 20:35:56 -07001800 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
1801 // runtime such as arrays and primitive classes).
Brian Carlstrom693267a2011-09-06 09:25:34 -07001802 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001803
1804 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001805 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001806
Brian Carlstrom693267a2011-09-06 09:25:34 -07001807 // instance fields
1808 //
1809 // These describe the layout of the contents of an Object.
1810 // Note that only the fields directly declared by this class are
1811 // listed in ifields; fields declared by a superclass are listed in
1812 // the superclass's Class.ifields.
1813 //
1814 // All instance fields that refer to objects are guaranteed to be at
1815 // the beginning of the field list. num_reference_instance_fields_
1816 // specifies the number of reference fields.
1817 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001818
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001819 // Interface table (iftable_), one entry per interface supported by
1820 // this class. That means one entry for each interface we support
1821 // directly, indirectly via superclass, or indirectly via
1822 // superinterface. This will be null if neither we nor our
1823 // superclass implement any interfaces.
1824 //
1825 // Why we need this: given "class Foo implements Face", declare
1826 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1827 // is part of the Face interface. We can't easily use a single
1828 // vtable.
1829 //
1830 // For every interface a concrete class implements, we create an array
1831 // of the concrete vtable_ methods for the methods in the interface.
1832 ObjectArray<InterfaceEntry>* iftable_;
1833
Ian Rogersd418eda2012-01-30 12:14:28 -08001834 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1835 String* name_;
1836
Brian Carlstromdbc05252011-09-09 01:59:59 -07001837 // Static fields
1838 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001839
Ian Rogersd418eda2012-01-30 12:14:28 -08001840 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001841 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001842
Brian Carlstromdbc05252011-09-09 01:59:59 -07001843 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001844 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001845
Brian Carlstromdbc05252011-09-09 01:59:59 -07001846 // virtual methods defined in this class; invoked through vtable
1847 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001848
Ian Rogers9074b992011-10-26 17:41:55 -07001849 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1850 // copied in, and virtual methods from our class either replace those from the super or are
1851 // appended. For abstract classes, methods may be created in the vtable that aren't in
1852 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001853 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001854
Brian Carlstromdbc05252011-09-09 01:59:59 -07001855 // access flags; low 16 bits are defined by VM spec
1856 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001857
Jesse Wilson6bf19152011-09-29 13:12:33 -04001858 // Total size of the Class instance; used when allocating storage on gc heap.
1859 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001860 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001861
Elliott Hughes5f791332011-09-15 17:45:30 -07001862 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001863 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001864
Ian Rogersd418eda2012-01-30 12:14:28 -08001865 // type index from dex file
1866 // TODO: really 16bits
1867 uint32_t dex_type_idx_;
1868
Brian Carlstromdbc05252011-09-09 01:59:59 -07001869 // number of instance fields that are object refs
1870 size_t num_reference_instance_fields_;
1871
1872 // number of static fields that are object refs
1873 size_t num_reference_static_fields_;
1874
1875 // Total object size; used when allocating storage on gc heap.
1876 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001877 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001878 size_t object_size_;
1879
Ian Rogersd418eda2012-01-30 12:14:28 -08001880 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001881 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001882
1883 // Bitmap of offsets of ifields.
1884 uint32_t reference_instance_offsets_;
1885
1886 // Bitmap of offsets of sfields.
1887 uint32_t reference_static_offsets_;
1888
Brian Carlstrom693267a2011-09-06 09:25:34 -07001889 // state of class initialization
1890 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001891
Brian Carlstrom693267a2011-09-06 09:25:34 -07001892 // TODO: ?
1893 // initiating class loader list
1894 // NOTE: for classes with low serialNumber, these are unused, and the
1895 // values are kept in a table in gDvm.
1896 // InitiatingLoaderList initiating_loader_list_;
1897
Brian Carlstrom4873d462011-08-21 15:23:39 -07001898 // Location of first static field.
1899 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001900
Brian Carlstrom693267a2011-09-06 09:25:34 -07001901 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001902 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001903};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001904
Elliott Hughes1f359b02011-07-17 14:27:17 -07001905std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001906
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001907inline void Object::SetClass(Class* new_klass) {
1908 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001909 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001910}
1911
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001912inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001913 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001914 DCHECK(GetClass() != NULL);
1915 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001916}
1917
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001918inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001919 Class* java_lang_Class = GetClass()->GetClass();
1920 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001921}
1922
1923inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001924 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001925}
1926
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001927template<class T>
1928inline ObjectArray<T>* Object::AsObjectArray() {
1929 DCHECK(IsObjectArray());
1930 return down_cast<ObjectArray<T>*>(this);
1931}
1932
1933template<class T>
1934inline const ObjectArray<T>* Object::AsObjectArray() const {
1935 DCHECK(IsObjectArray());
1936 return down_cast<const ObjectArray<T>*>(this);
1937}
1938
Brian Carlstromb63ec392011-08-27 17:38:27 -07001939inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001940 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001941}
1942
Brian Carlstroma663ea52011-08-19 23:33:41 -07001943inline bool Object::IsField() const {
1944 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001945 Class* java_lang_reflect_Field =
1946 java_lang_Class->GetInstanceField(0)->GetClass();
1947 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07001948}
1949
1950inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07001951 Class* c = GetClass();
1952 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001953}
1954
1955inline bool Object::IsReferenceInstance() const {
1956 return GetClass()->IsReferenceClass();
1957}
1958
1959inline bool Object::IsWeakReferenceInstance() const {
1960 return GetClass()->IsWeakReferenceClass();
1961}
1962
1963inline bool Object::IsSoftReferenceInstance() const {
1964 return GetClass()->IsSoftReferenceClass();
1965}
1966
1967inline bool Object::IsFinalizerReferenceInstance() const {
1968 return GetClass()->IsFinalizerReferenceClass();
1969}
1970
1971inline bool Object::IsPhantomReferenceInstance() const {
1972 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07001973}
1974
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001975inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001976 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07001977 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001978 result = AsArray()->SizeOf();
1979 } else if (IsClass()) {
1980 result = AsClass()->SizeOf();
1981 } else {
1982 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001983 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001984 DCHECK(!IsField() || result == sizeof(Field));
1985 DCHECK(!IsMethod() || result == sizeof(Method));
1986 return result;
1987}
1988
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001989inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001990 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001991 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001992 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001993 return result;
1994}
1995
1996inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001997 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001998}
1999
2000inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002001 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08002002 DCHECK(result != NULL) << this;
2003 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002004 return result;
2005}
2006
2007inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002008 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002009}
2010
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002011inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002012 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08002013 size_t component_size = GetClass()->GetComponentSize();
2014 int32_t component_count = GetLength();
2015 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
2016 size_t data_size = component_count * component_size;
2017 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002018}
2019
2020template<class T>
2021void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002022 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002023 if (object != NULL) {
2024 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002025 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002026 ThrowArrayStoreException(object);
2027 return;
2028 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002029 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08002030 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002031 SetFieldObject(data_offset, object, false);
2032 }
2033}
2034
2035template<class T>
2036void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2037 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002038 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002039 SetFieldObject(data_offset, object, false);
2040}
2041
2042template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002043T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2044 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002045 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002046 return GetFieldObject<T*>(data_offset, false);
2047}
2048
2049template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002050void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2051 ObjectArray<T>* dst, int dst_pos,
2052 size_t length) {
2053 if (src->IsValidIndex(src_pos) &&
2054 src->IsValidIndex(src_pos+length-1) &&
2055 dst->IsValidIndex(dst_pos) &&
2056 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002057 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2058 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002059 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002060 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002061 if (array_class == src->GetClass()) {
2062 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002063 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002064 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002065 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002066 // directly set field, we do a bulk write barrier at the end
2067 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002068 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2069 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2070 }
2071 } else {
2072 Class* element_class = array_class->GetComponentType();
2073 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002074 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002075 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002076 if (object != NULL && !object->InstanceOf(element_class)) {
2077 dst->ThrowArrayStoreException(object);
2078 return;
2079 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002080 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002081 // directly set field, we do a bulk write barrier at the end
2082 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002083 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2084 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2085 }
2086 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002087 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002088 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002089}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002090
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002091class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002092 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002093 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002094 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002095 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002096 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2097};
2098
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002099class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002100 private:
2101 CharArray* ASCII_;
2102 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002103 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002104 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002105 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002106 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2107};
2108
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002109class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002110 private:
2111 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002112 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002113 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2114};
2115
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002116class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002117 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002118 Object* ORDER_BY_SIGNATURE_;
2119 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002120 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2121};
2122
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002123template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002124class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002125 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002126 typedef T ElementType;
2127
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002128 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002129
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002130 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002131 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2132 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002133 }
2134
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002135 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002136 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2137 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002138 }
2139
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002140 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002141 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002142 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002143 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002144 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002145 }
2146
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002147 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002148 // TODO: ArrayStoreException
2149 if (IsValidIndex(i)) {
2150 GetData()[i] = value;
2151 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002152 }
2153
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002154 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002155 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002156 CHECK(array_class != NULL);
2157 array_class_ = array_class;
2158 }
2159
Brian Carlstroma663ea52011-08-19 23:33:41 -07002160 static void ResetArrayClass() {
2161 CHECK(array_class_ != NULL);
2162 array_class_ = NULL;
2163 }
2164
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002165 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002166 static Class* array_class_;
2167
Carl Shapirof88c9522011-08-06 15:47:38 -07002168 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002169};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002170
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002171// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002172class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002173 public:
buzbeefc9e6fa2012-03-23 15:14:29 -07002174
2175 static MemberOffset CountOffset() {
2176 return OFFSET_OF_OBJECT_MEMBER(String, count_);
2177 }
2178
2179 static MemberOffset ValueOffset() {
2180 return OFFSET_OF_OBJECT_MEMBER(String, array_);
2181 }
2182
2183 static MemberOffset OffsetOffset() {
2184 return OFFSET_OF_OBJECT_MEMBER(String, offset_);
2185 }
2186
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002187 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002188 const CharArray* result = GetFieldObject<const CharArray*>(
buzbeefc9e6fa2012-03-23 15:14:29 -07002189 ValueOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002190 DCHECK(result != NULL);
2191 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002192 }
2193
Elliott Hughes814e4032011-08-23 12:07:56 -07002194 int32_t GetOffset() const {
buzbeefc9e6fa2012-03-23 15:14:29 -07002195 int32_t result = GetField32(OffsetOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002196 DCHECK_LE(0, result);
2197 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002198 }
2199
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002200 int32_t GetLength() const;
2201
Brian Carlstrom395520e2011-09-25 19:35:00 -07002202 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002203
2204 void ComputeHashCode() {
2205 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002206 }
2207
Elliott Hughes814e4032011-08-23 12:07:56 -07002208 int32_t GetUtfLength() const {
jeffhao0ce13152012-03-27 19:45:50 -07002209 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002210 }
2211
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002212 uint16_t CharAt(int32_t index) const;
2213
Brian Carlstromc74255f2011-09-11 22:47:39 -07002214 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002215
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002216 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002217 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002218 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002219
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002220 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002221
Jesse Wilson8989d992011-08-02 13:39:42 -07002222 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002223 const char* utf8_data_in);
2224
2225 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2226
2227 static String* Alloc(Class* java_lang_String, CharArray* array);
2228
2229 bool Equals(const char* modified_utf8) const;
2230
2231 // TODO: do we need this overload? give it a more intention-revealing name.
2232 bool Equals(const StringPiece& modified_utf8) const;
2233
2234 bool Equals(const String* that) const;
2235
Ian Rogers0571d352011-11-03 19:51:38 -07002236 // Compare UTF-16 code point values not in a locale-sensitive manner
2237 int Compare(int32_t utf16_length, const char* utf8_data_in);
2238
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002239 // TODO: do we need this overload? give it a more intention-revealing name.
2240 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2241 int32_t that_length) const;
2242
2243 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2244 std::string ToModifiedUtf8() const;
2245
2246 static Class* GetJavaLangString() {
2247 DCHECK(java_lang_String_ != NULL);
2248 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002249 }
2250
Brian Carlstroma663ea52011-08-19 23:33:41 -07002251 static void SetClass(Class* java_lang_String);
2252 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002253
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002254 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002255 void SetHashCode(int32_t new_hash_code) {
2256 DCHECK_EQ(0u,
2257 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2258 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2259 new_hash_code, false);
2260 }
2261
2262 void SetCount(int32_t new_count) {
2263 DCHECK_LE(0, new_count);
2264 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2265 }
2266
2267 void SetOffset(int32_t new_offset) {
2268 DCHECK_LE(0, new_offset);
2269 DCHECK_GE(GetLength(), new_offset);
2270 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2271 }
2272
2273 void SetArray(CharArray* new_array) {
2274 DCHECK(new_array != NULL);
2275 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2276 }
2277
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002278 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2279 CharArray* array_;
2280
Brian Carlstromdbc05252011-09-09 01:59:59 -07002281 int32_t count_;
2282
Carl Shapirof88c9522011-08-06 15:47:38 -07002283 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002284
Elliott Hughes289da822011-08-16 10:11:20 -07002285 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002286
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002287 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002288
Brian Carlstrom693267a2011-09-06 09:25:34 -07002289 friend struct StringOffsets; // for verifying offset information
jeffhao0ce13152012-03-27 19:45:50 -07002290 FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002291 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002292};
2293
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002294struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002295 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002296 return string->GetHashCode();
2297 }
2298};
2299
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002300inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002301 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002302 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2303}
2304
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002305inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002306 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002307 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002308}
2309
2310inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002311 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002312 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002313}
2314
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002315inline uint32_t Class::GetAccessFlags() const {
2316 // Check class is loaded or this is java.lang.String that has a
2317 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002318 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002319 this == String::GetJavaLangString() ||
2320 this == Field::GetJavaLangReflectField() ||
2321 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002322 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002323 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2324}
2325
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002326inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002327 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002328 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2329}
2330
2331inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002332 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002333 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002334}
2335
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002336inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002337 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002338 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002339}
2340
Elliott Hughes76e36942012-03-16 13:44:56 -07002341inline void Method::AssertPcIsWithinCode(uintptr_t pc) const {
Elliott Hughes67d92002012-03-26 15:08:51 -07002342 if (!kIsDebugBuild) {
2343 return;
2344 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002345 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
2346 return;
2347 }
2348 Runtime* runtime = Runtime::Current();
2349 if (GetCode() == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
2350 return;
2351 }
2352 DCHECK(IsWithinCode(pc))
2353 << PrettyMethod(this)
2354 << " pc=" << std::hex << pc
2355 << " code=" << GetCode()
2356 << " size=" << GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002357}
2358
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002359inline String* Class::GetName() const {
2360 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002361}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002362inline void Class::SetName(String* name) {
2363 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002364}
2365
2366// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002367class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002368 public:
2369 void SetDetailMessage(String* new_detail_message) {
2370 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2371 new_detail_message, false);
2372 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002373 String* GetDetailMessage() const {
2374 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2375 }
Ian Rogers9074b992011-10-26 17:41:55 -07002376 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002377
Ian Rogers1c5eb702012-02-01 09:18:34 -08002378 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2379 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2380 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2381 void SetCause(Throwable* cause);
Ian Rogers466bb252011-10-14 03:29:56 -07002382 bool IsCheckedException() const;
Ian Rogers5167c972012-02-03 10:41:20 -08002383
2384 static Class* GetJavaLangThrowable() {
2385 DCHECK(java_lang_Throwable_ != NULL);
2386 return java_lang_Throwable_;
2387 }
2388
2389 static void SetClass(Class* java_lang_Throwable);
2390 static void ResetClass();
2391
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002392 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002393 Object* GetStackState() const {
2394 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2395 }
2396
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002397 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2398 Throwable* cause_;
2399 String* detail_message_;
2400 Object* stack_state_; // Note this is Java volatile:
2401 Object* stack_trace_;
2402 Object* suppressed_exceptions_;
2403
Ian Rogers5167c972012-02-03 10:41:20 -08002404 static Class* java_lang_Throwable_;
2405
Brian Carlstrom693267a2011-09-06 09:25:34 -07002406 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002407 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2408};
2409
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002410// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002411class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002412 public:
2413 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002414 return GetFieldObject<const String*>(
2415 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002416 }
2417
2418 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002419 return GetFieldObject<const String*>(
2420 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002421 }
2422
2423 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002424 return GetFieldObject<const String*>(
2425 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002426 }
2427
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002428 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002429 return GetField32(
2430 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002431 }
2432
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002433 static StackTraceElement* Alloc(String* declaring_class,
2434 String* method_name,
2435 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002436 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002437
2438 static void SetClass(Class* java_lang_StackTraceElement);
2439
2440 static void ResetClass();
2441
2442 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002443 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002444 String* declaring_class_;
2445 String* file_name_;
2446 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002447 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002448
2449 static Class* GetStackTraceElement() {
2450 DCHECK(java_lang_StackTraceElement_ != NULL);
2451 return java_lang_StackTraceElement_;
2452 }
2453
2454 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002455
2456 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002457 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2458};
2459
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002460class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002461 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002462 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002463 Class* interface = Get(kInterface)->AsClass();
2464 DCHECK(interface != NULL);
2465 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002466 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002467
Brian Carlstrom30b94452011-08-25 21:35:26 -07002468 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002469 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002470 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002471 DCHECK(Get(kInterface) == NULL);
2472 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002473 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002474
Brian Carlstrom86927212011-09-15 11:31:11 -07002475 size_t GetMethodArrayCount() const {
2476 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002477 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002478 return 0;
2479 }
2480 return method_array->GetLength();
2481 }
2482
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002483 ObjectArray<Method>* GetMethodArray() const {
2484 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2485 DCHECK(method_array != NULL);
2486 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002487 }
2488
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002489 void SetMethodArray(ObjectArray<Method>* new_ma) {
2490 DCHECK(new_ma != NULL);
2491 DCHECK(Get(kMethodArray) == NULL);
2492 Set(kMethodArray, new_ma);
2493 }
2494
2495 static size_t LengthAsArray() {
2496 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002497 }
2498
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002499 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002500
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002501 enum ArrayIndex {
2502 // Points to the interface class.
2503 kInterface = 0,
2504 // Method pointers into the vtable, allow fast map from interface
2505 // method index to concrete instance method.
2506 kMethodArray = 1,
2507 kMax = 2,
2508 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002509
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002510 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002511};
2512
Ian Rogersc2b44472011-12-14 21:17:17 -08002513class MANAGED SynthesizedProxyClass : public Class {
2514 public:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002515 ObjectArray<Class>* GetInterfaces() {
2516 return interfaces_;
2517 }
2518
Ian Rogersc2b44472011-12-14 21:17:17 -08002519 ObjectArray<ObjectArray<Class> >* GetThrows() {
2520 return throws_;
2521 }
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002522
Jesse Wilson95caa792011-10-12 18:14:17 -04002523 private:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002524 ObjectArray<Class>* interfaces_;
Ian Rogersc2b44472011-12-14 21:17:17 -08002525 ObjectArray<ObjectArray<Class> >* throws_;
2526 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002527};
2528
2529class MANAGED Proxy : public Object {
2530 private:
2531 Object* h_;
2532
2533 friend struct ProxyOffsets; // for verifying offset information
2534 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2535};
2536
Carl Shapiro1fb86202011-06-27 17:43:13 -07002537} // namespace art
2538
2539#endif // ART_SRC_OBJECT_H_