blob: 409ff6158851538b0be579ae5f514ae0c4f9a32a [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
8#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07009#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070011#include "dex_cache.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "dex_file.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070013
14namespace art {
15
Brian Carlstromf7ed11a2011-08-09 17:55:51 -070016bool Class::Implements(const Class* klass) const {
17 DCHECK(klass != NULL);
18 DCHECK(klass->IsInterface());
19 // All interfaces implemented directly and by our superclass, and
20 // recursively all super-interfaces of those interfaces, are listed
21 // in iftable_, so we can just do a linear scan through that.
22 for (size_t i = 0; i < iftable_count_; i++) {
23 if (iftable_[i].GetClass() == klass) {
24 return true;
25 }
26 }
27 return false;
28}
29
30// Determine whether "this" is assignable from "klazz", where both of these
31// are array classes.
32//
33// Consider an array class, e.g. Y[][], where Y is a subclass of X.
34// Y[][] = Y[][] --> true (identity)
35// X[][] = Y[][] --> true (element superclass)
36// Y = Y[][] --> false
37// Y[] = Y[][] --> false
38// Object = Y[][] --> true (everything is an object)
39// Object[] = Y[][] --> true
40// Object[][] = Y[][] --> true
41// Object[][][] = Y[][] --> false (too many []s)
42// Serializable = Y[][] --> true (all arrays are Serializable)
43// Serializable[] = Y[][] --> true
44// Serializable[][] = Y[][] --> false (unless Y is Serializable)
45//
46// Don't forget about primitive types.
47// int[] instanceof Object[] --> false
48//
49bool Class::IsArrayAssignableFromArray(const Class* klass) const {
50 DCHECK(IsArray());
51 DCHECK(klass->IsArray());
52 DCHECK_GT(array_rank_, 0);
53 DCHECK_GT(klass->array_rank_, 0);
54 DCHECK(component_type_ != NULL);
55 DCHECK(klass->component_type_ != NULL);
56 if (array_rank_ > klass->array_rank_) {
57 // Too many []s.
58 return false;
59 }
60 if (array_rank_ == klass->array_rank_) {
61 return component_type_->IsAssignableFrom(klass->component_type_);
62 }
63 DCHECK_LT(array_rank_, klass->array_rank_);
64 // The thing we might be assignable from has more dimensions. We
65 // must be an Object or array of Object, or a standard array
66 // interface or array of standard array interfaces (the standard
67 // interfaces being java/lang/Cloneable and java/io/Serializable).
68 if (component_type_->IsInterface()) {
69 // See if we implement our component type. We know the
70 // base element is an interface; if the array class implements
71 // it, we know it's a standard array interface.
72 return Implements(component_type_);
73 }
74 // See if this is an array of Object, Object[], etc. We know
75 // that the superclass of an array is always Object, so we
76 // just compare the element type to that.
77 Class* java_lang_Object = GetSuperClass();
78 DCHECK(java_lang_Object != NULL);
79 DCHECK(java_lang_Object->GetSuperClass() == NULL);
80 return (component_type_ == java_lang_Object);
81}
82
83bool Class::IsAssignableFromArray(const Class* klass) const {
84 DCHECK(!IsInterface()); // handled first in IsAssignableFrom
85 DCHECK(klass->IsArray());
86 if (!IsArray()) {
87 // If "this" is not also an array, it must be Object.
88 // klass's super should be java_lang_Object, since it is an array.
89 Class* java_lang_Object = klass->GetSuperClass();
90 DCHECK(java_lang_Object != NULL);
91 DCHECK(java_lang_Object->GetSuperClass() == NULL);
92 return this == java_lang_Object;
93 }
94 return IsArrayAssignableFromArray(klass);
95}
96
97bool Class::IsSubClass(const Class* klass) const {
98 DCHECK(!IsInterface());
99 DCHECK(!klass->IsArray());
100 const Class* current = this;
101 do {
102 if (current == klass) {
103 return true;
104 }
105 current = current->GetSuperClass();
106 } while (current != NULL);
107 return false;
108}
109
Ian Rogersb033c752011-07-20 12:22:35 -0700110bool Class::IsInSamePackage(const StringPiece& descriptor1,
111 const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700112 size_t i = 0;
113 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
114 ++i;
115 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700116 if (descriptor1.find('/', i) != StringPiece::npos ||
117 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700118 return false;
119 } else {
120 return true;
121 }
122}
123
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700124#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700125bool Class::IsInSamePackage(const StringPiece& descriptor1,
126 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700127 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700128 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700129 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
130 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700131 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
132}
133#endif
134
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700135bool Class::IsInSamePackage(const Class* that) const {
136 const Class* klass1 = this;
137 const Class* klass2 = that;
138 if (klass1 == klass2) {
139 return true;
140 }
141 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700142 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700143 return false;
144 }
145 // Arrays are in the same package when their element classes are.
146 if (klass1->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700147 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700148 }
149 if (klass2->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700151 }
152 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700153 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700154}
155
Jesse Wilson7833bd22011-08-09 18:31:44 -0400156bool StaticField::GetBoolean() {
157 CHECK_EQ(GetType(), 'Z');
158 return declaring_class_->static_32bit_primitives_->Get(java_slot_);
159}
160
161void StaticField::SetBoolean(bool z) {
162 CHECK_EQ(GetType(), 'Z');
163 declaring_class_->static_32bit_primitives_->Set(java_slot_, z);
164}
165
166int8_t StaticField::GetByte() {
167 CHECK_EQ(GetType(), 'B');
168 return declaring_class_->static_32bit_primitives_->Get(java_slot_);
169}
170
171void StaticField::SetByte(int8_t b) {
172 CHECK_EQ(GetType(), 'B');
173 declaring_class_->static_32bit_primitives_->Set(java_slot_, b);
174}
175
176uint16_t StaticField::GetChar() {
177 CHECK_EQ(GetType(), 'C');
178 return declaring_class_->static_32bit_primitives_->Get(java_slot_);
179}
180
181void StaticField::SetChar(uint16_t c) {
182 CHECK_EQ(GetType(), 'C');
183 declaring_class_->static_32bit_primitives_->Set(java_slot_, c);
184}
185
186uint16_t StaticField::GetShort() {
187 CHECK_EQ(GetType(), 'S');
188 return declaring_class_->static_32bit_primitives_->Get(java_slot_);
189}
190
191void StaticField::SetShort(uint16_t s) {
192 CHECK_EQ(GetType(), 'S');
193 declaring_class_->static_32bit_primitives_->Set(java_slot_, s);
194}
195
196int32_t StaticField::GetInt() {
197 CHECK_EQ(GetType(), 'I');
198 return declaring_class_->static_32bit_primitives_->Get(java_slot_);
199}
200
201void StaticField::SetInt(int32_t i) {
202 CHECK_EQ(GetType(), 'I');
203 declaring_class_->static_32bit_primitives_->Set(java_slot_, i);
204}
205
206int64_t StaticField::GetLong() {
207 CHECK_EQ(GetType(), 'J');
208 return declaring_class_->static_64bit_primitives_->Get(java_slot_);
209}
210
211void StaticField::SetLong(int64_t j) {
212 CHECK_EQ(GetType(), 'J');
213 declaring_class_->static_64bit_primitives_->Set(java_slot_, j);
214}
215
216float StaticField::GetFloat() {
217 CHECK_EQ(GetType(), 'F');
218 JValue float_bits;
219 float_bits.i = declaring_class_->static_32bit_primitives_->Get(java_slot_);
220 return float_bits.f;
221}
222
223void StaticField::SetFloat(float f) {
224 CHECK_EQ(GetType(), 'F');
225 JValue float_bits;
226 float_bits.f = f;
227 declaring_class_->static_32bit_primitives_->Set(java_slot_, float_bits.i);
228}
229
230double StaticField::GetDouble() {
231 CHECK_EQ(GetType(), 'D');
232 JValue double_bits;
233 double_bits.j = declaring_class_->static_64bit_primitives_->Get(java_slot_);
234 return double_bits.d;
235}
236
237void StaticField::SetDouble(double d) {
238 CHECK_EQ(GetType(), 'D');
239 JValue double_bits;
240 double_bits.d = d;
241 declaring_class_->static_64bit_primitives_->Set(java_slot_, double_bits.j);
242}
243
244Object* StaticField::GetObject() {
245 return declaring_class_->static_references_->Get(java_slot_);
246}
247
248const Object* StaticField::GetObject() const {
249 return declaring_class_->static_references_->Get(java_slot_);
250}
251
252void StaticField::SetObject(Object* l) {
253 CHECK(GetType() == 'L' || GetType() == '[');
254 declaring_class_->static_references_->Set(java_slot_, l); // TODO: write barrier
255}
256
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700257uint32_t Method::NumArgRegisters() {
258 CHECK(shorty_ != NULL);
259 uint32_t num_registers = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700260 for (int i = 1; i < shorty_.length(); ++i) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700261 char ch = shorty_[i];
262 if (ch == 'D' || ch == 'J') {
263 num_registers += 2;
264 } else {
265 num_registers += 1;
266 }
267 }
268 return num_registers;
269}
270
Ian Rogersb033c752011-07-20 12:22:35 -0700271// The number of reference arguments to this method including implicit this
272// pointer
273size_t Method::NumReferenceArgs() const {
274 size_t result = IsStatic() ? 0 : 1;
275 for (int i = 1; i < shorty_.length(); i++) {
276 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
277 result++;
278 }
279 }
280 return result;
281}
282
283// The number of long or double arguments
284size_t Method::NumLongOrDoubleArgs() const {
285 size_t result = 0;
286 for (int i = 1; i < shorty_.length(); i++) {
287 if ((shorty_[i] == 'D') || (shorty_[i] == 'J')) {
288 result++;
289 }
290 }
291 return result;
292}
293
294// The number of reference arguments to this method before the given parameter
295// index
296size_t Method::NumReferenceArgsBefore(unsigned int param) const {
297 CHECK_LT(param, NumArgs());
298 unsigned int result = IsStatic() ? 0 : 1;
299 for (unsigned int i = 1; (i < (unsigned int)shorty_.length()) &&
300 (i < (param + 1)); i++) {
301 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
302 result++;
303 }
304 }
305 return result;
306}
307
308// Is the given method parameter a reference?
309bool Method::IsParamAReference(unsigned int param) const {
310 CHECK_LT(param, NumArgs());
311 if (IsStatic()) {
312 param++; // 0th argument must skip return value at start of the shorty
313 } else if (param == 0) {
314 return true; // this argument
315 }
316 return ((shorty_[param] == 'L') || (shorty_[param] == '['));
317}
318
319// Is the given method parameter a long or double?
320bool Method::IsParamALongOrDouble(unsigned int param) const {
321 CHECK_LT(param, NumArgs());
322 if (IsStatic()) {
323 param++; // 0th argument must skip return value at start of the shorty
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700324 } else if (param == 0) {
325 return false; // this argument
Ian Rogersb033c752011-07-20 12:22:35 -0700326 }
327 return (shorty_[param] == 'J') || (shorty_[param] == 'D');
328}
329
Ian Rogersdf20fe02011-07-20 20:34:16 -0700330static size_t ShortyCharToSize(char x) {
331 switch (x) {
Ian Rogers45a76cb2011-07-21 22:00:15 -0700332 case 'V': return 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700333 case '[': return kPointerSize;
334 case 'L': return kPointerSize;
335 case 'D': return 8;
336 case 'J': return 8;
337 default: return 4;
338 }
339}
340
Ian Rogersdf20fe02011-07-20 20:34:16 -0700341size_t Method::ParamSize(unsigned int param) const {
342 CHECK_LT(param, NumArgs());
343 if (IsStatic()) {
344 param++; // 0th argument must skip return value at start of the shorty
345 } else if (param == 0) {
346 return kPointerSize; // this argument
347 }
348 return ShortyCharToSize(shorty_[param]);
349}
350
351size_t Method::ReturnSize() const {
352 return ShortyCharToSize(shorty_[0]);
353}
354
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700355bool Method::HasSameNameAndDescriptor(const Method* that) const {
356 return (this->GetName()->Equals(that->GetName()) &&
357 this->GetDescriptor()->Equals(that->GetDescriptor()));
358}
359
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700360Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
361 const StringPiece& descriptor) {
362 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700363 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700364 if (method->GetName()->Equals(name) &&
365 method->GetDescriptor()->Equals(descriptor)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700366 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700367 }
368 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700369 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700370}
371
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700372Method* Class::FindDirectMethod(const StringPiece& name,
373 const StringPiece& descriptor) {
374 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
375 Method* method = klass->FindDeclaredDirectMethod(name, descriptor);
376 if (method != NULL) {
377 return method;
378 }
379 }
380 return NULL;
381}
382
383Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
384 const StringPiece& descriptor) {
385 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700386 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700387 if (method->GetName()->Equals(name) &&
388 method->GetDescriptor()->Equals(descriptor)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700389 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700390 }
391 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700392 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700393}
394
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700395Method* Class::FindVirtualMethod(const StringPiece& name,
396 const StringPiece& descriptor) {
397 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
398 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
399 if (method != NULL) {
400 return method;
401 }
402 }
403 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404}
405
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700406// TODO: get global references for these
407Class* String::java_lang_String_ = NULL;
408Class* String::char_array_ = NULL;
409
410void String::InitClasses(Class* java_lang_String, Class* char_array) {
411 java_lang_String_ = java_lang_String;
412 char_array_ = char_array;
413}
414
Elliott Hughes1f359b02011-07-17 14:27:17 -0700415static const char* kClassStatusNames[] = {
416 "Error",
417 "NotReady",
418 "Idx",
419 "Loaded",
420 "Resolved",
421 "Verifying",
422 "Verified",
423 "Initializing",
424 "Initialized"
425};
426std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
427 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700428 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -0700429 } else {
Ian Rogersb033c752011-07-20 12:22:35 -0700430 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -0700431 }
432 return os;
433}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700434
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700435} // namespace art