blob: 7413bf980ea8816178b4ad0ceb7021d56a20c0d4 [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 {
Elliott Hughes1d878f32012-04-11 15:17:54 -070063 // We default initialize JValue instances to all-zeros.
64 JValue() : j(0) {}
65
Carl Shapiro3ee755d2011-06-28 12:11:04 -070066 uint8_t z;
67 int8_t b;
68 uint16_t c;
69 int16_t s;
70 int32_t i;
71 int64_t j;
72 float f;
73 double d;
74 Object* l;
75};
76
Logan Chienfca7e872011-12-20 20:08:22 +080077#if defined(ART_USE_LLVM_COMPILER)
78namespace compiler_llvm {
79 class InferredRegCategoryMap;
80}
81#endif
82
Brian Carlstrombe977852011-07-19 14:54:54 -070083static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
84static const uint32_t kAccPrivate = 0x0002; // field, method, ic
85static const uint32_t kAccProtected = 0x0004; // field, method, ic
86static const uint32_t kAccStatic = 0x0008; // field, method, ic
87static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
88static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -070089static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -070090static const uint32_t kAccVolatile = 0x0040; // field
91static const uint32_t kAccBridge = 0x0040; // method (1.5)
92static const uint32_t kAccTransient = 0x0080; // field
93static const uint32_t kAccVarargs = 0x0080; // method (1.5)
94static const uint32_t kAccNative = 0x0100; // method
95static const uint32_t kAccInterface = 0x0200; // class, ic
96static const uint32_t kAccAbstract = 0x0400; // class, method, ic
97static const uint32_t kAccStrict = 0x0800; // method
98static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
99static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
100static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700101
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700102static const uint32_t kAccMiranda = 0x8000; // method
103
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700104static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
105
Elliott Hughes582a7d12011-10-10 18:38:42 -0700106static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
107static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800108static const uint32_t kAccClassIsProxy = 0x00040000; // class (dex only)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700109static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700110
Elliott Hughes20cde902011-10-04 17:37:27 -0700111// Special runtime-only flags.
112// Note: if only kAccClassIsReference is set, we have a soft reference.
113static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
114static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
115static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
116static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
117static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700118
119static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
120 | kAccClassIsWeakReference
121 | kAccClassIsFinalizerReference
122 | kAccClassIsPhantomReference);
123
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700124/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700125 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126 */
127/*
128 * A magic value for refOffsets. Ignore the bits and walk the super
129 * chain when this is the value.
130 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
131 * fields followed by 2 ref instance fields.]
132 */
133#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
135#define CLASS_OFFSET_ALIGNMENT 4
136#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
137/*
138 * Given an offset, return the bit number which would encode that offset.
139 * Local use only.
140 */
141#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700142 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700143 CLASS_OFFSET_ALIGNMENT)
144/*
145 * Is the given offset too large to be encoded?
146 */
147#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
148 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
149/*
150 * Return a single bit, encoding the offset.
151 * Undefined if the offset is too large, as defined above.
152 */
153#define CLASS_BIT_FROM_OFFSET(byteOffset) \
154 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
155/*
156 * Return an offset, given a bit number as returned from CLZ.
157 */
158#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700159 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700160
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161#define OFFSET_OF_OBJECT_MEMBER(type, field) \
162 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700163
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700164// Classes shared with the managed side of the world need to be packed
165// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700166#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700167
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700169class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700170 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700171 static MemberOffset ClassOffset() {
172 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700173 }
174
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700175 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700176 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700177 }
178
179 void SetClass(Class* new_klass);
180
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700181 bool InstanceOf(const Class* klass) const;
182
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700183 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700184
Elliott Hughes081be7f2011-09-18 16:50:26 -0700185 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700186
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 static MemberOffset MonitorOffset() {
188 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
189 }
190
Elliott Hughes5f791332011-09-15 17:45:30 -0700191 volatile int32_t* GetRawLockWordAddress() {
192 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
193 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
194 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700195 }
196
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700197 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198
Elliott Hughes5f791332011-09-15 17:45:30 -0700199 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200
Ian Rogersff1ed472011-09-20 13:46:24 -0700201 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202
Elliott Hughes5f791332011-09-15 17:45:30 -0700203 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204
Elliott Hughes5f791332011-09-15 17:45:30 -0700205 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700206
Elliott Hughes5f791332011-09-15 17:45:30 -0700207 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700208
Elliott Hughes5f791332011-09-15 17:45:30 -0700209 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700210
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700211 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212
213 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700214 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700215 return down_cast<Class*>(this);
216 }
217
218 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700219 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220 return down_cast<const Class*>(this);
221 }
222
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700223 bool IsObjectArray() const;
224
225 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700226 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700228 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700229 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700230
Brian Carlstromb63ec392011-08-27 17:38:27 -0700231 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700232
233 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700234 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700235 return down_cast<Array*>(this);
236 }
237
238 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700239 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700240 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241 }
242
Elliott Hughesdbb40792011-11-18 17:05:22 -0800243 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700244
245 bool IsMethod() const;
246
247 Method* AsMethod() {
248 DCHECK(IsMethod());
249 return down_cast<Method*>(this);
250 }
251
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252 const Method* AsMethod() const {
253 DCHECK(IsMethod());
254 return down_cast<const Method*>(this);
255 }
256
Brian Carlstroma663ea52011-08-19 23:33:41 -0700257 bool IsField() const;
258
259 Field* AsField() {
260 DCHECK(IsField());
261 return down_cast<Field*>(this);
262 }
263
Brian Carlstrom4873d462011-08-21 15:23:39 -0700264 const Field* AsField() const {
265 DCHECK(IsField());
266 return down_cast<const Field*>(this);
267 }
268
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 bool IsReferenceInstance() const;
270
271 bool IsWeakReferenceInstance() const;
272
273 bool IsSoftReferenceInstance() const;
274
275 bool IsFinalizerReferenceInstance() const;
276
277 bool IsPhantomReferenceInstance() const;
278
279 // Accessors for Java type fields
280 template<class T>
281 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700282 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
283 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800284 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 return result;
286 }
287
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700288 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800289 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700290 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
291 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800292 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294 }
295
296 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800297 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700298 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
299 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700300 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700301 return android_atomic_acquire_load(word_addr);
302 } else {
303 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305 }
306
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700307 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
308 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800309 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700310 }
311 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700312 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700313 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700314 /*
315 * TODO: add an android_atomic_synchronization_store() function and
316 * use it in the 32-bit volatile set handlers. On some platforms we
317 * can use a fast atomic instruction and avoid the barriers.
318 */
319 ANDROID_MEMBAR_STORE();
320 *word_addr = new_value;
321 ANDROID_MEMBAR_FULL();
322 } else {
323 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700324 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 }
326
327 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800328 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700329 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700330 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700331 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700332 uint64_t result = QuasiAtomicRead64(addr);
333 ANDROID_MEMBAR_FULL();
334 return result;
335 } else {
336 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338 }
339
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700340 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800341 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700342 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
343 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700344 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700345 ANDROID_MEMBAR_STORE();
346 QuasiAtomicSwap64(new_value, addr);
347 // Post-store barrier not required due to use of atomic op or mutex.
348 } else {
349 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351 }
352
353 protected:
354 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 template<class T>
356 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700357 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358 }
359
360 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800361 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
362 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363 }
364
365 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700366 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700367
Elliott Hughes5f791332011-09-15 17:45:30 -0700368 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700369
Elliott Hughes5f791332011-09-15 17:45:30 -0700370 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700371 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700372 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700373};
374
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700375struct ObjectIdentityHash {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800376 size_t operator()(const Object* const& obj) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700377#ifdef MOVING_GARBAGE_COLLECTOR
378 // TODO: we'll need to use the Object's internal concept of identity
379 UNIMPLEMENTED(FATAL);
380#endif
381 return reinterpret_cast<size_t>(obj);
382 }
383};
384
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500386class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700387 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700389
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 void SetDeclaringClass(Class *new_declaring_class);
391
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 uint32_t GetAccessFlags() const;
393
394 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700395 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700396 }
397
Elliott Hughes80609252011-09-23 17:24:51 -0700398 bool IsPublic() const {
399 return (GetAccessFlags() & kAccPublic) != 0;
400 }
401
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700402 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700403 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404 }
405
jeffhaobdb76512011-09-07 11:43:16 -0700406 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700407 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700408 }
409
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800410 uint32_t GetDexFieldIndex() const {
411 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
412 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700413
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800414 void SetDexFieldIndex(uint32_t new_idx) {
415 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
416 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417
418 // Offset to field within an Object
419 MemberOffset GetOffset() const;
420
buzbee34cd9e52011-09-08 14:31:52 -0700421 static MemberOffset OffsetOffset() {
422 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
423 }
424
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 MemberOffset GetOffsetDuringLinking() const;
426
427 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700428
Brian Carlstrom4873d462011-08-21 15:23:39 -0700429 // field access, null object for static fields
430 bool GetBoolean(const Object* object) const;
431 void SetBoolean(Object* object, bool z) const;
432 int8_t GetByte(const Object* object) const;
433 void SetByte(Object* object, int8_t b) const;
434 uint16_t GetChar(const Object* object) const;
435 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700436 int16_t GetShort(const Object* object) const;
437 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700438 int32_t GetInt(const Object* object) const;
439 void SetInt(Object* object, int32_t i) const;
440 int64_t GetLong(const Object* object) const;
441 void SetLong(Object* object, int64_t j) const;
442 float GetFloat(const Object* object) const;
443 void SetFloat(Object* object, float f) const;
444 double GetDouble(const Object* object) const;
445 void SetDouble(Object* object, double d) const;
446 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700447 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700448
Ian Rogersce9eca62011-10-07 17:11:03 -0700449 // raw field accesses
450 uint32_t Get32(const Object* object) const;
451 void Set32(Object* object, uint32_t new_value) const;
452 uint64_t Get64(const Object* object) const;
453 void Set64(Object* object, uint64_t new_value) const;
454 Object* GetObj(const Object* object) const;
455 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 static Class* GetJavaLangReflectField() {
458 DCHECK(java_lang_reflect_Field_ != NULL);
459 return java_lang_reflect_Field_;
460 }
461
462 static void SetClass(Class* java_lang_reflect_Field);
463 static void ResetClass();
464
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700465 bool IsVolatile() const {
466 return (GetAccessFlags() & kAccVolatile) != 0;
467 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700468
buzbee1da522d2011-09-04 11:22:20 -0700469 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400470 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800471 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400472 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700473
Jesse Wilson35baaab2011-08-10 16:18:03 -0400474 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700475
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800476 // Dex cache index of field id
477 uint32_t field_dex_idx_;
478
Brian Carlstrom693267a2011-09-06 09:25:34 -0700479 // Offset of field within an instance or in the Class' static fields
480 uint32_t offset_;
481
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700482 static Class* java_lang_reflect_Field_;
483
Brian Carlstrom693267a2011-09-06 09:25:34 -0700484 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400485 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700486};
487
Jesse Wilson6bf19152011-09-29 13:12:33 -0400488// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500489class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700490 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700491 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700492 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700493 Object* obj,
494 Thread* thread,
Elliott Hughes77405792012-03-15 15:22:12 -0700495 JValue* args,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700496 JValue* result);
497
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700498 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700499
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700500 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700501
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700502 static MemberOffset DeclaringClassOffset() {
503 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700504 }
505
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700506 uint32_t GetAccessFlags() const;
507
508 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700509 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700510 }
511
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 // Returns true if the method is declared public.
513 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700514 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700515 }
516
517 // Returns true if the method is declared private.
518 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700519 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700520 }
521
522 // Returns true if the method is declared static.
523 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700524 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700525 }
526
Brian Carlstrom1f870082011-08-23 16:02:11 -0700527 // Returns true if the method is a constructor.
528 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700529 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700530 }
531
532 // Returns true if the method is static, private, or a constructor.
533 bool IsDirect() const {
Brian Carlstromf5822582012-03-19 22:34:31 -0700534 return IsDirect(GetAccessFlags());
535 }
536
537 static bool IsDirect(uint32_t access_flags) {
538 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700539 }
540
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700541 // Returns true if the method is declared synchronized.
542 bool IsSynchronized() const {
543 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700544 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700545 }
546
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700547 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700548 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700549 }
550
Elliott Hughes80609252011-09-23 17:24:51 -0700551 bool IsMiranda() const {
552 return (GetAccessFlags() & kAccMiranda) != 0;
553 }
554
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700555 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700556 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557 }
558
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700559 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700560 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700561 }
562
563 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700564 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700565 }
566
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800567 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700568
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700569 uint16_t GetMethodIndex() const;
570
571 size_t GetVtableIndex() const {
572 return GetMethodIndex();
573 }
574
575 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700576 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700577 }
578
579 static MemberOffset MethodIndexOffset() {
580 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
581 }
582
583 uint32_t GetCodeItemOffset() const {
584 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
585 }
586
587 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700588 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700589 }
590
591 // Number of 32bit registers that would be required to hold all the arguments
592 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700593
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800594 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700595
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800596 void SetDexMethodIndex(uint32_t new_idx) {
597 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700598 }
599
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700600 ObjectArray<String>* GetDexCacheStrings() const;
601 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
602
603 static MemberOffset DexCacheStringsOffset() {
604 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
605 }
606
Ian Rogers19846512012-02-24 11:42:47 -0800607 static MemberOffset DexCacheResolvedMethodsOffset() {
608 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
609 }
610
buzbee2a475e72011-09-07 17:19:17 -0700611 static MemberOffset DexCacheResolvedTypesOffset() {
612 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
613 }
614
buzbee1da522d2011-09-04 11:22:20 -0700615 static MemberOffset DexCacheInitializedStaticStorageOffset() {
616 return OFFSET_OF_OBJECT_MEMBER(Method,
617 dex_cache_initialized_static_storage_);
618 }
619
Ian Rogers19846512012-02-24 11:42:47 -0800620 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
621 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
622
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700623 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
624 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
625
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700626 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
627 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
628
Ian Rogers466bb252011-10-14 03:29:56 -0700629 // Find the method that this method overrides
630 Method* FindOverriddenMethod() const;
631
Elliott Hughes77405792012-03-15 15:22:12 -0700632 void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700633
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700634 const void* GetCode() const {
635 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700636 }
637
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700638 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700639 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
640 }
641
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700642 uint32_t GetCodeSize() const {
643 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
644 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
645 if (code == 0) {
646 return 0;
647 }
648 // TODO: make this Thumb2 specific
649 code &= ~0x1;
650 return reinterpret_cast<uint32_t*>(code)[-1];
651 }
652
653 bool IsWithinCode(uintptr_t pc) const {
654 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
655 if (code == 0) {
656 return pc == 0;
657 }
658 return (code <= pc && pc < code + GetCodeSize());
659 }
660
661 void AssertPcIsWithinCode(uintptr_t pc) const;
662
Brian Carlstrome24fa612011-09-29 00:53:55 -0700663 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700664 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700665 return reinterpret_cast<uint32_t>(GetCode());
666 }
667
668 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700669 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700670 SetCode(reinterpret_cast<void*>(code_offset));
671 }
672
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700673 static MemberOffset GetCodeOffset() {
674 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700675 }
676
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700677 const uint32_t* GetMappingTable() const {
678 const uint32_t* map = GetMappingTableRaw();
679 if (map == NULL) {
680 return map;
681 }
682 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700683 }
684
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700685 uint32_t GetMappingTableLength() const {
686 const uint32_t* map = GetMappingTableRaw();
687 if (map == NULL) {
688 return 0;
689 }
690 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700691 }
692
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700693 const uint32_t* GetMappingTableRaw() const {
694 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700695 }
696
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700697 void SetMappingTable(const uint32_t* mapping_table) {
698 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
699 mapping_table, false);
700 }
701
702 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700703 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700704 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
705 }
706
707 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700708 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700709 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
710 }
711
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800712 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700713 const uint16_t* GetVmapTableRaw() const {
714 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
715 }
716
717 void SetVmapTable(const uint16_t* vmap_table) {
718 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
719 }
720
721 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700722 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700723 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
724 }
725
726 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700727 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700728 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700729 }
730
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800731 const uint8_t* GetGcMap() const {
732 const uint8_t* gc_map_raw = GetGcMapRaw();
733 if (gc_map_raw == NULL) {
734 return gc_map_raw;
735 }
736 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 }
738
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800739 uint32_t GetGcMapLength() const {
740 const uint8_t* gc_map_raw = GetGcMapRaw();
741 if (gc_map_raw == NULL) {
742 return 0;
743 }
744 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
745 (gc_map_raw[1] << 16) |
746 (gc_map_raw[2] << 8) |
747 (gc_map_raw[3] << 0));
748 }
749
750 const uint8_t* GetGcMapRaw() const {
751 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
752 }
753 void SetGcMap(const uint8_t* data) {
754 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
755 }
756
757 uint32_t GetOatGcMapOffset() const {
758 DCHECK(!Runtime::Current()->IsStarted());
759 return reinterpret_cast<uint32_t>(GetGcMapRaw());
760 }
761 void SetOatGcMapOffset(uint32_t gc_map_offset) {
762 DCHECK(!Runtime::Current()->IsStarted());
763 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 }
765
Shih-wei Liaod11af152011-08-23 16:02:11 -0700766 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700767 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
768 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
TDYa12728f1a142012-03-15 21:51:52 -0700769#if !defined(ART_USE_LLVM_COMPILER) // LLVM uses shadow stack instead.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700770 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
TDYa12728f1a142012-03-15 21:51:52 -0700771#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700772 return result;
773 }
774
775 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700776 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700777 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
778 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700779 }
780
Shih-wei Liaod11af152011-08-23 16:02:11 -0700781 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700783 }
784
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700785 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700786
Ian Rogers60db5ab2012-02-20 17:02:00 -0800787 void RegisterNative(Thread* self, const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700788
Ian Rogers19846512012-02-24 11:42:47 -0800789 void UnregisterNative(Thread* self);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700790
Ian Rogersb033c752011-07-20 12:22:35 -0700791 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700792 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700793 }
794
Brian Carlstrom78128a62011-09-15 17:21:19 -0700795 const void* GetNativeMethod() const {
796 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
797 }
798
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700799 // Native to managed invocation stub entry point
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700800 const InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801 InvokeStub* result = GetFieldPtr<InvokeStub*>(
802 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
803 // TODO: DCHECK(result != NULL); should be ahead of time compiled
804 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700805 }
806
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700807 void SetInvokeStub(InvokeStub* invoke_stub) {
808 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
809 invoke_stub, false);
810 }
811
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700812 uint32_t GetInvokeStubSize() const {
813 const uint32_t* invoke_stub = reinterpret_cast<const uint32_t*>(GetInvokeStub());
814 if (invoke_stub == NULL) {
815 return 0;
816 }
817 return invoke_stub[-1];
818 }
819
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700820 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700821 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700822 return reinterpret_cast<uint32_t>(GetInvokeStub());
823 }
824
825 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700826 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700827 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
828 }
829
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700830 static MemberOffset GetInvokeStubOffset() {
831 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700832 }
833
buzbee561227c2011-09-02 15:28:19 -0700834 static MemberOffset GetMethodIndexOffset() {
835 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
836 }
837
Ian Rogers90865722011-09-19 11:11:44 -0700838 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700839 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700840 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700841
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700842 void SetCoreSpillMask(uint32_t core_spill_mask) {
843 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700844 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700845 }
846
Ian Rogers90865722011-09-19 11:11:44 -0700847 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700848 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
849 }
850
851 void SetFpSpillMask(uint32_t fp_spill_mask) {
852 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700853 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700854 }
855
Ian Rogers57b86d42012-03-27 16:05:41 -0700856 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
857 // conventions for a method of managed code.
858 bool IsRuntimeMethod() const {
859 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
860 }
861
Ian Rogers90865722011-09-19 11:11:44 -0700862 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700863 bool IsCalleeSaveMethod() const {
Ian Rogers57b86d42012-03-27 16:05:41 -0700864 if (!IsRuntimeMethod()) {
865 return false;
866 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700867 Runtime* runtime = Runtime::Current();
868 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700869 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700870 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
871 result = true;
872 break;
873 }
874 }
Ian Rogers90865722011-09-19 11:11:44 -0700875 return result;
876 }
877
Ian Rogers19846512012-02-24 11:42:47 -0800878 bool IsResolutionMethod() const {
879 bool result = this == Runtime::Current()->GetResolutionMethod();
880 // Check that if we do think it is phony it looks like the resolution method
881 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
882 return result;
883 }
884
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700885 // Converts a native PC to a dex PC. TODO: this is a no-op
886 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700887 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700888
889 // Converts a dex PC to a native PC. TODO: this is a no-op
890 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700891 uintptr_t ToNativePC(const uint32_t dex_pc) const;
892
893 // Find the catch block for the given exception type and dex_pc
894 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700895
Elliott Hughes80609252011-09-23 17:24:51 -0700896 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
897
898 static Class* GetConstructorClass() {
899 return java_lang_reflect_Constructor_;
900 }
901
902 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700903 return java_lang_reflect_Method_;
904 }
905
Elliott Hughes80609252011-09-23 17:24:51 -0700906 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700907
908 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700909 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800910 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700911 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700912
Brian Carlstrom693267a2011-09-06 09:25:34 -0700913 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800914 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700915
916 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800917 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700918
919 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700920 ObjectArray<Class>* dex_cache_resolved_types_;
921
922 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
923 ObjectArray<String>* dex_cache_strings_;
924
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700925 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700926 uint32_t access_flags_;
927
928 // Compiled code associated with this method for callers from managed code.
929 // May be compiled managed code or a bridge for invoking a native method.
930 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700931
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700932 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700933 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700934
Brian Carlstrom693267a2011-09-06 09:25:34 -0700935 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700936 uint32_t core_spill_mask_;
937
938 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700939 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700940
Brian Carlstrom693267a2011-09-06 09:25:34 -0700941 // Total size in bytes of the frame
942 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700943
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800944 // Garbage collection map
945 const uint8_t* gc_map_;
946
Brian Carlstrom693267a2011-09-06 09:25:34 -0700947 // Native invocation stub entry point for calling from native to managed code.
948 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700949
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700951 const uint32_t* mapping_table_;
952
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800953 // Index into method_ids of the dex file associated with this method
954 uint32_t method_dex_index_;
955
Ian Rogers466bb252011-10-14 03:29:56 -0700956 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700957 //
Ian Rogers466bb252011-10-14 03:29:56 -0700958 // For abstract methods in an interface class, this is the offset of the method in
959 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -0800960 //
961 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700962 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700963
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700964 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -0700965 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700966
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 // When a register is promoted into a register, the spill mask holds which registers hold dex
968 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
969 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700970 const uint16_t* vmap_table_;
971
Elliott Hughes80609252011-09-23 17:24:51 -0700972 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700973 static Class* java_lang_reflect_Method_;
974
Brian Carlstrom693267a2011-09-06 09:25:34 -0700975 friend class ImageWriter; // for relocating code_ and invoke_stub_
976 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700977 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700978};
979
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700980class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700981 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700982 // A convenience for code that doesn't know the component size,
983 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700984 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700985
Brian Carlstromb63ec392011-08-27 17:38:27 -0700986 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700987
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700988 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700989
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700990 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700991 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700992 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700993
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700994 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700995 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700996 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700997 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700998
buzbeec143c552011-08-20 17:38:58 -0700999 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001000 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001001 }
1002
Ian Rogersa15e67d2012-02-28 13:51:55 -08001003 static MemberOffset DataOffset(size_t component_size) {
1004 if (component_size != sizeof(int64_t)) {
1005 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
1006 } else {
1007 // Align longs and doubles.
1008 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
1009 }
buzbeec143c552011-08-20 17:38:58 -07001010 }
1011
Ian Rogersa15e67d2012-02-28 13:51:55 -08001012 void* GetRawData(size_t component_size) {
1013 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1014 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001015 }
1016
Elliott Hughes289da822011-08-16 10:11:20 -07001017 protected:
1018 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001019 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001020 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001021 }
1022 return true;
1023 }
1024
Elliott Hughes80609252011-09-23 17:24:51 -07001025 protected:
1026 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1027 bool ThrowArrayStoreException(Object* object) const;
1028
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001029 private:
1030 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001031 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001032 // Marker for the data (used by generated code)
1033 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001034
Carl Shapirof88c9522011-08-06 15:47:38 -07001035 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001036};
1037
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001038template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001039class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001040 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001041 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001042
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001043 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001044
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001045 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001046
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001047 // Set element without bound and element type checks, to be used in limited
1048 // circumstances, such as during boot image writing
1049 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001050
Ian Rogers5d76c432011-10-31 21:42:49 -07001051 T* GetWithoutChecks(int32_t i) const;
1052
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001053 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001054 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001055 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001056
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001057 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001058
1059 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001060 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001061};
1062
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001063template<class T>
1064ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001065 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001066 if (UNLIKELY(array == NULL)) {
1067 return NULL;
1068 } else {
1069 return array->AsObjectArray<T>();
1070 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001071}
1072
1073template<class T>
1074T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001075 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001076 return NULL;
1077 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001078 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001079 return GetFieldObject<T*>(data_offset, false);
1080}
1081
1082template<class T>
1083ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1084 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1085 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1086 return new_array;
1087}
1088
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001089// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001090// provides the static storage. However, this might change to an Array
1091// to improve image sharing, so we use this type to avoid assumptions
1092// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001093class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001094
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001095// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001096class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001097 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001098
1099 // Class Status
1100 //
1101 // kStatusNotReady: If a Class cannot be found in the class table by
1102 // FindClass, it allocates an new one with AllocClass in the
1103 // kStatusNotReady and calls LoadClass. Note if it does find a
1104 // class, it may not be kStatusResolved and it will try to push it
1105 // forward toward kStatusResolved.
1106 //
1107 // kStatusIdx: LoadClass populates with Class with information from
1108 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001109 // Class value in super_class_ has not been populated. The new Class
1110 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001111 //
1112 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1113 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001114 // using ResolveClass to initialize the super_class_ and ensuring the
1115 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001116 //
1117 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001118 // shows linking is complete and fields of the Class populated by making
1119 // it kStatusResolved. Java allows circularities of the form where a super
1120 // class has a field that is of the type of the sub class. We need to be able
1121 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001122
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001123 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001124 kStatusError = -1,
1125 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001126 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001127 kStatusLoaded = 2, // DEX idx values resolved
1128 kStatusResolved = 3, // part of linking
1129 kStatusVerifying = 4, // in the process of being verified
1130 kStatusVerified = 5, // logically part of linking; done pre-init
1131 kStatusInitializing = 6, // class init in progress
1132 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001133 };
1134
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001135 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001136 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1137 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001138 }
1139
1140 void SetStatus(Status new_status);
1141
1142 // Returns true if the class has failed to link.
1143 bool IsErroneous() const {
1144 return GetStatus() == kStatusError;
1145 }
1146
1147 // Returns true if the class has been loaded.
1148 bool IsIdxLoaded() const {
1149 return GetStatus() >= kStatusIdx;
1150 }
1151
1152 // Returns true if the class has been loaded.
1153 bool IsLoaded() const {
1154 return GetStatus() >= kStatusLoaded;
1155 }
1156
1157 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001158 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001159 return GetStatus() >= kStatusResolved;
1160 }
1161
1162 // Returns true if the class has been verified.
1163 bool IsVerified() const {
1164 return GetStatus() >= kStatusVerified;
1165 }
1166
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001167 // Returns true if the class is initializing.
1168 bool IsInitializing() const {
1169 return GetStatus() >= kStatusInitializing;
1170 }
1171
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001172 // Returns true if the class is initialized.
1173 bool IsInitialized() const {
1174 return GetStatus() == kStatusInitialized;
1175 }
1176
1177 uint32_t GetAccessFlags() const;
1178
1179 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001180 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001181 }
1182
1183 // Returns true if the class is an interface.
1184 bool IsInterface() const {
1185 return (GetAccessFlags() & kAccInterface) != 0;
1186 }
1187
1188 // Returns true if the class is declared public.
1189 bool IsPublic() const {
1190 return (GetAccessFlags() & kAccPublic) != 0;
1191 }
1192
1193 // Returns true if the class is declared final.
1194 bool IsFinal() const {
1195 return (GetAccessFlags() & kAccFinal) != 0;
1196 }
1197
Elliott Hughes20cde902011-10-04 17:37:27 -07001198 bool IsFinalizable() const {
1199 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1200 }
1201
1202 void SetFinalizable() {
1203 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1204 SetAccessFlags(flags | kAccClassIsFinalizable);
1205 }
1206
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001207 // Returns true if the class is abstract.
1208 bool IsAbstract() const {
1209 return (GetAccessFlags() & kAccAbstract) != 0;
1210 }
1211
1212 // Returns true if the class is an annotation.
1213 bool IsAnnotation() const {
1214 return (GetAccessFlags() & kAccAnnotation) != 0;
1215 }
1216
1217 // Returns true if the class is synthetic.
1218 bool IsSynthetic() const {
1219 return (GetAccessFlags() & kAccSynthetic) != 0;
1220 }
1221
1222 bool IsReferenceClass() const {
1223 return (GetAccessFlags() & kAccClassIsReference) != 0;
1224 }
1225
1226 bool IsWeakReferenceClass() const {
1227 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1228 }
1229
1230 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001231 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001232 }
1233
1234 bool IsFinalizerReferenceClass() const {
1235 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1236 }
1237
1238 bool IsPhantomReferenceClass() const {
1239 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1240 }
1241
Ian Rogersd418eda2012-01-30 12:14:28 -08001242
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001243 String* GetName() const; // Returns the cached name
Ian Rogersd418eda2012-01-30 12:14:28 -08001244 void SetName(String* name); // Sets the cached name
1245 String* ComputeName(); // Computes the name, then sets the cached value
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001246
1247 bool IsProxyClass() const {
1248 // Read access flags without using getter as whether something is a proxy can be check in
1249 // any loaded state
1250 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1251 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1252 return (access_flags & kAccClassIsProxy) != 0;
1253 }
1254
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001255 Primitive::Type GetPrimitiveType() const {
1256 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1257 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001258 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1259 }
1260
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001261 void SetPrimitiveType(Primitive::Type new_type) {
1262 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001263 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001264 }
1265
1266 // Returns true if the class is a primitive type.
1267 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001268 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001269 }
1270
1271 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001272 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001273 }
1274
1275 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001276 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001277 }
1278
1279 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001280 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001281 }
1282
1283 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001284 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001285 }
1286
1287 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001288 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001289 }
1290
1291 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001292 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001293 }
1294
1295 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001296 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001297 }
1298
1299 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001300 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001301 }
1302
1303 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001304 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001305 }
1306
Ian Rogersd81871c2011-10-03 13:57:23 -07001307 // Depth of class from java.lang.Object
1308 size_t Depth() {
1309 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001310 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 depth++;
1312 }
1313 return depth;
1314 }
1315
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001316 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001317 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001318 }
1319
Elliott Hughesdbb40792011-11-18 17:05:22 -08001320 bool IsClassClass() const;
1321
1322 bool IsStringClass() const;
1323
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001324 bool IsThrowableClass() const;
1325
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001326 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001327 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001328 }
1329
1330 void SetComponentType(Class* new_component_type) {
1331 DCHECK(GetComponentType() == NULL);
1332 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001333 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001334 }
1335
1336 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001337 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001338 }
1339
1340 bool IsObjectClass() const {
1341 return !IsPrimitive() && GetSuperClass() == NULL;
1342 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001343 bool IsInstantiable() const {
1344 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1345 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001346
Brian Carlstrom1f870082011-08-23 16:02:11 -07001347 // Creates a raw object instance but does not invoke the default constructor.
1348 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001349
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001350 bool IsVariableSize() const {
1351 // Classes and arrays vary in size, and so the object_size_ field cannot
1352 // be used to get their instance size
1353 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001354 }
1355
Brian Carlstrom4873d462011-08-21 15:23:39 -07001356 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001357 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001358 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001359 }
1360
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001361 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001362 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 }
1365
Ian Rogers0571d352011-11-03 19:51:38 -07001366 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001367
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001368 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001369 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001370 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001371 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001372 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001373 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 }
1375
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 void SetObjectSize(size_t new_object_size) {
1377 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001378 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001379 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001380 }
1381
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001382 // Returns true if this class is in the same packages as that class.
1383 bool IsInSamePackage(const Class* that) const;
1384
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001385 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001386
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001387 // Returns true if this class can access that class.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001388 bool CanAccess(Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001389 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001390 }
1391
Ian Rogersf2391652011-12-14 12:50:52 -08001392 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001393 // Note that access to the class isn't checked in case the declaring class is protected and the
1394 // method has been exposed by a public sub-class
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001395 bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
Ian Rogersf2391652011-12-14 12:50:52 -08001396 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001397 if (this == access_to) {
1398 return true;
1399 }
Ian Rogersf2391652011-12-14 12:50:52 -08001400 // Public members are trivially accessible
1401 if (member_flags & kAccPublic) {
1402 return true;
1403 }
1404 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001405 if (member_flags & kAccPrivate) {
1406 return false;
1407 }
Ian Rogersf2391652011-12-14 12:50:52 -08001408 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001409 if (member_flags & kAccProtected) {
1410 if (this->IsSubClass(access_to)) {
1411 return true;
1412 }
1413 }
Ian Rogersf2391652011-12-14 12:50:52 -08001414 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001415 return this->IsInSamePackage(access_to);
1416 }
1417
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001418 bool IsSubClass(const Class* klass) const;
1419
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1421 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1422 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1423 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1424 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001425 bool IsAssignableFrom(const Class* src) const {
1426 DCHECK(src != NULL);
1427 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001428 // Can always assign to things of the same type
1429 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001430 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001432 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001433 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001434 return src->Implements(this);
1435 } else if (src->IsArrayClass()) {
1436 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001437 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001439 }
1440 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001441
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001442 Class* GetSuperClass() const {
1443 // Can only get super class for loaded classes (hack for when runtime is
1444 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001445 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001446 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001447 }
1448
1449 void SetSuperClass(Class *new_super_class) {
1450 // super class is assigned once, except during class linker initialization
1451 Class* old_super_class = GetFieldObject<Class*>(
1452 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1453 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1454 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001455 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001456 }
1457
1458 bool HasSuperClass() const {
1459 return GetSuperClass() != NULL;
1460 }
1461
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001462 static MemberOffset SuperClassOffset() {
1463 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001464 }
1465
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001466 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001467
1468 void SetClassLoader(const ClassLoader* new_cl);
1469
1470 static MemberOffset DexCacheOffset() {
1471 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1472 }
1473
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001474 enum {
1475 kDumpClassFullDetail = 1,
1476 kDumpClassClassLoader = (1 << 1),
1477 kDumpClassInitialized = (1 << 2),
1478 };
1479
Elliott Hughes4681c802011-09-25 18:04:37 -07001480 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001481
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001482 DexCache* GetDexCache() const;
1483
1484 void SetDexCache(DexCache* new_dex_cache);
1485
1486 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001487 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001488 return GetFieldObject<ObjectArray<Method>*>(
1489 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1490 }
1491
1492 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1493 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1494 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1495 DCHECK_NE(0, new_direct_methods->GetLength());
1496 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1497 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001498 }
1499
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001500 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001501 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001502 }
1503
1504 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001505 ObjectArray<Method>* direct_methods =
1506 GetFieldObject<ObjectArray<Method>*>(
1507 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1508 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001509 }
1510
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001511 // Returns the number of static, private, and constructor methods.
1512 size_t NumDirectMethods() const {
1513 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1514 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001515
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001516 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001517 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001518 return GetFieldObject<ObjectArray<Method>*>(
1519 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1520 }
1521
1522 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1523 // TODO: we reassign virtual methods to grow the table for miranda
1524 // methods.. they should really just be assigned once
1525 DCHECK_NE(0, new_virtual_methods->GetLength());
1526 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1527 new_virtual_methods, false);
1528 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001529
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001530 // Returns the number of non-inherited virtual methods.
1531 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001532 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001533 }
1534
1535 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001536 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001537 return GetVirtualMethods()->Get(i);
1538 }
1539
1540 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001541 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001542 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001543 }
1544
1545 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001546 ObjectArray<Method>* virtual_methods =
1547 GetFieldObject<ObjectArray<Method>*>(
1548 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1549 virtual_methods->Set(i, f);
1550 }
1551
1552 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001553 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001554 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001555 }
1556
1557 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001558 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001559 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001560 }
1561
1562 void SetVTable(ObjectArray<Method>* new_vtable) {
1563 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1564 }
1565
1566 static MemberOffset VTableOffset() {
1567 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001568 }
1569
Brian Carlstrom30b94452011-08-25 21:35:26 -07001570 // Given a method implemented by this class but potentially from a
1571 // super class, return the specific implementation
1572 // method for this class.
1573 Method* FindVirtualMethodForVirtual(Method* method) {
1574 DCHECK(!method->GetDeclaringClass()->IsInterface());
1575 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001576 // Use the index to a potentially overridden one for this instance's class.
1577 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001578 }
1579
1580 // Given a method implemented by this class, but potentially from a
1581 // super class or interface, return the specific implementation
1582 // method for this class.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001583 Method* FindVirtualMethodForInterface(Method* method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001584
Ian Rogers466bb252011-10-14 03:29:56 -07001585 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001586
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001587 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1588
Brian Carlstrom30b94452011-08-25 21:35:26 -07001589 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001590 if (method->IsDirect()) {
1591 return method;
1592 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001593 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001594 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001595 }
1596 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001597 }
1598
Ian Rogers466bb252011-10-14 03:29:56 -07001599 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001600
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001601 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1602
Ian Rogers466bb252011-10-14 03:29:56 -07001603 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001604
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001605 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001606
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001607 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1608
1609 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1610
1611 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1612
1613 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001614
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001615 int32_t GetIfTableCount() const {
1616 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1617 if (iftable == NULL) {
1618 return 0;
1619 }
1620 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001621 }
1622
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001623 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001624 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001625 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001626 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1627 }
1628
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001629 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001630 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001631 }
1632
1633 // Get instance fields
1634 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001635 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001636 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001637 }
1638
1639 void SetIFields(ObjectArray<Field>* new_ifields) {
1640 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1641 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001642 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001643 }
1644
1645 size_t NumInstanceFields() const {
1646 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1647 }
1648
1649 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1650 DCHECK_NE(NumInstanceFields(), 0U);
1651 return GetIFields()->Get(i);
1652 }
1653
1654 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1655 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1656 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1657 ifields->Set(i, f);
1658 }
1659
1660 // Returns the number of instance fields containing reference types.
1661 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001662 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001663 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001664 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001665 }
1666
1667 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001668 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001669 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001670 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001671 }
1672
1673 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001674 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001675 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001676 }
1677
1678 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001679 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001680 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001681 }
1682
1683 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1684
1685 // Beginning of static field data
1686 static MemberOffset FieldsOffset() {
1687 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1688 }
1689
1690 // Returns the number of static fields containing reference types.
1691 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001692 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001693 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001694 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001695 }
1696
1697 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001698 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001699 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001700 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001701 }
1702
1703 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001704 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001705 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001706 }
1707
1708 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001709 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001710 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001711 }
1712
1713 void SetSFields(ObjectArray<Field>* new_sfields) {
1714 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1715 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001716 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001717 }
1718
1719 size_t NumStaticFields() const {
1720 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1721 }
1722
1723 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1724 return GetSFields()->Get(i);
1725 }
1726
1727 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1728 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1729 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1730 sfields->Set(i, f);
1731 }
1732
1733 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001734 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001735 }
1736
1737 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1738
Ian Rogersb067ac22011-12-13 18:05:09 -08001739 // Find a static or instance field using the JLS resolution order
1740 Field* FindField(const StringPiece& name, const StringPiece& type);
1741
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001742 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001743 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001744
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001745 // Finds the given instance field in this class or a superclass, only searches classes that
1746 // have the same dex cache.
1747 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1748
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001749 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001750
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001751 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1752
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001753 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001754 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001755
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001756 // Finds the given static field in this class or superclass, only searches classes that
1757 // have the same dex cache.
1758 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1759
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001760 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001761
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001762 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1763
Elliott Hughesdcc24742011-09-07 14:02:44 -07001764 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001765 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001766 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1767 }
1768
Elliott Hughesdcc24742011-09-07 14:02:44 -07001769 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001770 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001771 }
1772
1773 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001774 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001775 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001776 }
1777
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001778 uint16_t GetDexTypeIndex() const {
1779 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001780 }
1781
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001782 void SetDexTypeIndex(uint16_t type_idx) {
1783 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001784 }
1785
jeffhaobdb76512011-09-07 11:43:16 -07001786 private:
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001787 void SetVerifyErrorClass(Class* klass) {
1788 CHECK(klass != NULL) << PrettyClass(this);
1789 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1790 }
1791
jeffhao5dbddee2011-09-07 16:38:26 -07001792 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001793 bool IsArrayAssignableFromArray(const Class* klass) const;
1794 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001795
Brian Carlstrom693267a2011-09-06 09:25:34 -07001796 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001797 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001798
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001799 // For array classes, the component class object for instanceof/checkcast
1800 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001801 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001802
Elliott Hughes81ff3182012-03-23 20:35:56 -07001803 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
1804 // runtime such as arrays and primitive classes).
Brian Carlstrom693267a2011-09-06 09:25:34 -07001805 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001806
1807 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001808 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001809
Brian Carlstrom693267a2011-09-06 09:25:34 -07001810 // instance fields
1811 //
1812 // These describe the layout of the contents of an Object.
1813 // Note that only the fields directly declared by this class are
1814 // listed in ifields; fields declared by a superclass are listed in
1815 // the superclass's Class.ifields.
1816 //
1817 // All instance fields that refer to objects are guaranteed to be at
1818 // the beginning of the field list. num_reference_instance_fields_
1819 // specifies the number of reference fields.
1820 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001821
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001822 // Interface table (iftable_), one entry per interface supported by
1823 // this class. That means one entry for each interface we support
1824 // directly, indirectly via superclass, or indirectly via
1825 // superinterface. This will be null if neither we nor our
1826 // superclass implement any interfaces.
1827 //
1828 // Why we need this: given "class Foo implements Face", declare
1829 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1830 // is part of the Face interface. We can't easily use a single
1831 // vtable.
1832 //
1833 // For every interface a concrete class implements, we create an array
1834 // of the concrete vtable_ methods for the methods in the interface.
1835 ObjectArray<InterfaceEntry>* iftable_;
1836
Ian Rogersd418eda2012-01-30 12:14:28 -08001837 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1838 String* name_;
1839
Brian Carlstromdbc05252011-09-09 01:59:59 -07001840 // Static fields
1841 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001842
Ian Rogersd418eda2012-01-30 12:14:28 -08001843 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001844 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001845
Brian Carlstromdbc05252011-09-09 01:59:59 -07001846 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001847 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001848
Brian Carlstromdbc05252011-09-09 01:59:59 -07001849 // virtual methods defined in this class; invoked through vtable
1850 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001851
Ian Rogers9074b992011-10-26 17:41:55 -07001852 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1853 // copied in, and virtual methods from our class either replace those from the super or are
1854 // appended. For abstract classes, methods may be created in the vtable that aren't in
1855 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001856 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001857
Brian Carlstromdbc05252011-09-09 01:59:59 -07001858 // access flags; low 16 bits are defined by VM spec
1859 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001860
Jesse Wilson6bf19152011-09-29 13:12:33 -04001861 // Total size of the Class instance; used when allocating storage on gc heap.
1862 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001863 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001864
Elliott Hughes5f791332011-09-15 17:45:30 -07001865 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001866 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001867
Ian Rogersd418eda2012-01-30 12:14:28 -08001868 // type index from dex file
1869 // TODO: really 16bits
1870 uint32_t dex_type_idx_;
1871
Brian Carlstromdbc05252011-09-09 01:59:59 -07001872 // number of instance fields that are object refs
1873 size_t num_reference_instance_fields_;
1874
1875 // number of static fields that are object refs
1876 size_t num_reference_static_fields_;
1877
1878 // Total object size; used when allocating storage on gc heap.
1879 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001880 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001881 size_t object_size_;
1882
Ian Rogersd418eda2012-01-30 12:14:28 -08001883 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001884 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001885
1886 // Bitmap of offsets of ifields.
1887 uint32_t reference_instance_offsets_;
1888
1889 // Bitmap of offsets of sfields.
1890 uint32_t reference_static_offsets_;
1891
Brian Carlstrom693267a2011-09-06 09:25:34 -07001892 // state of class initialization
1893 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001894
Brian Carlstrom693267a2011-09-06 09:25:34 -07001895 // TODO: ?
1896 // initiating class loader list
1897 // NOTE: for classes with low serialNumber, these are unused, and the
1898 // values are kept in a table in gDvm.
1899 // InitiatingLoaderList initiating_loader_list_;
1900
Brian Carlstrom4873d462011-08-21 15:23:39 -07001901 // Location of first static field.
1902 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001903
Brian Carlstrom693267a2011-09-06 09:25:34 -07001904 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001905 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001906};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001907
Elliott Hughes1f359b02011-07-17 14:27:17 -07001908std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001909
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001910inline void Object::SetClass(Class* new_klass) {
1911 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001912 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001913}
1914
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001915inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001916 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001917 DCHECK(GetClass() != NULL);
1918 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001919}
1920
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001921inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001922 Class* java_lang_Class = GetClass()->GetClass();
1923 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001924}
1925
1926inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001927 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001928}
1929
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001930template<class T>
1931inline ObjectArray<T>* Object::AsObjectArray() {
1932 DCHECK(IsObjectArray());
1933 return down_cast<ObjectArray<T>*>(this);
1934}
1935
1936template<class T>
1937inline const ObjectArray<T>* Object::AsObjectArray() const {
1938 DCHECK(IsObjectArray());
1939 return down_cast<const ObjectArray<T>*>(this);
1940}
1941
Brian Carlstromb63ec392011-08-27 17:38:27 -07001942inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001943 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001944}
1945
Brian Carlstroma663ea52011-08-19 23:33:41 -07001946inline bool Object::IsField() const {
1947 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001948 Class* java_lang_reflect_Field =
1949 java_lang_Class->GetInstanceField(0)->GetClass();
1950 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07001951}
1952
1953inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07001954 Class* c = GetClass();
1955 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001956}
1957
1958inline bool Object::IsReferenceInstance() const {
1959 return GetClass()->IsReferenceClass();
1960}
1961
1962inline bool Object::IsWeakReferenceInstance() const {
1963 return GetClass()->IsWeakReferenceClass();
1964}
1965
1966inline bool Object::IsSoftReferenceInstance() const {
1967 return GetClass()->IsSoftReferenceClass();
1968}
1969
1970inline bool Object::IsFinalizerReferenceInstance() const {
1971 return GetClass()->IsFinalizerReferenceClass();
1972}
1973
1974inline bool Object::IsPhantomReferenceInstance() const {
1975 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07001976}
1977
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001978inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001979 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07001980 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001981 result = AsArray()->SizeOf();
1982 } else if (IsClass()) {
1983 result = AsClass()->SizeOf();
1984 } else {
1985 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001986 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001987 DCHECK(!IsField() || result == sizeof(Field));
1988 DCHECK(!IsMethod() || result == sizeof(Method));
1989 return result;
1990}
1991
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001992inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001993 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001994 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001995 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001996 return result;
1997}
1998
1999inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002000 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002001}
2002
2003inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002004 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08002005 DCHECK(result != NULL) << this;
2006 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002007 return result;
2008}
2009
2010inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002011 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002012}
2013
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002014inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002015 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08002016 size_t component_size = GetClass()->GetComponentSize();
2017 int32_t component_count = GetLength();
2018 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
2019 size_t data_size = component_count * component_size;
2020 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002021}
2022
2023template<class T>
2024void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002025 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002026 if (object != NULL) {
2027 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002028 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002029 ThrowArrayStoreException(object);
2030 return;
2031 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002032 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08002033 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002034 SetFieldObject(data_offset, object, false);
2035 }
2036}
2037
2038template<class T>
2039void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2040 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002041 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002042 SetFieldObject(data_offset, object, false);
2043}
2044
2045template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002046T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2047 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002048 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002049 return GetFieldObject<T*>(data_offset, false);
2050}
2051
2052template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002053void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2054 ObjectArray<T>* dst, int dst_pos,
2055 size_t length) {
2056 if (src->IsValidIndex(src_pos) &&
2057 src->IsValidIndex(src_pos+length-1) &&
2058 dst->IsValidIndex(dst_pos) &&
2059 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002060 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2061 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002062 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002063 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002064 if (array_class == src->GetClass()) {
2065 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002066 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002067 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002068 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002069 // directly set field, we do a bulk write barrier at the end
2070 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002071 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2072 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2073 }
2074 } else {
2075 Class* element_class = array_class->GetComponentType();
2076 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002077 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002078 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002079 if (object != NULL && !object->InstanceOf(element_class)) {
2080 dst->ThrowArrayStoreException(object);
2081 return;
2082 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002083 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002084 // directly set field, we do a bulk write barrier at the end
2085 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002086 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2087 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2088 }
2089 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002090 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002091 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002092}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002093
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002094class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002095 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002096 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002097 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002098 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002099 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2100};
2101
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002102class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002103 private:
2104 CharArray* ASCII_;
2105 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002106 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002107 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002108 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002109 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2110};
2111
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002112class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002113 private:
2114 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002115 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002116 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2117};
2118
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002119class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002120 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002121 Object* ORDER_BY_SIGNATURE_;
2122 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002123 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2124};
2125
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002126template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002127class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002128 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002129 typedef T ElementType;
2130
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002131 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002132
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002133 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002134 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2135 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002136 }
2137
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002138 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002139 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2140 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002141 }
2142
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002143 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002144 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002145 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002146 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002147 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002148 }
2149
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002150 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002151 // TODO: ArrayStoreException
2152 if (IsValidIndex(i)) {
2153 GetData()[i] = value;
2154 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002155 }
2156
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002157 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002158 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002159 CHECK(array_class != NULL);
2160 array_class_ = array_class;
2161 }
2162
Brian Carlstroma663ea52011-08-19 23:33:41 -07002163 static void ResetArrayClass() {
2164 CHECK(array_class_ != NULL);
2165 array_class_ = NULL;
2166 }
2167
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002168 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002169 static Class* array_class_;
2170
Carl Shapirof88c9522011-08-06 15:47:38 -07002171 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002172};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002173
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002174// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002175class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002176 public:
buzbeefc9e6fa2012-03-23 15:14:29 -07002177
2178 static MemberOffset CountOffset() {
2179 return OFFSET_OF_OBJECT_MEMBER(String, count_);
2180 }
2181
2182 static MemberOffset ValueOffset() {
2183 return OFFSET_OF_OBJECT_MEMBER(String, array_);
2184 }
2185
2186 static MemberOffset OffsetOffset() {
2187 return OFFSET_OF_OBJECT_MEMBER(String, offset_);
2188 }
2189
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002190 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002191 const CharArray* result = GetFieldObject<const CharArray*>(
buzbeefc9e6fa2012-03-23 15:14:29 -07002192 ValueOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002193 DCHECK(result != NULL);
2194 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002195 }
2196
Elliott Hughes814e4032011-08-23 12:07:56 -07002197 int32_t GetOffset() const {
buzbeefc9e6fa2012-03-23 15:14:29 -07002198 int32_t result = GetField32(OffsetOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002199 DCHECK_LE(0, result);
2200 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002201 }
2202
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002203 int32_t GetLength() const;
2204
Brian Carlstrom395520e2011-09-25 19:35:00 -07002205 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002206
2207 void ComputeHashCode() {
2208 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002209 }
2210
Elliott Hughes814e4032011-08-23 12:07:56 -07002211 int32_t GetUtfLength() const {
jeffhao0ce13152012-03-27 19:45:50 -07002212 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002213 }
2214
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002215 uint16_t CharAt(int32_t index) const;
2216
Brian Carlstromc74255f2011-09-11 22:47:39 -07002217 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002218
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002219 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002220 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002221 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002222
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002223 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002224
Jesse Wilson8989d992011-08-02 13:39:42 -07002225 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002226 const char* utf8_data_in);
2227
2228 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2229
2230 static String* Alloc(Class* java_lang_String, CharArray* array);
2231
2232 bool Equals(const char* modified_utf8) const;
2233
2234 // TODO: do we need this overload? give it a more intention-revealing name.
2235 bool Equals(const StringPiece& modified_utf8) const;
2236
2237 bool Equals(const String* that) const;
2238
Ian Rogers0571d352011-11-03 19:51:38 -07002239 // Compare UTF-16 code point values not in a locale-sensitive manner
2240 int Compare(int32_t utf16_length, const char* utf8_data_in);
2241
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002242 // TODO: do we need this overload? give it a more intention-revealing name.
2243 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2244 int32_t that_length) const;
2245
2246 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2247 std::string ToModifiedUtf8() const;
2248
2249 static Class* GetJavaLangString() {
2250 DCHECK(java_lang_String_ != NULL);
2251 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002252 }
2253
Brian Carlstroma663ea52011-08-19 23:33:41 -07002254 static void SetClass(Class* java_lang_String);
2255 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002256
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002257 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002258 void SetHashCode(int32_t new_hash_code) {
2259 DCHECK_EQ(0u,
2260 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2261 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2262 new_hash_code, false);
2263 }
2264
2265 void SetCount(int32_t new_count) {
2266 DCHECK_LE(0, new_count);
2267 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2268 }
2269
2270 void SetOffset(int32_t new_offset) {
2271 DCHECK_LE(0, new_offset);
2272 DCHECK_GE(GetLength(), new_offset);
2273 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2274 }
2275
2276 void SetArray(CharArray* new_array) {
2277 DCHECK(new_array != NULL);
2278 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2279 }
2280
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002281 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2282 CharArray* array_;
2283
Brian Carlstromdbc05252011-09-09 01:59:59 -07002284 int32_t count_;
2285
Carl Shapirof88c9522011-08-06 15:47:38 -07002286 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002287
Elliott Hughes289da822011-08-16 10:11:20 -07002288 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002289
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002290 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002291
Brian Carlstrom693267a2011-09-06 09:25:34 -07002292 friend struct StringOffsets; // for verifying offset information
jeffhao0ce13152012-03-27 19:45:50 -07002293 FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002294 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002295};
2296
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002297struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002298 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002299 return string->GetHashCode();
2300 }
2301};
2302
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002303inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002304 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002305 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2306}
2307
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002308inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002309 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002310 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002311}
2312
2313inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002314 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002315 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002316}
2317
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002318inline uint32_t Class::GetAccessFlags() const {
2319 // Check class is loaded or this is java.lang.String that has a
2320 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002321 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002322 this == String::GetJavaLangString() ||
2323 this == Field::GetJavaLangReflectField() ||
2324 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002325 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002326 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2327}
2328
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002329inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002330 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002331 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2332}
2333
2334inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002335 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002336 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002337}
2338
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002339inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002340 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002341 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002342}
2343
Elliott Hughes76e36942012-03-16 13:44:56 -07002344inline void Method::AssertPcIsWithinCode(uintptr_t pc) const {
Elliott Hughes67d92002012-03-26 15:08:51 -07002345 if (!kIsDebugBuild) {
2346 return;
2347 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002348 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
2349 return;
2350 }
2351 Runtime* runtime = Runtime::Current();
2352 if (GetCode() == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
2353 return;
2354 }
2355 DCHECK(IsWithinCode(pc))
2356 << PrettyMethod(this)
2357 << " pc=" << std::hex << pc
2358 << " code=" << GetCode()
2359 << " size=" << GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002360}
2361
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002362inline String* Class::GetName() const {
2363 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002364}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002365inline void Class::SetName(String* name) {
2366 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002367}
2368
2369// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002370class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002371 public:
2372 void SetDetailMessage(String* new_detail_message) {
2373 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2374 new_detail_message, false);
2375 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002376 String* GetDetailMessage() const {
2377 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2378 }
Ian Rogers9074b992011-10-26 17:41:55 -07002379 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002380
Ian Rogers1c5eb702012-02-01 09:18:34 -08002381 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2382 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2383 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2384 void SetCause(Throwable* cause);
Ian Rogers466bb252011-10-14 03:29:56 -07002385 bool IsCheckedException() const;
Ian Rogers5167c972012-02-03 10:41:20 -08002386
2387 static Class* GetJavaLangThrowable() {
2388 DCHECK(java_lang_Throwable_ != NULL);
2389 return java_lang_Throwable_;
2390 }
2391
2392 static void SetClass(Class* java_lang_Throwable);
2393 static void ResetClass();
2394
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002395 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002396 Object* GetStackState() const {
2397 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2398 }
2399
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002400 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2401 Throwable* cause_;
2402 String* detail_message_;
2403 Object* stack_state_; // Note this is Java volatile:
2404 Object* stack_trace_;
2405 Object* suppressed_exceptions_;
2406
Ian Rogers5167c972012-02-03 10:41:20 -08002407 static Class* java_lang_Throwable_;
2408
Brian Carlstrom693267a2011-09-06 09:25:34 -07002409 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002410 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2411};
2412
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002413// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002414class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002415 public:
2416 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002417 return GetFieldObject<const String*>(
2418 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002419 }
2420
2421 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002422 return GetFieldObject<const String*>(
2423 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002424 }
2425
2426 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002427 return GetFieldObject<const String*>(
2428 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002429 }
2430
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002431 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002432 return GetField32(
2433 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002434 }
2435
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002436 static StackTraceElement* Alloc(String* declaring_class,
2437 String* method_name,
2438 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002439 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002440
2441 static void SetClass(Class* java_lang_StackTraceElement);
2442
2443 static void ResetClass();
2444
2445 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002446 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002447 String* declaring_class_;
2448 String* file_name_;
2449 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002450 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002451
2452 static Class* GetStackTraceElement() {
2453 DCHECK(java_lang_StackTraceElement_ != NULL);
2454 return java_lang_StackTraceElement_;
2455 }
2456
2457 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002458
2459 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002460 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2461};
2462
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002463class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002464 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002465 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002466 Class* interface = Get(kInterface)->AsClass();
2467 DCHECK(interface != NULL);
2468 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002469 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002470
Brian Carlstrom30b94452011-08-25 21:35:26 -07002471 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002472 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002473 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002474 DCHECK(Get(kInterface) == NULL);
2475 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002476 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002477
Brian Carlstrom86927212011-09-15 11:31:11 -07002478 size_t GetMethodArrayCount() const {
2479 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002480 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002481 return 0;
2482 }
2483 return method_array->GetLength();
2484 }
2485
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002486 ObjectArray<Method>* GetMethodArray() const {
2487 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2488 DCHECK(method_array != NULL);
2489 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002490 }
2491
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002492 void SetMethodArray(ObjectArray<Method>* new_ma) {
2493 DCHECK(new_ma != NULL);
2494 DCHECK(Get(kMethodArray) == NULL);
2495 Set(kMethodArray, new_ma);
2496 }
2497
2498 static size_t LengthAsArray() {
2499 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002500 }
2501
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002502 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002503
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002504 enum ArrayIndex {
2505 // Points to the interface class.
2506 kInterface = 0,
2507 // Method pointers into the vtable, allow fast map from interface
2508 // method index to concrete instance method.
2509 kMethodArray = 1,
2510 kMax = 2,
2511 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002512
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002513 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002514};
2515
Ian Rogersc2b44472011-12-14 21:17:17 -08002516class MANAGED SynthesizedProxyClass : public Class {
2517 public:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002518 ObjectArray<Class>* GetInterfaces() {
2519 return interfaces_;
2520 }
2521
Ian Rogersc2b44472011-12-14 21:17:17 -08002522 ObjectArray<ObjectArray<Class> >* GetThrows() {
2523 return throws_;
2524 }
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002525
Jesse Wilson95caa792011-10-12 18:14:17 -04002526 private:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002527 ObjectArray<Class>* interfaces_;
Ian Rogersc2b44472011-12-14 21:17:17 -08002528 ObjectArray<ObjectArray<Class> >* throws_;
2529 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002530};
2531
2532class MANAGED Proxy : public Object {
2533 private:
2534 Object* h_;
2535
2536 friend struct ProxyOffsets; // for verifying offset information
2537 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2538};
2539
Carl Shapiro1fb86202011-06-27 17:43:13 -07002540} // namespace art
2541
2542#endif // ART_SRC_OBJECT_H_