blob: 2adee047a28c5f43c5926c13324ba05c3ba6bc93 [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
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include <vector>
21
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070023#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070025#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
29#include "macros.h"
30#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070031#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070034#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070035
36namespace art {
37
38class Array;
39class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070040class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070041class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070042class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040043class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070044class InterfaceEntry;
45class Monitor;
46class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070047class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070048class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040049class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070050template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070051template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070052typedef PrimitiveArray<uint8_t> BooleanArray;
53typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070054typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070055typedef PrimitiveArray<double> DoubleArray;
56typedef PrimitiveArray<float> FloatArray;
57typedef PrimitiveArray<int32_t> IntArray;
58typedef PrimitiveArray<int64_t> LongArray;
59typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070060
Carl Shapiro3ee755d2011-06-28 12:11:04 -070061union JValue {
62 uint8_t z;
63 int8_t b;
64 uint16_t c;
65 int16_t s;
66 int32_t i;
67 int64_t j;
68 float f;
69 double d;
70 Object* l;
71};
72
Brian Carlstrombe977852011-07-19 14:54:54 -070073static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
74static const uint32_t kAccPrivate = 0x0002; // field, method, ic
75static const uint32_t kAccProtected = 0x0004; // field, method, ic
76static const uint32_t kAccStatic = 0x0008; // field, method, ic
77static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
78static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
79static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
80static const uint32_t kAccVolatile = 0x0040; // field
81static const uint32_t kAccBridge = 0x0040; // method (1.5)
82static const uint32_t kAccTransient = 0x0080; // field
83static const uint32_t kAccVarargs = 0x0080; // method (1.5)
84static const uint32_t kAccNative = 0x0100; // method
85static const uint32_t kAccInterface = 0x0200; // class, ic
86static const uint32_t kAccAbstract = 0x0400; // class, method, ic
87static const uint32_t kAccStrict = 0x0800; // method
88static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
89static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
90static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070091
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070092static const uint32_t kAccMiranda = 0x8000; // method
93
Brian Carlstroma331b3c2011-07-18 17:47:56 -070094static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
95
Brian Carlstrombe977852011-07-19 14:54:54 -070096static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
97static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070098static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070099
Brian Carlstrom1f870082011-08-23 16:02:11 -0700100static const uint32_t kAccClassFlagsMask = (kAccPublic
101 | kAccFinal
102 | kAccInterface
103 | kAccAbstract
104 | kAccSynthetic
105 | kAccAnnotation
106 | kAccEnum);
107static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
108 | kAccPrivate
109 | kAccProtected
110 | kAccStatic);
111static const uint32_t kAccFieldFlagsMask = (kAccPublic
112 | kAccPrivate
113 | kAccProtected
114 | kAccStatic
115 | kAccFinal
116 | kAccVolatile
117 | kAccTransient
118 | kAccSynthetic
119 | kAccEnum);
120static const uint32_t kAccMethodFlagsMask = (kAccPublic
121 | kAccPrivate
122 | kAccProtected
123 | kAccStatic
124 | kAccFinal
125 | kAccSynchronized
126 | kAccBridge
127 | kAccVarargs
128 | kAccNative
129 | kAccAbstract
130 | kAccStrict
131 | kAccSynthetic
132 | kAccConstructor
133 | kAccDeclaredSynchronized);
134
135// if only kAccClassIsReference is set, we have a soft reference
136static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
137static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
138static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
139static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
140
141static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
142 | kAccClassIsWeakReference
143 | kAccClassIsFinalizerReference
144 | kAccClassIsPhantomReference);
145
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700146/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700147 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700148 */
149/*
150 * A magic value for refOffsets. Ignore the bits and walk the super
151 * chain when this is the value.
152 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
153 * fields followed by 2 ref instance fields.]
154 */
155#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
157#define CLASS_OFFSET_ALIGNMENT 4
158#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
159/*
160 * Given an offset, return the bit number which would encode that offset.
161 * Local use only.
162 */
163#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700164 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700165 CLASS_OFFSET_ALIGNMENT)
166/*
167 * Is the given offset too large to be encoded?
168 */
169#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
170 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
171/*
172 * Return a single bit, encoding the offset.
173 * Undefined if the offset is too large, as defined above.
174 */
175#define CLASS_BIT_FROM_OFFSET(byteOffset) \
176 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
177/*
178 * Return an offset, given a bit number as returned from CLZ.
179 */
180#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700181 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700182
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700183#define OFFSET_OF_OBJECT_MEMBER(type, field) \
184 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700185
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700186// Classes shared with the managed side of the world need to be packed
187// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700188#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700189
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700191class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700192 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700193 static bool InstanceOf(const Object* object, const Class* klass) {
194 if (object == NULL) {
195 return false;
196 }
197 return object->InstanceOf(klass);
198 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700199
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 static MemberOffset ClassOffset() {
201 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202 }
203
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700205 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 }
207
208 void SetClass(Class* new_klass);
209
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700210 bool InstanceOf(const Class* klass) const;
211
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700212 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700213
Elliott Hughes081be7f2011-09-18 16:50:26 -0700214 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700215
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 static MemberOffset MonitorOffset() {
217 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
218 }
219
Elliott Hughes5f791332011-09-15 17:45:30 -0700220 volatile int32_t* GetRawLockWordAddress() {
221 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
222 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
223 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 }
225
Elliott Hughes5f791332011-09-15 17:45:30 -0700226 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227
Elliott Hughes5f791332011-09-15 17:45:30 -0700228 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Elliott Hughes5f791332011-09-15 17:45:30 -0700230 void MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Elliott Hughes5f791332011-09-15 17:45:30 -0700232 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233
Elliott Hughes5f791332011-09-15 17:45:30 -0700234 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235
Elliott Hughes5f791332011-09-15 17:45:30 -0700236 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700237
Elliott Hughes5f791332011-09-15 17:45:30 -0700238 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700239
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700240 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241
242 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700243 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244 return down_cast<Class*>(this);
245 }
246
247 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700248 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700249 return down_cast<const Class*>(this);
250 }
251
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252 bool IsClassClass() const;
253
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700254 bool IsObjectArray() const;
255
256 template<class T>
257 ObjectArray<T>* AsObjectArray() {
258 DCHECK(IsObjectArray());
259 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700260 }
261
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700262 template<class T>
263 const ObjectArray<T>* AsObjectArray() const {
264 DCHECK(IsObjectArray());
265 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700266 }
267
Brian Carlstromb63ec392011-08-27 17:38:27 -0700268 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700269
270 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700271 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700272 return down_cast<Array*>(this);
273 }
274
275 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700276 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700277 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700278 }
279
Brian Carlstroma663ea52011-08-19 23:33:41 -0700280 bool IsString() const;
281
282 String* AsString() {
283 DCHECK(IsString());
284 return down_cast<String*>(this);
285 }
286
287 bool IsMethod() const;
288
289 Method* AsMethod() {
290 DCHECK(IsMethod());
291 return down_cast<Method*>(this);
292 }
293
Brian Carlstrom4873d462011-08-21 15:23:39 -0700294 const Method* AsMethod() const {
295 DCHECK(IsMethod());
296 return down_cast<const Method*>(this);
297 }
298
Brian Carlstroma663ea52011-08-19 23:33:41 -0700299 bool IsField() const;
300
301 Field* AsField() {
302 DCHECK(IsField());
303 return down_cast<Field*>(this);
304 }
305
Brian Carlstrom4873d462011-08-21 15:23:39 -0700306 const Field* AsField() const {
307 DCHECK(IsField());
308 return down_cast<const Field*>(this);
309 }
310
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 bool IsReferenceInstance() const;
312
313 bool IsWeakReferenceInstance() const;
314
315 bool IsSoftReferenceInstance() const;
316
317 bool IsFinalizerReferenceInstance() const;
318
319 bool IsPhantomReferenceInstance() const;
320
321 // Accessors for Java type fields
322 template<class T>
323 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700324 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
325 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700326 Heap::VerifyObject(result);
327 return result;
328 }
329
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700330 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700332 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
333 if (new_value != NULL) {
334 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
337
338 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
339 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700340 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
341 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700343 return android_atomic_acquire_load(word_addr);
344 } else {
345 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 }
348
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700349 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
350 if (this_is_valid) {
351 Heap::VerifyObject(this);
352 }
353 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700354 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700356 /*
357 * TODO: add an android_atomic_synchronization_store() function and
358 * use it in the 32-bit volatile set handlers. On some platforms we
359 * can use a fast atomic instruction and avoid the barriers.
360 */
361 ANDROID_MEMBAR_STORE();
362 *word_addr = new_value;
363 ANDROID_MEMBAR_FULL();
364 } else {
365 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 }
368
369 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
370 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700371 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700372 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700374 uint64_t result = QuasiAtomicRead64(addr);
375 ANDROID_MEMBAR_FULL();
376 return result;
377 } else {
378 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 }
381
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700382 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700384 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
385 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700387 ANDROID_MEMBAR_STORE();
388 QuasiAtomicSwap64(new_value, addr);
389 // Post-store barrier not required due to use of atomic op or mutex.
390 } else {
391 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 }
394
395 protected:
396 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700397 template<class T>
398 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700399 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700400 }
401
402 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700403 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
404 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700405 }
406
407 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700408 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700409
Elliott Hughes5f791332011-09-15 17:45:30 -0700410 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700411
Elliott Hughes5f791332011-09-15 17:45:30 -0700412 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700413 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700414 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700415};
416
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700418class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400419 private:
420 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700422 friend struct AccessibleObjectOffsets; // for verifying offset information
423 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400424};
425
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700427class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700428 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700430
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700431 void SetDeclaringClass(Class *new_declaring_class);
432
433 const String* GetName() const;
434
435 void SetName(String* new_name);
436
437 uint32_t GetAccessFlags() const;
438
439 void SetAccessFlags(uint32_t new_access_flags) {
440 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags,
441 false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700442 }
443
444 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700445 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700446 }
447
jeffhaobdb76512011-09-07 11:43:16 -0700448 bool IsFinal() const {
449 return (access_flags_ & kAccFinal) != 0;
450 }
451
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700452 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700453
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700454 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700455
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700456 // Gets type using type index and resolved types in the dex cache, may be null
457 // if type isn't yet resolved
458 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700459
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700460 // Performs full resolution, may return null and set exceptions if type cannot
461 // be resolved
462 Class* GetType() const;
463
464 // Offset to field within an Object
465 MemberOffset GetOffset() const;
466
buzbee34cd9e52011-09-08 14:31:52 -0700467 static MemberOffset OffsetOffset() {
468 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
469 }
470
471 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer);
472
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700473 MemberOffset GetOffsetDuringLinking() const;
474
475 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700476
Brian Carlstrom4873d462011-08-21 15:23:39 -0700477 // field access, null object for static fields
478 bool GetBoolean(const Object* object) const;
479 void SetBoolean(Object* object, bool z) const;
480 int8_t GetByte(const Object* object) const;
481 void SetByte(Object* object, int8_t b) const;
482 uint16_t GetChar(const Object* object) const;
483 void SetChar(Object* object, uint16_t c) const;
484 uint16_t GetShort(const Object* object) const;
485 void SetShort(Object* object, uint16_t s) const;
486 int32_t GetInt(const Object* object) const;
487 void SetInt(Object* object, int32_t i) const;
488 int64_t GetLong(const Object* object) const;
489 void SetLong(Object* object, int64_t j) const;
490 float GetFloat(const Object* object) const;
491 void SetFloat(Object* object, float f) const;
492 double GetDouble(const Object* object) const;
493 void SetDouble(Object* object, double d) const;
494 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700495 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700496
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497 // Slow path routines for static field access when field was unresolved at
498 // compile time
499 static uint32_t Get32StaticFromCode(uint32_t field_idx,
500 const Method* referrer);
501 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
502 uint32_t new_value);
503 static uint64_t Get64StaticFromCode(uint32_t field_idx,
504 const Method* referrer);
505 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
506 uint64_t new_value);
507 static Object* GetObjStaticFromCode(uint32_t field_idx,
508 const Method* referrer);
509 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
510 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700511
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700512 static Class* GetJavaLangReflectField() {
513 DCHECK(java_lang_reflect_Field_ != NULL);
514 return java_lang_reflect_Field_;
515 }
516
517 static void SetClass(Class* java_lang_reflect_Field);
518 static void ResetClass();
519
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700520 bool IsVolatile() const {
521 return (GetAccessFlags() & kAccVolatile) != 0;
522 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700523
buzbee1da522d2011-09-04 11:22:20 -0700524 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700525 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700526 uint32_t Get32(const Object* object) const;
527 void Set32(Object* object, uint32_t new_value) const;
528 uint64_t Get64(const Object* object) const;
529 void Set64(Object* object, uint64_t new_value) const;
530 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700531 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700532
Jesse Wilson35baaab2011-08-10 16:18:03 -0400533 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700534
Jesse Wilson35baaab2011-08-10 16:18:03 -0400535 // The class in which this field is declared.
536 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537
Jesse Wilson35baaab2011-08-10 16:18:03 -0400538 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700539
Brian Carlstromdbc05252011-09-09 01:59:59 -0700540 const String* name_;
541
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542 // Type of the field
Jesse Wilson35baaab2011-08-10 16:18:03 -0400543 Class* type_;
544
Brian Carlstromdbc05252011-09-09 01:59:59 -0700545 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700546
Jesse Wilson35baaab2011-08-10 16:18:03 -0400547 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700548
549 // Offset of field within an instance or in the Class' static fields
550 uint32_t offset_;
551
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700552 // Dex cache index of resolved type
553 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400554
Brian Carlstrom693267a2011-09-06 09:25:34 -0700555 int32_t slot_;
556
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700557 static Class* java_lang_reflect_Field_;
558
Brian Carlstrom693267a2011-09-06 09:25:34 -0700559 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400560 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700561};
562
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700563// C++ mirror of java.lang.reflect.Method
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700564class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700565 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700566 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700567 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700568 Object* obj,
569 Thread* thread,
570 byte* args,
571 JValue* result);
572
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700573 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700574
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700575 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700576
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700577 static MemberOffset DeclaringClassOffset() {
578 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700579 }
580
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700581 // Returns the method name, e.g. "<init>" or "eatLunch"
582 const String* GetName() const;
583
584 void SetName(String* new_name);
585
jeffhaoe23d93c2011-09-15 14:48:43 -0700586 ByteArray* GetRegisterMapData() const;
587
jeffhaoa0a764a2011-09-16 10:43:38 -0700588 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700589
590 ByteArray* GetRegisterMapHeader() const;
591
jeffhaoa0a764a2011-09-16 10:43:38 -0700592 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700593
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700594 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700595
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700596 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700597
598 const String* GetSignature() const;
599
600 void SetSignature(String* new_signature);
601
602 bool HasSameNameAndDescriptor(const Method* that) const;
603
604 uint32_t GetAccessFlags() const;
605
606 void SetAccessFlags(uint32_t new_access_flags) {
607 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags,
608 false);
609 }
610
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700611 // Returns true if the method is declared public.
612 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700613 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700614 }
615
616 // Returns true if the method is declared private.
617 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700618 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700619 }
620
621 // Returns true if the method is declared static.
622 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700623 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700624 }
625
Brian Carlstrom1f870082011-08-23 16:02:11 -0700626 // Returns true if the method is a constructor.
627 bool IsConstructor() const {
628 return (access_flags_ & kAccConstructor) != 0;
629 }
630
631 // Returns true if the method is static, private, or a constructor.
632 bool IsDirect() const {
633 return IsStatic() || IsPrivate() || IsConstructor();
634 }
635
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700636 // Returns true if the method is declared synchronized.
637 bool IsSynchronized() const {
638 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700639 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700640 }
641
642 // Returns true if the method is declared final.
643 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700644 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700645 }
646
647 // Returns true if the method is declared native.
648 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700649 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700650 }
651
652 // Returns true if the method is declared abstract.
653 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700654 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700655 }
656
657 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700658 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700659 }
660
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700661 uint16_t GetMethodIndex() const;
662
663 size_t GetVtableIndex() const {
664 return GetMethodIndex();
665 }
666
667 void SetMethodIndex(uint16_t new_method_index) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700668 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700669 new_method_index, false);
670 }
671
672 static MemberOffset MethodIndexOffset() {
673 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
674 }
675
676 uint32_t GetCodeItemOffset() const {
677 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
678 }
679
680 void SetCodeItemOffset(uint32_t new_code_off) {
681 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
682 new_code_off, false);
683 }
684
685 // Number of 32bit registers that would be required to hold all the arguments
686 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700687
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700688 // Number of argument bytes required for densely packing the
689 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700690 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700691
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700692 uint16_t NumRegisters() const;
693
694 void SetNumRegisters(uint16_t new_num_registers) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700695 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700696 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700697 }
698
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700699 uint16_t NumIns() const;
700
701 void SetNumIns(uint16_t new_num_ins) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700702 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700703 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700704 }
705
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700706 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700707
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700708 void SetNumOuts(uint16_t new_num_outs) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700709 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700710 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700711 }
712
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700713 uint32_t GetProtoIdx() const;
714
715 void SetProtoIdx(uint32_t new_proto_idx) {
716 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700717 }
718
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700719 ObjectArray<String>* GetDexCacheStrings() const;
720 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
721
722 static MemberOffset DexCacheStringsOffset() {
723 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
724 }
725
buzbee2a475e72011-09-07 17:19:17 -0700726 static MemberOffset DexCacheResolvedTypesOffset() {
727 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
728 }
729
buzbee34cd9e52011-09-08 14:31:52 -0700730 static MemberOffset DexCacheResolvedFieldsOffset() {
731 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
732 }
733
buzbee1da522d2011-09-04 11:22:20 -0700734 static MemberOffset DexCacheInitializedStaticStorageOffset() {
735 return OFFSET_OF_OBJECT_MEMBER(Method,
736 dex_cache_initialized_static_storage_);
737 }
738
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700739 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
740 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
741
742 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
743 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
744
745 ObjectArray<Field>* GetDexCacheResolvedFields() const;
746 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
747
748 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
749 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
750
751 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
752 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
753
754 void SetReturnTypeIdx(uint32_t new_return_type_idx);
755
756 Class* GetReturnType() const;
757
758 bool IsReturnAReference() const;
759
760 bool IsReturnAFloat() const;
761
762 bool IsReturnADouble() const;
763
Ian Rogersb033c752011-07-20 12:22:35 -0700764 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700765 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700766 }
767
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700768 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700769
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700770 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700771
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700772 // "Args" may refer to any of the 3 levels of "Args."
773 // To avoid confusion, our code will denote which "Args" clearly:
774 // 1. UserArgs: Args that a user see.
775 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
776 // receiver.
777 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
778 // E.g., the first in Args is Method* for both static and non-static
779 // methods. And CConvArgs doesn't deal with the receiver because
780 // receiver is hardwired in an implicit register, so CConvArgs doesn't
781 // need to deal with it.
782 //
783 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700784 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700785
786 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700787 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700788 size_t NumReferenceArgs() const;
789
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700790 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700791 size_t NumLongOrDoubleArgs() const;
792
Ian Rogersb033c752011-07-20 12:22:35 -0700793 // Is the given method parameter a reference?
794 bool IsParamAReference(unsigned int param) const;
795
796 // Is the given method parameter a long or double?
797 bool IsParamALongOrDouble(unsigned int param) const;
798
Ian Rogersdf20fe02011-07-20 20:34:16 -0700799 // Size in bytes of the given parameter
800 size_t ParamSize(unsigned int param) const;
801
802 // Size in bytes of the return value
803 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700804
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700805 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
806
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700807 const ByteArray* GetCodeArray() const {
808 return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false);
809 }
810
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700811 const void* GetCode() const {
812 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700813 }
814
buzbee4ef76522011-09-08 10:00:32 -0700815 void SetCode(ByteArray* code_array, InstructionSet instruction_set,
Ian Rogersbdb03912011-09-14 00:55:44 -0700816 IntArray* mapping_table = NULL);
buzbeec143c552011-08-20 17:38:58 -0700817
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700818 static MemberOffset GetCodeOffset() {
819 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700820 }
821
Ian Rogersbdb03912011-09-14 00:55:44 -0700822 // Is the given PC within the code array?
823 bool IsWithinCode(uintptr_t pc) const;
824
825 IntArray* GetMappingTable() const {
826 return GetFieldObject<IntArray*>(
827 OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
828 }
829
Shih-wei Liaod11af152011-08-23 16:02:11 -0700830 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700831 DCHECK(sizeof(size_t) == sizeof(uint32_t));
832 size_t result = GetField32(
833 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
834 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
835 return result;
836 }
837
838 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
839 DCHECK(sizeof(size_t) == sizeof(uint32_t));
840 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
841 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
842 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700843 }
844
Shih-wei Liaod11af152011-08-23 16:02:11 -0700845 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700846 DCHECK(sizeof(size_t) == sizeof(uint32_t));
847 return GetField32(
848 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700849 }
850
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700851 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
852 DCHECK(sizeof(size_t) == sizeof(uint32_t));
853 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
854 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
855 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700856 }
857
Brian Carlstrom16192862011-09-12 17:50:06 -0700858 bool IsRegistered();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700859
Brian Carlstrom16192862011-09-12 17:50:06 -0700860 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700861
Brian Carlstrom16192862011-09-12 17:50:06 -0700862 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700863
Ian Rogersb033c752011-07-20 12:22:35 -0700864 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700865 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700866 }
867
Brian Carlstrom78128a62011-09-15 17:21:19 -0700868 const void* GetNativeMethod() const {
869 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
870 }
871
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700872 ByteArray* GetInvokeStubArray() const {
873 ByteArray* result = GetFieldPtr<ByteArray*>(
874 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false);
875 // TODO: DCHECK(result != NULL); should be ahead of time compiled
876 return result;
877 }
878
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700879 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700880 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700881 InvokeStub* result = GetFieldPtr<InvokeStub*>(
882 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
883 // TODO: DCHECK(result != NULL); should be ahead of time compiled
884 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700885 }
886
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700887 static MemberOffset GetInvokeStubOffset() {
888 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700889 }
890
buzbee561227c2011-09-02 15:28:19 -0700891 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
892 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
893 }
894
895 static MemberOffset GetDexCacheResolvedMethodsOffset() {
896 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
897 }
898
899 static MemberOffset GetMethodIndexOffset() {
900 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
901 }
902
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700903 void SetInvokeStub(const ByteArray* invoke_stub_array);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700904
Ian Rogersbdb03912011-09-14 00:55:44 -0700905 uint32_t GetCoreSpillMask() {
906 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700907 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700908
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700909 void SetCoreSpillMask(uint32_t core_spill_mask) {
910 // Computed during compilation
911 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_),
912 core_spill_mask, false);
913 }
914
Ian Rogersbdb03912011-09-14 00:55:44 -0700915 uint32_t GetFpSpillMask() {
916 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
917 }
918
919 void SetFpSpillMask(uint32_t fp_spill_mask) {
920 // Computed during compilation
921 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_),
922 fp_spill_mask, false);
923 }
924
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700925 // Converts a native PC to a dex PC. TODO: this is a no-op
926 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700927 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700928
929 // Converts a dex PC to a native PC. TODO: this is a no-op
930 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700931 uintptr_t ToNativePC(const uint32_t dex_pc) const;
932
933 // Find the catch block for the given exception type and dex_pc
934 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700935
936 static Class* GetJavaLangReflectMethod() {
937 DCHECK(java_lang_reflect_Method_ != NULL);
938 return java_lang_reflect_Method_;
939 }
940
941 static void SetClass(Class* java_lang_reflect_Method);
942 static void ResetClass();
943
944 private:
945 uint32_t GetReturnTypeIdx() const;
946
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700947 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
948 // the class we are a part of
949 Class* declaring_class_;
950 ObjectArray<Class>* java_exception_types_;
951 Object* java_formal_type_parameters_;
952 Object* java_generic_exception_types_;
953 Object* java_generic_parameter_types_;
954 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700955
Brian Carlstrom693267a2011-09-06 09:25:34 -0700956 String* name_;
957
958 ObjectArray<Class>* java_parameter_types_;
959
960 Class* java_return_type_; // Unused by ART
961
Brian Carlstrom693267a2011-09-06 09:25:34 -0700962 // Storage for code_
963 const ByteArray* code_array_;
964
965 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -0700966 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
967
968 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
969 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
970
971 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
972 ObjectArray<Field>* dex_cache_resolved_fields_;
973
974 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
975 ObjectArray<Method>* dex_cache_resolved_methods_;
976
Brian Carlstromdbc05252011-09-09 01:59:59 -0700977 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
978 ObjectArray<Class>* dex_cache_resolved_types_;
979
980 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
981 ObjectArray<String>* dex_cache_strings_;
982
983 // Storage for invoke_stub_
984 const ByteArray* invoke_stub_array_;
985
986 // Storage for mapping_table_
Ian Rogersbdb03912011-09-14 00:55:44 -0700987 IntArray* mapping_table_;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700988
jeffhaoe23d93c2011-09-15 14:48:43 -0700989 // Byte arrays that hold data for the register maps
990 const ByteArray* register_map_data_;
991 const ByteArray* register_map_header_;
992
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700993 // The short-form method descriptor string.
994 String* shorty_;
995
Brian Carlstromdbc05252011-09-09 01:59:59 -0700996 // The method descriptor. This represents the parameters a method
997 // takes and value it returns. This string is a list of the type
998 // descriptors for the parameters enclosed in parenthesis followed
999 // by the return type descriptor. For example, for the method
1000 //
1001 // Object mymethod(int i, double d, Thread t)
1002 //
1003 // the method descriptor would be
1004 //
1005 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1006 String* signature_;
1007
1008 uint32_t java_generic_types_are_initialized_;
1009
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001010 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001011 uint32_t access_flags_;
1012
1013 // Compiled code associated with this method for callers from managed code.
1014 // May be compiled managed code or a bridge for invoking a native method.
1015 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001016
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001017 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001018 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001019
Brian Carlstrom693267a2011-09-06 09:25:34 -07001020 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001021 uint32_t core_spill_mask_;
1022
1023 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001024 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001025
Brian Carlstrom693267a2011-09-06 09:25:34 -07001026 // Total size in bytes of the frame
1027 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001028
Brian Carlstrom693267a2011-09-06 09:25:34 -07001029 // Native invocation stub entry point for calling from native to managed code.
1030 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001031
Brian Carlstrom693267a2011-09-06 09:25:34 -07001032 // Index of the return type
1033 uint32_t java_return_type_idx_;
1034
Brian Carlstrom693267a2011-09-06 09:25:34 -07001035 // For concrete virtual methods, this is the offset of the method
1036 // in Class::vtable_.
1037 //
1038 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001039 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001040 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001041
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001042 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001043 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001044
Brian Carlstrom693267a2011-09-06 09:25:34 -07001045 // Method bounds; not needed for an abstract method.
1046 //
1047 // For a native method, we compute the size of the argument list, and
1048 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001049 uint32_t num_ins_;
1050 uint32_t num_outs_;
1051 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001052
Brian Carlstrom693267a2011-09-06 09:25:34 -07001053 // Method prototype descriptor string (return and argument types).
1054 uint32_t proto_idx_;
1055
1056 // Offset of return PC within frame for compiled code (in bytes)
1057 size_t return_pc_offset_in_bytes_;
1058
Brian Carlstrom693267a2011-09-06 09:25:34 -07001059 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001060
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001061 static Class* java_lang_reflect_Method_;
1062
Brian Carlstrom693267a2011-09-06 09:25:34 -07001063 friend class ImageWriter; // for relocating code_ and invoke_stub_
1064 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001065 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001066};
1067
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001068class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001069 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001070 static size_t SizeOf(size_t component_count,
1071 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001072 return sizeof(Array) + component_count * component_size;
1073 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001074
Brian Carlstromb63ec392011-08-27 17:38:27 -07001075 // Given the context of a calling Method, use its DexCache to
1076 // resolve a type to an array Class. If it cannot be resolved, throw
1077 // an error. If it can, use it to create an array.
1078 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
1079
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001080 // A convenience for code that doesn't know the component size,
1081 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001082 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001083
Brian Carlstromb63ec392011-08-27 17:38:27 -07001084 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001085
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001086 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001087
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001088 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001089 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001090 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001091
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001092 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001093 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001094 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001095 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001096
buzbeec143c552011-08-20 17:38:58 -07001097 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001098 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001099 }
1100
1101 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001102 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001103 }
1104
Elliott Hughesbf86d042011-08-31 17:53:14 -07001105 void* GetRawData() {
1106 return reinterpret_cast<void*>(first_element_);
1107 }
1108
Elliott Hughes289da822011-08-16 10:11:20 -07001109 protected:
1110 bool IsValidIndex(int32_t index) const {
1111 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001112 Thread* self = Thread::Current();
1113 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
1114 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001115 return false;
Elliott Hughes289da822011-08-16 10:11:20 -07001116 }
1117 return true;
1118 }
1119
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001120 private:
1121 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001122 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001123 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1124 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001125 // Marker for the data (used by generated code)
1126 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001127
Carl Shapirof88c9522011-08-06 15:47:38 -07001128 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001129};
1130
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001131template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001132class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001133 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001134 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001135
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001136 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001137
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001138 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001139
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001140 // Set element without bound and element type checks, to be used in limited
1141 // circumstances, such as during boot image writing
1142 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001143
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001144 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001145 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001146 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001147
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001148 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001149
1150 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001151 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001152};
1153
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001154template<class T>
1155ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1156 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1157}
1158
1159template<class T>
1160T* ObjectArray<T>::Get(int32_t i) const {
1161 if (!IsValidIndex(i)) {
1162 return NULL;
1163 }
1164 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1165 return GetFieldObject<T*>(data_offset, false);
1166}
1167
1168template<class T>
1169ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1170 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1171 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1172 return new_array;
1173}
1174
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001175// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001176// provides the static storage. However, this might change to an Array
1177// to improve image sharing, so we use this type to avoid assumptions
1178// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001179class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001180
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001181// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001182class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001183 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001184
1185 // Class Status
1186 //
1187 // kStatusNotReady: If a Class cannot be found in the class table by
1188 // FindClass, it allocates an new one with AllocClass in the
1189 // kStatusNotReady and calls LoadClass. Note if it does find a
1190 // class, it may not be kStatusResolved and it will try to push it
1191 // forward toward kStatusResolved.
1192 //
1193 // kStatusIdx: LoadClass populates with Class with information from
1194 // the DexFile, moving the status to kStatusIdx, indicating that the
1195 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001196 // populated based on super_class_type_idx_ and
1197 // interfaces_type_idx_. The new Class can then be inserted into the
1198 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001199 //
1200 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1201 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1202 // using ResolveClass to initialize the super_class_ and interfaces_.
1203 //
1204 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001205 // shows linking is complete and fields of the Class populated by making
1206 // it kStatusResolved. Java allows circularities of the form where a super
1207 // class has a field that is of the type of the sub class. We need to be able
1208 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001209
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001210 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001211 kStatusError = -1,
1212 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001213 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001214 kStatusLoaded = 2, // DEX idx values resolved
1215 kStatusResolved = 3, // part of linking
1216 kStatusVerifying = 4, // in the process of being verified
1217 kStatusVerified = 5, // logically part of linking; done pre-init
1218 kStatusInitializing = 6, // class init in progress
1219 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001220 };
1221
1222 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001223 kPrimNot = 0,
1224 kPrimBoolean,
1225 kPrimByte,
1226 kPrimChar,
1227 kPrimShort,
1228 kPrimInt,
1229 kPrimLong,
1230 kPrimFloat,
1231 kPrimDouble,
1232 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001233 };
1234
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001235 Status GetStatus() const {
1236 CHECK(sizeof(Status) == sizeof(uint32_t));
1237 return static_cast<Status>(
1238 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1239 }
1240
1241 void SetStatus(Status new_status);
1242
1243 // Returns true if the class has failed to link.
1244 bool IsErroneous() const {
1245 return GetStatus() == kStatusError;
1246 }
1247
1248 // Returns true if the class has been loaded.
1249 bool IsIdxLoaded() const {
1250 return GetStatus() >= kStatusIdx;
1251 }
1252
1253 // Returns true if the class has been loaded.
1254 bool IsLoaded() const {
1255 return GetStatus() >= kStatusLoaded;
1256 }
1257
1258 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001259 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001260 return GetStatus() >= kStatusResolved;
1261 }
1262
1263 // Returns true if the class has been verified.
1264 bool IsVerified() const {
1265 return GetStatus() >= kStatusVerified;
1266 }
1267
1268 // Returns true if the class is initialized.
1269 bool IsInitialized() const {
1270 return GetStatus() == kStatusInitialized;
1271 }
1272
1273 uint32_t GetAccessFlags() const;
1274
1275 void SetAccessFlags(uint32_t new_access_flags) {
1276 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags,
1277 false);
1278 }
1279
1280 // Returns true if the class is an interface.
1281 bool IsInterface() const {
1282 return (GetAccessFlags() & kAccInterface) != 0;
1283 }
1284
1285 // Returns true if the class is declared public.
1286 bool IsPublic() const {
1287 return (GetAccessFlags() & kAccPublic) != 0;
1288 }
1289
1290 // Returns true if the class is declared final.
1291 bool IsFinal() const {
1292 return (GetAccessFlags() & kAccFinal) != 0;
1293 }
1294
1295 // Returns true if the class is abstract.
1296 bool IsAbstract() const {
1297 return (GetAccessFlags() & kAccAbstract) != 0;
1298 }
1299
1300 // Returns true if the class is an annotation.
1301 bool IsAnnotation() const {
1302 return (GetAccessFlags() & kAccAnnotation) != 0;
1303 }
1304
1305 // Returns true if the class is synthetic.
1306 bool IsSynthetic() const {
1307 return (GetAccessFlags() & kAccSynthetic) != 0;
1308 }
1309
1310 bool IsReferenceClass() const {
1311 return (GetAccessFlags() & kAccClassIsReference) != 0;
1312 }
1313
1314 bool IsWeakReferenceClass() const {
1315 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1316 }
1317
1318 bool IsSoftReferenceClass() const {
1319 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1320 }
1321
1322 bool IsFinalizerReferenceClass() const {
1323 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1324 }
1325
1326 bool IsPhantomReferenceClass() const {
1327 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1328 }
1329
1330 PrimitiveType GetPrimitiveType() const {
1331 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1332 return static_cast<PrimitiveType>(
1333 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1334 }
1335
1336 void SetPrimitiveType(PrimitiveType new_type) {
1337 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1338 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type,
1339 false);
1340 }
1341
1342 // Returns true if the class is a primitive type.
1343 bool IsPrimitive() const {
1344 return GetPrimitiveType() != kPrimNot;
1345 }
1346
1347 bool IsPrimitiveBoolean() const {
1348 return GetPrimitiveType() == kPrimBoolean;
1349 }
1350
1351 bool IsPrimitiveByte() const {
1352 return GetPrimitiveType() == kPrimByte;
1353 }
1354
1355 bool IsPrimitiveChar() const {
1356 return GetPrimitiveType() == kPrimChar;
1357 }
1358
1359 bool IsPrimitiveShort() const {
1360 return GetPrimitiveType() == kPrimShort;
1361 }
1362
1363 bool IsPrimitiveInt() const {
1364 return GetPrimitiveType() == kPrimInt;
1365 }
1366
1367 bool IsPrimitiveLong() const {
1368 return GetPrimitiveType() == kPrimLong;
1369 }
1370
1371 bool IsPrimitiveFloat() const {
1372 return GetPrimitiveType() == kPrimFloat;
1373 }
1374
1375 bool IsPrimitiveDouble() const {
1376 return GetPrimitiveType() == kPrimDouble;
1377 }
1378
1379 bool IsPrimitiveVoid() const {
1380 return GetPrimitiveType() == kPrimVoid;
1381 }
1382
1383 size_t PrimitiveSize() const;
1384
1385 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001386 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001387 }
1388
1389 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001390 return GetFieldObject<Class*>(
1391 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1392 }
1393
1394 void SetComponentType(Class* new_component_type) {
1395 DCHECK(GetComponentType() == NULL);
1396 DCHECK(new_component_type != NULL);
1397 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_),
1398 new_component_type, false);
1399 }
1400
1401 size_t GetComponentSize() const {
1402 return GetTypeSize(GetComponentType()->GetDescriptor());
1403 }
1404
1405 bool IsObjectClass() const {
1406 return !IsPrimitive() && GetSuperClass() == NULL;
1407 }
1408
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001409 // Tests whether an element of type 'object_class' can be
1410 // assigned into an array of type 'array_class'.
1411 static bool CanPutArrayElement(const Class* object_class, const Class* array_class);
1412 // Like CanPutArrayElement, but throws an exception and
1413 // unwinds the stack instead of returning false.
buzbee9a195c92011-09-16 13:26:02 -07001414 static void CanPutArrayElementFromCode(const Object* element, const Class* array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415
Brian Carlstromb63ec392011-08-27 17:38:27 -07001416 // Given the context of a calling Method, use its DexCache to
1417 // resolve a type to a Class. If it cannot be resolved, throw an
1418 // error. If it can, use it to create an instance.
Brian Carlstrom1f870082011-08-23 16:02:11 -07001419 static Object* AllocObjectFromCode(uint32_t type_idx, Method* method);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001420
Brian Carlstrom1f870082011-08-23 16:02:11 -07001421 // Creates a raw object instance but does not invoke the default constructor.
1422 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001423
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001424 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001425
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001426 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001427 const String* result = GetFieldObject<const String*>(
1428 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1429 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1430 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1431 return result;
1432 }
1433
1434 void SetDescriptor(String* new_descriptor);
1435
1436 bool IsVariableSize() const {
1437 // Classes and arrays vary in size, and so the object_size_ field cannot
1438 // be used to get their instance size
1439 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001440 }
1441
Brian Carlstrom4873d462011-08-21 15:23:39 -07001442 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001443 CHECK(sizeof(size_t) == sizeof(int32_t));
1444 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001445 }
1446
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001447 size_t GetClassSize() const {
1448 CHECK(sizeof(size_t) == sizeof(uint32_t));
1449 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001450 }
1451
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001452 void SetClassSize(size_t new_class_size) {
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001453 DCHECK(new_class_size >= GetClassSize())
Elliott Hughes54e7df12011-09-16 11:47:04 -07001454 << " class=" << PrettyTypeOf(this)
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001455 << " new_class_size=" << new_class_size
1456 << " GetClassSize=" << GetClassSize();
Elliott Hughes54e7df12011-09-16 11:47:04 -07001457 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001458 }
1459
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001460 size_t GetObjectSize() const {
1461 CHECK(!IsVariableSize());
1462 CHECK(sizeof(size_t) == sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001463 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001464 CHECK_GE(result, sizeof(Object));
1465 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 }
1467
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001468 void SetObjectSize(size_t new_object_size) {
1469 DCHECK(!IsVariableSize());
1470 CHECK(sizeof(size_t) == sizeof(int32_t));
1471 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_),
1472 new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001473 }
1474
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001475 // Returns true if this class is in the same packages as that class.
1476 bool IsInSamePackage(const Class* that) const;
1477
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001478 static bool IsInSamePackage(const String* descriptor1,
1479 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001480
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001481 // Returns true if this class can access that class.
1482 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001483 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001484 }
1485
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001486 bool IsAssignableFrom(const Class* src) const {
1487 DCHECK(src != NULL);
1488 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001489 // Can always assign to things of the same type
1490 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001491 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001492 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001493 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001494 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001495 return src->Implements(this);
1496 } else if (src->IsArrayClass()) {
1497 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001498 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001499 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001500 }
1501 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001502
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001503 Class* GetSuperClass() const {
1504 // Can only get super class for loaded classes (hack for when runtime is
1505 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001506 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001507 return GetFieldObject<Class*>(
1508 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1509 }
1510
buzbee4a3164f2011-09-03 11:25:10 -07001511 static MemberOffset SuperClassOffset() {
1512 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1513 }
1514
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001515 void SetSuperClass(Class *new_super_class) {
1516 // super class is assigned once, except during class linker initialization
1517 Class* old_super_class = GetFieldObject<Class*>(
1518 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1519 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1520 DCHECK(new_super_class != NULL);
1521 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_),
1522 new_super_class, false);
1523 }
1524
1525 bool HasSuperClass() const {
1526 return GetSuperClass() != NULL;
1527 }
1528
1529 uint32_t GetSuperClassTypeIdx() const {
1530 DCHECK(IsIdxLoaded());
1531 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1532 false);
1533 }
1534
1535 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
1536 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1537 new_super_class_idx, false);
1538 }
1539
1540 const ClassLoader* GetClassLoader() const;
1541
1542 void SetClassLoader(const ClassLoader* new_cl);
1543
1544 static MemberOffset DexCacheOffset() {
1545 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1546 }
1547
1548 DexCache* GetDexCache() const;
1549
1550 void SetDexCache(DexCache* new_dex_cache);
1551
1552 ObjectArray<Method>* GetDirectMethods() const {
1553 DCHECK(IsLoaded());
1554 return GetFieldObject<ObjectArray<Method>*>(
1555 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1556 }
1557
1558 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1559 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1560 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1561 DCHECK_NE(0, new_direct_methods->GetLength());
1562 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1563 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001564 }
1565
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001566 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001567 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001568 }
1569
1570 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001571 ObjectArray<Method>* direct_methods =
1572 GetFieldObject<ObjectArray<Method>*>(
1573 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1574 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001575 }
1576
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001577 // Returns the number of static, private, and constructor methods.
1578 size_t NumDirectMethods() const {
1579 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1580 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001581
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001582 ObjectArray<Method>* GetVirtualMethods() const {
1583 DCHECK(IsLoaded());
1584 return GetFieldObject<ObjectArray<Method>*>(
1585 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1586 }
1587
1588 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1589 // TODO: we reassign virtual methods to grow the table for miranda
1590 // methods.. they should really just be assigned once
1591 DCHECK_NE(0, new_virtual_methods->GetLength());
1592 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1593 new_virtual_methods, false);
1594 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001595
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001596 // Returns the number of non-inherited virtual methods.
1597 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001598 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001599 }
1600
1601 Method* GetVirtualMethod(uint32_t i) const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001602 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001603 return GetVirtualMethods()->Get(i);
1604 }
1605
1606 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
1607 DCHECK(IsLoaded());
1608 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001609 }
1610
1611 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001612 ObjectArray<Method>* virtual_methods =
1613 GetFieldObject<ObjectArray<Method>*>(
1614 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1615 virtual_methods->Set(i, f);
1616 }
1617
1618 ObjectArray<Method>* GetVTable() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001619 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001620 return GetFieldObject<ObjectArray<Method>*>(
1621 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1622 }
1623
1624 ObjectArray<Method>* GetVTableDuringLinking() const {
1625 DCHECK(IsLoaded());
1626 return GetFieldObject<ObjectArray<Method>*>(
1627 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1628 }
1629
1630 void SetVTable(ObjectArray<Method>* new_vtable) {
1631 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1632 }
1633
1634 static MemberOffset VTableOffset() {
1635 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001636 }
1637
Brian Carlstrom30b94452011-08-25 21:35:26 -07001638 // Given a method implemented by this class but potentially from a
1639 // super class, return the specific implementation
1640 // method for this class.
1641 Method* FindVirtualMethodForVirtual(Method* method) {
1642 DCHECK(!method->GetDeclaringClass()->IsInterface());
1643 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001644 // Use the index to a potentially overridden one for this instance's class.
1645 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001646 }
1647
1648 // Given a method implemented by this class, but potentially from a
1649 // super class or interface, return the specific implementation
1650 // method for this class.
1651 Method* FindVirtualMethodForInterface(Method* method);
1652
jeffhaobdb76512011-09-07 11:43:16 -07001653 Method* FindInterfaceMethod(const StringPiece& name,
1654 const StringPiece& descriptor);
1655
Brian Carlstrom30b94452011-08-25 21:35:26 -07001656 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1657 if (method->GetDeclaringClass()->IsInterface()) {
1658 return FindVirtualMethodForInterface(method);
1659 }
1660 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001661 }
1662
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001663 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001664 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001665
1666 Method* FindVirtualMethod(const StringPiece& name,
1667 const StringPiece& descriptor);
1668
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001669 Method* FindDeclaredDirectMethod(const StringPiece& name,
1670 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001671
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001672 Method* FindDirectMethod(const StringPiece& name,
1673 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001674
Carl Shapiro69759ea2011-07-21 18:13:35 -07001675 size_t NumInterfaces() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001676 CHECK(IsIdxLoaded()); // used during loading
1677 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1678 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1679 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001680 }
1681
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001682 IntArray* GetInterfacesTypeIdx() const {
1683 CHECK(IsIdxLoaded());
1684 return GetFieldObject<IntArray*>(
1685 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1686 }
1687
1688 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1689
1690 ObjectArray<Class>* GetInterfaces() const {
1691 CHECK(IsLoaded());
1692 return GetFieldObject<ObjectArray<Class>*>(
1693 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1694 }
1695
1696 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1697 DCHECK(NULL == GetFieldObject<Object*>(
1698 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
1699 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_),
1700 new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001701 }
1702
1703 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001704 DCHECK_NE(NumInterfaces(), 0U);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001705 ObjectArray<Class>* interfaces =
1706 GetFieldObject<ObjectArray<Class>*>(
1707 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1708 interfaces->Set(i, f);
1709 }
1710
1711 Class* GetInterface(uint32_t i) const {
1712 DCHECK_NE(NumInterfaces(), 0U);
1713 return GetInterfaces()->Get(i);
1714 }
1715
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001716 int32_t GetIfTableCount() const {
1717 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1718 if (iftable == NULL) {
1719 return 0;
1720 }
1721 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001722 }
1723
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001724 ObjectArray<InterfaceEntry>* GetIfTable() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001725 DCHECK(IsResolved());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001726 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001727 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1728 }
1729
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001730 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001731 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001732 }
1733
1734 // Get instance fields
1735 ObjectArray<Field>* GetIFields() const {
1736 DCHECK(IsLoaded());
1737 return GetFieldObject<ObjectArray<Field>*>(
1738 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1739 }
1740
1741 void SetIFields(ObjectArray<Field>* new_ifields) {
1742 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1743 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
1744 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_),
1745 new_ifields, false);
1746 }
1747
1748 size_t NumInstanceFields() const {
1749 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1750 }
1751
1752 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1753 DCHECK_NE(NumInstanceFields(), 0U);
1754 return GetIFields()->Get(i);
1755 }
1756
1757 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1758 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1759 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1760 ifields->Set(i, f);
1761 }
1762
1763 // Returns the number of instance fields containing reference types.
1764 size_t NumReferenceInstanceFields() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001765 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001766 DCHECK(sizeof(size_t) == sizeof(int32_t));
1767 return GetField32(
1768 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1769 }
1770
1771 size_t NumReferenceInstanceFieldsDuringLinking() const {
1772 DCHECK(IsLoaded());
1773 DCHECK(sizeof(size_t) == sizeof(int32_t));
1774 return GetField32(
1775 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1776 }
1777
1778 void SetNumReferenceInstanceFields(size_t new_num) {
1779 DCHECK(sizeof(size_t) == sizeof(int32_t));
1780 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_),
1781 new_num, false);
1782 }
1783
1784 uint32_t GetReferenceInstanceOffsets() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001785 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001786 return GetField32(
1787 OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
1788 }
1789
1790 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1791
1792 // Beginning of static field data
1793 static MemberOffset FieldsOffset() {
1794 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1795 }
1796
1797 // Returns the number of static fields containing reference types.
1798 size_t NumReferenceStaticFields() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001799 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001800 DCHECK(sizeof(size_t) == sizeof(int32_t));
1801 return GetField32(
1802 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1803 }
1804
1805 size_t NumReferenceStaticFieldsDuringLinking() const {
1806 DCHECK(IsLoaded());
1807 DCHECK(sizeof(size_t) == sizeof(int32_t));
1808 return GetField32(
1809 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1810 }
1811
1812 void SetNumReferenceStaticFields(size_t new_num) {
1813 DCHECK(sizeof(size_t) == sizeof(int32_t));
1814 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_),
1815 new_num, false);
1816 }
1817
1818 ObjectArray<Field>* GetSFields() const {
1819 DCHECK(IsLoaded());
1820 return GetFieldObject<ObjectArray<Field>*>(
1821 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1822 }
1823
1824 void SetSFields(ObjectArray<Field>* new_sfields) {
1825 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1826 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
1827 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_),
1828 new_sfields, false);
1829 }
1830
1831 size_t NumStaticFields() const {
1832 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1833 }
1834
1835 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1836 return GetSFields()->Get(i);
1837 }
1838
1839 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1840 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1841 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1842 sfields->Set(i, f);
1843 }
1844
1845 uint32_t GetReferenceStaticOffsets() const {
1846 return GetField32(
1847 OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
1848 }
1849
1850 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1851
1852 // Finds the given instance field in this class or a superclass.
1853 Field* FindInstanceField(const StringPiece& name, Class* type);
1854
1855 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1856
1857 // Finds the given static field in this class or a superclass.
1858 Field* FindStaticField(const StringPiece& name, Class* type);
1859
1860 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1861
Elliott Hughesdcc24742011-09-07 14:02:44 -07001862 pid_t GetClinitThreadId() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001863 DCHECK(IsIdxLoaded());
1864 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1865 }
1866
Elliott Hughesdcc24742011-09-07 14:02:44 -07001867 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001868 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_),
1869 new_clinit_thread_id, false);
1870 }
1871
1872 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001873 // DCHECK(IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001874 return GetFieldObject<Class*>(
1875 OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001876 }
1877
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001878 void SetVerifyErrorClass(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001879 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_),
1880 klass, false);
1881 }
1882
Brian Carlstromc74255f2011-09-11 22:47:39 -07001883 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001884
Brian Carlstromc74255f2011-09-11 22:47:39 -07001885 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001886
jeffhaobdb76512011-09-07 11:43:16 -07001887 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001888 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001889 bool IsArrayAssignableFromArray(const Class* klass) const;
1890 bool IsAssignableFromArray(const Class* klass) const;
1891 bool IsSubClass(const Class* klass) const;
1892
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001893 // descriptor for the class such as "java.lang.Class" or "[C"
1894 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001895
Brian Carlstrom693267a2011-09-06 09:25:34 -07001896 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001897 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001898
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001899 // For array classes, the component class object for instanceof/checkcast
1900 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001901 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001902
Brian Carlstrom693267a2011-09-06 09:25:34 -07001903 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1904 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001905
Brian Carlstrom693267a2011-09-06 09:25:34 -07001906 // DexCache of resolved constant pool entries
1907 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1908 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001909
1910 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001911 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001912
Brian Carlstrom693267a2011-09-06 09:25:34 -07001913 // instance fields
1914 //
1915 // These describe the layout of the contents of an Object.
1916 // Note that only the fields directly declared by this class are
1917 // listed in ifields; fields declared by a superclass are listed in
1918 // the superclass's Class.ifields.
1919 //
1920 // All instance fields that refer to objects are guaranteed to be at
1921 // the beginning of the field list. num_reference_instance_fields_
1922 // specifies the number of reference fields.
1923 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001924
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001925 // Interface table (iftable_), one entry per interface supported by
1926 // this class. That means one entry for each interface we support
1927 // directly, indirectly via superclass, or indirectly via
1928 // superinterface. This will be null if neither we nor our
1929 // superclass implement any interfaces.
1930 //
1931 // Why we need this: given "class Foo implements Face", declare
1932 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1933 // is part of the Face interface. We can't easily use a single
1934 // vtable.
1935 //
1936 // For every interface a concrete class implements, we create an array
1937 // of the concrete vtable_ methods for the methods in the interface.
1938 ObjectArray<InterfaceEntry>* iftable_;
1939
Brian Carlstromdbc05252011-09-09 01:59:59 -07001940 // array of interfaces this class implements directly
1941 // see also interfaces_type_idx_
1942 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001943
1944 // array of type_idx's for interfaces this class implements directly
1945 // see also interfaces_
1946 IntArray* interfaces_type_idx_;
1947
Brian Carlstromdbc05252011-09-09 01:59:59 -07001948 // Static fields
1949 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001950
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001951 // source file name, if known. Otherwise, NULL.
1952 String* source_file_;
1953
Brian Carlstromdbc05252011-09-09 01:59:59 -07001954 // The superclass, or NULL if this is java.lang.Object or a
1955 // primitive type.
1956 // see also super_class_type_idx_;
1957 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001958
Brian Carlstromdbc05252011-09-09 01:59:59 -07001959 // If class verify fails, we must return same error on subsequent tries.
1960 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1961 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001962
Brian Carlstromdbc05252011-09-09 01:59:59 -07001963 // virtual methods defined in this class; invoked through vtable
1964 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001965
Brian Carlstromdbc05252011-09-09 01:59:59 -07001966 // Virtual method table (vtable), for use by "invoke-virtual". The
1967 // vtable from the superclass is copied in, and virtual methods from
1968 // our class either replace those from the super or are appended.
1969 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001970
Brian Carlstromdbc05252011-09-09 01:59:59 -07001971 // access flags; low 16 bits are defined by VM spec
1972 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001973
Brian Carlstromdbc05252011-09-09 01:59:59 -07001974 // Total class size; used when allocating storage on gc heap.
1975 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001976
Elliott Hughes5f791332011-09-15 17:45:30 -07001977 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001978 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001979
Brian Carlstromdbc05252011-09-09 01:59:59 -07001980 // number of instance fields that are object refs
1981 size_t num_reference_instance_fields_;
1982
1983 // number of static fields that are object refs
1984 size_t num_reference_static_fields_;
1985
1986 // Total object size; used when allocating storage on gc heap.
1987 // (For interfaces and abstract classes this will be zero.)
1988 size_t object_size_;
1989
1990 // primitive type index, or kPrimNot (0); set for generated prim classes
1991 PrimitiveType primitive_type_;
1992
1993 // Bitmap of offsets of ifields.
1994 uint32_t reference_instance_offsets_;
1995
1996 // Bitmap of offsets of sfields.
1997 uint32_t reference_static_offsets_;
1998
Brian Carlstrom693267a2011-09-06 09:25:34 -07001999 // state of class initialization
2000 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002001
Brian Carlstrom693267a2011-09-06 09:25:34 -07002002 // Set in LoadClass, used to LinkClass
2003 // see also super_class_
2004 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002005
Brian Carlstrom693267a2011-09-06 09:25:34 -07002006 // TODO: ?
2007 // initiating class loader list
2008 // NOTE: for classes with low serialNumber, these are unused, and the
2009 // values are kept in a table in gDvm.
2010 // InitiatingLoaderList initiating_loader_list_;
2011
Brian Carlstrom4873d462011-08-21 15:23:39 -07002012 // Location of first static field.
2013 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002014
Brian Carlstrom693267a2011-09-06 09:25:34 -07002015 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002016 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002017};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002018
Elliott Hughes1f359b02011-07-17 14:27:17 -07002019std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002020
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002021inline void Object::SetClass(Class* new_klass) {
2022 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002023 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002024}
2025
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002026inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002027 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002028 DCHECK(GetClass() != NULL);
2029 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002030}
2031
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002032inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002033 Class* java_lang_Class = GetClass()->GetClass();
2034 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002035}
2036
Brian Carlstrom4873d462011-08-21 15:23:39 -07002037inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002038 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002039 return this == java_lang_Class;
2040}
2041
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002042inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002043 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002044}
2045
Brian Carlstromb63ec392011-08-27 17:38:27 -07002046inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002047 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002048}
2049
Brian Carlstroma663ea52011-08-19 23:33:41 -07002050inline bool Object::IsField() const {
2051 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002052 Class* java_lang_reflect_Field =
2053 java_lang_Class->GetInstanceField(0)->GetClass();
2054 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002055}
2056
2057inline bool Object::IsMethod() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002058 Class* java_lang_Class = GetClass()->GetClass();
2059 Class* java_lang_reflect_Method =
2060 java_lang_Class->GetDirectMethod(0)->GetClass();
2061 return GetClass() == java_lang_reflect_Method;
2062}
2063
2064inline bool Object::IsReferenceInstance() const {
2065 return GetClass()->IsReferenceClass();
2066}
2067
2068inline bool Object::IsWeakReferenceInstance() const {
2069 return GetClass()->IsWeakReferenceClass();
2070}
2071
2072inline bool Object::IsSoftReferenceInstance() const {
2073 return GetClass()->IsSoftReferenceClass();
2074}
2075
2076inline bool Object::IsFinalizerReferenceInstance() const {
2077 return GetClass()->IsFinalizerReferenceClass();
2078}
2079
2080inline bool Object::IsPhantomReferenceInstance() const {
2081 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002082}
2083
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002084inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002085 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002086 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002087 result = AsArray()->SizeOf();
2088 } else if (IsClass()) {
2089 result = AsClass()->SizeOf();
2090 } else {
2091 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002092 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002093 DCHECK(!IsField() || result == sizeof(Field));
2094 DCHECK(!IsMethod() || result == sizeof(Method));
2095 return result;
2096}
2097
2098inline void Field::SetOffset(MemberOffset num_bytes) {
2099 DCHECK(GetDeclaringClass()->IsLoaded());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002100 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002101 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2102 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002103 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002104 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2105 num_bytes.Uint32Value(), false);
2106}
2107
2108inline Class* Field::GetDeclaringClass() const {
2109 Class* result = GetFieldObject<Class*>(
2110 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2111 DCHECK(result != NULL);
2112 DCHECK(result->IsLoaded());
2113 return result;
2114}
2115
2116inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2117 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2118 new_declaring_class, false);
2119}
2120
2121inline Class* Method::GetDeclaringClass() const {
2122 Class* result =
2123 GetFieldObject<Class*>(
2124 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2125 DCHECK(result != NULL);
2126 DCHECK(result->IsLoaded());
2127 return result;
2128}
2129
2130inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2131 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2132 new_declaring_class, false);
2133}
2134
2135inline uint32_t Method::GetReturnTypeIdx() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002136 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002137 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2138 false);
2139}
2140
2141inline bool Method::IsReturnAReference() const {
2142 return !GetReturnType()->IsPrimitive();
2143}
2144
2145inline bool Method::IsReturnAFloat() const {
2146 return GetReturnType()->IsPrimitiveFloat();
2147}
2148
2149inline bool Method::IsReturnADouble() const {
2150 return GetReturnType()->IsPrimitiveDouble();
2151}
2152
2153inline bool Method::IsReturnALong() const {
2154 return GetReturnType()->IsPrimitiveLong();
2155}
2156
2157inline bool Method::IsReturnVoid() const {
2158 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002159}
2160
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002161inline size_t Array::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002162 return SizeOf(GetLength(), GetClass()->GetComponentSize());
2163}
2164
2165template<class T>
2166void ObjectArray<T>::Set(int32_t i, T* object) {
2167 if (IsValidIndex(i)) {
2168 if (object != NULL) {
2169 Class* element_class = GetClass()->GetComponentType();
2170 DCHECK(!element_class->IsPrimitive());
2171 // TODO: ArrayStoreException
2172 CHECK(object->InstanceOf(element_class));
2173 }
2174 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2175 SetFieldObject(data_offset, object, false);
2176 }
2177}
2178
2179template<class T>
2180void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2181 DCHECK(IsValidIndex(i));
2182 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2183 SetFieldObject(data_offset, object, false);
2184}
2185
2186template<class T>
2187void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2188 ObjectArray<T>* dst, int dst_pos,
2189 size_t length) {
2190 if (src->IsValidIndex(src_pos) &&
2191 src->IsValidIndex(src_pos+length-1) &&
2192 dst->IsValidIndex(dst_pos) &&
2193 dst->IsValidIndex(dst_pos+length-1)) {
2194 MemberOffset src_offset(DataOffset().Int32Value() +
2195 src_pos * sizeof(Object*));
2196 MemberOffset dst_offset(DataOffset().Int32Value() +
2197 dst_pos * sizeof(Object*));
2198 Class* array_class = dst->GetClass();
2199 if (array_class == src->GetClass()) {
2200 // No need for array store checks if arrays are of the same type
2201 for (size_t i=0; i < length; i++) {
2202 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2203 dst->SetFieldObject(dst_offset, object, false);
2204 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2205 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2206 }
2207 } else {
2208 Class* element_class = array_class->GetComponentType();
2209 CHECK(!element_class->IsPrimitive());
2210 for (size_t i=0; i < length; i++) {
2211 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2212 // TODO: ArrayStoreException
2213 CHECK(object == NULL || object->InstanceOf(element_class));
2214 dst->SetFieldObject(dst_offset, object, false);
2215 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2216 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2217 }
2218 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002219 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002220 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002221}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002222
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002223class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002224 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002225 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002226 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002227 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002228 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2229};
2230
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002231class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002232 private:
2233 CharArray* ASCII_;
2234 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002235 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002236 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002237 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002238 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2239};
2240
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002241class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002242 private:
2243 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2244 uint32_t TYPE_BOOLEAN_;
2245 uint32_t TYPE_BYTE_;
2246 uint32_t TYPE_CHAR_;
2247 uint32_t TYPE_DOUBLE_;
2248 uint32_t TYPE_FLOAT_;
2249 uint32_t TYPE_INTEGER_;
2250 uint32_t TYPE_LONG_;
2251 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002252 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002253 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2254};
2255
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002256class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002257 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002258 ObjectArray<Object>* NO_ANNOTATIONS_;
2259 Object* ORDER_BY_SIGNATURE_;
2260 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002261 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2262};
2263
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002264template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002265class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002266 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002267 typedef T ElementType;
2268
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002269 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002270
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002271 const T* GetData() const {
2272 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002273 }
2274
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002275 T* GetData() {
2276 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002277 }
2278
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002279 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002280 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002281 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002282 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002283 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002284 }
2285
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002286 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002287 // TODO: ArrayStoreException
2288 if (IsValidIndex(i)) {
2289 GetData()[i] = value;
2290 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002291 }
2292
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002293 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002294 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002295 CHECK(array_class != NULL);
2296 array_class_ = array_class;
2297 }
2298
Brian Carlstroma663ea52011-08-19 23:33:41 -07002299 static void ResetArrayClass() {
2300 CHECK(array_class_ != NULL);
2301 array_class_ = NULL;
2302 }
2303
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002304 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002305 // Location of first element.
2306 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002307
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002308 static Class* array_class_;
2309
Carl Shapirof88c9522011-08-06 15:47:38 -07002310 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002311};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002312
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002313inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2314 DCHECK(NULL == GetFieldObject<IntArray*>(
2315 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2316 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2317 new_interfaces_idx, false);
2318}
2319
2320// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002321class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002322 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002323 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002324 const CharArray* result = GetFieldObject<const CharArray*>(
2325 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2326 DCHECK(result != NULL);
2327 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002328 }
2329
Elliott Hughes814e4032011-08-23 12:07:56 -07002330 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002331 int32_t result = GetField32(
2332 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2333 DCHECK_LE(0, result);
2334 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002335 }
2336
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002337 int32_t GetLength() const;
2338
2339 int32_t GetHashCode() const;
2340
2341 void ComputeHashCode() {
2342 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002343 }
2344
Elliott Hughes814e4032011-08-23 12:07:56 -07002345 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002346 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002347 }
2348
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002349 uint16_t CharAt(int32_t index) const;
2350
Brian Carlstromc74255f2011-09-11 22:47:39 -07002351 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002352
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002353 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002354 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002355 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002356
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002357 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002358
Jesse Wilson8989d992011-08-02 13:39:42 -07002359 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002360 const char* utf8_data_in);
2361
2362 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2363
2364 static String* Alloc(Class* java_lang_String, CharArray* array);
2365
2366 bool Equals(const char* modified_utf8) const;
2367
2368 // TODO: do we need this overload? give it a more intention-revealing name.
2369 bool Equals(const StringPiece& modified_utf8) const;
2370
2371 bool Equals(const String* that) const;
2372
2373 // TODO: do we need this overload? give it a more intention-revealing name.
2374 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2375 int32_t that_length) const;
2376
2377 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2378 std::string ToModifiedUtf8() const;
2379
2380 static Class* GetJavaLangString() {
2381 DCHECK(java_lang_String_ != NULL);
2382 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002383 }
2384
Brian Carlstroma663ea52011-08-19 23:33:41 -07002385 static void SetClass(Class* java_lang_String);
2386 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002387
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002388 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002389 void SetHashCode(int32_t new_hash_code) {
2390 DCHECK_EQ(0u,
2391 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2392 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2393 new_hash_code, false);
2394 }
2395
2396 void SetCount(int32_t new_count) {
2397 DCHECK_LE(0, new_count);
2398 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2399 }
2400
2401 void SetOffset(int32_t new_offset) {
2402 DCHECK_LE(0, new_offset);
2403 DCHECK_GE(GetLength(), new_offset);
2404 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2405 }
2406
2407 void SetArray(CharArray* new_array) {
2408 DCHECK(new_array != NULL);
2409 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2410 }
2411
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002412 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2413 CharArray* array_;
2414
Brian Carlstromdbc05252011-09-09 01:59:59 -07002415 int32_t count_;
2416
Carl Shapirof88c9522011-08-06 15:47:38 -07002417 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002418
Elliott Hughes289da822011-08-16 10:11:20 -07002419 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002420
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002421 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002422
Brian Carlstrom693267a2011-09-06 09:25:34 -07002423 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002424 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002425};
2426
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002427inline const String* Field::GetName() const {
2428 DCHECK(GetDeclaringClass()->IsLoaded());
2429 String* result =
2430 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2431 DCHECK(result != NULL);
2432 return result;
2433}
2434
2435inline void Field::SetName(String* new_name) {
2436 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2437 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002438}
2439
2440inline uint32_t Field::GetAccessFlags() const {
2441 DCHECK(GetDeclaringClass()->IsLoaded());
2442 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2443}
2444
2445inline uint32_t Field::GetTypeIdx() const {
2446 DCHECK(GetDeclaringClass()->IsIdxLoaded());
2447 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2448}
2449
2450inline MemberOffset Field::GetOffset() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002451 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002452 return MemberOffset(
2453 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2454}
2455
2456inline MemberOffset Field::GetOffsetDuringLinking() const {
2457 DCHECK(GetDeclaringClass()->IsLoaded());
2458 return MemberOffset(
2459 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2460}
2461
2462inline const String* Method::GetName() const {
2463 DCHECK(GetDeclaringClass()->IsLoaded());
2464 const String* result =
2465 GetFieldObject<const String*>(
2466 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2467 DCHECK(result != NULL);
2468 return result;
2469}
2470
2471inline void Method::SetName(String* new_name) {
2472 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2473 new_name, false);
2474
2475}
2476
jeffhaoe23d93c2011-09-15 14:48:43 -07002477inline ByteArray* Method::GetRegisterMapData() const {
2478 return GetFieldObject<ByteArray*>(
2479 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2480}
2481
jeffhaoa0a764a2011-09-16 10:43:38 -07002482inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002483 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002484 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002485}
2486
2487inline ByteArray* Method::GetRegisterMapHeader() const {
2488 return GetFieldObject<ByteArray*>(
2489 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2490}
2491
jeffhaoa0a764a2011-09-16 10:43:38 -07002492inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002493 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002494 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002495}
2496
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002497inline String* Method::GetShorty() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002498 DCHECK(GetDeclaringClass()->IsLoaded());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002499 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002500 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2501}
2502
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002503inline void Method::SetShorty(String* new_shorty) {
2504 DCHECK(NULL == GetFieldObject<String*>(
2505 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2506 DCHECK_LE(1, new_shorty->GetLength());
2507 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2508}
2509
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002510inline const String* Method::GetSignature() const {
2511 DCHECK(GetDeclaringClass()->IsLoaded());
2512 const String* result =
2513 GetFieldObject<const String*>(
2514 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2515 DCHECK(result != NULL);
2516 return result;
2517}
2518
2519inline void Method::SetSignature(String* new_signature) {
2520 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2521 new_signature, false);
2522}
2523
2524inline uint32_t Class::GetAccessFlags() const {
2525 // Check class is loaded or this is java.lang.String that has a
2526 // circularity issue during loading the names of its members
2527 DCHECK(IsLoaded() || this == String::GetJavaLangString() ||
2528 this == Field::GetJavaLangReflectField() ||
2529 this == Method::GetJavaLangReflectMethod());
2530 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2531}
2532
2533inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002534 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002535 DCHECK(new_descriptor != NULL);
2536 DCHECK_NE(0, new_descriptor->GetLength());
2537 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2538 new_descriptor, false);
2539}
2540
Brian Carlstromc74255f2011-09-11 22:47:39 -07002541inline String* Class::GetSourceFile() const {
2542 DCHECK(IsLoaded());
2543 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2544}
2545
2546inline void Class::SetSourceFile(String* new_source_file) {
2547 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2548}
2549
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002550inline uint32_t Method::GetAccessFlags() const {
2551 DCHECK(GetDeclaringClass()->IsLoaded());
2552 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2553}
2554
2555inline uint16_t Method::GetMethodIndex() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002556 DCHECK(GetDeclaringClass()->IsResolved());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002557 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002558}
2559
2560inline uint16_t Method::NumRegisters() const {
2561 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002562 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002563}
2564
2565inline uint16_t Method::NumIns() const {
2566 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002567 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002568}
2569
2570inline uint16_t Method::NumOuts() const {
2571 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002572 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002573}
2574
2575inline uint32_t Method::GetProtoIdx() const {
2576 DCHECK(GetDeclaringClass()->IsLoaded());
2577 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2578}
2579
2580// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002581class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002582 public:
2583 void SetDetailMessage(String* new_detail_message) {
2584 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2585 new_detail_message, false);
2586 }
2587
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002588 private:
2589 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2590 Throwable* cause_;
2591 String* detail_message_;
2592 Object* stack_state_; // Note this is Java volatile:
2593 Object* stack_trace_;
2594 Object* suppressed_exceptions_;
2595
Brian Carlstrom693267a2011-09-06 09:25:34 -07002596 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002597 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2598};
2599
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002600// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002601class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002602 public:
2603 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002604 return GetFieldObject<const String*>(
2605 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002606 }
2607
2608 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002609 return GetFieldObject<const String*>(
2610 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002611 }
2612
2613 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002614 return GetFieldObject<const String*>(
2615 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002616 }
2617
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002618 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002619 return GetField32(
2620 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002621 }
2622
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002623 static StackTraceElement* Alloc(const String* declaring_class,
2624 const String* method_name,
2625 const String* file_name,
2626 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002627
2628 static void SetClass(Class* java_lang_StackTraceElement);
2629
2630 static void ResetClass();
2631
2632 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002633 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002634 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002635 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002636 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002637 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002638
2639 static Class* GetStackTraceElement() {
2640 DCHECK(java_lang_StackTraceElement_ != NULL);
2641 return java_lang_StackTraceElement_;
2642 }
2643
2644 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002645
2646 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002647 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2648};
2649
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002650class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002651 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002652 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002653 Class* interface = Get(kInterface)->AsClass();
2654 DCHECK(interface != NULL);
2655 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002656 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002657
Brian Carlstrom30b94452011-08-25 21:35:26 -07002658 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002659 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002660 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002661 DCHECK(Get(kInterface) == NULL);
2662 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002663 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002664
Brian Carlstrom86927212011-09-15 11:31:11 -07002665 size_t GetMethodArrayCount() const {
2666 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2667 if (method_array == 0) {
2668 return 0;
2669 }
2670 return method_array->GetLength();
2671 }
2672
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002673 ObjectArray<Method>* GetMethodArray() const {
2674 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2675 DCHECK(method_array != NULL);
2676 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002677 }
2678
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002679 void SetMethodArray(ObjectArray<Method>* new_ma) {
2680 DCHECK(new_ma != NULL);
2681 DCHECK(Get(kMethodArray) == NULL);
2682 Set(kMethodArray, new_ma);
2683 }
2684
2685 static size_t LengthAsArray() {
2686 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002687 }
2688
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002689 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002690
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002691 enum ArrayIndex {
2692 // Points to the interface class.
2693 kInterface = 0,
2694 // Method pointers into the vtable, allow fast map from interface
2695 // method index to concrete instance method.
2696 kMethodArray = 1,
2697 kMax = 2,
2698 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002699
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002700 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002701};
2702
2703} // namespace art
2704
2705#endif // ART_SRC_OBJECT_H_