blob: f938bc2fbf4a34f8d3e1a890bd62b4be69b9d10d [file] [log] [blame]
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "object.h"
4
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <string.h>
Ian Rogersdf20fe02011-07-20 20:34:16 -07006#include <algorithm>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07008#include "class_linker.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070010#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "logging.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070012#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "dex_file.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070014
15namespace art {
16
Brian Carlstromb63ec392011-08-27 17:38:27 -070017Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
18 DCHECK_GE(component_count, 0);
19 DCHECK(array_class->IsArrayClass());
20 size_t size = SizeOf(component_count, component_size);
21 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
22 if (array != NULL) {
23 DCHECK(array->IsArrayInstance());
24 array->SetLength(component_count);
25 }
26 return array;
27}
28
29Array* Array::Alloc(Class* array_class, int32_t component_count) {
Elliott Hughes68f4fa02011-08-21 10:46:59 -070030 return Alloc(array_class, component_count, array_class->GetComponentSize());
31}
32
Brian Carlstromb63ec392011-08-27 17:38:27 -070033Array* Array::AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070034 // TODO: throw on negative component_count
35 Class* klass = method->dex_cache_resolved_types_->Get(type_idx);
Brian Carlstromb63ec392011-08-27 17:38:27 -070036 if (klass == NULL) {
37 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
38 if (klass == NULL || !klass->IsArrayClass()) {
39 UNIMPLEMENTED(FATAL) << "throw an error";
40 return NULL;
41 }
42 }
43 return Array::Alloc(klass, component_count);
44}
45
46Object* Class::NewInstanceFromCode(uint32_t type_idx, Method* method) {
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070047 Class* klass = method->dex_cache_resolved_types_->Get(type_idx);
Brian Carlstromb63ec392011-08-27 17:38:27 -070048 if (klass == NULL) {
49 klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
50 if (klass == NULL) {
51 UNIMPLEMENTED(FATAL) << "throw an error";
52 return NULL;
53 }
54 }
55 return klass->NewInstance();
56}
57
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070058bool Class::Implements(const Class* klass) const {
59 DCHECK(klass != NULL);
60 DCHECK(klass->IsInterface());
61 // All interfaces implemented directly and by our superclass, and
62 // recursively all super-interfaces of those interfaces, are listed
63 // in iftable_, so we can just do a linear scan through that.
64 for (size_t i = 0; i < iftable_count_; i++) {
Brian Carlstrom30b94452011-08-25 21:35:26 -070065 if (iftable_[i].GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070066 return true;
67 }
68 }
69 return false;
70}
71
72// Determine whether "this" is assignable from "klazz", where both of these
73// are array classes.
74//
75// Consider an array class, e.g. Y[][], where Y is a subclass of X.
76// Y[][] = Y[][] --> true (identity)
77// X[][] = Y[][] --> true (element superclass)
78// Y = Y[][] --> false
79// Y[] = Y[][] --> false
80// Object = Y[][] --> true (everything is an object)
81// Object[] = Y[][] --> true
82// Object[][] = Y[][] --> true
83// Object[][][] = Y[][] --> false (too many []s)
84// Serializable = Y[][] --> true (all arrays are Serializable)
85// Serializable[] = Y[][] --> true
86// Serializable[][] = Y[][] --> false (unless Y is Serializable)
87//
88// Don't forget about primitive types.
89// int[] instanceof Object[] --> false
90//
91bool Class::IsArrayAssignableFromArray(const Class* klass) const {
Brian Carlstromb63ec392011-08-27 17:38:27 -070092 DCHECK(IsArrayClass());
93 DCHECK(klass->IsArrayClass());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070094 DCHECK_GT(array_rank_, 0);
95 DCHECK_GT(klass->array_rank_, 0);
96 DCHECK(component_type_ != NULL);
97 DCHECK(klass->component_type_ != NULL);
98 if (array_rank_ > klass->array_rank_) {
99 // Too many []s.
100 return false;
101 }
102 if (array_rank_ == klass->array_rank_) {
103 return component_type_->IsAssignableFrom(klass->component_type_);
104 }
105 DCHECK_LT(array_rank_, klass->array_rank_);
106 // The thing we might be assignable from has more dimensions. We
107 // must be an Object or array of Object, or a standard array
108 // interface or array of standard array interfaces (the standard
109 // interfaces being java/lang/Cloneable and java/io/Serializable).
110 if (component_type_->IsInterface()) {
111 // See if we implement our component type. We know the
112 // base element is an interface; if the array class implements
113 // it, we know it's a standard array interface.
114 return Implements(component_type_);
115 }
116 // See if this is an array of Object, Object[], etc. We know
117 // that the superclass of an array is always Object, so we
118 // just compare the element type to that.
119 Class* java_lang_Object = GetSuperClass();
120 DCHECK(java_lang_Object != NULL);
121 DCHECK(java_lang_Object->GetSuperClass() == NULL);
122 return (component_type_ == java_lang_Object);
123}
124
125bool Class::IsAssignableFromArray(const Class* klass) const {
126 DCHECK(!IsInterface()); // handled first in IsAssignableFrom
Brian Carlstromb63ec392011-08-27 17:38:27 -0700127 DCHECK(klass->IsArrayClass());
128 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700129 // If "this" is not also an array, it must be Object.
130 // klass's super should be java_lang_Object, since it is an array.
131 Class* java_lang_Object = klass->GetSuperClass();
132 DCHECK(java_lang_Object != NULL);
133 DCHECK(java_lang_Object->GetSuperClass() == NULL);
134 return this == java_lang_Object;
135 }
136 return IsArrayAssignableFromArray(klass);
137}
138
139bool Class::IsSubClass(const Class* klass) const {
140 DCHECK(!IsInterface());
Brian Carlstromb63ec392011-08-27 17:38:27 -0700141 DCHECK(!klass->IsArrayClass());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700142 const Class* current = this;
143 do {
144 if (current == klass) {
145 return true;
146 }
147 current = current->GetSuperClass();
148 } while (current != NULL);
149 return false;
150}
151
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700152bool Class::IsInSamePackage(const String* descriptor_string_1,
153 const String* descriptor_string_2) {
154 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
155 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
156
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700157 size_t i = 0;
158 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
159 ++i;
160 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700161 if (descriptor1.find('/', i) != StringPiece::npos ||
162 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700163 return false;
164 } else {
165 return true;
166 }
167}
168
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700169#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700170bool Class::IsInSamePackage(const StringPiece& descriptor1,
171 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700172 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700173 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700174 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
175 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700176 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
177}
178#endif
179
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700180bool Class::IsInSamePackage(const Class* that) const {
181 const Class* klass1 = this;
182 const Class* klass2 = that;
183 if (klass1 == klass2) {
184 return true;
185 }
186 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700187 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700188 return false;
189 }
190 // Arrays are in the same package when their element classes are.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700191 if (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700193 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700194 if (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700195 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700196 }
197 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700198 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700199}
200
Brian Carlstrom4873d462011-08-21 15:23:39 -0700201uint32_t Field::Get32(const Object* object) const {
202 CHECK((object == NULL) == IsStatic());
203 if (IsStatic()) {
204 object = declaring_class_;
205 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700206 // TODO: volatile
Brian Carlstrom4873d462011-08-21 15:23:39 -0700207 return object->GetField32(GetOffset());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400208}
209
Brian Carlstrom4873d462011-08-21 15:23:39 -0700210void Field::Set32(Object* object, uint32_t new_value) const {
211 CHECK((object == NULL) == IsStatic());
212 if (IsStatic()) {
213 object = declaring_class_;
214 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700215 // TODO: volatile
Brian Carlstrom4873d462011-08-21 15:23:39 -0700216 object->SetField32(GetOffset(), new_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400217}
218
Brian Carlstrom4873d462011-08-21 15:23:39 -0700219uint64_t Field::Get64(const Object* object) const {
220 CHECK((object == NULL) == IsStatic());
221 if (IsStatic()) {
222 object = declaring_class_;
223 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700224 // TODO: volatile
Brian Carlstrom4873d462011-08-21 15:23:39 -0700225 return object->GetField64(GetOffset());
226}
227
228void Field::Set64(Object* object, uint64_t new_value) const {
229 CHECK((object == NULL) == IsStatic());
230 if (IsStatic()) {
231 object = declaring_class_;
232 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700233 // TODO: volatile
Brian Carlstrom4873d462011-08-21 15:23:39 -0700234 object->SetField64(GetOffset(), new_value);
235}
236
237Object* Field::GetObj(const Object* object) const {
238 CHECK((object == NULL) == IsStatic());
239 if (IsStatic()) {
240 object = declaring_class_;
241 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700242 // TODO: volatile
Brian Carlstrom4873d462011-08-21 15:23:39 -0700243 return object->GetFieldObject(GetOffset());
244}
245
246void Field::SetObj(Object* object, Object* new_value) const {
247 CHECK((object == NULL) == IsStatic());
248 if (IsStatic()) {
249 object = declaring_class_;
250 }
Elliott Hughes37f7a402011-08-22 18:56:01 -0700251 // TODO: volatile
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252 object->SetFieldObject(GetOffset(), new_value);
253}
254
255bool Field::GetBoolean(const Object* object) const {
256 CHECK_EQ(GetType(), 'Z');
257 return Get32(object);
258}
259
260void Field::SetBoolean(Object* object, bool z) const {
261 CHECK_EQ(GetType(), 'Z');
262 Set32(object, z);
263}
264
265int8_t Field::GetByte(const Object* object) const {
266 CHECK_EQ(GetType(), 'B');
267 return Get32(object);
268}
269
270void Field::SetByte(Object* object, int8_t b) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400271 CHECK_EQ(GetType(), 'B');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700272 Set32(object, b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400273}
274
Brian Carlstrom4873d462011-08-21 15:23:39 -0700275uint16_t Field::GetChar(const Object* object) const {
276 CHECK_EQ(GetType(), 'C');
277 return Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400278}
279
Brian Carlstrom4873d462011-08-21 15:23:39 -0700280void Field::SetChar(Object* object, uint16_t c) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400281 CHECK_EQ(GetType(), 'C');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700282 Set32(object, c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400283}
284
Brian Carlstrom4873d462011-08-21 15:23:39 -0700285uint16_t Field::GetShort(const Object* object) const {
286 CHECK_EQ(GetType(), 'S');
287 return Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400288}
289
Brian Carlstrom4873d462011-08-21 15:23:39 -0700290void Field::SetShort(Object* object, uint16_t s) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400291 CHECK_EQ(GetType(), 'S');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700292 Set32(object, s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400293}
294
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295int32_t Field::GetInt(const Object* object) const {
296 CHECK_EQ(GetType(), 'I');
297 return Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400298}
299
Brian Carlstrom4873d462011-08-21 15:23:39 -0700300void Field::SetInt(Object* object, int32_t i) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400301 CHECK_EQ(GetType(), 'I');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700302 Set32(object, i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400303}
304
Brian Carlstrom4873d462011-08-21 15:23:39 -0700305int64_t Field::GetLong(const Object* object) const {
306 CHECK_EQ(GetType(), 'J');
307 return Get64(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400308}
309
Brian Carlstrom4873d462011-08-21 15:23:39 -0700310void Field::SetLong(Object* object, int64_t j) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400311 CHECK_EQ(GetType(), 'J');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700312 Set64(object, j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400313}
314
Brian Carlstrom4873d462011-08-21 15:23:39 -0700315float Field::GetFloat(const Object* object) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400316 CHECK_EQ(GetType(), 'F');
317 JValue float_bits;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700318 float_bits.i = Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400319 return float_bits.f;
320}
321
Brian Carlstrom4873d462011-08-21 15:23:39 -0700322void Field::SetFloat(Object* object, float f) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400323 CHECK_EQ(GetType(), 'F');
324 JValue float_bits;
325 float_bits.f = f;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700326 Set32(object, float_bits.i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400327}
328
Brian Carlstrom4873d462011-08-21 15:23:39 -0700329double Field::GetDouble(const Object* object) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400330 CHECK_EQ(GetType(), 'D');
331 JValue double_bits;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700332 double_bits.j = Get64(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400333 return double_bits.d;
334}
335
Brian Carlstrom4873d462011-08-21 15:23:39 -0700336void Field::SetDouble(Object* object, double d) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400337 CHECK_EQ(GetType(), 'D');
338 JValue double_bits;
339 double_bits.d = d;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700340 Set64(object, double_bits.j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400341}
342
Brian Carlstrom4873d462011-08-21 15:23:39 -0700343Object* Field::GetObject(const Object* object) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400344 CHECK(GetType() == 'L' || GetType() == '[');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700345 return GetObj(object);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400346}
347
Brian Carlstrom4873d462011-08-21 15:23:39 -0700348void Field::SetObject(Object* object, Object* l) const {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400349 CHECK(GetType() == 'L' || GetType() == '[');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700350 SetObj(object, l);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400351}
352
Brian Carlstrom4873d462011-08-21 15:23:39 -0700353uint32_t Method::NumArgRegisters() const {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700354 CHECK(shorty_ != NULL);
355 uint32_t num_registers = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700356 for (int i = 1; i < shorty_.length(); ++i) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700357 char ch = shorty_[i];
358 if (ch == 'D' || ch == 'J') {
359 num_registers += 2;
360 } else {
361 num_registers += 1;
362 }
363 }
364 return num_registers;
365}
366
Brian Carlstrom4873d462011-08-21 15:23:39 -0700367size_t Method::NumArgArrayBytes() const {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700368 const StringPiece& shorty = GetShorty();
369 size_t num_bytes = 0;
370 for (int i = 1; i < shorty.size(); ++i) {
371 char ch = shorty[i];
372 if (ch == 'D' || ch == 'J') {
373 num_bytes += 8;
Carl Shapiro84d00e62011-08-18 14:57:20 -0700374 } else if (ch == 'L') {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700375 // Argument is a reference or an array. The shorty descriptor
376 // does not distinguish between these types.
377 num_bytes += sizeof(Object*);
378 } else {
379 num_bytes += 4;
380 }
381 }
382 return num_bytes;
383}
384
Ian Rogersb033c752011-07-20 12:22:35 -0700385// The number of reference arguments to this method including implicit this
386// pointer
387size_t Method::NumReferenceArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700388 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700389 for (int i = 1; i < shorty_.length(); i++) {
390 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
391 result++;
392 }
393 }
394 return result;
395}
396
397// The number of long or double arguments
398size_t Method::NumLongOrDoubleArgs() const {
399 size_t result = 0;
400 for (int i = 1; i < shorty_.length(); i++) {
401 if ((shorty_[i] == 'D') || (shorty_[i] == 'J')) {
402 result++;
403 }
404 }
405 return result;
406}
407
408// The number of reference arguments to this method before the given parameter
409// index
410size_t Method::NumReferenceArgsBefore(unsigned int param) const {
411 CHECK_LT(param, NumArgs());
412 unsigned int result = IsStatic() ? 0 : 1;
413 for (unsigned int i = 1; (i < (unsigned int)shorty_.length()) &&
414 (i < (param + 1)); i++) {
415 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
416 result++;
417 }
418 }
419 return result;
420}
421
422// Is the given method parameter a reference?
423bool Method::IsParamAReference(unsigned int param) const {
424 CHECK_LT(param, NumArgs());
425 if (IsStatic()) {
426 param++; // 0th argument must skip return value at start of the shorty
427 } else if (param == 0) {
428 return true; // this argument
429 }
430 return ((shorty_[param] == 'L') || (shorty_[param] == '['));
431}
432
433// Is the given method parameter a long or double?
434bool Method::IsParamALongOrDouble(unsigned int param) const {
435 CHECK_LT(param, NumArgs());
436 if (IsStatic()) {
437 param++; // 0th argument must skip return value at start of the shorty
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700438 } else if (param == 0) {
439 return false; // this argument
Ian Rogersb033c752011-07-20 12:22:35 -0700440 }
441 return (shorty_[param] == 'J') || (shorty_[param] == 'D');
442}
443
Ian Rogersdf20fe02011-07-20 20:34:16 -0700444static size_t ShortyCharToSize(char x) {
445 switch (x) {
Ian Rogers45a76cb2011-07-21 22:00:15 -0700446 case 'V': return 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700447 case '[': return kPointerSize;
448 case 'L': return kPointerSize;
449 case 'D': return 8;
450 case 'J': return 8;
451 default: return 4;
452 }
453}
454
Ian Rogersdf20fe02011-07-20 20:34:16 -0700455size_t Method::ParamSize(unsigned int param) const {
456 CHECK_LT(param, NumArgs());
457 if (IsStatic()) {
458 param++; // 0th argument must skip return value at start of the shorty
459 } else if (param == 0) {
460 return kPointerSize; // this argument
461 }
462 return ShortyCharToSize(shorty_[param]);
463}
464
465size_t Method::ReturnSize() const {
466 return ShortyCharToSize(shorty_[0]);
467}
468
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700469bool Method::HasSameNameAndDescriptor(const Method* that) const {
470 return (this->GetName()->Equals(that->GetName()) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700471 this->GetSignature()->Equals(that->GetSignature()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700472}
473
Brian Carlstrom30b94452011-08-25 21:35:26 -0700474Method* Class::FindVirtualMethodForInterface(Method* method) {
475 Class* declaring_class = method->GetDeclaringClass();
476 DCHECK(declaring_class->IsInterface());
477 // TODO cache to improve lookup speed
478 for (size_t i = 0; i < iftable_count_; i++) {
479 InterfaceEntry& interface_entry = iftable_[i];
480 if (interface_entry.GetInterface() == declaring_class) {
481 return vtable_->Get(interface_entry.method_index_array_[method->method_index_]);
482 }
483 }
484 UNIMPLEMENTED(FATAL) << "Need to throw an error of some kind";
485 return NULL;
486}
487
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700488Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700489 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700490 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700491 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700492 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700493 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700494 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700495 }
496 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700497 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700498}
499
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700500Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700501 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700502 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700503 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700504 if (method != NULL) {
505 return method;
506 }
507 }
508 return NULL;
509}
510
511Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700512 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700513 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700514 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700515 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700516 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700517 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700518 }
519 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700520 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700521}
522
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700523Method* Class::FindVirtualMethod(const StringPiece& name,
524 const StringPiece& descriptor) {
525 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
526 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
527 if (method != NULL) {
528 return method;
529 }
530 }
531 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700532}
533
Elliott Hughescdf53122011-08-19 15:46:09 -0700534Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& descriptor) {
535 // Is the field in this class?
536 // Interfaces are not relevant because they can't contain instance fields.
537 for (size_t i = 0; i < NumInstanceFields(); ++i) {
538 Field* f = GetInstanceField(i);
539 if (f->GetName()->Equals(name) && f->GetDescriptor() == descriptor) {
540 return f;
541 }
542 }
543 return NULL;
544}
545
546Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& descriptor) {
547 // Is the field in this class, or any of its superclasses?
548 // Interfaces are not relevant because they can't contain instance fields.
549 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
550 Field* f = c->FindDeclaredInstanceField(name, descriptor);
551 if (f != NULL) {
552 return f;
553 }
554 }
555 return NULL;
556}
557
558Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& descriptor) {
559 for (size_t i = 0; i < NumStaticFields(); ++i) {
560 Field* f = GetStaticField(i);
561 if (f->GetName()->Equals(name) && f->GetDescriptor() == descriptor) {
562 return f;
563 }
564 }
565 return NULL;
566}
567
568Field* Class::FindStaticField(const StringPiece& name, const StringPiece& descriptor) {
569 // Is the field in this class (or its interfaces), or any of its
570 // superclasses (or their interfaces)?
571 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
572 // Is the field in this class?
573 Field* f = c->FindDeclaredStaticField(name, descriptor);
574 if (f != NULL) {
575 return f;
576 }
577
578 // Is this field in any of this class' interfaces?
579 for (size_t i = 0; i < c->NumInterfaces(); ++i) {
580 Class* interface = c->GetInterface(i);
581 f = interface->FindDeclaredStaticField(name, descriptor);
582 if (f != NULL) {
583 return f;
584 }
585 }
586 }
587 return NULL;
588}
589
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700590template<typename T>
591PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700592 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700593 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
594 return down_cast<PrimitiveArray<T>*>(raw_array);
595}
596
597template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
598
599// Explicitly instantiate all the primitive array types.
600template class PrimitiveArray<uint8_t>; // BooleanArray
601template class PrimitiveArray<int8_t>; // ByteArray
602template class PrimitiveArray<uint16_t>; // CharArray
603template class PrimitiveArray<double>; // DoubleArray
604template class PrimitiveArray<float>; // FloatArray
605template class PrimitiveArray<int32_t>; // IntArray
606template class PrimitiveArray<int64_t>; // LongArray
607template class PrimitiveArray<int16_t>; // ShortArray
608
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700609// TODO: get global references for these
610Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700611
Brian Carlstroma663ea52011-08-19 23:33:41 -0700612void String::SetClass(Class* java_lang_String) {
613 CHECK(java_lang_String_ == NULL);
614 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700615 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700616}
Brian Carlstroma663ea52011-08-19 23:33:41 -0700617void String::ResetClass() {
618 CHECK(java_lang_String_ != NULL);
619 java_lang_String_ = NULL;
620}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700621
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700622// TODO: get global references for these
623Class* PathClassLoader::dalvik_system_PathClassLoader_ = NULL;
624
625PathClassLoader* PathClassLoader::Alloc(std::vector<const DexFile*> dex_files) {
626 PathClassLoader* p = down_cast<PathClassLoader*>(dalvik_system_PathClassLoader_->NewInstance());
627 p->SetClassPath(dex_files);
628 return p;
629}
630
631void PathClassLoader::SetClass(Class* dalvik_system_PathClassLoader) {
632 CHECK(dalvik_system_PathClassLoader_ == NULL);
633 CHECK(dalvik_system_PathClassLoader != NULL);
634 dalvik_system_PathClassLoader_ = dalvik_system_PathClassLoader;
635}
636
637void PathClassLoader::ResetClass() {
638 CHECK(dalvik_system_PathClassLoader_ != NULL);
639 dalvik_system_PathClassLoader_ = NULL;
640}
641
Shih-wei Liao55df06b2011-08-26 14:39:27 -0700642Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
643
644void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
645 CHECK(java_lang_StackTraceElement_ == NULL);
646 CHECK(java_lang_StackTraceElement != NULL);
647 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
648}
649
650void StackTraceElement::ResetClass() {
651 CHECK(java_lang_StackTraceElement_ != NULL);
652 java_lang_StackTraceElement_ = NULL;
653}
654
Elliott Hughes1f359b02011-07-17 14:27:17 -0700655static const char* kClassStatusNames[] = {
656 "Error",
657 "NotReady",
658 "Idx",
659 "Loaded",
660 "Resolved",
661 "Verifying",
662 "Verified",
663 "Initializing",
664 "Initialized"
665};
666std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
667 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700668 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -0700669 } else {
Ian Rogersb033c752011-07-20 12:22:35 -0700670 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -0700671 }
672 return os;
673}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700674
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700675} // namespace art