blob: 62d5fed3d43444e5f5bd74fdd199fb3f2be3d9ce [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070032#include "primitive.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070033#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070035#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070036#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070037
38namespace art {
39
40class Array;
41class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070042class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070043class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070044class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040045class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070046class InterfaceEntry;
47class Monitor;
48class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070049class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070050class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040051class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070052template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070053template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070054typedef PrimitiveArray<uint8_t> BooleanArray;
55typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070056typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070057typedef PrimitiveArray<double> DoubleArray;
58typedef PrimitiveArray<float> FloatArray;
59typedef PrimitiveArray<int32_t> IntArray;
60typedef PrimitiveArray<int64_t> LongArray;
61typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070062
Carl Shapiro3ee755d2011-06-28 12:11:04 -070063union JValue {
64 uint8_t z;
65 int8_t b;
66 uint16_t c;
67 int16_t s;
68 int32_t i;
69 int64_t j;
70 float f;
71 double d;
72 Object* l;
73};
74
Logan Chienfca7e872011-12-20 20:08:22 +080075#if defined(ART_USE_LLVM_COMPILER)
76namespace compiler_llvm {
77 class InferredRegCategoryMap;
78}
79#endif
80
Brian Carlstrombe977852011-07-19 14:54:54 -070081static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
82static const uint32_t kAccPrivate = 0x0002; // field, method, ic
83static const uint32_t kAccProtected = 0x0004; // field, method, ic
84static const uint32_t kAccStatic = 0x0008; // field, method, ic
85static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
86static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -070087static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -070088static const uint32_t kAccVolatile = 0x0040; // field
89static const uint32_t kAccBridge = 0x0040; // method (1.5)
90static const uint32_t kAccTransient = 0x0080; // field
91static const uint32_t kAccVarargs = 0x0080; // method (1.5)
92static const uint32_t kAccNative = 0x0100; // method
93static const uint32_t kAccInterface = 0x0200; // class, ic
94static const uint32_t kAccAbstract = 0x0400; // class, method, ic
95static const uint32_t kAccStrict = 0x0800; // method
96static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
97static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
98static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070099
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700100static const uint32_t kAccMiranda = 0x8000; // method
101
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700102static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
103
Elliott Hughes582a7d12011-10-10 18:38:42 -0700104static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
105static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106static const uint32_t kAccClassIsProxy = 0x00040000; // class (dex only)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700107static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700108
Elliott Hughes20cde902011-10-04 17:37:27 -0700109// Special runtime-only flags.
110// Note: if only kAccClassIsReference is set, we have a soft reference.
111static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
112static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
113static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
114static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
115static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700116
117static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
118 | kAccClassIsWeakReference
119 | kAccClassIsFinalizerReference
120 | kAccClassIsPhantomReference);
121
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700122/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700123 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700124 */
125/*
126 * A magic value for refOffsets. Ignore the bits and walk the super
127 * chain when this is the value.
128 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
129 * fields followed by 2 ref instance fields.]
130 */
131#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700132#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
133#define CLASS_OFFSET_ALIGNMENT 4
134#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
135/*
136 * Given an offset, return the bit number which would encode that offset.
137 * Local use only.
138 */
139#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700140 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700141 CLASS_OFFSET_ALIGNMENT)
142/*
143 * Is the given offset too large to be encoded?
144 */
145#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
146 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
147/*
148 * Return a single bit, encoding the offset.
149 * Undefined if the offset is too large, as defined above.
150 */
151#define CLASS_BIT_FROM_OFFSET(byteOffset) \
152 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
153/*
154 * Return an offset, given a bit number as returned from CLZ.
155 */
156#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700157 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159#define OFFSET_OF_OBJECT_MEMBER(type, field) \
160 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700161
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700162// Classes shared with the managed side of the world need to be packed
163// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700164#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700165
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700167class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700168 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700169 static MemberOffset ClassOffset() {
170 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700171 }
172
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700173 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700174 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700175 }
176
177 void SetClass(Class* new_klass);
178
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700179 bool InstanceOf(const Class* klass) const;
180
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700181 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700182
Elliott Hughes081be7f2011-09-18 16:50:26 -0700183 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700184
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700185 static MemberOffset MonitorOffset() {
186 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
187 }
188
Elliott Hughes5f791332011-09-15 17:45:30 -0700189 volatile int32_t* GetRawLockWordAddress() {
190 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
191 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
192 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193 }
194
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700195 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196
Elliott Hughes5f791332011-09-15 17:45:30 -0700197 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700198
Ian Rogersff1ed472011-09-20 13:46:24 -0700199 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200
Elliott Hughes5f791332011-09-15 17:45:30 -0700201 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202
Elliott Hughes5f791332011-09-15 17:45:30 -0700203 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204
Elliott Hughes5f791332011-09-15 17:45:30 -0700205 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700206
Elliott Hughes5f791332011-09-15 17:45:30 -0700207 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700208
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700209 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700210
211 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700212 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700213 return down_cast<Class*>(this);
214 }
215
216 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700217 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218 return down_cast<const Class*>(this);
219 }
220
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700221 bool IsObjectArray() const;
222
223 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700224 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700226 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700227 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228
Brian Carlstromb63ec392011-08-27 17:38:27 -0700229 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700230
231 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700232 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700233 return down_cast<Array*>(this);
234 }
235
236 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700237 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700238 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239 }
240
Elliott Hughesdbb40792011-11-18 17:05:22 -0800241 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700242
243 bool IsMethod() const;
244
245 Method* AsMethod() {
246 DCHECK(IsMethod());
247 return down_cast<Method*>(this);
248 }
249
Brian Carlstrom4873d462011-08-21 15:23:39 -0700250 const Method* AsMethod() const {
251 DCHECK(IsMethod());
252 return down_cast<const Method*>(this);
253 }
254
Brian Carlstroma663ea52011-08-19 23:33:41 -0700255 bool IsField() const;
256
257 Field* AsField() {
258 DCHECK(IsField());
259 return down_cast<Field*>(this);
260 }
261
Brian Carlstrom4873d462011-08-21 15:23:39 -0700262 const Field* AsField() const {
263 DCHECK(IsField());
264 return down_cast<const Field*>(this);
265 }
266
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 bool IsReferenceInstance() const;
268
269 bool IsWeakReferenceInstance() const;
270
271 bool IsSoftReferenceInstance() const;
272
273 bool IsFinalizerReferenceInstance() const;
274
275 bool IsPhantomReferenceInstance() const;
276
277 // Accessors for Java type fields
278 template<class T>
279 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700280 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
281 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800282 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 return result;
284 }
285
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700286 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800287 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700288 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
289 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 }
293
294 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800295 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700296 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
297 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700298 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700299 return android_atomic_acquire_load(word_addr);
300 } else {
301 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 }
304
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700305 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
306 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800307 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700308 }
309 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700310 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700311 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700312 /*
313 * TODO: add an android_atomic_synchronization_store() function and
314 * use it in the 32-bit volatile set handlers. On some platforms we
315 * can use a fast atomic instruction and avoid the barriers.
316 */
317 ANDROID_MEMBAR_STORE();
318 *word_addr = new_value;
319 ANDROID_MEMBAR_FULL();
320 } else {
321 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 }
324
325 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800326 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700327 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700328 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700329 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700330 uint64_t result = QuasiAtomicRead64(addr);
331 ANDROID_MEMBAR_FULL();
332 return result;
333 } else {
334 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
337
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700338 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800339 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700340 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
341 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700342 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700343 ANDROID_MEMBAR_STORE();
344 QuasiAtomicSwap64(new_value, addr);
345 // Post-store barrier not required due to use of atomic op or mutex.
346 } else {
347 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 }
350
351 protected:
352 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 template<class T>
354 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700355 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 }
357
358 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800359 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
360 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361 }
362
363 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700364 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700365
Elliott Hughes5f791332011-09-15 17:45:30 -0700366 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700367
Elliott Hughes5f791332011-09-15 17:45:30 -0700368 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700369 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700370 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700371};
372
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373struct ObjectIdentityHash {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800374 size_t operator()(const Object* const& obj) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700375#ifdef MOVING_GARBAGE_COLLECTOR
376 // TODO: we'll need to use the Object's internal concept of identity
377 UNIMPLEMENTED(FATAL);
378#endif
379 return reinterpret_cast<size_t>(obj);
380 }
381};
382
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500384class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700385 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 void SetDeclaringClass(Class *new_declaring_class);
389
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 uint32_t GetAccessFlags() const;
391
392 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700393 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700394 }
395
Elliott Hughes80609252011-09-23 17:24:51 -0700396 bool IsPublic() const {
397 return (GetAccessFlags() & kAccPublic) != 0;
398 }
399
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700400 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700401 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700402 }
403
jeffhaobdb76512011-09-07 11:43:16 -0700404 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700405 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700406 }
407
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 uint32_t GetDexFieldIndex() const {
409 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
410 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700411
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 void SetDexFieldIndex(uint32_t new_idx) {
413 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
414 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700415
416 // Offset to field within an Object
417 MemberOffset GetOffset() const;
418
buzbee34cd9e52011-09-08 14:31:52 -0700419 static MemberOffset OffsetOffset() {
420 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
421 }
422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 MemberOffset GetOffsetDuringLinking() const;
424
425 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700426
Brian Carlstrom4873d462011-08-21 15:23:39 -0700427 // field access, null object for static fields
428 bool GetBoolean(const Object* object) const;
429 void SetBoolean(Object* object, bool z) const;
430 int8_t GetByte(const Object* object) const;
431 void SetByte(Object* object, int8_t b) const;
432 uint16_t GetChar(const Object* object) const;
433 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700434 int16_t GetShort(const Object* object) const;
435 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700436 int32_t GetInt(const Object* object) const;
437 void SetInt(Object* object, int32_t i) const;
438 int64_t GetLong(const Object* object) const;
439 void SetLong(Object* object, int64_t j) const;
440 float GetFloat(const Object* object) const;
441 void SetFloat(Object* object, float f) const;
442 double GetDouble(const Object* object) const;
443 void SetDouble(Object* object, double d) const;
444 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700445 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700446
Ian Rogersce9eca62011-10-07 17:11:03 -0700447 // raw field accesses
448 uint32_t Get32(const Object* object) const;
449 void Set32(Object* object, uint32_t new_value) const;
450 uint64_t Get64(const Object* object) const;
451 void Set64(Object* object, uint64_t new_value) const;
452 Object* GetObj(const Object* object) const;
453 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700454
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 static Class* GetJavaLangReflectField() {
456 DCHECK(java_lang_reflect_Field_ != NULL);
457 return java_lang_reflect_Field_;
458 }
459
460 static void SetClass(Class* java_lang_reflect_Field);
461 static void ResetClass();
462
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 bool IsVolatile() const {
464 return (GetAccessFlags() & kAccVolatile) != 0;
465 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700466
buzbee1da522d2011-09-04 11:22:20 -0700467 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400468 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400470 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700471
Jesse Wilson35baaab2011-08-10 16:18:03 -0400472 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700473
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 // Dex cache index of field id
475 uint32_t field_dex_idx_;
476
Brian Carlstrom693267a2011-09-06 09:25:34 -0700477 // Offset of field within an instance or in the Class' static fields
478 uint32_t offset_;
479
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700480 static Class* java_lang_reflect_Field_;
481
Brian Carlstrom693267a2011-09-06 09:25:34 -0700482 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400483 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700484};
485
Jesse Wilson6bf19152011-09-29 13:12:33 -0400486// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500487class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700488 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700489 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700490 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700491 Object* obj,
492 Thread* thread,
Elliott Hughes77405792012-03-15 15:22:12 -0700493 JValue* args,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700494 JValue* result);
495
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700496 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700497
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700498 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700499
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700500 static MemberOffset DeclaringClassOffset() {
501 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700502 }
503
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700504 uint32_t GetAccessFlags() const;
505
506 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700507 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700508 }
509
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510 // Returns true if the method is declared public.
511 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700512 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700513 }
514
515 // Returns true if the method is declared private.
516 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700517 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700518 }
519
520 // Returns true if the method is declared static.
521 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700522 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700523 }
524
Brian Carlstrom1f870082011-08-23 16:02:11 -0700525 // Returns true if the method is a constructor.
526 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700527 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700528 }
529
530 // Returns true if the method is static, private, or a constructor.
531 bool IsDirect() const {
Brian Carlstromf5822582012-03-19 22:34:31 -0700532 return IsDirect(GetAccessFlags());
533 }
534
535 static bool IsDirect(uint32_t access_flags) {
536 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700537 }
538
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700539 // Returns true if the method is declared synchronized.
540 bool IsSynchronized() const {
541 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543 }
544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700545 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700546 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700547 }
548
Elliott Hughes80609252011-09-23 17:24:51 -0700549 bool IsMiranda() const {
550 return (GetAccessFlags() & kAccMiranda) != 0;
551 }
552
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700553 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700555 }
556
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700558 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700559 }
560
561 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700562 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700563 }
564
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800565 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700566
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700567 uint16_t GetMethodIndex() const;
568
569 size_t GetVtableIndex() const {
570 return GetMethodIndex();
571 }
572
573 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700574 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700575 }
576
577 static MemberOffset MethodIndexOffset() {
578 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
579 }
580
581 uint32_t GetCodeItemOffset() const {
582 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
583 }
584
585 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700586 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700587 }
588
589 // Number of 32bit registers that would be required to hold all the arguments
590 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700591
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800592 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700593
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800594 void SetDexMethodIndex(uint32_t new_idx) {
595 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700596 }
597
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700598 ObjectArray<String>* GetDexCacheStrings() const;
599 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
600
601 static MemberOffset DexCacheStringsOffset() {
602 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
603 }
604
Ian Rogers19846512012-02-24 11:42:47 -0800605 static MemberOffset DexCacheResolvedMethodsOffset() {
606 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
607 }
608
buzbee2a475e72011-09-07 17:19:17 -0700609 static MemberOffset DexCacheResolvedTypesOffset() {
610 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
611 }
612
buzbee1da522d2011-09-04 11:22:20 -0700613 static MemberOffset DexCacheInitializedStaticStorageOffset() {
614 return OFFSET_OF_OBJECT_MEMBER(Method,
615 dex_cache_initialized_static_storage_);
616 }
617
Ian Rogers19846512012-02-24 11:42:47 -0800618 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
619 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
620
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
622 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
623
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700624 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
625 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
626
Ian Rogers466bb252011-10-14 03:29:56 -0700627 // Find the method that this method overrides
628 Method* FindOverriddenMethod() const;
629
Elliott Hughes77405792012-03-15 15:22:12 -0700630 void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700631
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700632 const void* GetCode() const {
633 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700634 }
635
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700636 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700637 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
638 }
639
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700640 uint32_t GetCodeSize() const {
641 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
642 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
643 if (code == 0) {
644 return 0;
645 }
646 // TODO: make this Thumb2 specific
647 code &= ~0x1;
648 return reinterpret_cast<uint32_t*>(code)[-1];
649 }
650
651 bool IsWithinCode(uintptr_t pc) const {
652 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
653 if (code == 0) {
654 return pc == 0;
655 }
656 return (code <= pc && pc < code + GetCodeSize());
657 }
658
659 void AssertPcIsWithinCode(uintptr_t pc) const;
660
Brian Carlstrome24fa612011-09-29 00:53:55 -0700661 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700662 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700663 return reinterpret_cast<uint32_t>(GetCode());
664 }
665
666 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700667 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700668 SetCode(reinterpret_cast<void*>(code_offset));
669 }
670
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700671 static MemberOffset GetCodeOffset() {
672 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700673 }
674
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700675 const uint32_t* GetMappingTable() const {
676 const uint32_t* map = GetMappingTableRaw();
677 if (map == NULL) {
678 return map;
679 }
680 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700681 }
682
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700683 uint32_t GetMappingTableLength() const {
684 const uint32_t* map = GetMappingTableRaw();
685 if (map == NULL) {
686 return 0;
687 }
688 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700689 }
690
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700691 const uint32_t* GetMappingTableRaw() const {
692 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700693 }
694
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700695 void SetMappingTable(const uint32_t* mapping_table) {
696 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
697 mapping_table, false);
698 }
699
700 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700701 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700702 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
703 }
704
705 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700706 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700707 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
708 }
709
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800710 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700711 const uint16_t* GetVmapTableRaw() const {
712 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
713 }
714
715 void SetVmapTable(const uint16_t* vmap_table) {
716 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
717 }
718
719 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700720 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700721 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
722 }
723
724 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700725 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700726 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700727 }
728
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800729 const uint8_t* GetGcMap() const {
730 const uint8_t* gc_map_raw = GetGcMapRaw();
731 if (gc_map_raw == NULL) {
732 return gc_map_raw;
733 }
734 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 }
736
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800737 uint32_t GetGcMapLength() const {
738 const uint8_t* gc_map_raw = GetGcMapRaw();
739 if (gc_map_raw == NULL) {
740 return 0;
741 }
742 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
743 (gc_map_raw[1] << 16) |
744 (gc_map_raw[2] << 8) |
745 (gc_map_raw[3] << 0));
746 }
747
748 const uint8_t* GetGcMapRaw() const {
749 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
750 }
751 void SetGcMap(const uint8_t* data) {
752 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
753 }
754
755 uint32_t GetOatGcMapOffset() const {
756 DCHECK(!Runtime::Current()->IsStarted());
757 return reinterpret_cast<uint32_t>(GetGcMapRaw());
758 }
759 void SetOatGcMapOffset(uint32_t gc_map_offset) {
760 DCHECK(!Runtime::Current()->IsStarted());
761 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 }
763
Logan Chienfca7e872011-12-20 20:08:22 +0800764#if defined(ART_USE_LLVM_COMPILER)
765 // NOTE: In order not to change the Oat file format, we are reusing the
766 // gc_map_ field, so be careful while altering the GC map related code.
767
768 const compiler_llvm::InferredRegCategoryMap* GetInferredRegCategoryMap() const;
769
770 void SetInferredRegCategoryMap(const compiler_llvm::InferredRegCategoryMap* map);
771 void ResetInferredRegCategoryMap();
772#endif
773
Shih-wei Liaod11af152011-08-23 16:02:11 -0700774 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700775 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
776 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700777 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
778 return result;
779 }
780
781 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700782 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800783#if !defined(ART_USE_LLVM_COMPILER) // LLVM uses shadow stack instead.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700784 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800785#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700786 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
787 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700788 }
789
Shih-wei Liaod11af152011-08-23 16:02:11 -0700790 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700792 }
793
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700794 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700795
Ian Rogers60db5ab2012-02-20 17:02:00 -0800796 void RegisterNative(Thread* self, const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700797
Ian Rogers19846512012-02-24 11:42:47 -0800798 void UnregisterNative(Thread* self);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700799
Ian Rogersb033c752011-07-20 12:22:35 -0700800 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700802 }
803
Brian Carlstrom78128a62011-09-15 17:21:19 -0700804 const void* GetNativeMethod() const {
805 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
806 }
807
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700808 // Native to managed invocation stub entry point
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700809 const InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700810 InvokeStub* result = GetFieldPtr<InvokeStub*>(
811 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
812 // TODO: DCHECK(result != NULL); should be ahead of time compiled
813 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700814 }
815
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700816 void SetInvokeStub(InvokeStub* invoke_stub) {
817 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
818 invoke_stub, false);
819 }
820
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700821 uint32_t GetInvokeStubSize() const {
822 const uint32_t* invoke_stub = reinterpret_cast<const uint32_t*>(GetInvokeStub());
823 if (invoke_stub == NULL) {
824 return 0;
825 }
826 return invoke_stub[-1];
827 }
828
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700829 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700830 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700831 return reinterpret_cast<uint32_t>(GetInvokeStub());
832 }
833
834 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700835 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700836 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
837 }
838
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700839 static MemberOffset GetInvokeStubOffset() {
840 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700841 }
842
buzbee561227c2011-09-02 15:28:19 -0700843 static MemberOffset GetMethodIndexOffset() {
844 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
845 }
846
Ian Rogers90865722011-09-19 11:11:44 -0700847 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700848 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700849 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700850
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700851 void SetCoreSpillMask(uint32_t core_spill_mask) {
852 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700853 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 }
855
Ian Rogers90865722011-09-19 11:11:44 -0700856 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700857 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
858 }
859
860 void SetFpSpillMask(uint32_t fp_spill_mask) {
861 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700862 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700863 }
864
Ian Rogers90865722011-09-19 11:11:44 -0700865 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700866 bool IsCalleeSaveMethod() const {
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 // Check that if we do think it is phony it looks like the callee save method
Ian Rogers19846512012-02-24 11:42:47 -0800876 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
Ian Rogers90865722011-09-19 11:11:44 -0700877 return result;
878 }
879
Ian Rogers19846512012-02-24 11:42:47 -0800880 bool IsResolutionMethod() const {
881 bool result = this == Runtime::Current()->GetResolutionMethod();
882 // Check that if we do think it is phony it looks like the resolution method
883 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
884 return result;
885 }
886
887 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
888 // conventions for a method of managed code.
889 bool IsRuntimeMethod() const {
890 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
891 }
892
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700893 // Converts a native PC to a dex PC. TODO: this is a no-op
894 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700895 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700896
897 // Converts a dex PC to a native PC. TODO: this is a no-op
898 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700899 uintptr_t ToNativePC(const uint32_t dex_pc) const;
900
901 // Find the catch block for the given exception type and dex_pc
902 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700903
Elliott Hughes80609252011-09-23 17:24:51 -0700904 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
905
906 static Class* GetConstructorClass() {
907 return java_lang_reflect_Constructor_;
908 }
909
910 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700911 return java_lang_reflect_Method_;
912 }
913
Elliott Hughes80609252011-09-23 17:24:51 -0700914 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700915
916 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700917 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800918 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700919 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700920
Brian Carlstrom693267a2011-09-06 09:25:34 -0700921 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800922 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700923
924 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800925 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700926
927 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700928 ObjectArray<Class>* dex_cache_resolved_types_;
929
930 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
931 ObjectArray<String>* dex_cache_strings_;
932
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700933 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700934 uint32_t access_flags_;
935
936 // Compiled code associated with this method for callers from managed code.
937 // May be compiled managed code or a bridge for invoking a native method.
938 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700939
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700940 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700941 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700942
Brian Carlstrom693267a2011-09-06 09:25:34 -0700943 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700944 uint32_t core_spill_mask_;
945
946 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700947 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700948
Brian Carlstrom693267a2011-09-06 09:25:34 -0700949 // Total size in bytes of the frame
950 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700951
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800952 // Garbage collection map
953 const uint8_t* gc_map_;
954
Brian Carlstrom693267a2011-09-06 09:25:34 -0700955 // Native invocation stub entry point for calling from native to managed code.
956 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700957
Ian Rogersd81871c2011-10-03 13:57:23 -0700958 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700959 const uint32_t* mapping_table_;
960
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800961 // Index into method_ids of the dex file associated with this method
962 uint32_t method_dex_index_;
963
Ian Rogers466bb252011-10-14 03:29:56 -0700964 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700965 //
Ian Rogers466bb252011-10-14 03:29:56 -0700966 // For abstract methods in an interface class, this is the offset of the method in
967 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -0800968 //
969 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700970 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700971
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700972 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -0700973 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700974
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 // When a register is promoted into a register, the spill mask holds which registers hold dex
976 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
977 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700978 const uint16_t* vmap_table_;
979
Elliott Hughes80609252011-09-23 17:24:51 -0700980 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700981 static Class* java_lang_reflect_Method_;
982
Brian Carlstrom693267a2011-09-06 09:25:34 -0700983 friend class ImageWriter; // for relocating code_ and invoke_stub_
984 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700985 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700986};
987
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700988class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700989 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700990 // A convenience for code that doesn't know the component size,
991 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700992 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700993
Brian Carlstromb63ec392011-08-27 17:38:27 -0700994 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700995
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700996 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700997
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700998 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700999 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001000 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001001
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001002 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001003 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001004 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001005 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001006
buzbeec143c552011-08-20 17:38:58 -07001007 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001008 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001009 }
1010
Ian Rogersa15e67d2012-02-28 13:51:55 -08001011 static MemberOffset DataOffset(size_t component_size) {
1012 if (component_size != sizeof(int64_t)) {
1013 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
1014 } else {
1015 // Align longs and doubles.
1016 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
1017 }
buzbeec143c552011-08-20 17:38:58 -07001018 }
1019
Ian Rogersa15e67d2012-02-28 13:51:55 -08001020 void* GetRawData(size_t component_size) {
1021 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1022 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001023 }
1024
Elliott Hughes289da822011-08-16 10:11:20 -07001025 protected:
1026 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001027 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001028 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001029 }
1030 return true;
1031 }
1032
Elliott Hughes80609252011-09-23 17:24:51 -07001033 protected:
1034 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1035 bool ThrowArrayStoreException(Object* object) const;
1036
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001037 private:
1038 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001039 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001040 // Marker for the data (used by generated code)
1041 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001042
Carl Shapirof88c9522011-08-06 15:47:38 -07001043 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001044};
1045
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001046template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001047class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001048 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001049 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001050
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001051 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001052
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001053 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001054
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001055 // Set element without bound and element type checks, to be used in limited
1056 // circumstances, such as during boot image writing
1057 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001058
Ian Rogers5d76c432011-10-31 21:42:49 -07001059 T* GetWithoutChecks(int32_t i) const;
1060
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001061 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001062 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001063 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001064
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001065 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001066
1067 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001068 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001069};
1070
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001071template<class T>
1072ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001073 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001074 if (UNLIKELY(array == NULL)) {
1075 return NULL;
1076 } else {
1077 return array->AsObjectArray<T>();
1078 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001079}
1080
1081template<class T>
1082T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001083 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001084 return NULL;
1085 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001086 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001087 return GetFieldObject<T*>(data_offset, false);
1088}
1089
1090template<class T>
1091ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1092 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1093 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1094 return new_array;
1095}
1096
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001097// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001098// provides the static storage. However, this might change to an Array
1099// to improve image sharing, so we use this type to avoid assumptions
1100// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001101class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001102
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001103// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001104class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001105 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001106
1107 // Class Status
1108 //
1109 // kStatusNotReady: If a Class cannot be found in the class table by
1110 // FindClass, it allocates an new one with AllocClass in the
1111 // kStatusNotReady and calls LoadClass. Note if it does find a
1112 // class, it may not be kStatusResolved and it will try to push it
1113 // forward toward kStatusResolved.
1114 //
1115 // kStatusIdx: LoadClass populates with Class with information from
1116 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001117 // Class value in super_class_ has not been populated. The new Class
1118 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001119 //
1120 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1121 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001122 // using ResolveClass to initialize the super_class_ and ensuring the
1123 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001124 //
1125 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001126 // shows linking is complete and fields of the Class populated by making
1127 // it kStatusResolved. Java allows circularities of the form where a super
1128 // class has a field that is of the type of the sub class. We need to be able
1129 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001130
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001131 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001132 kStatusError = -1,
1133 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001134 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001135 kStatusLoaded = 2, // DEX idx values resolved
1136 kStatusResolved = 3, // part of linking
1137 kStatusVerifying = 4, // in the process of being verified
1138 kStatusVerified = 5, // logically part of linking; done pre-init
1139 kStatusInitializing = 6, // class init in progress
1140 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001141 };
1142
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001143 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001144 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1145 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001146 }
1147
1148 void SetStatus(Status new_status);
1149
1150 // Returns true if the class has failed to link.
1151 bool IsErroneous() const {
1152 return GetStatus() == kStatusError;
1153 }
1154
1155 // Returns true if the class has been loaded.
1156 bool IsIdxLoaded() const {
1157 return GetStatus() >= kStatusIdx;
1158 }
1159
1160 // Returns true if the class has been loaded.
1161 bool IsLoaded() const {
1162 return GetStatus() >= kStatusLoaded;
1163 }
1164
1165 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001166 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001167 return GetStatus() >= kStatusResolved;
1168 }
1169
1170 // Returns true if the class has been verified.
1171 bool IsVerified() const {
1172 return GetStatus() >= kStatusVerified;
1173 }
1174
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001175 // Returns true if the class is initializing.
1176 bool IsInitializing() const {
1177 return GetStatus() >= kStatusInitializing;
1178 }
1179
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001180 // Returns true if the class is initialized.
1181 bool IsInitialized() const {
1182 return GetStatus() == kStatusInitialized;
1183 }
1184
1185 uint32_t GetAccessFlags() const;
1186
1187 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001188 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001189 }
1190
1191 // Returns true if the class is an interface.
1192 bool IsInterface() const {
1193 return (GetAccessFlags() & kAccInterface) != 0;
1194 }
1195
1196 // Returns true if the class is declared public.
1197 bool IsPublic() const {
1198 return (GetAccessFlags() & kAccPublic) != 0;
1199 }
1200
1201 // Returns true if the class is declared final.
1202 bool IsFinal() const {
1203 return (GetAccessFlags() & kAccFinal) != 0;
1204 }
1205
Elliott Hughes20cde902011-10-04 17:37:27 -07001206 bool IsFinalizable() const {
1207 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1208 }
1209
1210 void SetFinalizable() {
1211 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1212 SetAccessFlags(flags | kAccClassIsFinalizable);
1213 }
1214
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001215 // Returns true if the class is abstract.
1216 bool IsAbstract() const {
1217 return (GetAccessFlags() & kAccAbstract) != 0;
1218 }
1219
1220 // Returns true if the class is an annotation.
1221 bool IsAnnotation() const {
1222 return (GetAccessFlags() & kAccAnnotation) != 0;
1223 }
1224
1225 // Returns true if the class is synthetic.
1226 bool IsSynthetic() const {
1227 return (GetAccessFlags() & kAccSynthetic) != 0;
1228 }
1229
1230 bool IsReferenceClass() const {
1231 return (GetAccessFlags() & kAccClassIsReference) != 0;
1232 }
1233
1234 bool IsWeakReferenceClass() const {
1235 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1236 }
1237
1238 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001239 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001240 }
1241
1242 bool IsFinalizerReferenceClass() const {
1243 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1244 }
1245
1246 bool IsPhantomReferenceClass() const {
1247 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1248 }
1249
Ian Rogersd418eda2012-01-30 12:14:28 -08001250
1251 String* GetName() const ; // Returns the cached name
1252 void SetName(String* name); // Sets the cached name
1253 String* ComputeName(); // Computes the name, then sets the cached value
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001254
1255 bool IsProxyClass() const {
1256 // Read access flags without using getter as whether something is a proxy can be check in
1257 // any loaded state
1258 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1259 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1260 return (access_flags & kAccClassIsProxy) != 0;
1261 }
1262
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001263 Primitive::Type GetPrimitiveType() const {
1264 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1265 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001266 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1267 }
1268
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001269 void SetPrimitiveType(Primitive::Type new_type) {
1270 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001271 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001272 }
1273
1274 // Returns true if the class is a primitive type.
1275 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001276 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001277 }
1278
1279 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001280 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001281 }
1282
1283 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001284 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001285 }
1286
1287 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001288 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001289 }
1290
1291 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001292 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001293 }
1294
1295 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001296 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001297 }
1298
1299 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001300 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001301 }
1302
1303 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001304 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001305 }
1306
1307 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001308 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001309 }
1310
1311 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001312 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001313 }
1314
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 // Depth of class from java.lang.Object
1316 size_t Depth() {
1317 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001318 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 depth++;
1320 }
1321 return depth;
1322 }
1323
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001324 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001325 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001326 }
1327
Elliott Hughesdbb40792011-11-18 17:05:22 -08001328 bool IsClassClass() const;
1329
1330 bool IsStringClass() const;
1331
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001332 bool IsThrowableClass() const;
1333
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001334 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001335 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001336 }
1337
1338 void SetComponentType(Class* new_component_type) {
1339 DCHECK(GetComponentType() == NULL);
1340 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001341 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001342 }
1343
1344 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001345 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001346 }
1347
1348 bool IsObjectClass() const {
1349 return !IsPrimitive() && GetSuperClass() == NULL;
1350 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001351 bool IsInstantiable() const {
1352 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1353 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001354
Brian Carlstrom1f870082011-08-23 16:02:11 -07001355 // Creates a raw object instance but does not invoke the default constructor.
1356 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001357
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001358 bool IsVariableSize() const {
1359 // Classes and arrays vary in size, and so the object_size_ field cannot
1360 // be used to get their instance size
1361 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001362 }
1363
Brian Carlstrom4873d462011-08-21 15:23:39 -07001364 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001365 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001366 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001367 }
1368
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001369 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001370 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001371 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001372 }
1373
Ian Rogers0571d352011-11-03 19:51:38 -07001374 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001375
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001377 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001378 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001379 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001380 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001381 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382 }
1383
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001384 void SetObjectSize(size_t new_object_size) {
1385 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001386 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001387 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001388 }
1389
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001390 // Returns true if this class is in the same packages as that class.
1391 bool IsInSamePackage(const Class* that) const;
1392
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001393 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001394
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001395 // Returns true if this class can access that class.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001396 bool CanAccess(Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001397 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001398 }
1399
Ian Rogersf2391652011-12-14 12:50:52 -08001400 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001401 // Note that access to the class isn't checked in case the declaring class is protected and the
1402 // method has been exposed by a public sub-class
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001403 bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
Ian Rogersf2391652011-12-14 12:50:52 -08001404 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001405 if (this == access_to) {
1406 return true;
1407 }
Ian Rogersf2391652011-12-14 12:50:52 -08001408 // Public members are trivially accessible
1409 if (member_flags & kAccPublic) {
1410 return true;
1411 }
1412 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001413 if (member_flags & kAccPrivate) {
1414 return false;
1415 }
Ian Rogersf2391652011-12-14 12:50:52 -08001416 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001417 if (member_flags & kAccProtected) {
1418 if (this->IsSubClass(access_to)) {
1419 return true;
1420 }
1421 }
Ian Rogersf2391652011-12-14 12:50:52 -08001422 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001423 return this->IsInSamePackage(access_to);
1424 }
1425
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001426 bool IsSubClass(const Class* klass) const;
1427
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1429 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1430 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1431 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1432 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001433 bool IsAssignableFrom(const Class* src) const {
1434 DCHECK(src != NULL);
1435 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001436 // Can always assign to things of the same type
1437 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001438 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001439 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001440 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001441 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001442 return src->Implements(this);
1443 } else if (src->IsArrayClass()) {
1444 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001445 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001447 }
1448 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001449
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001450 Class* GetSuperClass() const {
1451 // Can only get super class for loaded classes (hack for when runtime is
1452 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001453 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001454 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001455 }
1456
1457 void SetSuperClass(Class *new_super_class) {
1458 // super class is assigned once, except during class linker initialization
1459 Class* old_super_class = GetFieldObject<Class*>(
1460 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1461 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1462 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001463 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001464 }
1465
1466 bool HasSuperClass() const {
1467 return GetSuperClass() != NULL;
1468 }
1469
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001470 static MemberOffset SuperClassOffset() {
1471 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001472 }
1473
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001474 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001475
1476 void SetClassLoader(const ClassLoader* new_cl);
1477
1478 static MemberOffset DexCacheOffset() {
1479 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1480 }
1481
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001482 enum {
1483 kDumpClassFullDetail = 1,
1484 kDumpClassClassLoader = (1 << 1),
1485 kDumpClassInitialized = (1 << 2),
1486 };
1487
Elliott Hughes4681c802011-09-25 18:04:37 -07001488 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001489
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001490 DexCache* GetDexCache() const;
1491
1492 void SetDexCache(DexCache* new_dex_cache);
1493
1494 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001495 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001496 return GetFieldObject<ObjectArray<Method>*>(
1497 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1498 }
1499
1500 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1501 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1502 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1503 DCHECK_NE(0, new_direct_methods->GetLength());
1504 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1505 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001506 }
1507
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001508 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001509 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001510 }
1511
1512 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001513 ObjectArray<Method>* direct_methods =
1514 GetFieldObject<ObjectArray<Method>*>(
1515 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1516 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001517 }
1518
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001519 // Returns the number of static, private, and constructor methods.
1520 size_t NumDirectMethods() const {
1521 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1522 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001523
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001524 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001525 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001526 return GetFieldObject<ObjectArray<Method>*>(
1527 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1528 }
1529
1530 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1531 // TODO: we reassign virtual methods to grow the table for miranda
1532 // methods.. they should really just be assigned once
1533 DCHECK_NE(0, new_virtual_methods->GetLength());
1534 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1535 new_virtual_methods, false);
1536 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001537
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001538 // Returns the number of non-inherited virtual methods.
1539 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001540 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001541 }
1542
1543 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001544 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001545 return GetVirtualMethods()->Get(i);
1546 }
1547
1548 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001549 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001550 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001551 }
1552
1553 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001554 ObjectArray<Method>* virtual_methods =
1555 GetFieldObject<ObjectArray<Method>*>(
1556 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1557 virtual_methods->Set(i, f);
1558 }
1559
1560 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001561 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001562 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001563 }
1564
1565 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001566 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001567 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001568 }
1569
1570 void SetVTable(ObjectArray<Method>* new_vtable) {
1571 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1572 }
1573
1574 static MemberOffset VTableOffset() {
1575 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001576 }
1577
Brian Carlstrom30b94452011-08-25 21:35:26 -07001578 // Given a method implemented by this class but potentially from a
1579 // super class, return the specific implementation
1580 // method for this class.
1581 Method* FindVirtualMethodForVirtual(Method* method) {
1582 DCHECK(!method->GetDeclaringClass()->IsInterface());
1583 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001584 // Use the index to a potentially overridden one for this instance's class.
1585 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001586 }
1587
1588 // Given a method implemented by this class, but potentially from a
1589 // super class or interface, return the specific implementation
1590 // method for this class.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001591 Method* FindVirtualMethodForInterface(Method* method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001592
Ian Rogers466bb252011-10-14 03:29:56 -07001593 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001594
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001595 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1596
Brian Carlstrom30b94452011-08-25 21:35:26 -07001597 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001598 if (method->IsDirect()) {
1599 return method;
1600 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001601 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001602 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001603 }
1604 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001605 }
1606
Ian Rogers466bb252011-10-14 03:29:56 -07001607 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001608
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001609 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1610
Ian Rogers466bb252011-10-14 03:29:56 -07001611 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001612
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001613 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001614
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001615 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1616
1617 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1618
1619 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1620
1621 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001622
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001623 int32_t GetIfTableCount() const {
1624 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1625 if (iftable == NULL) {
1626 return 0;
1627 }
1628 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001629 }
1630
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001631 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001632 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001633 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001634 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1635 }
1636
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001637 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001638 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001639 }
1640
1641 // Get instance fields
1642 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001643 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001644 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001645 }
1646
1647 void SetIFields(ObjectArray<Field>* new_ifields) {
1648 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1649 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001650 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001651 }
1652
1653 size_t NumInstanceFields() const {
1654 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1655 }
1656
1657 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1658 DCHECK_NE(NumInstanceFields(), 0U);
1659 return GetIFields()->Get(i);
1660 }
1661
1662 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1663 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1664 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1665 ifields->Set(i, f);
1666 }
1667
1668 // Returns the number of instance fields containing reference types.
1669 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001670 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001671 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001672 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001673 }
1674
1675 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001676 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001677 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001678 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001679 }
1680
1681 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001682 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001683 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001684 }
1685
1686 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001687 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001688 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001689 }
1690
1691 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1692
1693 // Beginning of static field data
1694 static MemberOffset FieldsOffset() {
1695 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1696 }
1697
1698 // Returns the number of static fields containing reference types.
1699 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001700 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001701 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001702 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001703 }
1704
1705 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001706 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001707 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001708 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001709 }
1710
1711 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001712 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001713 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001714 }
1715
1716 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001717 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001718 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001719 }
1720
1721 void SetSFields(ObjectArray<Field>* new_sfields) {
1722 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1723 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001724 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001725 }
1726
1727 size_t NumStaticFields() const {
1728 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1729 }
1730
1731 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1732 return GetSFields()->Get(i);
1733 }
1734
1735 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1736 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1737 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1738 sfields->Set(i, f);
1739 }
1740
1741 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001742 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001743 }
1744
1745 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1746
Ian Rogersb067ac22011-12-13 18:05:09 -08001747 // Find a static or instance field using the JLS resolution order
1748 Field* FindField(const StringPiece& name, const StringPiece& type);
1749
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001750 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001751 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001752
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001753 // Finds the given instance field in this class or a superclass, only searches classes that
1754 // have the same dex cache.
1755 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1756
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001757 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001759 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1760
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001761 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001762 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001763
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001764 // Finds the given static field in this class or superclass, only searches classes that
1765 // have the same dex cache.
1766 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1767
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001768 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001769
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001770 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1771
Elliott Hughesdcc24742011-09-07 14:02:44 -07001772 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001773 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001774 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1775 }
1776
Elliott Hughesdcc24742011-09-07 14:02:44 -07001777 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001778 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001779 }
1780
1781 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001782 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001783 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001784 }
1785
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001786 uint16_t GetDexTypeIndex() const {
1787 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001788 }
1789
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001790 void SetDexTypeIndex(uint16_t type_idx) {
1791 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001792 }
1793
jeffhaobdb76512011-09-07 11:43:16 -07001794 private:
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001795 void SetVerifyErrorClass(Class* klass) {
1796 CHECK(klass != NULL) << PrettyClass(this);
1797 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1798 }
1799
jeffhao5dbddee2011-09-07 16:38:26 -07001800 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001801 bool IsArrayAssignableFromArray(const Class* klass) const;
1802 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001803
Brian Carlstrom693267a2011-09-06 09:25:34 -07001804 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001805 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001806
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001807 // For array classes, the component class object for instanceof/checkcast
1808 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001809 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001810
Brian Carlstrom693267a2011-09-06 09:25:34 -07001811 // DexCache of resolved constant pool entries
1812 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1813 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001814
1815 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001816 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001817
Brian Carlstrom693267a2011-09-06 09:25:34 -07001818 // instance fields
1819 //
1820 // These describe the layout of the contents of an Object.
1821 // Note that only the fields directly declared by this class are
1822 // listed in ifields; fields declared by a superclass are listed in
1823 // the superclass's Class.ifields.
1824 //
1825 // All instance fields that refer to objects are guaranteed to be at
1826 // the beginning of the field list. num_reference_instance_fields_
1827 // specifies the number of reference fields.
1828 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001829
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001830 // Interface table (iftable_), one entry per interface supported by
1831 // this class. That means one entry for each interface we support
1832 // directly, indirectly via superclass, or indirectly via
1833 // superinterface. This will be null if neither we nor our
1834 // superclass implement any interfaces.
1835 //
1836 // Why we need this: given "class Foo implements Face", declare
1837 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1838 // is part of the Face interface. We can't easily use a single
1839 // vtable.
1840 //
1841 // For every interface a concrete class implements, we create an array
1842 // of the concrete vtable_ methods for the methods in the interface.
1843 ObjectArray<InterfaceEntry>* iftable_;
1844
Ian Rogersd418eda2012-01-30 12:14:28 -08001845 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1846 String* name_;
1847
Brian Carlstromdbc05252011-09-09 01:59:59 -07001848 // Static fields
1849 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001850
Ian Rogersd418eda2012-01-30 12:14:28 -08001851 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001852 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001853
Brian Carlstromdbc05252011-09-09 01:59:59 -07001854 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001855 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001856
Brian Carlstromdbc05252011-09-09 01:59:59 -07001857 // virtual methods defined in this class; invoked through vtable
1858 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001859
Ian Rogers9074b992011-10-26 17:41:55 -07001860 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1861 // copied in, and virtual methods from our class either replace those from the super or are
1862 // appended. For abstract classes, methods may be created in the vtable that aren't in
1863 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001864 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001865
Brian Carlstromdbc05252011-09-09 01:59:59 -07001866 // access flags; low 16 bits are defined by VM spec
1867 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001868
Jesse Wilson6bf19152011-09-29 13:12:33 -04001869 // Total size of the Class instance; used when allocating storage on gc heap.
1870 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001871 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001872
Elliott Hughes5f791332011-09-15 17:45:30 -07001873 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001874 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001875
Ian Rogersd418eda2012-01-30 12:14:28 -08001876 // type index from dex file
1877 // TODO: really 16bits
1878 uint32_t dex_type_idx_;
1879
Brian Carlstromdbc05252011-09-09 01:59:59 -07001880 // number of instance fields that are object refs
1881 size_t num_reference_instance_fields_;
1882
1883 // number of static fields that are object refs
1884 size_t num_reference_static_fields_;
1885
1886 // Total object size; used when allocating storage on gc heap.
1887 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001888 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001889 size_t object_size_;
1890
Ian Rogersd418eda2012-01-30 12:14:28 -08001891 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001892 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001893
1894 // Bitmap of offsets of ifields.
1895 uint32_t reference_instance_offsets_;
1896
1897 // Bitmap of offsets of sfields.
1898 uint32_t reference_static_offsets_;
1899
Brian Carlstrom693267a2011-09-06 09:25:34 -07001900 // state of class initialization
1901 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001902
Brian Carlstrom693267a2011-09-06 09:25:34 -07001903 // TODO: ?
1904 // initiating class loader list
1905 // NOTE: for classes with low serialNumber, these are unused, and the
1906 // values are kept in a table in gDvm.
1907 // InitiatingLoaderList initiating_loader_list_;
1908
Brian Carlstrom4873d462011-08-21 15:23:39 -07001909 // Location of first static field.
1910 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001911
Brian Carlstrom693267a2011-09-06 09:25:34 -07001912 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001913 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001914};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001915
Elliott Hughes1f359b02011-07-17 14:27:17 -07001916std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001917
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001918inline void Object::SetClass(Class* new_klass) {
1919 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001920 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001921}
1922
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001923inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001924 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001925 DCHECK(GetClass() != NULL);
1926 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001927}
1928
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001929inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001930 Class* java_lang_Class = GetClass()->GetClass();
1931 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001932}
1933
1934inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001935 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001936}
1937
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001938template<class T>
1939inline ObjectArray<T>* Object::AsObjectArray() {
1940 DCHECK(IsObjectArray());
1941 return down_cast<ObjectArray<T>*>(this);
1942}
1943
1944template<class T>
1945inline const ObjectArray<T>* Object::AsObjectArray() const {
1946 DCHECK(IsObjectArray());
1947 return down_cast<const ObjectArray<T>*>(this);
1948}
1949
Brian Carlstromb63ec392011-08-27 17:38:27 -07001950inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001951 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001952}
1953
Brian Carlstroma663ea52011-08-19 23:33:41 -07001954inline bool Object::IsField() const {
1955 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001956 Class* java_lang_reflect_Field =
1957 java_lang_Class->GetInstanceField(0)->GetClass();
1958 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07001959}
1960
1961inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07001962 Class* c = GetClass();
1963 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001964}
1965
1966inline bool Object::IsReferenceInstance() const {
1967 return GetClass()->IsReferenceClass();
1968}
1969
1970inline bool Object::IsWeakReferenceInstance() const {
1971 return GetClass()->IsWeakReferenceClass();
1972}
1973
1974inline bool Object::IsSoftReferenceInstance() const {
1975 return GetClass()->IsSoftReferenceClass();
1976}
1977
1978inline bool Object::IsFinalizerReferenceInstance() const {
1979 return GetClass()->IsFinalizerReferenceClass();
1980}
1981
1982inline bool Object::IsPhantomReferenceInstance() const {
1983 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07001984}
1985
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001986inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001987 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07001988 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001989 result = AsArray()->SizeOf();
1990 } else if (IsClass()) {
1991 result = AsClass()->SizeOf();
1992 } else {
1993 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001994 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001995 DCHECK(!IsField() || result == sizeof(Field));
1996 DCHECK(!IsMethod() || result == sizeof(Method));
1997 return result;
1998}
1999
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002000inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002001 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002002 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002003 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002004 return result;
2005}
2006
2007inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002008 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002009}
2010
2011inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002012 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08002013 DCHECK(result != NULL) << this;
2014 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002015 return result;
2016}
2017
2018inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002019 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002020}
2021
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002022inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002023 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08002024 size_t component_size = GetClass()->GetComponentSize();
2025 int32_t component_count = GetLength();
2026 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
2027 size_t data_size = component_count * component_size;
2028 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002029}
2030
2031template<class T>
2032void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002033 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002034 if (object != NULL) {
2035 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002036 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002037 ThrowArrayStoreException(object);
2038 return;
2039 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002040 }
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}
2045
2046template<class T>
2047void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2048 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002049 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002050 SetFieldObject(data_offset, object, false);
2051}
2052
2053template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002054T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2055 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002056 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002057 return GetFieldObject<T*>(data_offset, false);
2058}
2059
2060template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002061void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2062 ObjectArray<T>* dst, int dst_pos,
2063 size_t length) {
2064 if (src->IsValidIndex(src_pos) &&
2065 src->IsValidIndex(src_pos+length-1) &&
2066 dst->IsValidIndex(dst_pos) &&
2067 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002068 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2069 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002070 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002071 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002072 if (array_class == src->GetClass()) {
2073 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002074 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002075 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002076 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002077 // directly set field, we do a bulk write barrier at the end
2078 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002079 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2080 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2081 }
2082 } else {
2083 Class* element_class = array_class->GetComponentType();
2084 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002085 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002086 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002087 if (object != NULL && !object->InstanceOf(element_class)) {
2088 dst->ThrowArrayStoreException(object);
2089 return;
2090 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002091 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002092 // directly set field, we do a bulk write barrier at the end
2093 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002094 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2095 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2096 }
2097 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002098 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002099 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002100}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002101
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002102class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002103 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002104 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002105 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002106 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002107 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2108};
2109
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002110class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002111 private:
2112 CharArray* ASCII_;
2113 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002114 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002115 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002116 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002117 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2118};
2119
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002120class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002121 private:
2122 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002123 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002124 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2125};
2126
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002127class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002128 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002129 Object* ORDER_BY_SIGNATURE_;
2130 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002131 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2132};
2133
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002134template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002135class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002136 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002137 typedef T ElementType;
2138
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002139 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002140
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002141 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002142 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2143 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002144 }
2145
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002146 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002147 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2148 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002149 }
2150
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002151 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002152 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002153 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002154 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002155 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002156 }
2157
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002158 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002159 // TODO: ArrayStoreException
2160 if (IsValidIndex(i)) {
2161 GetData()[i] = value;
2162 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002163 }
2164
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002165 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002166 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002167 CHECK(array_class != NULL);
2168 array_class_ = array_class;
2169 }
2170
Brian Carlstroma663ea52011-08-19 23:33:41 -07002171 static void ResetArrayClass() {
2172 CHECK(array_class_ != NULL);
2173 array_class_ = NULL;
2174 }
2175
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002176 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002177 static Class* array_class_;
2178
Carl Shapirof88c9522011-08-06 15:47:38 -07002179 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002180};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002181
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002182// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002183class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002184 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002185 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002186 const CharArray* result = GetFieldObject<const CharArray*>(
2187 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2188 DCHECK(result != NULL);
2189 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002190 }
2191
Elliott Hughes814e4032011-08-23 12:07:56 -07002192 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002193 int32_t result = GetField32(
2194 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2195 DCHECK_LE(0, result);
2196 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002197 }
2198
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002199 int32_t GetLength() const;
2200
Brian Carlstrom395520e2011-09-25 19:35:00 -07002201 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002202
2203 void ComputeHashCode() {
2204 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002205 }
2206
Elliott Hughes814e4032011-08-23 12:07:56 -07002207 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002208 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002209 }
2210
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002211 uint16_t CharAt(int32_t index) const;
2212
Brian Carlstromc74255f2011-09-11 22:47:39 -07002213 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002214
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002215 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002216 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002217 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002218
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002219 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002220
Jesse Wilson8989d992011-08-02 13:39:42 -07002221 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002222 const char* utf8_data_in);
2223
2224 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2225
2226 static String* Alloc(Class* java_lang_String, CharArray* array);
2227
2228 bool Equals(const char* modified_utf8) const;
2229
2230 // TODO: do we need this overload? give it a more intention-revealing name.
2231 bool Equals(const StringPiece& modified_utf8) const;
2232
2233 bool Equals(const String* that) const;
2234
Ian Rogers0571d352011-11-03 19:51:38 -07002235 // Compare UTF-16 code point values not in a locale-sensitive manner
2236 int Compare(int32_t utf16_length, const char* utf8_data_in);
2237
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002238 // TODO: do we need this overload? give it a more intention-revealing name.
2239 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2240 int32_t that_length) const;
2241
2242 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2243 std::string ToModifiedUtf8() const;
2244
2245 static Class* GetJavaLangString() {
2246 DCHECK(java_lang_String_ != NULL);
2247 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002248 }
2249
Brian Carlstroma663ea52011-08-19 23:33:41 -07002250 static void SetClass(Class* java_lang_String);
2251 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002252
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002253 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002254 void SetHashCode(int32_t new_hash_code) {
2255 DCHECK_EQ(0u,
2256 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2257 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2258 new_hash_code, false);
2259 }
2260
2261 void SetCount(int32_t new_count) {
2262 DCHECK_LE(0, new_count);
2263 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2264 }
2265
2266 void SetOffset(int32_t new_offset) {
2267 DCHECK_LE(0, new_offset);
2268 DCHECK_GE(GetLength(), new_offset);
2269 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2270 }
2271
2272 void SetArray(CharArray* new_array) {
2273 DCHECK(new_array != NULL);
2274 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2275 }
2276
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002277 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2278 CharArray* array_;
2279
Brian Carlstromdbc05252011-09-09 01:59:59 -07002280 int32_t count_;
2281
Carl Shapirof88c9522011-08-06 15:47:38 -07002282 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002283
Elliott Hughes289da822011-08-16 10:11:20 -07002284 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002285
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002286 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002287
Brian Carlstrom693267a2011-09-06 09:25:34 -07002288 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002289 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002290};
2291
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002292struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002293 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002294 return string->GetHashCode();
2295 }
2296};
2297
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002298inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002299 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002300 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2301}
2302
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002303inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002304 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002305 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002306}
2307
2308inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002309 DCHECK(GetDeclaringClass()->IsLoaded() || 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
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002313inline uint32_t Class::GetAccessFlags() const {
2314 // Check class is loaded or this is java.lang.String that has a
2315 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002316 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002317 this == String::GetJavaLangString() ||
2318 this == Field::GetJavaLangReflectField() ||
2319 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002320 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002321 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2322}
2323
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002324inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002325 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002326 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2327}
2328
2329inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002330 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002331 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002332}
2333
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002334inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002335 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002336 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002337}
2338
Elliott Hughes76e36942012-03-16 13:44:56 -07002339#if !defined(NDEBUG)
2340inline void Method::AssertPcIsWithinCode(uintptr_t pc) const {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002341 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
2342 return;
2343 }
2344 Runtime* runtime = Runtime::Current();
2345 if (GetCode() == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
2346 return;
2347 }
2348 DCHECK(IsWithinCode(pc))
2349 << PrettyMethod(this)
2350 << " pc=" << std::hex << pc
2351 << " code=" << GetCode()
2352 << " size=" << GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002353}
Elliott Hughes76e36942012-03-16 13:44:56 -07002354#else
2355inline void Method::AssertPcIsWithinCode(uintptr_t) const {}
2356#endif
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002357
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002358inline String* Class::GetName() const {
2359 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002360}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002361inline void Class::SetName(String* name) {
2362 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002363}
2364
2365// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002366class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002367 public:
2368 void SetDetailMessage(String* new_detail_message) {
2369 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2370 new_detail_message, false);
2371 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002372 String* GetDetailMessage() const {
2373 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2374 }
Ian Rogers9074b992011-10-26 17:41:55 -07002375 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002376
Ian Rogers1c5eb702012-02-01 09:18:34 -08002377 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2378 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2379 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2380 void SetCause(Throwable* cause);
Ian Rogers466bb252011-10-14 03:29:56 -07002381 bool IsCheckedException() const;
Ian Rogers5167c972012-02-03 10:41:20 -08002382
2383 static Class* GetJavaLangThrowable() {
2384 DCHECK(java_lang_Throwable_ != NULL);
2385 return java_lang_Throwable_;
2386 }
2387
2388 static void SetClass(Class* java_lang_Throwable);
2389 static void ResetClass();
2390
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002391 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002392 Object* GetStackState() const {
2393 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2394 }
2395
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002396 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2397 Throwable* cause_;
2398 String* detail_message_;
2399 Object* stack_state_; // Note this is Java volatile:
2400 Object* stack_trace_;
2401 Object* suppressed_exceptions_;
2402
Ian Rogers5167c972012-02-03 10:41:20 -08002403 static Class* java_lang_Throwable_;
2404
Brian Carlstrom693267a2011-09-06 09:25:34 -07002405 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002406 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2407};
2408
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002409// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002410class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002411 public:
2412 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002413 return GetFieldObject<const String*>(
2414 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002415 }
2416
2417 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002418 return GetFieldObject<const String*>(
2419 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002420 }
2421
2422 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002423 return GetFieldObject<const String*>(
2424 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002425 }
2426
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002427 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002428 return GetField32(
2429 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002430 }
2431
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002432 static StackTraceElement* Alloc(String* declaring_class,
2433 String* method_name,
2434 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002435 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002436
2437 static void SetClass(Class* java_lang_StackTraceElement);
2438
2439 static void ResetClass();
2440
2441 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002442 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002443 String* declaring_class_;
2444 String* file_name_;
2445 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002446 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002447
2448 static Class* GetStackTraceElement() {
2449 DCHECK(java_lang_StackTraceElement_ != NULL);
2450 return java_lang_StackTraceElement_;
2451 }
2452
2453 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002454
2455 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002456 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2457};
2458
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002459class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002460 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002461 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002462 Class* interface = Get(kInterface)->AsClass();
2463 DCHECK(interface != NULL);
2464 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002465 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002466
Brian Carlstrom30b94452011-08-25 21:35:26 -07002467 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002468 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002469 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002470 DCHECK(Get(kInterface) == NULL);
2471 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002472 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002473
Brian Carlstrom86927212011-09-15 11:31:11 -07002474 size_t GetMethodArrayCount() const {
2475 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002476 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002477 return 0;
2478 }
2479 return method_array->GetLength();
2480 }
2481
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002482 ObjectArray<Method>* GetMethodArray() const {
2483 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2484 DCHECK(method_array != NULL);
2485 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002486 }
2487
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002488 void SetMethodArray(ObjectArray<Method>* new_ma) {
2489 DCHECK(new_ma != NULL);
2490 DCHECK(Get(kMethodArray) == NULL);
2491 Set(kMethodArray, new_ma);
2492 }
2493
2494 static size_t LengthAsArray() {
2495 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002496 }
2497
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002498 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002499
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002500 enum ArrayIndex {
2501 // Points to the interface class.
2502 kInterface = 0,
2503 // Method pointers into the vtable, allow fast map from interface
2504 // method index to concrete instance method.
2505 kMethodArray = 1,
2506 kMax = 2,
2507 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002508
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002509 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002510};
2511
Ian Rogersc2b44472011-12-14 21:17:17 -08002512class MANAGED SynthesizedProxyClass : public Class {
2513 public:
2514 ObjectArray<ObjectArray<Class> >* GetThrows() {
2515 return throws_;
2516 }
Jesse Wilson95caa792011-10-12 18:14:17 -04002517 private:
Ian Rogersc2b44472011-12-14 21:17:17 -08002518 ObjectArray<ObjectArray<Class> >* throws_;
2519 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002520};
2521
2522class MANAGED Proxy : public Object {
2523 private:
2524 Object* h_;
2525
2526 friend struct ProxyOffsets; // for verifying offset information
2527 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2528};
2529
Carl Shapiro1fb86202011-06-27 17:43:13 -07002530} // namespace art
2531
2532#endif // ART_SRC_OBJECT_H_