blob: 03aadda8a6231b5d3bbdd90f7e63be6c540b5e12 [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
26#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "heap.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070028#include "invoke_type.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
Ian Rogers08f753d2012-08-24 14:35:25 -070031#include "modifiers.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "offsets.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070033#include "primitive.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070034#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070037#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070038
39namespace art {
40
41class Array;
42class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070043class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070044class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070045class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040046class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070047class InterfaceEntry;
48class Monitor;
49class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070050class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070051class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040052class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070053template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070054template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070055typedef PrimitiveArray<uint8_t> BooleanArray;
56typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070057typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070058typedef PrimitiveArray<double> DoubleArray;
59typedef PrimitiveArray<float> FloatArray;
60typedef PrimitiveArray<int32_t> IntArray;
61typedef PrimitiveArray<int64_t> LongArray;
62typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070063
Carl Shapiro3ee755d2011-06-28 12:11:04 -070064union JValue {
Elliott Hughes1d878f32012-04-11 15:17:54 -070065 // We default initialize JValue instances to all-zeros.
66 JValue() : j(0) {}
67
Elliott Hughesf24d3ce2012-04-11 17:43:37 -070068 int8_t GetB() const { return b; }
69 void SetB(int8_t new_b) {
70 i = ((static_cast<int32_t>(new_b) << 24) >> 24); // Sign-extend.
71 }
72
73 uint16_t GetC() const { return c; }
74 void SetC(uint16_t new_c) { c = new_c; }
75
76 double GetD() const { return d; }
77 void SetD(double new_d) { d = new_d; }
78
79 float GetF() const { return f; }
80 void SetF(float new_f) { f = new_f; }
81
82 int32_t GetI() const { return i; }
83 void SetI(int32_t new_i) { i = new_i; }
84
85 int64_t GetJ() const { return j; }
86 void SetJ(int64_t new_j) { j = new_j; }
87
88 Object* GetL() const { return l; }
89 void SetL(Object* new_l) { l = new_l; }
90
91 int16_t GetS() const { return s; }
92 void SetS(int16_t new_s) {
93 i = ((static_cast<int32_t>(new_s) << 16) >> 16); // Sign-extend.
94 }
95
96 uint8_t GetZ() const { return z; }
97 void SetZ(uint8_t new_z) { z = new_z; }
98
99 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700100 uint8_t z;
101 int8_t b;
102 uint16_t c;
103 int16_t s;
104 int32_t i;
105 int64_t j;
106 float f;
107 double d;
108 Object* l;
109};
110
Logan Chienfca7e872011-12-20 20:08:22 +0800111#if defined(ART_USE_LLVM_COMPILER)
112namespace compiler_llvm {
113 class InferredRegCategoryMap;
Elliott Hughes48257562012-06-06 17:42:44 -0700114} // namespace compiler_llvm
Logan Chienfca7e872011-12-20 20:08:22 +0800115#endif
116
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700117/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700118 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700119 */
120/*
121 * A magic value for refOffsets. Ignore the bits and walk the super
122 * chain when this is the value.
123 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
124 * fields followed by 2 ref instance fields.]
125 */
126#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700127#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
128#define CLASS_OFFSET_ALIGNMENT 4
129#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
130/*
131 * Given an offset, return the bit number which would encode that offset.
132 * Local use only.
133 */
134#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700135 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700136 CLASS_OFFSET_ALIGNMENT)
137/*
138 * Is the given offset too large to be encoded?
139 */
140#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
141 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
142/*
143 * Return a single bit, encoding the offset.
144 * Undefined if the offset is too large, as defined above.
145 */
146#define CLASS_BIT_FROM_OFFSET(byteOffset) \
147 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
148/*
149 * Return an offset, given a bit number as returned from CLZ.
150 */
151#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700152 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700153
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154#define OFFSET_OF_OBJECT_MEMBER(type, field) \
155 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700157// Classes shared with the managed side of the world need to be packed
158// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700159#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700160
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700162class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700163 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700164 static MemberOffset ClassOffset() {
165 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700166 }
167
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700169 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 }
171
172 void SetClass(Class* new_klass);
173
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 bool InstanceOf(const Class* klass) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700176
Ian Rogersb726dcb2012-09-05 08:57:23 -0700177 size_t SizeOf() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700178
Ian Rogersb726dcb2012-09-05 08:57:23 -0700179 Object* Clone() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700180
181 int32_t IdentityHashCode() const {
182 #ifdef MOVING_GARBAGE_COLLECTOR
183 // TODO: we'll need to use the Object's internal concept of identity
184 UNIMPLEMENTED(FATAL);
185 #endif
186 return reinterpret_cast<int32_t>(this);
187 }
Elliott Hughesbf86d042011-08-31 17:53:14 -0700188
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189 static MemberOffset MonitorOffset() {
190 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
191 }
192
Elliott Hughes5f791332011-09-15 17:45:30 -0700193 volatile int32_t* GetRawLockWordAddress() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194 byte* raw_addr = reinterpret_cast<byte*>(this) +
195 OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
Elliott Hughes5f791332011-09-15 17:45:30 -0700196 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
197 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 }
199
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700200 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201
Ian Rogersb726dcb2012-09-05 08:57:23 -0700202 void MonitorEnter(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 EXCLUSIVE_LOCK_FUNCTION(monitor_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204
Ian Rogersb726dcb2012-09-05 08:57:23 -0700205 bool MonitorExit(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 UNLOCK_FUNCTION(monitor_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700207
Ian Rogersb726dcb2012-09-05 08:57:23 -0700208 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209
Ian Rogersb726dcb2012-09-05 08:57:23 -0700210 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700211
Ian Rogersb726dcb2012-09-05 08:57:23 -0700212 void Wait(int64_t timeout) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700213
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700214 void Wait(int64_t timeout, int32_t nanos)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700216
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700217 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218
219 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700220 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221 return down_cast<Class*>(this);
222 }
223
224 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700225 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226 return down_cast<const Class*>(this);
227 }
228
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700229 bool IsObjectArray() const;
230
231 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700232 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700233
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700234 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700235 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236
Brian Carlstromb63ec392011-08-27 17:38:27 -0700237 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700238
239 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700240 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700241 return down_cast<Array*>(this);
242 }
243
244 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700245 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700246 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 }
248
Elliott Hughesdbb40792011-11-18 17:05:22 -0800249 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700250
251 bool IsMethod() const;
252
253 Method* AsMethod() {
254 DCHECK(IsMethod());
255 return down_cast<Method*>(this);
256 }
257
Brian Carlstrom4873d462011-08-21 15:23:39 -0700258 const Method* AsMethod() const {
259 DCHECK(IsMethod());
260 return down_cast<const Method*>(this);
261 }
262
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 bool IsField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700264
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 Field* AsField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700266 DCHECK(IsField());
267 return down_cast<Field*>(this);
268 }
269
Ian Rogersb726dcb2012-09-05 08:57:23 -0700270 const Field* AsField() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom4873d462011-08-21 15:23:39 -0700271 DCHECK(IsField());
272 return down_cast<const Field*>(this);
273 }
274
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 bool IsReferenceInstance() const;
276
277 bool IsWeakReferenceInstance() const;
278
279 bool IsSoftReferenceInstance() const;
280
281 bool IsFinalizerReferenceInstance() const;
282
283 bool IsPhantomReferenceInstance() const;
284
285 // Accessors for Java type fields
286 template<class T>
287 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700288 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
289 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 return result;
292 }
293
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700294 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800295 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700296 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
297 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800298 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 }
301
302 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800303 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700304 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
305 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700306 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700307 return android_atomic_acquire_load(word_addr);
308 } else {
309 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700310 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 }
312
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700313 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
314 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800315 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700316 }
317 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700318 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700319 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700320 /*
321 * TODO: add an android_atomic_synchronization_store() function and
322 * use it in the 32-bit volatile set handlers. On some platforms we
323 * can use a fast atomic instruction and avoid the barriers.
324 */
325 ANDROID_MEMBAR_STORE();
326 *word_addr = new_value;
327 ANDROID_MEMBAR_FULL();
328 } else {
329 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 }
332
333 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800334 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700335 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700336 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700337 if (UNLIKELY(is_volatile)) {
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700338 uint64_t result = QuasiAtomic::Read64(addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700339 ANDROID_MEMBAR_FULL();
340 return result;
341 } else {
342 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700344 }
345
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700346 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800347 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700348 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
349 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700350 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700351 ANDROID_MEMBAR_STORE();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700352 QuasiAtomic::Swap64(new_value, addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700353 // Post-store barrier not required due to use of atomic op or mutex.
354 } else {
355 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 }
358
359 protected:
360 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361 template<class T>
362 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700363 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700364 }
365
366 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800367 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
368 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 }
370
371 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700372 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700373
Elliott Hughes5f791332011-09-15 17:45:30 -0700374 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700375
Elliott Hughes5f791332011-09-15 17:45:30 -0700376 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700377 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700378 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700379};
380
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500382class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700383 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700385
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 void SetDeclaringClass(Class *new_declaring_class);
387
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 uint32_t GetAccessFlags() const;
389
390 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700391 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700392 }
393
Elliott Hughes80609252011-09-23 17:24:51 -0700394 bool IsPublic() const {
395 return (GetAccessFlags() & kAccPublic) != 0;
396 }
397
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700398 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700400 }
401
jeffhaobdb76512011-09-07 11:43:16 -0700402 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700403 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700404 }
405
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 uint32_t GetDexFieldIndex() const {
407 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
408 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700409
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800410 void SetDexFieldIndex(uint32_t new_idx) {
411 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
412 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700413
414 // Offset to field within an Object
415 MemberOffset GetOffset() const;
416
buzbee34cd9e52011-09-08 14:31:52 -0700417 static MemberOffset OffsetOffset() {
418 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
419 }
420
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 MemberOffset GetOffsetDuringLinking() const;
422
423 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700424
Brian Carlstrom4873d462011-08-21 15:23:39 -0700425 // field access, null object for static fields
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700426 bool GetBoolean(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700427 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700428 void SetBoolean(Object* object, bool z) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700429 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700430 int8_t GetByte(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700431 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700432 void SetByte(Object* object, int8_t b) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700433 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700434 uint16_t GetChar(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700436 void SetChar(Object* object, uint16_t c) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700437 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700438 int16_t GetShort(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700439 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440 void SetShort(Object* object, int16_t s) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700441 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700442 int32_t GetInt(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700443 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700444 void SetInt(Object* object, int32_t i) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700446 int64_t GetLong(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700447 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700448 void SetLong(Object* object, int64_t j) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700449 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 float GetFloat(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700452 void SetFloat(Object* object, float f) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700454 double GetDouble(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700456 void SetDouble(Object* object, double d) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700458 Object* GetObject(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700460 void SetObject(Object* object, const Object* l) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700462
Ian Rogersce9eca62011-10-07 17:11:03 -0700463 // raw field accesses
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464 uint32_t Get32(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700466 void Set32(Object* object, uint32_t new_value) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700467 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700468 uint64_t Get64(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700469 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700470 void Set64(Object* object, uint64_t new_value) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700471 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700472 Object* GetObj(const Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700473 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700474 void SetObj(Object* object, const Object* new_value) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700476
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700477 static Class* GetJavaLangReflectField() {
478 DCHECK(java_lang_reflect_Field_ != NULL);
479 return java_lang_reflect_Field_;
480 }
481
482 static void SetClass(Class* java_lang_reflect_Field);
483 static void ResetClass();
484
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700485 bool IsVolatile() const {
486 return (GetAccessFlags() & kAccVolatile) != 0;
487 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700488
buzbee1da522d2011-09-04 11:22:20 -0700489 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400490 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800491 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400492 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700493
Jesse Wilson35baaab2011-08-10 16:18:03 -0400494 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700495
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800496 // Dex cache index of field id
497 uint32_t field_dex_idx_;
498
Brian Carlstrom693267a2011-09-06 09:25:34 -0700499 // Offset of field within an instance or in the Class' static fields
500 uint32_t offset_;
501
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700502 static Class* java_lang_reflect_Field_;
503
Brian Carlstrom693267a2011-09-06 09:25:34 -0700504 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400505 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700506};
507
Jesse Wilson6bf19152011-09-29 13:12:33 -0400508// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500509class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510 public:
Ian Rogers1b09b092012-08-20 15:35:52 -0700511 // A function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700512 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700513 Object* obj,
514 Thread* thread,
Elliott Hughes77405792012-03-15 15:22:12 -0700515 JValue* args,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700516 JValue* result);
517
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700518 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700519
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700520 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700521
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700522 static MemberOffset DeclaringClassOffset() {
523 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700524 }
525
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700526 uint32_t GetAccessFlags() const;
527
528 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700529 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700530 }
531
Ian Rogers08f753d2012-08-24 14:35:25 -0700532 // Approximate what kind of method call would be used for this method.
533 InvokeType GetInvokeType() const;
534
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535 // Returns true if the method is declared public.
536 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700537 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700538 }
539
540 // Returns true if the method is declared private.
541 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543 }
544
545 // Returns true if the method is declared static.
546 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700547 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700548 }
549
Brian Carlstrom1f870082011-08-23 16:02:11 -0700550 // Returns true if the method is a constructor.
551 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700552 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700553 }
554
555 // Returns true if the method is static, private, or a constructor.
556 bool IsDirect() const {
Brian Carlstromf5822582012-03-19 22:34:31 -0700557 return IsDirect(GetAccessFlags());
558 }
559
560 static bool IsDirect(uint32_t access_flags) {
561 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700562 }
563
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700564 // Returns true if the method is declared synchronized.
565 bool IsSynchronized() const {
566 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700567 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568 }
569
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700570 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700571 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700572 }
573
Elliott Hughes80609252011-09-23 17:24:51 -0700574 bool IsMiranda() const {
575 return (GetAccessFlags() & kAccMiranda) != 0;
576 }
577
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700579 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700580 }
581
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700582 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700583 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700584 }
585
586 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700587 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700588 }
589
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800590 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700591
Ian Rogers08f753d2012-08-24 14:35:25 -0700592 bool CheckIncompatibleClassChange(InvokeType type);
593
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700594 uint16_t GetMethodIndex() const;
595
596 size_t GetVtableIndex() const {
597 return GetMethodIndex();
598 }
599
600 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700601 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700602 }
603
604 static MemberOffset MethodIndexOffset() {
605 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
606 }
607
608 uint32_t GetCodeItemOffset() const {
609 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
610 }
611
612 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700613 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700614 }
615
616 // Number of 32bit registers that would be required to hold all the arguments
617 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700618
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800619 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700620
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800621 void SetDexMethodIndex(uint32_t new_idx) {
622 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700623 }
624
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700625 ObjectArray<String>* GetDexCacheStrings() const;
626 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
627
628 static MemberOffset DexCacheStringsOffset() {
629 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
630 }
631
Ian Rogers19846512012-02-24 11:42:47 -0800632 static MemberOffset DexCacheResolvedMethodsOffset() {
633 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
634 }
635
buzbee2a475e72011-09-07 17:19:17 -0700636 static MemberOffset DexCacheResolvedTypesOffset() {
637 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
638 }
639
buzbee1da522d2011-09-04 11:22:20 -0700640 static MemberOffset DexCacheInitializedStaticStorageOffset() {
641 return OFFSET_OF_OBJECT_MEMBER(Method,
642 dex_cache_initialized_static_storage_);
643 }
644
Ian Rogers19846512012-02-24 11:42:47 -0800645 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
646 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
647
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700648 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
649 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
650
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
652 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
653
Ian Rogers466bb252011-10-14 03:29:56 -0700654 // Find the method that this method overrides
Ian Rogersb726dcb2012-09-05 08:57:23 -0700655 Method* FindOverriddenMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers466bb252011-10-14 03:29:56 -0700656
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700658 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700659
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700660 const void* GetCode() const {
661 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700662 }
663
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700664 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700665 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
666 }
667
Ian Rogersb726dcb2012-09-05 08:57:23 -0700668 uint32_t GetCodeSize() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700669 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
670 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
671 if (code == 0) {
672 return 0;
673 }
674 // TODO: make this Thumb2 specific
675 code &= ~0x1;
676 return reinterpret_cast<uint32_t*>(code)[-1];
677 }
678
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700679 bool IsWithinCode(uintptr_t pc) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700680 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700681 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
682 if (code == 0) {
683 return pc == 0;
684 }
buzbee8320f382012-09-11 16:29:42 -0700685 /*
686 * During a stack walk, a return PC may point to the end of the code + 1
687 * (in the case that the last instruction is a call that isn't expected to
688 * return. Thus, we check <= code + GetCodeSize().
689 */
690 return (code <= pc && pc <= code + GetCodeSize());
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700691 }
692
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 void AssertPcIsWithinCode(uintptr_t pc) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700695
Brian Carlstrome24fa612011-09-29 00:53:55 -0700696 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700697 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700698 return reinterpret_cast<uint32_t>(GetCode());
699 }
700
701 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700702 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700703 SetCode(reinterpret_cast<void*>(code_offset));
704 }
705
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700706 static MemberOffset GetCodeOffset() {
707 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700708 }
709
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700710 const uint32_t* GetMappingTable() const {
711 const uint32_t* map = GetMappingTableRaw();
712 if (map == NULL) {
713 return map;
714 }
715 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700716 }
717
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700718 uint32_t GetMappingTableLength() const {
719 const uint32_t* map = GetMappingTableRaw();
720 if (map == NULL) {
721 return 0;
722 }
723 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700724 }
725
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700726 const uint32_t* GetMappingTableRaw() const {
727 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700728 }
729
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700730 void SetMappingTable(const uint32_t* mapping_table) {
731 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
732 mapping_table, false);
733 }
734
735 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700736 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700737 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
738 }
739
740 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700741 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700742 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
743 }
744
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800745 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700746 const uint16_t* GetVmapTableRaw() const {
747 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
748 }
749
750 void SetVmapTable(const uint16_t* vmap_table) {
751 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
752 }
753
754 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700755 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700756 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
757 }
758
759 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700760 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700761 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700762 }
763
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800764 const uint8_t* GetGcMap() const {
765 const uint8_t* gc_map_raw = GetGcMapRaw();
766 if (gc_map_raw == NULL) {
767 return gc_map_raw;
768 }
769 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 }
771
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800772 uint32_t GetGcMapLength() const {
773 const uint8_t* gc_map_raw = GetGcMapRaw();
774 if (gc_map_raw == NULL) {
775 return 0;
776 }
777 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
778 (gc_map_raw[1] << 16) |
779 (gc_map_raw[2] << 8) |
780 (gc_map_raw[3] << 0));
781 }
782
783 const uint8_t* GetGcMapRaw() const {
784 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
785 }
786 void SetGcMap(const uint8_t* data) {
787 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
788 }
789
790 uint32_t GetOatGcMapOffset() const {
791 DCHECK(!Runtime::Current()->IsStarted());
792 return reinterpret_cast<uint32_t>(GetGcMapRaw());
793 }
794 void SetOatGcMapOffset(uint32_t gc_map_offset) {
795 DCHECK(!Runtime::Current()->IsStarted());
796 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 }
798
Shih-wei Liaod11af152011-08-23 16:02:11 -0700799 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700800 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
801 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700802 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
803 return result;
804 }
805
806 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700807 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700808 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
809 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700810 }
811
Shih-wei Liaod11af152011-08-23 16:02:11 -0700812 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700814 }
815
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700816 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700817
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700818 void RegisterNative(Thread* self, const void* native_method)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700819 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb033c752011-07-20 12:22:35 -0700820
Ian Rogersb726dcb2012-09-05 08:57:23 -0700821 void UnregisterNative(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700822
Ian Rogersb033c752011-07-20 12:22:35 -0700823 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700824 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700825 }
826
Brian Carlstrom78128a62011-09-15 17:21:19 -0700827 const void* GetNativeMethod() const {
828 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
829 }
830
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700831 // Native to managed invocation stub entry point
Ian Rogers1b09b092012-08-20 15:35:52 -0700832 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700833 InvokeStub* result = GetFieldPtr<InvokeStub*>(
834 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
835 // TODO: DCHECK(result != NULL); should be ahead of time compiled
836 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700837 }
838
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700839 void SetInvokeStub(InvokeStub* invoke_stub) {
Ian Rogers1b09b092012-08-20 15:35:52 -0700840 SetFieldPtr<InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
841 invoke_stub, false);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700842 }
843
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700844 uint32_t GetInvokeStubSize() const {
Logan Chien4284bb92012-06-06 15:30:44 +0800845 uintptr_t invoke_stub = reinterpret_cast<uintptr_t>(GetInvokeStub());
846 if (invoke_stub == 0) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700847 return 0;
848 }
Logan Chien4284bb92012-06-06 15:30:44 +0800849 // TODO: make this Thumb2 specific
850 invoke_stub &= ~0x1;
851 return reinterpret_cast<const uint32_t*>(invoke_stub)[-1];
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700852 }
853
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700854 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700855 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700856 return reinterpret_cast<uint32_t>(GetInvokeStub());
857 }
858
859 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700860 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700861 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
862 }
863
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700864 static MemberOffset GetInvokeStubOffset() {
865 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700866 }
867
buzbee561227c2011-09-02 15:28:19 -0700868 static MemberOffset GetMethodIndexOffset() {
869 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
870 }
871
Ian Rogers90865722011-09-19 11:11:44 -0700872 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700873 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700874 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700875
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700876 void SetCoreSpillMask(uint32_t core_spill_mask) {
877 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700878 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700879 }
880
Ian Rogers90865722011-09-19 11:11:44 -0700881 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700882 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
883 }
884
885 void SetFpSpillMask(uint32_t fp_spill_mask) {
886 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700887 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700888 }
889
Ian Rogers57b86d42012-03-27 16:05:41 -0700890 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
Ian Rogers640495b2012-06-22 15:15:47 -0700891 // conventions for a method of managed code. Returns false for Proxy methods.
Ian Rogers57b86d42012-03-27 16:05:41 -0700892 bool IsRuntimeMethod() const {
893 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
894 }
895
Ian Rogers90865722011-09-19 11:11:44 -0700896 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700897 bool IsCalleeSaveMethod() const {
Ian Rogers57b86d42012-03-27 16:05:41 -0700898 if (!IsRuntimeMethod()) {
899 return false;
900 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700901 Runtime* runtime = Runtime::Current();
902 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700903 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700904 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
905 result = true;
906 break;
907 }
908 }
Ian Rogers90865722011-09-19 11:11:44 -0700909 return result;
910 }
911
Ian Rogers19846512012-02-24 11:42:47 -0800912 bool IsResolutionMethod() const {
913 bool result = this == Runtime::Current()->GetResolutionMethod();
914 // Check that if we do think it is phony it looks like the resolution method
915 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
916 return result;
917 }
918
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700919 // Converts a native PC to a dex PC. TODO: this is a no-op
920 // until we associate a PC mapping table with each method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700921 uint32_t ToDexPC(const uintptr_t pc) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700922 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700923
924 // Converts a dex PC to a native PC. TODO: this is a no-op
925 // until we associate a PC mapping table with each method.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700926 uintptr_t ToNativePC(const uint32_t dex_pc) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700927 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersbdb03912011-09-14 00:55:44 -0700928
929 // Find the catch block for the given exception type and dex_pc
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700930 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700931 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700932
Elliott Hughes80609252011-09-23 17:24:51 -0700933 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
934
935 static Class* GetConstructorClass() {
936 return java_lang_reflect_Constructor_;
937 }
938
939 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700940 return java_lang_reflect_Method_;
941 }
942
Elliott Hughes80609252011-09-23 17:24:51 -0700943 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700944
945 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700946 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800947 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700948 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700949
Brian Carlstrom693267a2011-09-06 09:25:34 -0700950 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800951 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700952
953 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800954 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700955
956 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700957 ObjectArray<Class>* dex_cache_resolved_types_;
958
959 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
960 ObjectArray<String>* dex_cache_strings_;
961
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700962 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700963 uint32_t access_flags_;
964
965 // Compiled code associated with this method for callers from managed code.
966 // May be compiled managed code or a bridge for invoking a native method.
967 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700968
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700969 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700970 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700971
Brian Carlstrom693267a2011-09-06 09:25:34 -0700972 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700973 uint32_t core_spill_mask_;
974
975 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700976 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700977
Brian Carlstrom693267a2011-09-06 09:25:34 -0700978 // Total size in bytes of the frame
979 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700980
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800981 // Garbage collection map
982 const uint8_t* gc_map_;
983
Brian Carlstrom693267a2011-09-06 09:25:34 -0700984 // Native invocation stub entry point for calling from native to managed code.
Ian Rogers1b09b092012-08-20 15:35:52 -0700985 InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700986
Ian Rogersd81871c2011-10-03 13:57:23 -0700987 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700988 const uint32_t* mapping_table_;
989
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800990 // Index into method_ids of the dex file associated with this method
991 uint32_t method_dex_index_;
992
Ian Rogers466bb252011-10-14 03:29:56 -0700993 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700994 //
Ian Rogers466bb252011-10-14 03:29:56 -0700995 // For abstract methods in an interface class, this is the offset of the method in
996 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -0800997 //
998 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700999 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001000
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001001 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001002 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001003
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 // When a register is promoted into a register, the spill mask holds which registers hold dex
1005 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
1006 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001007 const uint16_t* vmap_table_;
1008
Elliott Hughes80609252011-09-23 17:24:51 -07001009 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001010 static Class* java_lang_reflect_Method_;
1011
Brian Carlstrom693267a2011-09-06 09:25:34 -07001012 friend class ImageWriter; // for relocating code_ and invoke_stub_
1013 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001014 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001015};
1016
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001017class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001018 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001019 // A convenience for code that doesn't know the component size,
1020 // and doesn't want to have to work it out itself.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001021 static Array* Alloc(Class* array_class, int32_t component_count)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001022 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001023
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001024 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001025 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapirof88c9522011-08-06 15:47:38 -07001026
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001027 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001028
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001029 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001030 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001031 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001032
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001033 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001034 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001035 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001036 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001037
buzbeec143c552011-08-20 17:38:58 -07001038 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001039 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001040 }
1041
Ian Rogersa15e67d2012-02-28 13:51:55 -08001042 static MemberOffset DataOffset(size_t component_size) {
1043 if (component_size != sizeof(int64_t)) {
1044 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
1045 } else {
1046 // Align longs and doubles.
1047 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
1048 }
buzbeec143c552011-08-20 17:38:58 -07001049 }
1050
Ian Rogersa15e67d2012-02-28 13:51:55 -08001051 void* GetRawData(size_t component_size) {
1052 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1053 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001054 }
1055
Elliott Hughesa21039c2012-06-21 12:09:25 -07001056 const void* GetRawData(size_t component_size) const {
1057 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1058 return reinterpret_cast<const void*>(data);
1059 }
1060
Elliott Hughes289da822011-08-16 10:11:20 -07001061 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001062 bool IsValidIndex(int32_t index) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001063 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001064 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001065 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001066 }
1067 return true;
1068 }
1069
Elliott Hughes80609252011-09-23 17:24:51 -07001070 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001071 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001072 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001073 bool ThrowArrayStoreException(Object* object) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001074 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes80609252011-09-23 17:24:51 -07001075
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001076 private:
1077 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001078 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001079 // Marker for the data (used by generated code)
1080 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001081
Carl Shapirof88c9522011-08-06 15:47:38 -07001082 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001083};
1084
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001085template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001086class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001087 public:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001090
Ian Rogersb726dcb2012-09-05 08:57:23 -07001091 T* Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001092
Ian Rogersb726dcb2012-09-05 08:57:23 -07001093 void Set(int32_t i, T* object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001094
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001095 // Set element without bound and element type checks, to be used in limited
1096 // circumstances, such as during boot image writing
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097 void SetWithoutChecks(int32_t i, T* object)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapirof88c9522011-08-06 15:47:38 -07001099
Ian Rogersb726dcb2012-09-05 08:57:23 -07001100 T* GetWithoutChecks(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers5d76c432011-10-31 21:42:49 -07001101
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001102 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001103 ObjectArray<T>* dst, int dst_pos,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001104 size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapirof88c9522011-08-06 15:47:38 -07001106
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107 ObjectArray<T>* CopyOf(int32_t new_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001109
1110 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001111 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001112};
1113
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001114template<class T>
1115ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001116 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001117 if (UNLIKELY(array == NULL)) {
1118 return NULL;
1119 } else {
1120 return array->AsObjectArray<T>();
1121 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122}
1123
1124template<class T>
1125T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001126 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001127 return NULL;
1128 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001129 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001130 return GetFieldObject<T*>(data_offset, false);
1131}
1132
1133template<class T>
1134ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1135 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1136 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1137 return new_array;
1138}
1139
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001140// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001141// provides the static storage. However, this might change to an Array
1142// to improve image sharing, so we use this type to avoid assumptions
1143// on the current storage.
Elliott Hughes48257562012-06-06 17:42:44 -07001144class MANAGED StaticStorageBase : public Object {
1145};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001146
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001147// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001148class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001149 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001150 // Class Status
1151 //
1152 // kStatusNotReady: If a Class cannot be found in the class table by
1153 // FindClass, it allocates an new one with AllocClass in the
1154 // kStatusNotReady and calls LoadClass. Note if it does find a
1155 // class, it may not be kStatusResolved and it will try to push it
1156 // forward toward kStatusResolved.
1157 //
1158 // kStatusIdx: LoadClass populates with Class with information from
1159 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001160 // Class value in super_class_ has not been populated. The new Class
1161 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001162 //
1163 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1164 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001165 // using ResolveClass to initialize the super_class_ and ensuring the
1166 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001167 //
1168 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001169 // shows linking is complete and fields of the Class populated by making
1170 // it kStatusResolved. Java allows circularities of the form where a super
1171 // class has a field that is of the type of the sub class. We need to be able
1172 // to fully resolve super classes while resolving types for fields.
jeffhaof1e6b7c2012-06-05 18:33:30 -07001173 //
1174 // kStatusRetryVerificationAtRuntime: The verifier sets a class to
1175 // this state if it encounters a soft failure at compile time. This
1176 // often happens when there are unresolved classes in other dex
1177 // files, and this status marks a class as needing to be verified
1178 // again at runtime.
1179 //
1180 // TODO: Explain the other states
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001182 kStatusError = -1,
1183 kStatusNotReady = 0,
Ian Rogers9ffb0392012-09-10 11:56:50 -07001184 kStatusIdx = 1, // Loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_.
1185 kStatusLoaded = 2, // DEX idx values resolved.
1186 kStatusResolved = 3, // Part of linking.
1187 kStatusVerifying = 4, // In the process of being verified.
1188 kStatusRetryVerificationAtRuntime = 5, // Compile time verification failed, retry at runtime.
1189 kStatusVerifyingAtRuntime = 6, // Retrying verification at runtime.
1190 kStatusVerified = 7, // Logically part of linking; done pre-init.
1191 kStatusInitializing = 8, // Class init in progress.
1192 kStatusInitialized = 9, // Ready to go.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001193 };
1194
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001195 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001196 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1197 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001198 }
1199
Ian Rogersb726dcb2012-09-05 08:57:23 -07001200 void SetStatus(Status new_status) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001201
1202 // Returns true if the class has failed to link.
1203 bool IsErroneous() const {
1204 return GetStatus() == kStatusError;
1205 }
1206
1207 // Returns true if the class has been loaded.
1208 bool IsIdxLoaded() const {
1209 return GetStatus() >= kStatusIdx;
1210 }
1211
1212 // Returns true if the class has been loaded.
1213 bool IsLoaded() const {
1214 return GetStatus() >= kStatusLoaded;
1215 }
1216
1217 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001218 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001219 return GetStatus() >= kStatusResolved;
1220 }
1221
jeffhaof1e6b7c2012-06-05 18:33:30 -07001222 // Returns true if the class was compile-time verified.
1223 bool IsCompileTimeVerified() const {
1224 return GetStatus() >= kStatusRetryVerificationAtRuntime;
1225 }
1226
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001227 // Returns true if the class has been verified.
1228 bool IsVerified() const {
1229 return GetStatus() >= kStatusVerified;
1230 }
1231
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001232 // Returns true if the class is initializing.
1233 bool IsInitializing() const {
1234 return GetStatus() >= kStatusInitializing;
1235 }
1236
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001237 // Returns true if the class is initialized.
1238 bool IsInitialized() const {
1239 return GetStatus() == kStatusInitialized;
1240 }
1241
1242 uint32_t GetAccessFlags() const;
1243
1244 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001245 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001246 }
1247
1248 // Returns true if the class is an interface.
1249 bool IsInterface() const {
1250 return (GetAccessFlags() & kAccInterface) != 0;
1251 }
1252
1253 // Returns true if the class is declared public.
1254 bool IsPublic() const {
1255 return (GetAccessFlags() & kAccPublic) != 0;
1256 }
1257
1258 // Returns true if the class is declared final.
1259 bool IsFinal() const {
1260 return (GetAccessFlags() & kAccFinal) != 0;
1261 }
1262
Elliott Hughes20cde902011-10-04 17:37:27 -07001263 bool IsFinalizable() const {
1264 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1265 }
1266
1267 void SetFinalizable() {
1268 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1269 SetAccessFlags(flags | kAccClassIsFinalizable);
1270 }
1271
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001272 // Returns true if the class is abstract.
1273 bool IsAbstract() const {
1274 return (GetAccessFlags() & kAccAbstract) != 0;
1275 }
1276
1277 // Returns true if the class is an annotation.
1278 bool IsAnnotation() const {
1279 return (GetAccessFlags() & kAccAnnotation) != 0;
1280 }
1281
1282 // Returns true if the class is synthetic.
1283 bool IsSynthetic() const {
1284 return (GetAccessFlags() & kAccSynthetic) != 0;
1285 }
1286
1287 bool IsReferenceClass() const {
1288 return (GetAccessFlags() & kAccClassIsReference) != 0;
1289 }
1290
1291 bool IsWeakReferenceClass() const {
1292 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1293 }
1294
1295 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001296 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001297 }
1298
1299 bool IsFinalizerReferenceClass() const {
1300 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1301 }
1302
1303 bool IsPhantomReferenceClass() const {
1304 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1305 }
1306
Ian Rogersd418eda2012-01-30 12:14:28 -08001307
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001308 String* GetName() const; // Returns the cached name
Ian Rogersd418eda2012-01-30 12:14:28 -08001309 void SetName(String* name); // Sets the cached name
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001310 String* ComputeName() // Computes the name, then sets the cached value
Ian Rogersb726dcb2012-09-05 08:57:23 -07001311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001312
1313 bool IsProxyClass() const {
1314 // Read access flags without using getter as whether something is a proxy can be check in
1315 // any loaded state
1316 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1317 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1318 return (access_flags & kAccClassIsProxy) != 0;
1319 }
1320
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001321 Primitive::Type GetPrimitiveType() const {
1322 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1323 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001324 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1325 }
1326
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001327 void SetPrimitiveType(Primitive::Type new_type) {
1328 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001329 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001330 }
1331
1332 // Returns true if the class is a primitive type.
1333 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001334 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001335 }
1336
1337 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001338 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339 }
1340
1341 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001342 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001343 }
1344
1345 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001346 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001347 }
1348
1349 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001350 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001351 }
1352
1353 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001354 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001355 }
1356
1357 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001358 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001359 }
1360
1361 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001362 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 }
1364
1365 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001366 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 }
1368
1369 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001370 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001371 }
1372
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 // Depth of class from java.lang.Object
1374 size_t Depth() {
1375 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001376 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001377 depth++;
1378 }
1379 return depth;
1380 }
1381
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001382 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001383 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001384 }
1385
Elliott Hughesdbb40792011-11-18 17:05:22 -08001386 bool IsClassClass() const;
1387
1388 bool IsStringClass() const;
1389
Ian Rogersb726dcb2012-09-05 08:57:23 -07001390 bool IsThrowableClass() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001391
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001392 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001393 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394 }
1395
1396 void SetComponentType(Class* new_component_type) {
1397 DCHECK(GetComponentType() == NULL);
1398 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001399 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400 }
1401
1402 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001403 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001404 }
1405
1406 bool IsObjectClass() const {
1407 return !IsPrimitive() && GetSuperClass() == NULL;
1408 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001409 bool IsInstantiable() const {
1410 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1411 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001412
Brian Carlstrom1f870082011-08-23 16:02:11 -07001413 // Creates a raw object instance but does not invoke the default constructor.
Ian Rogersb726dcb2012-09-05 08:57:23 -07001414 Object* AllocObject() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001415
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001416 bool IsVariableSize() const {
1417 // Classes and arrays vary in size, and so the object_size_ field cannot
1418 // be used to get their instance size
1419 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001420 }
1421
Brian Carlstrom4873d462011-08-21 15:23:39 -07001422 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001423 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001424 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001425 }
1426
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001427 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001428 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001429 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001430 }
1431
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001432 void SetClassSize(size_t new_class_size)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001433 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001434
Ian Rogersb726dcb2012-09-05 08:57:23 -07001435 size_t GetObjectSize() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001436 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001437 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001438 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001439 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001440 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001441 }
1442
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001443 void SetObjectSize(size_t new_object_size) {
1444 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001445 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001446 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001447 }
1448
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001449 // Returns true if this class is in the same packages as that class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001450 bool IsInSamePackage(const Class* that) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001452
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001453 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001454
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001455 // Returns true if this class can access that class.
Ian Rogersb726dcb2012-09-05 08:57:23 -07001456 bool CanAccess(Class* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001457 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001458 }
1459
Ian Rogersf2391652011-12-14 12:50:52 -08001460 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001461 // Note that access to the class isn't checked in case the declaring class is protected and the
1462 // method has been exposed by a public sub-class
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001463 bool CanAccessMember(Class* access_to, uint32_t member_flags) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersf2391652011-12-14 12:50:52 -08001465 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001466 if (this == access_to) {
1467 return true;
1468 }
Ian Rogersf2391652011-12-14 12:50:52 -08001469 // Public members are trivially accessible
1470 if (member_flags & kAccPublic) {
1471 return true;
1472 }
1473 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001474 if (member_flags & kAccPrivate) {
1475 return false;
1476 }
Ian Rogersf2391652011-12-14 12:50:52 -08001477 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001478 if (member_flags & kAccProtected) {
1479 if (this->IsSubClass(access_to)) {
1480 return true;
1481 }
1482 }
Ian Rogersf2391652011-12-14 12:50:52 -08001483 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001484 return this->IsInSamePackage(access_to);
1485 }
1486
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001487 bool IsSubClass(const Class* klass) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001489
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1491 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1492 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1493 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1494 // to themselves. Classes for primitive types may not assign to each other.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001495 bool IsAssignableFrom(const Class* src) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001496 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001497 DCHECK(src != NULL);
1498 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001499 // Can always assign to things of the same type
1500 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001501 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001502 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001503 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001504 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001505 return src->Implements(this);
1506 } else if (src->IsArrayClass()) {
1507 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001508 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001510 }
1511 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001512
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001513 Class* GetSuperClass() const {
1514 // Can only get super class for loaded classes (hack for when runtime is
1515 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001516 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001517 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001518 }
1519
1520 void SetSuperClass(Class *new_super_class) {
1521 // super class is assigned once, except during class linker initialization
1522 Class* old_super_class = GetFieldObject<Class*>(
1523 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1524 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1525 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001526 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001527 }
1528
1529 bool HasSuperClass() const {
1530 return GetSuperClass() != NULL;
1531 }
1532
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001533 static MemberOffset SuperClassOffset() {
1534 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001535 }
1536
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001537 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001538
Ian Rogers365c1022012-06-22 15:05:28 -07001539 void SetClassLoader(ClassLoader* new_cl);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001540
1541 static MemberOffset DexCacheOffset() {
1542 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1543 }
1544
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001545 enum {
1546 kDumpClassFullDetail = 1,
1547 kDumpClassClassLoader = (1 << 1),
1548 kDumpClassInitialized = (1 << 2),
1549 };
1550
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001551 void DumpClass(std::ostream& os, int flags) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001552 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001553
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001554 DexCache* GetDexCache() const;
1555
1556 void SetDexCache(DexCache* new_dex_cache);
1557
1558 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001559 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001560 return GetFieldObject<ObjectArray<Method>*>(
1561 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1562 }
1563
1564 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1565 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1566 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1567 DCHECK_NE(0, new_direct_methods->GetLength());
1568 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1569 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001570 }
1571
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001572 Method* GetDirectMethod(int32_t i) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001573 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001574 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001575 }
1576
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001577 void SetDirectMethod(uint32_t i, Method* f) // TODO: uint16_t
Ian Rogersb726dcb2012-09-05 08:57:23 -07001578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001579 ObjectArray<Method>* direct_methods =
1580 GetFieldObject<ObjectArray<Method>*>(
1581 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1582 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001583 }
1584
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001585 // Returns the number of static, private, and constructor methods.
1586 size_t NumDirectMethods() const {
1587 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1588 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001589
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001590 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001591 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001592 return GetFieldObject<ObjectArray<Method>*>(
1593 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1594 }
1595
1596 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1597 // TODO: we reassign virtual methods to grow the table for miranda
1598 // methods.. they should really just be assigned once
1599 DCHECK_NE(0, new_virtual_methods->GetLength());
1600 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1601 new_virtual_methods, false);
1602 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001603
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001604 // Returns the number of non-inherited virtual methods.
1605 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001606 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001607 }
1608
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001609 Method* GetVirtualMethod(uint32_t i) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001610 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001611 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001612 return GetVirtualMethods()->Get(i);
1613 }
1614
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001615 Method* GetVirtualMethodDuringLinking(uint32_t i) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001616 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001617 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001618 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001619 }
1620
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001621 void SetVirtualMethod(uint32_t i, Method* f) // TODO: uint16_t
Ian Rogersb726dcb2012-09-05 08:57:23 -07001622 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001623 ObjectArray<Method>* virtual_methods =
1624 GetFieldObject<ObjectArray<Method>*>(
1625 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1626 virtual_methods->Set(i, f);
1627 }
1628
1629 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001630 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001631 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001632 }
1633
1634 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001635 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001636 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001637 }
1638
1639 void SetVTable(ObjectArray<Method>* new_vtable) {
1640 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1641 }
1642
1643 static MemberOffset VTableOffset() {
1644 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001645 }
1646
Brian Carlstrom30b94452011-08-25 21:35:26 -07001647 // Given a method implemented by this class but potentially from a
1648 // super class, return the specific implementation
1649 // method for this class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001650 Method* FindVirtualMethodForVirtual(Method* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001651 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001652 DCHECK(!method->GetDeclaringClass()->IsInterface());
1653 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001654 // Use the index to a potentially overridden one for this instance's class.
1655 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001656 }
1657
1658 // Given a method implemented by this class, but potentially from a
1659 // super class or interface, return the specific implementation
1660 // method for this class.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001661 Method* FindVirtualMethodForInterface(Method* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001663
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001664 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001665 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhaobdb76512011-09-07 11:43:16 -07001666
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001667 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001668 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001669
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001670 Method* FindVirtualMethodForVirtualOrInterface(Method* method)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001672 if (method->IsDirect()) {
1673 return method;
1674 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001675 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001676 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001677 }
1678 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001679 }
1680
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001681 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001682 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001683
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001684 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001685 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001686
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001687 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001688 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001689
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001690 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001691 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001692
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001693 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001695
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001696 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001697 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001698
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001699 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001700 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001701
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001702 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001703 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001704
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001705 int32_t GetIfTableCount() const {
1706 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1707 if (iftable == NULL) {
1708 return 0;
1709 }
1710 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001711 }
1712
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001713 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001714 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001715 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001716 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1717 }
1718
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001719 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001720 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001721 }
1722
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001723 // Get instance fields of the class (See also GetSFields).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001724 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001725 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001726 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001727 }
1728
1729 void SetIFields(ObjectArray<Field>* new_ifields) {
1730 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1731 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001732 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001733 }
1734
1735 size_t NumInstanceFields() const {
1736 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1737 }
1738
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001739 Field* GetInstanceField(uint32_t i) const // TODO: uint16_t
Ian Rogersb726dcb2012-09-05 08:57:23 -07001740 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001741 DCHECK_NE(NumInstanceFields(), 0U);
1742 return GetIFields()->Get(i);
1743 }
1744
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001745 void SetInstanceField(uint32_t i, Field* f) // TODO: uint16_t
Ian Rogersb726dcb2012-09-05 08:57:23 -07001746 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001747 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1748 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1749 ifields->Set(i, f);
1750 }
1751
1752 // Returns the number of instance fields containing reference types.
1753 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001754 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001755 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001756 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001757 }
1758
1759 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001760 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001761 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001762 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001763 }
1764
1765 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001766 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001767 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001768 }
1769
1770 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001771 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001772 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001773 }
1774
1775 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1776
1777 // Beginning of static field data
1778 static MemberOffset FieldsOffset() {
1779 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1780 }
1781
1782 // Returns the number of static fields containing reference types.
1783 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001784 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001785 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001786 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001787 }
1788
1789 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001790 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001791 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001792 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001793 }
1794
1795 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001796 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001797 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001798 }
1799
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001800 // Gets the static fields of the class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001801 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001802 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001803 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804 }
1805
1806 void SetSFields(ObjectArray<Field>* new_sfields) {
1807 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1808 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001809 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001810 }
1811
1812 size_t NumStaticFields() const {
1813 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1814 }
1815
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001816 Field* GetStaticField(uint32_t i) const // TODO: uint16_t
Ian Rogersb726dcb2012-09-05 08:57:23 -07001817 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001818 return GetSFields()->Get(i);
1819 }
1820
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001821 void SetStaticField(uint32_t i, Field* f) // TODO: uint16_t
Ian Rogersb726dcb2012-09-05 08:57:23 -07001822 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001823 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1824 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1825 sfields->Set(i, f);
1826 }
1827
1828 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001829 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001830 }
1831
1832 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1833
Ian Rogersb067ac22011-12-13 18:05:09 -08001834 // Find a static or instance field using the JLS resolution order
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001835 Field* FindField(const StringPiece& name, const StringPiece& type)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001836 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersb067ac22011-12-13 18:05:09 -08001837
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001838 // Finds the given instance field in this class or a superclass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001839 Field* FindInstanceField(const StringPiece& name, const StringPiece& type)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001840 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001841
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001842 // Finds the given instance field in this class or a superclass, only searches classes that
1843 // have the same dex cache.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001844 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001845 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001846
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001847 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001848 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001849
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001850 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001851 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001852
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001853 // Finds the given static field in this class or a superclass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001854 Field* FindStaticField(const StringPiece& name, const StringPiece& type)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001855 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001856
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001857 // Finds the given static field in this class or superclass, only searches classes that
1858 // have the same dex cache.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001859 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001860 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001861
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001862 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001863 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001864
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001865 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001866 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001867
Elliott Hughesdcc24742011-09-07 14:02:44 -07001868 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001869 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001870 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1871 }
1872
Elliott Hughesdcc24742011-09-07 14:02:44 -07001873 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001874 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001875 }
1876
1877 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001878 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001879 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001880 }
1881
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001882 uint16_t GetDexTypeIndex() const {
1883 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001884 }
1885
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001886 void SetDexTypeIndex(uint16_t type_idx) {
1887 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001888 }
1889
jeffhaobdb76512011-09-07 11:43:16 -07001890 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001891 void SetVerifyErrorClass(Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001892 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001893 CHECK(klass != NULL) << PrettyClass(this);
1894 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1895 }
1896
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001897 bool Implements(const Class* klass) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001898 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001899 bool IsArrayAssignableFromArray(const Class* klass) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001900 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001901 bool IsAssignableFromArray(const Class* klass) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07001902 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001903
Brian Carlstrom693267a2011-09-06 09:25:34 -07001904 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001905 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001906
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001907 // For array classes, the component class object for instanceof/checkcast
1908 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001909 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001910
Elliott Hughes81ff3182012-03-23 20:35:56 -07001911 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
1912 // runtime such as arrays and primitive classes).
Brian Carlstrom693267a2011-09-06 09:25:34 -07001913 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001914
1915 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001916 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001917
Brian Carlstrom693267a2011-09-06 09:25:34 -07001918 // instance fields
1919 //
1920 // These describe the layout of the contents of an Object.
1921 // Note that only the fields directly declared by this class are
1922 // listed in ifields; fields declared by a superclass are listed in
1923 // the superclass's Class.ifields.
1924 //
1925 // All instance fields that refer to objects are guaranteed to be at
1926 // the beginning of the field list. num_reference_instance_fields_
1927 // specifies the number of reference fields.
1928 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001929
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001930 // Interface table (iftable_), one entry per interface supported by
1931 // this class. That means one entry for each interface we support
1932 // directly, indirectly via superclass, or indirectly via
1933 // superinterface. This will be null if neither we nor our
1934 // superclass implement any interfaces.
1935 //
1936 // Why we need this: given "class Foo implements Face", declare
1937 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1938 // is part of the Face interface. We can't easily use a single
1939 // vtable.
1940 //
1941 // For every interface a concrete class implements, we create an array
1942 // of the concrete vtable_ methods for the methods in the interface.
1943 ObjectArray<InterfaceEntry>* iftable_;
1944
Ian Rogersd418eda2012-01-30 12:14:28 -08001945 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1946 String* name_;
1947
Brian Carlstromdbc05252011-09-09 01:59:59 -07001948 // Static fields
1949 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001950
Ian Rogersd418eda2012-01-30 12:14:28 -08001951 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001952 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001953
Brian Carlstromdbc05252011-09-09 01:59:59 -07001954 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001955 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001956
Brian Carlstromdbc05252011-09-09 01:59:59 -07001957 // virtual methods defined in this class; invoked through vtable
1958 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001959
Ian Rogers9074b992011-10-26 17:41:55 -07001960 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1961 // copied in, and virtual methods from our class either replace those from the super or are
1962 // appended. For abstract classes, methods may be created in the vtable that aren't in
1963 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001964 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001965
Brian Carlstromdbc05252011-09-09 01:59:59 -07001966 // access flags; low 16 bits are defined by VM spec
1967 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001968
Jesse Wilson6bf19152011-09-29 13:12:33 -04001969 // Total size of the Class instance; used when allocating storage on gc heap.
1970 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001971 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001972
Elliott Hughes5f791332011-09-15 17:45:30 -07001973 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001974 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001975
Ian Rogersd418eda2012-01-30 12:14:28 -08001976 // type index from dex file
1977 // TODO: really 16bits
1978 uint32_t dex_type_idx_;
1979
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.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001988 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001989 size_t object_size_;
1990
Ian Rogersd418eda2012-01-30 12:14:28 -08001991 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001992 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001993
1994 // Bitmap of offsets of ifields.
1995 uint32_t reference_instance_offsets_;
1996
1997 // Bitmap of offsets of sfields.
1998 uint32_t reference_static_offsets_;
1999
Brian Carlstrom693267a2011-09-06 09:25:34 -07002000 // state of class initialization
2001 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002002
Brian Carlstrom693267a2011-09-06 09:25:34 -07002003 // TODO: ?
2004 // initiating class loader list
2005 // NOTE: for classes with low serialNumber, these are unused, and the
2006 // values are kept in a table in gDvm.
2007 // InitiatingLoaderList initiating_loader_list_;
2008
Brian Carlstrom4873d462011-08-21 15:23:39 -07002009 // Location of first static field.
2010 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002011
Brian Carlstrom693267a2011-09-06 09:25:34 -07002012 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002013 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002014};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002015
Elliott Hughes1f359b02011-07-17 14:27:17 -07002016std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002017
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002018inline void Object::SetClass(Class* new_klass) {
2019 // new_klass may be NULL prior to class linker initialization
Mathieu Chartierfd678be2012-08-30 14:50:54 -07002020 // We don't mark the card since the class is guaranteed to be referenced from another location.
2021 // Proxy classes are held live by the class loader, and other classes are roots of the class
2022 // linker.
2023 SetFieldPtr(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
2037inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002038 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002039}
2040
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002041template<class T>
2042inline ObjectArray<T>* Object::AsObjectArray() {
2043 DCHECK(IsObjectArray());
2044 return down_cast<ObjectArray<T>*>(this);
2045}
2046
2047template<class T>
2048inline const ObjectArray<T>* Object::AsObjectArray() const {
2049 DCHECK(IsObjectArray());
2050 return down_cast<const ObjectArray<T>*>(this);
2051}
2052
Brian Carlstromb63ec392011-08-27 17:38:27 -07002053inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002054 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002055}
2056
Brian Carlstroma663ea52011-08-19 23:33:41 -07002057inline bool Object::IsField() const {
2058 Class* java_lang_Class = klass_->klass_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002059 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002060 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002061}
2062
2063inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002064 Class* c = GetClass();
2065 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002066}
2067
2068inline bool Object::IsReferenceInstance() const {
2069 return GetClass()->IsReferenceClass();
2070}
2071
2072inline bool Object::IsWeakReferenceInstance() const {
2073 return GetClass()->IsWeakReferenceClass();
2074}
2075
2076inline bool Object::IsSoftReferenceInstance() const {
2077 return GetClass()->IsSoftReferenceClass();
2078}
2079
2080inline bool Object::IsFinalizerReferenceInstance() const {
2081 return GetClass()->IsFinalizerReferenceClass();
2082}
2083
2084inline bool Object::IsPhantomReferenceInstance() const {
2085 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002086}
2087
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002088inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002089 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002090 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002091 result = AsArray()->SizeOf();
2092 } else if (IsClass()) {
2093 result = AsClass()->SizeOf();
2094 } else {
2095 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002096 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002097 DCHECK(!IsField() || result == sizeof(Field));
2098 DCHECK(!IsMethod() || result == sizeof(Method));
2099 return result;
2100}
2101
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002102inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002103 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002104 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002105 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002106 return result;
2107}
2108
2109inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002110 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002111}
2112
2113inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002114 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08002115 DCHECK(result != NULL) << this;
2116 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002117 return result;
2118}
2119
2120inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002121 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002122}
2123
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002124inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002125 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08002126 size_t component_size = GetClass()->GetComponentSize();
2127 int32_t component_count = GetLength();
2128 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
2129 size_t data_size = component_count * component_size;
2130 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002131}
2132
2133template<class T>
2134void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002135 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002136 if (object != NULL) {
2137 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002138 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002139 ThrowArrayStoreException(object);
2140 return;
2141 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002142 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08002143 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002144 SetFieldObject(data_offset, object, false);
2145 }
2146}
2147
2148template<class T>
2149void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2150 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002151 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002152 SetFieldObject(data_offset, object, false);
2153}
2154
2155template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002156T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2157 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002158 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002159 return GetFieldObject<T*>(data_offset, false);
2160}
2161
2162template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002163void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2164 ObjectArray<T>* dst, int dst_pos,
2165 size_t length) {
2166 if (src->IsValidIndex(src_pos) &&
2167 src->IsValidIndex(src_pos+length-1) &&
2168 dst->IsValidIndex(dst_pos) &&
2169 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002170 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2171 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002172 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002173 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002174 if (array_class == src->GetClass()) {
2175 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002176 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002177 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002178 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002179 // directly set field, we do a bulk write barrier at the end
2180 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002181 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2182 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2183 }
2184 } else {
2185 Class* element_class = array_class->GetComponentType();
2186 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002187 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002188 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002189 if (object != NULL && !object->InstanceOf(element_class)) {
2190 dst->ThrowArrayStoreException(object);
2191 return;
2192 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002193 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002194 // directly set field, we do a bulk write barrier at the end
2195 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002196 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2197 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2198 }
2199 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002200 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002201 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002202}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002203
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002204class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002205 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002206 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002207 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002208 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002209 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2210};
2211
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002212class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002213 private:
2214 CharArray* ASCII_;
2215 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002216 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002217 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002218 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002219 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2220};
2221
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002222class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002223 private:
2224 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002225 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002226 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2227};
2228
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002229class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002230 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002231 Object* ORDER_BY_SIGNATURE_;
2232 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002233 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2234};
2235
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002236template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002237class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002238 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002239 typedef T ElementType;
2240
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002241 static PrimitiveArray<T>* Alloc(size_t length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002242 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002243
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002244 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002245 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2246 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002247 }
2248
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002249 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002250 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2251 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002252 }
2253
Ian Rogersb726dcb2012-09-05 08:57:23 -07002254 T Get(int32_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes289da822011-08-16 10:11:20 -07002255 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002256 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002257 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002258 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002259 }
2260
Ian Rogersb726dcb2012-09-05 08:57:23 -07002261 void Set(int32_t i, T value) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes289da822011-08-16 10:11:20 -07002262 // TODO: ArrayStoreException
2263 if (IsValidIndex(i)) {
2264 GetData()[i] = value;
2265 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002266 }
2267
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002268 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002269 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002270 CHECK(array_class != NULL);
2271 array_class_ = array_class;
2272 }
2273
Brian Carlstroma663ea52011-08-19 23:33:41 -07002274 static void ResetArrayClass() {
2275 CHECK(array_class_ != NULL);
2276 array_class_ = NULL;
2277 }
2278
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002279 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002280 static Class* array_class_;
2281
Carl Shapirof88c9522011-08-06 15:47:38 -07002282 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002283};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002284
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002285// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002286class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002287 public:
buzbeefc9e6fa2012-03-23 15:14:29 -07002288 static MemberOffset CountOffset() {
2289 return OFFSET_OF_OBJECT_MEMBER(String, count_);
2290 }
2291
2292 static MemberOffset ValueOffset() {
2293 return OFFSET_OF_OBJECT_MEMBER(String, array_);
2294 }
2295
2296 static MemberOffset OffsetOffset() {
2297 return OFFSET_OF_OBJECT_MEMBER(String, offset_);
2298 }
2299
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002300 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002301 const CharArray* result = GetFieldObject<const CharArray*>(
buzbeefc9e6fa2012-03-23 15:14:29 -07002302 ValueOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002303 DCHECK(result != NULL);
2304 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002305 }
2306
Elliott Hughes814e4032011-08-23 12:07:56 -07002307 int32_t GetOffset() const {
buzbeefc9e6fa2012-03-23 15:14:29 -07002308 int32_t result = GetField32(OffsetOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002309 DCHECK_LE(0, result);
2310 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002311 }
2312
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002313 int32_t GetLength() const;
2314
Ian Rogersb726dcb2012-09-05 08:57:23 -07002315 int32_t GetHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002316
Ian Rogersb726dcb2012-09-05 08:57:23 -07002317 void ComputeHashCode() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002318 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002319 }
2320
Elliott Hughes814e4032011-08-23 12:07:56 -07002321 int32_t GetUtfLength() const {
jeffhao0ce13152012-03-27 19:45:50 -07002322 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002323 }
2324
Ian Rogersb726dcb2012-09-05 08:57:23 -07002325 uint16_t CharAt(int32_t index) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002326
Ian Rogersb726dcb2012-09-05 08:57:23 -07002327 String* Intern() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002328
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002329 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002330 const uint16_t* utf16_data_in,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002331 int32_t hash_code = 0)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002332 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002333
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002334 static String* AllocFromModifiedUtf8(const char* utf)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002336
Jesse Wilson8989d992011-08-02 13:39:42 -07002337 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002338 const char* utf8_data_in)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002339 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002340
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002341 static String* Alloc(Class* java_lang_String, int32_t utf16_length)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002343
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002344 static String* Alloc(Class* java_lang_String, CharArray* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002346
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002347 bool Equals(const char* modified_utf8) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07002348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002349
2350 // TODO: do we need this overload? give it a more intention-revealing name.
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002351 bool Equals(const StringPiece& modified_utf8) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07002352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002353
Ian Rogersb726dcb2012-09-05 08:57:23 -07002354 bool Equals(const String* that) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002355
Ian Rogers0571d352011-11-03 19:51:38 -07002356 // Compare UTF-16 code point values not in a locale-sensitive manner
2357 int Compare(int32_t utf16_length, const char* utf8_data_in);
2358
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002359 // TODO: do we need this overload? give it a more intention-revealing name.
2360 bool Equals(const uint16_t* that_chars, int32_t that_offset,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002361 int32_t that_length) const
Ian Rogersb726dcb2012-09-05 08:57:23 -07002362 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002363
2364 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2365 std::string ToModifiedUtf8() const;
2366
2367 static Class* GetJavaLangString() {
2368 DCHECK(java_lang_String_ != NULL);
2369 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002370 }
2371
Brian Carlstroma663ea52011-08-19 23:33:41 -07002372 static void SetClass(Class* java_lang_String);
2373 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002374
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002375 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002376 void SetHashCode(int32_t new_hash_code) {
2377 DCHECK_EQ(0u,
2378 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2379 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2380 new_hash_code, false);
2381 }
2382
2383 void SetCount(int32_t new_count) {
2384 DCHECK_LE(0, new_count);
2385 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2386 }
2387
2388 void SetOffset(int32_t new_offset) {
2389 DCHECK_LE(0, new_offset);
2390 DCHECK_GE(GetLength(), new_offset);
2391 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2392 }
2393
2394 void SetArray(CharArray* new_array) {
2395 DCHECK(new_array != NULL);
2396 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2397 }
2398
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002399 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2400 CharArray* array_;
2401
Brian Carlstromdbc05252011-09-09 01:59:59 -07002402 int32_t count_;
2403
Carl Shapirof88c9522011-08-06 15:47:38 -07002404 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002405
Elliott Hughes289da822011-08-16 10:11:20 -07002406 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002407
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002408 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002409
Brian Carlstrom693267a2011-09-06 09:25:34 -07002410 friend struct StringOffsets; // for verifying offset information
jeffhao0ce13152012-03-27 19:45:50 -07002411 FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002412 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002413};
2414
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002415// TODO: remove? only used in a unit test of itself.
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002416struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002417 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002418 return string->GetHashCode();
2419 }
2420};
2421
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002422inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002423 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002424 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2425}
2426
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002427inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002428 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002429 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002430}
2431
2432inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002433 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002434 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002435}
2436
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002437inline uint32_t Class::GetAccessFlags() const {
2438 // Check class is loaded or this is java.lang.String that has a
2439 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002440 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002441 this == String::GetJavaLangString() ||
2442 this == Field::GetJavaLangReflectField() ||
2443 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002444 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002445 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2446}
2447
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002448inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002449 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002450 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2451}
2452
2453inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002454 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002455 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002456}
2457
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002458inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002459 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002460 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002461}
2462
Ian Rogers08f753d2012-08-24 14:35:25 -07002463inline bool Method::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers08f753d2012-08-24 14:35:25 -07002464 switch (type) {
2465 case kStatic:
Ian Rogers87e552d2012-08-31 15:54:48 -07002466 return !IsStatic();
Ian Rogers08f753d2012-08-24 14:35:25 -07002467 case kDirect:
Ian Rogers87e552d2012-08-31 15:54:48 -07002468 return !IsDirect() || IsStatic();
2469 case kVirtual: {
2470 Class* methods_class = GetDeclaringClass();
2471 return IsDirect() || (methods_class->IsInterface() && !IsMiranda());
2472 }
Ian Rogers08f753d2012-08-24 14:35:25 -07002473 case kSuper:
Ian Rogers87e552d2012-08-31 15:54:48 -07002474 return false; // TODO: appropriate checks for call to super class.
Ian Rogers08f753d2012-08-24 14:35:25 -07002475 case kInterface: {
2476 Class* methods_class = GetDeclaringClass();
Ian Rogers87e552d2012-08-31 15:54:48 -07002477 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
Ian Rogers08f753d2012-08-24 14:35:25 -07002478 }
Ian Rogers87e552d2012-08-31 15:54:48 -07002479 default:
Ian Rogersb726dcb2012-09-05 08:57:23 -07002480 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers87e552d2012-08-31 15:54:48 -07002481 return true;
Ian Rogers08f753d2012-08-24 14:35:25 -07002482 }
Ian Rogers08f753d2012-08-24 14:35:25 -07002483}
2484
Elliott Hughes76e36942012-03-16 13:44:56 -07002485inline void Method::AssertPcIsWithinCode(uintptr_t pc) const {
Elliott Hughes67d92002012-03-26 15:08:51 -07002486 if (!kIsDebugBuild) {
2487 return;
2488 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002489 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
2490 return;
2491 }
2492 Runtime* runtime = Runtime::Current();
2493 if (GetCode() == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
2494 return;
2495 }
2496 DCHECK(IsWithinCode(pc))
2497 << PrettyMethod(this)
2498 << " pc=" << std::hex << pc
2499 << " code=" << GetCode()
2500 << " size=" << GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002501}
2502
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002503inline String* Class::GetName() const {
2504 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002505}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002506inline void Class::SetName(String* name) {
2507 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002508}
2509
2510// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002511class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002512 public:
2513 void SetDetailMessage(String* new_detail_message) {
2514 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2515 new_detail_message, false);
2516 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002517 String* GetDetailMessage() const {
2518 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2519 }
Ian Rogersb726dcb2012-09-05 08:57:23 -07002520 std::string Dump() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002521
Ian Rogers1c5eb702012-02-01 09:18:34 -08002522 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2523 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2524 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2525 void SetCause(Throwable* cause);
Ian Rogersb726dcb2012-09-05 08:57:23 -07002526 bool IsCheckedException() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers5167c972012-02-03 10:41:20 -08002527
2528 static Class* GetJavaLangThrowable() {
2529 DCHECK(java_lang_Throwable_ != NULL);
2530 return java_lang_Throwable_;
2531 }
2532
2533 static void SetClass(Class* java_lang_Throwable);
2534 static void ResetClass();
2535
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002536 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002537 Object* GetStackState() const {
2538 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2539 }
2540
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002541 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2542 Throwable* cause_;
2543 String* detail_message_;
2544 Object* stack_state_; // Note this is Java volatile:
2545 Object* stack_trace_;
2546 Object* suppressed_exceptions_;
2547
Ian Rogers5167c972012-02-03 10:41:20 -08002548 static Class* java_lang_Throwable_;
2549
Brian Carlstrom693267a2011-09-06 09:25:34 -07002550 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002551 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2552};
2553
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002554// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002555class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002556 public:
2557 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002558 return GetFieldObject<const String*>(
2559 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002560 }
2561
2562 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002563 return GetFieldObject<const String*>(
2564 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002565 }
2566
2567 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002568 return GetFieldObject<const String*>(
2569 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002570 }
2571
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002572 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002573 return GetField32(
2574 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002575 }
2576
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002577 static StackTraceElement* Alloc(String* declaring_class,
2578 String* method_name,
2579 String* file_name,
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002580 int32_t line_number)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002582
2583 static void SetClass(Class* java_lang_StackTraceElement);
2584
2585 static void ResetClass();
2586
2587 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002588 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002589 String* declaring_class_;
2590 String* file_name_;
2591 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002592 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002593
2594 static Class* GetStackTraceElement() {
2595 DCHECK(java_lang_StackTraceElement_ != NULL);
2596 return java_lang_StackTraceElement_;
2597 }
2598
2599 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002600
2601 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002602 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2603};
2604
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002605class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002606 public:
Ian Rogersb726dcb2012-09-05 08:57:23 -07002607 Class* GetInterface() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002608 Class* interface = Get(kInterface)->AsClass();
2609 DCHECK(interface != NULL);
2610 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002611 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002612
Ian Rogersb726dcb2012-09-05 08:57:23 -07002613 void SetInterface(Class* interface) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002614 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002615 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002616 DCHECK(Get(kInterface) == NULL);
2617 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002618 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002619
Ian Rogersb726dcb2012-09-05 08:57:23 -07002620 size_t GetMethodArrayCount() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002621 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002622 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002623 return 0;
2624 }
2625 return method_array->GetLength();
2626 }
2627
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002628 ObjectArray<Method>* GetMethodArray() const
Ian Rogersb726dcb2012-09-05 08:57:23 -07002629 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002630 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2631 DCHECK(method_array != NULL);
2632 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002633 }
2634
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002635 void SetMethodArray(ObjectArray<Method>* new_ma)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002636 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002637 DCHECK(new_ma != NULL);
2638 DCHECK(Get(kMethodArray) == NULL);
2639 Set(kMethodArray, new_ma);
2640 }
2641
2642 static size_t LengthAsArray() {
2643 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002644 }
2645
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002646 private:
Elliott Hughes48257562012-06-06 17:42:44 -07002647 enum {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002648 // Points to the interface class.
2649 kInterface = 0,
2650 // Method pointers into the vtable, allow fast map from interface
2651 // method index to concrete instance method.
2652 kMethodArray = 1,
2653 kMax = 2,
2654 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002655
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002656 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002657};
2658
Ian Rogersc2b44472011-12-14 21:17:17 -08002659class MANAGED SynthesizedProxyClass : public Class {
2660 public:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002661 ObjectArray<Class>* GetInterfaces() {
2662 return interfaces_;
2663 }
2664
Ian Rogersc2b44472011-12-14 21:17:17 -08002665 ObjectArray<ObjectArray<Class> >* GetThrows() {
2666 return throws_;
2667 }
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002668
Jesse Wilson95caa792011-10-12 18:14:17 -04002669 private:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002670 ObjectArray<Class>* interfaces_;
Ian Rogersc2b44472011-12-14 21:17:17 -08002671 ObjectArray<ObjectArray<Class> >* throws_;
2672 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002673};
2674
2675class MANAGED Proxy : public Object {
2676 private:
2677 Object* h_;
2678
2679 friend struct ProxyOffsets; // for verifying offset information
2680 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2681};
2682
Carl Shapiro1fb86202011-06-27 17:43:13 -07002683} // namespace art
2684
2685#endif // ART_SRC_OBJECT_H_