blob: 514eef589b1688203dae3bea9afdb09ee29e4e05 [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
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700156uint32_t Method::NumArgRegisters() {
157 CHECK(shorty_ != NULL);
158 uint32_t num_registers = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -0700159 for (int i = 1; i < shorty_.length(); ++i) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700160 char ch = shorty_[i];
161 if (ch == 'D' || ch == 'J') {
162 num_registers += 2;
163 } else {
164 num_registers += 1;
165 }
166 }
167 return num_registers;
168}
169
Ian Rogersb033c752011-07-20 12:22:35 -0700170// The number of reference arguments to this method including implicit this
171// pointer
172size_t Method::NumReferenceArgs() const {
173 size_t result = IsStatic() ? 0 : 1;
174 for (int i = 1; i < shorty_.length(); i++) {
175 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
176 result++;
177 }
178 }
179 return result;
180}
181
182// The number of long or double arguments
183size_t Method::NumLongOrDoubleArgs() const {
184 size_t result = 0;
185 for (int i = 1; i < shorty_.length(); i++) {
186 if ((shorty_[i] == 'D') || (shorty_[i] == 'J')) {
187 result++;
188 }
189 }
190 return result;
191}
192
193// The number of reference arguments to this method before the given parameter
194// index
195size_t Method::NumReferenceArgsBefore(unsigned int param) const {
196 CHECK_LT(param, NumArgs());
197 unsigned int result = IsStatic() ? 0 : 1;
198 for (unsigned int i = 1; (i < (unsigned int)shorty_.length()) &&
199 (i < (param + 1)); i++) {
200 if ((shorty_[i] == 'L') || (shorty_[i] == '[')) {
201 result++;
202 }
203 }
204 return result;
205}
206
207// Is the given method parameter a reference?
208bool Method::IsParamAReference(unsigned int param) const {
209 CHECK_LT(param, NumArgs());
210 if (IsStatic()) {
211 param++; // 0th argument must skip return value at start of the shorty
212 } else if (param == 0) {
213 return true; // this argument
214 }
215 return ((shorty_[param] == 'L') || (shorty_[param] == '['));
216}
217
218// Is the given method parameter a long or double?
219bool Method::IsParamALongOrDouble(unsigned int param) const {
220 CHECK_LT(param, NumArgs());
221 if (IsStatic()) {
222 param++; // 0th argument must skip return value at start of the shorty
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700223 } else if (param == 0) {
224 return false; // this argument
Ian Rogersb033c752011-07-20 12:22:35 -0700225 }
226 return (shorty_[param] == 'J') || (shorty_[param] == 'D');
227}
228
Ian Rogersdf20fe02011-07-20 20:34:16 -0700229static size_t ShortyCharToSize(char x) {
230 switch (x) {
Ian Rogers45a76cb2011-07-21 22:00:15 -0700231 case 'V': return 0;
Ian Rogersb033c752011-07-20 12:22:35 -0700232 case '[': return kPointerSize;
233 case 'L': return kPointerSize;
234 case 'D': return 8;
235 case 'J': return 8;
236 default: return 4;
237 }
238}
239
Ian Rogersdf20fe02011-07-20 20:34:16 -0700240size_t Method::ParamSize(unsigned int param) const {
241 CHECK_LT(param, NumArgs());
242 if (IsStatic()) {
243 param++; // 0th argument must skip return value at start of the shorty
244 } else if (param == 0) {
245 return kPointerSize; // this argument
246 }
247 return ShortyCharToSize(shorty_[param]);
248}
249
250size_t Method::ReturnSize() const {
251 return ShortyCharToSize(shorty_[0]);
252}
253
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700254bool Method::HasSameNameAndDescriptor(const Method* that) const {
255 return (this->GetName()->Equals(that->GetName()) &&
256 this->GetDescriptor()->Equals(that->GetDescriptor()));
257}
258
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700259Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
260 const StringPiece& descriptor) {
261 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700262 Method* method = GetDirectMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700263 if (method->GetName()->Equals(name) &&
264 method->GetDescriptor()->Equals(descriptor)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700265 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700266 }
267 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700268 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700269}
270
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700271Method* Class::FindDirectMethod(const StringPiece& name,
272 const StringPiece& descriptor) {
273 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
274 Method* method = klass->FindDeclaredDirectMethod(name, descriptor);
275 if (method != NULL) {
276 return method;
277 }
278 }
279 return NULL;
280}
281
282Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
283 const StringPiece& descriptor) {
284 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700285 Method* method = GetVirtualMethod(i);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700286 if (method->GetName()->Equals(name) &&
287 method->GetDescriptor()->Equals(descriptor)) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700288 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700289 }
290 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700291 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700292}
293
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700294Method* Class::FindVirtualMethod(const StringPiece& name,
295 const StringPiece& descriptor) {
296 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
297 Method* method = klass->FindDeclaredVirtualMethod(name, descriptor);
298 if (method != NULL) {
299 return method;
300 }
301 }
302 return NULL;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700303}
304
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700305// TODO: get global references for these
306Class* String::java_lang_String_ = NULL;
307Class* String::char_array_ = NULL;
308
309void String::InitClasses(Class* java_lang_String, Class* char_array) {
310 java_lang_String_ = java_lang_String;
311 char_array_ = char_array;
312}
313
Elliott Hughes1f359b02011-07-17 14:27:17 -0700314static const char* kClassStatusNames[] = {
315 "Error",
316 "NotReady",
317 "Idx",
318 "Loaded",
319 "Resolved",
320 "Verifying",
321 "Verified",
322 "Initializing",
323 "Initialized"
324};
325std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
326 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -0700327 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -0700328 } else {
Ian Rogersb033c752011-07-20 12:22:35 -0700329 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -0700330 }
331 return os;
332}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700333
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700334} // namespace art