blob: 93166602d8dfa174df4dbe4f923573af6c0b77c0 [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
Elliott Hughes68f4fa02011-08-21 10:46:59 -070017Array* Array::Alloc(Class* array_class, size_t component_count) {
18 return Alloc(array_class, component_count, array_class->GetComponentSize());
19}
20
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070021bool Class::Implements(const Class* klass) const {
22 DCHECK(klass != NULL);
23 DCHECK(klass->IsInterface());
24 // All interfaces implemented directly and by our superclass, and
25 // recursively all super-interfaces of those interfaces, are listed
26 // in iftable_, so we can just do a linear scan through that.
27 for (size_t i = 0; i < iftable_count_; i++) {
28 if (iftable_[i].GetClass() == klass) {
29 return true;
30 }
31 }
32 return false;
33}
34
35// Determine whether "this" is assignable from "klazz", where both of these
36// are array classes.
37//
38// Consider an array class, e.g. Y[][], where Y is a subclass of X.
39// Y[][] = Y[][] --> true (identity)
40// X[][] = Y[][] --> true (element superclass)
41// Y = Y[][] --> false
42// Y[] = Y[][] --> false
43// Object = Y[][] --> true (everything is an object)
44// Object[] = Y[][] --> true
45// Object[][] = Y[][] --> true
46// Object[][][] = Y[][] --> false (too many []s)
47// Serializable = Y[][] --> true (all arrays are Serializable)
48// Serializable[] = Y[][] --> true
49// Serializable[][] = Y[][] --> false (unless Y is Serializable)
50//
51// Don't forget about primitive types.
52// int[] instanceof Object[] --> false
53//
54bool Class::IsArrayAssignableFromArray(const Class* klass) const {
55 DCHECK(IsArray());
56 DCHECK(klass->IsArray());
57 DCHECK_GT(array_rank_, 0);
58 DCHECK_GT(klass->array_rank_, 0);
59 DCHECK(component_type_ != NULL);
60 DCHECK(klass->component_type_ != NULL);
61 if (array_rank_ > klass->array_rank_) {
62 // Too many []s.
63 return false;
64 }
65 if (array_rank_ == klass->array_rank_) {
66 return component_type_->IsAssignableFrom(klass->component_type_);
67 }
68 DCHECK_LT(array_rank_, klass->array_rank_);
69 // The thing we might be assignable from has more dimensions. We
70 // must be an Object or array of Object, or a standard array
71 // interface or array of standard array interfaces (the standard
72 // interfaces being java/lang/Cloneable and java/io/Serializable).
73 if (component_type_->IsInterface()) {
74 // See if we implement our component type. We know the
75 // base element is an interface; if the array class implements
76 // it, we know it's a standard array interface.
77 return Implements(component_type_);
78 }
79 // See if this is an array of Object, Object[], etc. We know
80 // that the superclass of an array is always Object, so we
81 // just compare the element type to that.
82 Class* java_lang_Object = GetSuperClass();
83 DCHECK(java_lang_Object != NULL);
84 DCHECK(java_lang_Object->GetSuperClass() == NULL);
85 return (component_type_ == java_lang_Object);
86}
87
88bool Class::IsAssignableFromArray(const Class* klass) const {
89 DCHECK(!IsInterface()); // handled first in IsAssignableFrom
90 DCHECK(klass->IsArray());
91 if (!IsArray()) {
92 // If "this" is not also an array, it must be Object.
93 // klass's super should be java_lang_Object, since it is an array.
94 Class* java_lang_Object = klass->GetSuperClass();
95 DCHECK(java_lang_Object != NULL);
96 DCHECK(java_lang_Object->GetSuperClass() == NULL);
97 return this == java_lang_Object;
98 }
99 return IsArrayAssignableFromArray(klass);
100}
101
102bool Class::IsSubClass(const Class* klass) const {
103 DCHECK(!IsInterface());
104 DCHECK(!klass->IsArray());
105 const Class* current = this;
106 do {
107 if (current == klass) {
108 return true;
109 }
110 current = current->GetSuperClass();
111 } while (current != NULL);
112 return false;
113}
114
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700115bool Class::IsInSamePackage(const String* descriptor_string_1,
116 const String* descriptor_string_2) {
117 const std::string descriptor1(descriptor_string_1->ToModifiedUtf8());
118 const std::string descriptor2(descriptor_string_2->ToModifiedUtf8());
119
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700120 size_t i = 0;
121 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
122 ++i;
123 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700124 if (descriptor1.find('/', i) != StringPiece::npos ||
125 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700126 return false;
127 } else {
128 return true;
129 }
130}
131
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700132#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700133bool Class::IsInSamePackage(const StringPiece& descriptor1,
134 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700135 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700136 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700137 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
138 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700139 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
140}
141#endif
142
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700143bool Class::IsInSamePackage(const Class* that) const {
144 const Class* klass1 = this;
145 const Class* klass2 = that;
146 if (klass1 == klass2) {
147 return true;
148 }
149 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700151 return false;
152 }
153 // Arrays are in the same package when their element classes are.
154 if (klass1->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700155 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700156 }
157 if (klass2->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700159 }
160 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700161 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700162}
163
Brian Carlstrom4873d462011-08-21 15:23:39 -0700164uint32_t Field::Get32(const Object* object) const {
165 CHECK((object == NULL) == IsStatic());
166 if (IsStatic()) {
167 object = declaring_class_;
168 }
169 return object->GetField32(GetOffset());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400170}
171
Brian Carlstrom4873d462011-08-21 15:23:39 -0700172void Field::Set32(Object* object, uint32_t new_value) const {
173 CHECK((object == NULL) == IsStatic());
174 if (IsStatic()) {
175 object = declaring_class_;
176 }
177 object->SetField32(GetOffset(), new_value);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400178}
179
Brian Carlstrom4873d462011-08-21 15:23:39 -0700180uint64_t Field::Get64(const Object* object) const {
181 CHECK((object == NULL) == IsStatic());
182 if (IsStatic()) {
183 object = declaring_class_;
184 }
185 return object->GetField64(GetOffset());
186}
187
188void Field::Set64(Object* object, uint64_t new_value) const {
189 CHECK((object == NULL) == IsStatic());
190 if (IsStatic()) {
191 object = declaring_class_;
192 }
193 object->SetField64(GetOffset(), new_value);
194}
195
196Object* Field::GetObj(const Object* object) const {
197 CHECK((object == NULL) == IsStatic());
198 if (IsStatic()) {
199 object = declaring_class_;
200 }
201 return object->GetFieldObject(GetOffset());
202}
203
204void Field::SetObj(Object* object, Object* new_value) const {
205 CHECK((object == NULL) == IsStatic());
206 if (IsStatic()) {
207 object = declaring_class_;
208 }
209 object->SetFieldObject(GetOffset(), new_value);
210}
211
212bool Field::GetBoolean(const Object* object) const {
213 CHECK_EQ(GetType(), 'Z');
214 return Get32(object);
215}
216
217void Field::SetBoolean(Object* object, bool z) const {
218 CHECK_EQ(GetType(), 'Z');
219 Set32(object, z);
220}
221
222int8_t Field::GetByte(const Object* object) const {
223 CHECK_EQ(GetType(), 'B');
224 return Get32(object);
225}
226
227void Field::SetByte(Object* object, int8_t b) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400228 CHECK_EQ(GetType(), 'B');
Jesse Wilson35baaab2011-08-10 16:18:03 -0400229 CHECK(IsStatic());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700230 Set32(object, b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400231}
232
Brian Carlstrom4873d462011-08-21 15:23:39 -0700233uint16_t Field::GetChar(const Object* object) const {
234 CHECK_EQ(GetType(), 'C');
235 return Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400236}
237
Brian Carlstrom4873d462011-08-21 15:23:39 -0700238void Field::SetChar(Object* object, uint16_t c) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400239 CHECK_EQ(GetType(), 'C');
Jesse Wilson35baaab2011-08-10 16:18:03 -0400240 CHECK(IsStatic());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700241 Set32(object, c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400242}
243
Brian Carlstrom4873d462011-08-21 15:23:39 -0700244uint16_t Field::GetShort(const Object* object) const {
245 CHECK_EQ(GetType(), 'S');
246 return Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400247}
248
Brian Carlstrom4873d462011-08-21 15:23:39 -0700249void Field::SetShort(Object* object, uint16_t s) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400250 CHECK_EQ(GetType(), 'S');
Jesse Wilson35baaab2011-08-10 16:18:03 -0400251 CHECK(IsStatic());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700252 Set32(object, s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400253}
254
Brian Carlstrom4873d462011-08-21 15:23:39 -0700255int32_t Field::GetInt(const Object* object) const {
256 CHECK_EQ(GetType(), 'I');
257 return Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400258}
259
Brian Carlstrom4873d462011-08-21 15:23:39 -0700260void Field::SetInt(Object* object, int32_t i) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400261 CHECK_EQ(GetType(), 'I');
Jesse Wilson35baaab2011-08-10 16:18:03 -0400262 CHECK(IsStatic());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700263 Set32(object, i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400264}
265
Brian Carlstrom4873d462011-08-21 15:23:39 -0700266int64_t Field::GetLong(const Object* object) const {
267 CHECK_EQ(GetType(), 'J');
268 return Get64(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400269}
270
Brian Carlstrom4873d462011-08-21 15:23:39 -0700271void Field::SetLong(Object* object, int64_t j) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400272 CHECK_EQ(GetType(), 'J');
Jesse Wilson35baaab2011-08-10 16:18:03 -0400273 CHECK(IsStatic());
Brian Carlstrom4873d462011-08-21 15:23:39 -0700274 Set64(object, j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400275}
276
Brian Carlstrom4873d462011-08-21 15:23:39 -0700277float Field::GetFloat(const Object* object) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400278 CHECK_EQ(GetType(), 'F');
279 JValue float_bits;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700280 float_bits.i = Get32(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400281 return float_bits.f;
282}
283
Brian Carlstrom4873d462011-08-21 15:23:39 -0700284void Field::SetFloat(Object* object, float f) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400285 CHECK_EQ(GetType(), 'F');
Jesse Wilson35baaab2011-08-10 16:18:03 -0400286 CHECK(IsStatic());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400287 JValue float_bits;
288 float_bits.f = f;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700289 Set32(object, float_bits.i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400290}
291
Brian Carlstrom4873d462011-08-21 15:23:39 -0700292double Field::GetDouble(const Object* object) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400293 CHECK_EQ(GetType(), 'D');
294 JValue double_bits;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295 double_bits.j = Get64(object);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400296 return double_bits.d;
297}
298
Brian Carlstrom4873d462011-08-21 15:23:39 -0700299void Field::SetDouble(Object* object, double d) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400300 CHECK_EQ(GetType(), 'D');
Jesse Wilson35baaab2011-08-10 16:18:03 -0400301 CHECK(IsStatic());
Jesse Wilson7833bd22011-08-09 18:31:44 -0400302 JValue double_bits;
303 double_bits.d = d;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700304 Set64(object, double_bits.j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400305}
306
Brian Carlstrom4873d462011-08-21 15:23:39 -0700307Object* Field::GetObject(const Object* object) const {
Jesse Wilson7833bd22011-08-09 18:31:44 -0400308 CHECK(GetType() == 'L' || GetType() == '[');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700309 return GetObj(object);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400310}
311
Brian Carlstrom4873d462011-08-21 15:23:39 -0700312void Field::SetObject(Object* object, Object* l) const {
Jesse Wilson35baaab2011-08-10 16:18:03 -0400313 CHECK(GetType() == 'L' || GetType() == '[');
Brian Carlstrom4873d462011-08-21 15:23:39 -0700314 SetObj(object, l);
Jesse Wilson35baaab2011-08-10 16:18:03 -0400315}
316
Brian Carlstrom4873d462011-08-21 15:23:39 -0700317uint32_t Method::NumArgRegisters() const {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700318 CHECK(shorty_ != NULL);
319 uint32_t num_registers = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700320 for (int i = 1; i < shorty_.length(); ++i) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700321 char ch = shorty_[i];
322 if (ch == 'D' || ch == 'J') {
323 num_registers += 2;
324 } else {
325 num_registers += 1;
326 }
327 }
328 return num_registers;
329}
330
Brian Carlstrom4873d462011-08-21 15:23:39 -0700331size_t Method::NumArgArrayBytes() const {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700332 const StringPiece& shorty = GetShorty();
333 size_t num_bytes = 0;
334 for (int i = 1; i < shorty.size(); ++i) {
335 char ch = shorty[i];
336 if (ch == 'D' || ch == 'J') {
337 num_bytes += 8;
Carl Shapiro84d00e62011-08-18 14:57:20 -0700338 } else if (ch == 'L') {
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700339 // Argument is a reference or an array. The shorty descriptor
340 // does not distinguish between these types.
341 num_bytes += sizeof(Object*);
342 } else {
343 num_bytes += 4;
344 }
345 }
346 return num_bytes;
347}
348
Ian Rogersb033c752011-07-20 12:22:35 -0700349// The number of reference arguments to this method including implicit this
350// pointer
351size_t Method::NumReferenceArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700352 size_t result = IsStatic() ? 0 : 1; // The implicit this pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700353 for (int i = 1; i < shorty_.length(); i++) {
354 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
355 result++;
356 }
357 }
358 return result;
359}
360
361// The number of long or double arguments
362size_t Method::NumLongOrDoubleArgs() const {
363 size_t result = 0;
364 for (int i = 1; i < shorty_.length(); i++) {
365 if ((shorty_[i] == 'D') || (shorty_[i] == 'J')) {
366 result++;
367 }
368 }
369 return result;
370}
371
372// The number of reference arguments to this method before the given parameter
373// index
374size_t Method::NumReferenceArgsBefore(unsigned int param) const {
375 CHECK_LT(param, NumArgs());
376 unsigned int result = IsStatic() ? 0 : 1;
377 for (unsigned int i = 1; (i < (unsigned int)shorty_.length()) &&
378 (i < (param + 1)); i++) {
379 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
380 result++;
381 }
382 }
383 return result;
384}
385
386// Is the given method parameter a reference?
387bool Method::IsParamAReference(unsigned int param) const {
388 CHECK_LT(param, NumArgs());
389 if (IsStatic()) {
390 param++; // 0th argument must skip return value at start of the shorty
391 } else if (param == 0) {
392 return true; // this argument
393 }
394 return ((shorty_[param] == 'L') || (shorty_[param] == '['));
395}
396
397// Is the given method parameter a long or double?
398bool Method::IsParamALongOrDouble(unsigned int param) const {
399 CHECK_LT(param, NumArgs());
400 if (IsStatic()) {
401 param++; // 0th argument must skip return value at start of the shorty
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700402 } else if (param == 0) {
403 return false; // this argument
Ian Rogersb033c752011-07-20 12:22:35 -0700404 }
405 return (shorty_[param] == 'J') || (shorty_[param] == 'D');
406}
407
Ian Rogersdf20fe02011-07-20 20:34:16 -0700408static size_t ShortyCharToSize(char x) {
409 switch (x) {
Ian Rogers45a76cb2011-07-21 22:00:15 -0700410 case 'V': return 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700411 case '[': return kPointerSize;
412 case 'L': return kPointerSize;
413 case 'D': return 8;
414 case 'J': return 8;
415 default: return 4;
416 }
417}
418
Ian Rogersdf20fe02011-07-20 20:34:16 -0700419size_t Method::ParamSize(unsigned int param) const {
420 CHECK_LT(param, NumArgs());
421 if (IsStatic()) {
422 param++; // 0th argument must skip return value at start of the shorty
423 } else if (param == 0) {
424 return kPointerSize; // this argument
425 }
426 return ShortyCharToSize(shorty_[param]);
427}
428
429size_t Method::ReturnSize() const {
430 return ShortyCharToSize(shorty_[0]);
431}
432
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700433bool Method::HasSameNameAndDescriptor(const Method* that) const {
434 return (this->GetName()->Equals(that->GetName()) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700435 this->GetSignature()->Equals(that->GetSignature()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700436}
437
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700438Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700439 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700440 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700441 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700442 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700443 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700444 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700445 }
446 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700447 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700448}
449
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700450Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700451 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700452 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700453 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700454 if (method != NULL) {
455 return method;
456 }
457 }
458 return NULL;
459}
460
461Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700462 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700463 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700464 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700465 if (method->GetName()->Equals(name) &&
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700466 method->GetSignature()->Equals(signature)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700467 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700468 }
469 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700470 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700471}
472
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700473Method* Class::FindVirtualMethod(const StringPiece& name,
474 const StringPiece& descriptor) {
475 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
476 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
477 if (method != NULL) {
478 return method;
479 }
480 }
481 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700482}
483
Elliott Hughescdf53122011-08-19 15:46:09 -0700484Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& descriptor) {
485 // Is the field in this class?
486 // Interfaces are not relevant because they can't contain instance fields.
487 for (size_t i = 0; i < NumInstanceFields(); ++i) {
488 Field* f = GetInstanceField(i);
489 if (f->GetName()->Equals(name) && f->GetDescriptor() == descriptor) {
490 return f;
491 }
492 }
493 return NULL;
494}
495
496Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& descriptor) {
497 // Is the field in this class, or any of its superclasses?
498 // Interfaces are not relevant because they can't contain instance fields.
499 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
500 Field* f = c->FindDeclaredInstanceField(name, descriptor);
501 if (f != NULL) {
502 return f;
503 }
504 }
505 return NULL;
506}
507
508Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& descriptor) {
509 for (size_t i = 0; i < NumStaticFields(); ++i) {
510 Field* f = GetStaticField(i);
511 if (f->GetName()->Equals(name) && f->GetDescriptor() == descriptor) {
512 return f;
513 }
514 }
515 return NULL;
516}
517
518Field* Class::FindStaticField(const StringPiece& name, const StringPiece& descriptor) {
519 // Is the field in this class (or its interfaces), or any of its
520 // superclasses (or their interfaces)?
521 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
522 // Is the field in this class?
523 Field* f = c->FindDeclaredStaticField(name, descriptor);
524 if (f != NULL) {
525 return f;
526 }
527
528 // Is this field in any of this class' interfaces?
529 for (size_t i = 0; i < c->NumInterfaces(); ++i) {
530 Class* interface = c->GetInterface(i);
531 f = interface->FindDeclaredStaticField(name, descriptor);
532 if (f != NULL) {
533 return f;
534 }
535 }
536 }
537 return NULL;
538}
539
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700540template<typename T>
541PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
542 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
543 return down_cast<PrimitiveArray<T>*>(raw_array);
544}
545
546template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
547
548// Explicitly instantiate all the primitive array types.
549template class PrimitiveArray<uint8_t>; // BooleanArray
550template class PrimitiveArray<int8_t>; // ByteArray
551template class PrimitiveArray<uint16_t>; // CharArray
552template class PrimitiveArray<double>; // DoubleArray
553template class PrimitiveArray<float>; // FloatArray
554template class PrimitiveArray<int32_t>; // IntArray
555template class PrimitiveArray<int64_t>; // LongArray
556template class PrimitiveArray<int16_t>; // ShortArray
557
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700558// TODO: get global references for these
559Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700560
Brian Carlstroma663ea52011-08-19 23:33:41 -0700561void String::SetClass(Class* java_lang_String) {
562 CHECK(java_lang_String_ == NULL);
563 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700564 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700565}
Brian Carlstroma663ea52011-08-19 23:33:41 -0700566void String::ResetClass() {
567 CHECK(java_lang_String_ != NULL);
568 java_lang_String_ = NULL;
569}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700570
Elliott Hughes1f359b02011-07-17 14:27:17 -0700571static const char* kClassStatusNames[] = {
572 "Error",
573 "NotReady",
574 "Idx",
575 "Loaded",
576 "Resolved",
577 "Verifying",
578 "Verified",
579 "Initializing",
580 "Initialized"
581};
582std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
583 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700584 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -0700585 } else {
Ian Rogersb033c752011-07-20 12:22:35 -0700586 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -0700587 }
588 return os;
589}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700590
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700591} // namespace art