Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "object.h" |
| 4 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 5 | #include <string.h> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 8 | #include <iostream> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 11 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 12 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 13 | #include "class_loader.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 14 | #include "dex_cache.h" |
| 15 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | #include "globals.h" |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 17 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 18 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 19 | #include "logging.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 20 | #include "monitor.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 21 | #include "runtime.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 22 | #include "stack.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 26 | Object* Object::Clone() { |
| 27 | Class* c = GetClass(); |
| 28 | DCHECK(!c->IsClassClass()); |
| 29 | |
| 30 | // Object::SizeOf gets the right size even if we're an array. |
| 31 | // Using c->AllocObject() here would be wrong. |
| 32 | size_t num_bytes = SizeOf(); |
| 33 | Object* copy = Heap::AllocObject(c, num_bytes); |
| 34 | if (copy == NULL) { |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | // Copy instance data. We assume memcpy copies by words. |
| 39 | // TODO: expose and use move32. |
| 40 | byte* src_bytes = reinterpret_cast<byte*>(this); |
| 41 | byte* dst_bytes = reinterpret_cast<byte*>(copy); |
| 42 | size_t offset = sizeof(Object); |
| 43 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
| 44 | |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 45 | if (c->IsFinalizable()) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 46 | Heap::AddFinalizerReference(copy); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 47 | } |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 48 | |
| 49 | return copy; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 52 | uint32_t Object::GetLockOwner() { |
| 53 | return Monitor::GetLockOwner(monitor_); |
| 54 | } |
| 55 | |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 56 | bool Object::IsString() const { |
| 57 | // TODO use "klass_ == String::GetJavaLangString()" instead? |
| 58 | return GetClass() == GetClass()->GetDescriptor()->GetClass(); |
| 59 | } |
| 60 | |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 61 | void Object::MonitorEnter(Thread* thread) { |
| 62 | Monitor::MonitorEnter(thread, this); |
| 63 | } |
| 64 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 65 | bool Object::MonitorExit(Thread* thread) { |
| 66 | return Monitor::MonitorExit(thread, this); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void Object::Notify() { |
| 70 | Monitor::Notify(Thread::Current(), this); |
| 71 | } |
| 72 | |
| 73 | void Object::NotifyAll() { |
| 74 | Monitor::NotifyAll(Thread::Current(), this); |
| 75 | } |
| 76 | |
| 77 | void Object::Wait(int64_t ms, int32_t ns) { |
| 78 | Monitor::Wait(Thread::Current(), this, ms, ns, true); |
| 79 | } |
| 80 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 81 | // TODO: get global references for these |
| 82 | Class* Field::java_lang_reflect_Field_ = NULL; |
| 83 | |
| 84 | void Field::SetClass(Class* java_lang_reflect_Field) { |
| 85 | CHECK(java_lang_reflect_Field_ == NULL); |
| 86 | CHECK(java_lang_reflect_Field != NULL); |
| 87 | java_lang_reflect_Field_ = java_lang_reflect_Field; |
| 88 | } |
| 89 | |
| 90 | void Field::ResetClass() { |
| 91 | CHECK(java_lang_reflect_Field_ != NULL); |
| 92 | java_lang_reflect_Field_ = NULL; |
| 93 | } |
| 94 | |
| 95 | void Field::SetTypeIdx(uint32_t type_idx) { |
| 96 | SetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), type_idx, false); |
| 97 | } |
| 98 | |
| 99 | Class* Field::GetTypeDuringLinking() const { |
| 100 | // We are assured that the necessary primitive types are in the dex cache |
| 101 | // early during class linking |
| 102 | return GetDeclaringClass()->GetDexCache()->GetResolvedType(GetTypeIdx()); |
| 103 | } |
| 104 | |
| 105 | Class* Field::GetType() const { |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 106 | if (type_ == NULL) { |
| 107 | type_ = Runtime::Current()->GetClassLinker()->ResolveType(GetTypeIdx(), this); |
| 108 | } |
| 109 | return type_; |
| 110 | } |
| 111 | |
| 112 | void Field::InitJavaFields() { |
| 113 | Thread* self = Thread::Current(); |
| 114 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 115 | MonitorEnter(self); |
| 116 | if (type_ == NULL) { |
| 117 | InitJavaFieldsLocked(); |
| 118 | } |
| 119 | MonitorExit(self); |
| 120 | } |
| 121 | |
| 122 | void Field::InitJavaFieldsLocked() { |
| 123 | GetType(); // Sets type_ as a side-effect. May throw. |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 126 | uint32_t Field::Get32(const Object* object) const { |
| 127 | CHECK((object == NULL) == IsStatic()); |
| 128 | if (IsStatic()) { |
| 129 | object = declaring_class_; |
| 130 | } |
| 131 | return object->GetField32(GetOffset(), IsVolatile()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 134 | void Field::Set32(Object* object, uint32_t new_value) const { |
| 135 | CHECK((object == NULL) == IsStatic()); |
| 136 | if (IsStatic()) { |
| 137 | object = declaring_class_; |
| 138 | } |
| 139 | object->SetField32(GetOffset(), new_value, IsVolatile()); |
| 140 | } |
| 141 | |
| 142 | uint64_t Field::Get64(const Object* object) const { |
| 143 | CHECK((object == NULL) == IsStatic()); |
| 144 | if (IsStatic()) { |
| 145 | object = declaring_class_; |
| 146 | } |
| 147 | return object->GetField64(GetOffset(), IsVolatile()); |
| 148 | } |
| 149 | |
| 150 | void Field::Set64(Object* object, uint64_t new_value) const { |
| 151 | CHECK((object == NULL) == IsStatic()); |
| 152 | if (IsStatic()) { |
| 153 | object = declaring_class_; |
| 154 | } |
| 155 | object->SetField64(GetOffset(), new_value, IsVolatile()); |
| 156 | } |
| 157 | |
| 158 | Object* Field::GetObj(const Object* object) const { |
| 159 | CHECK((object == NULL) == IsStatic()); |
| 160 | if (IsStatic()) { |
| 161 | object = declaring_class_; |
| 162 | } |
| 163 | return object->GetFieldObject<Object*>(GetOffset(), IsVolatile()); |
| 164 | } |
| 165 | |
| 166 | void Field::SetObj(Object* object, const Object* new_value) const { |
| 167 | CHECK((object == NULL) == IsStatic()); |
| 168 | if (IsStatic()) { |
| 169 | object = declaring_class_; |
| 170 | } |
| 171 | object->SetFieldObject(GetOffset(), new_value, IsVolatile()); |
| 172 | } |
| 173 | |
| 174 | bool Field::GetBoolean(const Object* object) const { |
| 175 | DCHECK(GetType()->IsPrimitiveBoolean()); |
| 176 | return Get32(object); |
| 177 | } |
| 178 | |
| 179 | void Field::SetBoolean(Object* object, bool z) const { |
| 180 | DCHECK(GetType()->IsPrimitiveBoolean()); |
| 181 | Set32(object, z); |
| 182 | } |
| 183 | |
| 184 | int8_t Field::GetByte(const Object* object) const { |
| 185 | DCHECK(GetType()->IsPrimitiveByte()); |
| 186 | return Get32(object); |
| 187 | } |
| 188 | |
| 189 | void Field::SetByte(Object* object, int8_t b) const { |
| 190 | DCHECK(GetType()->IsPrimitiveByte()); |
| 191 | Set32(object, b); |
| 192 | } |
| 193 | |
| 194 | uint16_t Field::GetChar(const Object* object) const { |
| 195 | DCHECK(GetType()->IsPrimitiveChar()); |
| 196 | return Get32(object); |
| 197 | } |
| 198 | |
| 199 | void Field::SetChar(Object* object, uint16_t c) const { |
| 200 | DCHECK(GetType()->IsPrimitiveChar()); |
| 201 | Set32(object, c); |
| 202 | } |
| 203 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 204 | int16_t Field::GetShort(const Object* object) const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 205 | DCHECK(GetType()->IsPrimitiveShort()); |
| 206 | return Get32(object); |
| 207 | } |
| 208 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 209 | void Field::SetShort(Object* object, int16_t s) const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 210 | DCHECK(GetType()->IsPrimitiveShort()); |
| 211 | Set32(object, s); |
| 212 | } |
| 213 | |
| 214 | int32_t Field::GetInt(const Object* object) const { |
| 215 | DCHECK(GetType()->IsPrimitiveInt()); |
| 216 | return Get32(object); |
| 217 | } |
| 218 | |
| 219 | void Field::SetInt(Object* object, int32_t i) const { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 220 | DCHECK(GetType()->IsPrimitiveInt()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 221 | Set32(object, i); |
| 222 | } |
| 223 | |
| 224 | int64_t Field::GetLong(const Object* object) const { |
| 225 | DCHECK(GetType()->IsPrimitiveLong()); |
| 226 | return Get64(object); |
| 227 | } |
| 228 | |
| 229 | void Field::SetLong(Object* object, int64_t j) const { |
| 230 | DCHECK(GetType()->IsPrimitiveLong()); |
| 231 | Set64(object, j); |
| 232 | } |
| 233 | |
| 234 | float Field::GetFloat(const Object* object) const { |
| 235 | DCHECK(GetType()->IsPrimitiveFloat()); |
| 236 | JValue float_bits; |
| 237 | float_bits.i = Get32(object); |
| 238 | return float_bits.f; |
| 239 | } |
| 240 | |
| 241 | void Field::SetFloat(Object* object, float f) const { |
| 242 | DCHECK(GetType()->IsPrimitiveFloat()); |
| 243 | JValue float_bits; |
| 244 | float_bits.f = f; |
| 245 | Set32(object, float_bits.i); |
| 246 | } |
| 247 | |
| 248 | double Field::GetDouble(const Object* object) const { |
| 249 | DCHECK(GetType()->IsPrimitiveDouble()); |
| 250 | JValue double_bits; |
| 251 | double_bits.j = Get64(object); |
| 252 | return double_bits.d; |
| 253 | } |
| 254 | |
| 255 | void Field::SetDouble(Object* object, double d) const { |
| 256 | DCHECK(GetType()->IsPrimitiveDouble()); |
| 257 | JValue double_bits; |
| 258 | double_bits.d = d; |
| 259 | Set64(object, double_bits.j); |
| 260 | } |
| 261 | |
| 262 | Object* Field::GetObject(const Object* object) const { |
| 263 | CHECK(!GetType()->IsPrimitive()); |
| 264 | return GetObj(object); |
| 265 | } |
| 266 | |
| 267 | void Field::SetObject(Object* object, const Object* l) const { |
| 268 | CHECK(!GetType()->IsPrimitive()); |
| 269 | SetObj(object, l); |
| 270 | } |
| 271 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 272 | bool Method::IsClassInitializer() const { |
| 273 | return IsStatic() && GetName()->Equals("<clinit>"); |
| 274 | } |
| 275 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 276 | // TODO: get global references for these |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 277 | Class* Method::java_lang_reflect_Constructor_ = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 278 | Class* Method::java_lang_reflect_Method_ = NULL; |
| 279 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 280 | void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) { |
| 281 | CHECK(java_lang_reflect_Constructor_ == NULL); |
| 282 | CHECK(java_lang_reflect_Constructor != NULL); |
| 283 | java_lang_reflect_Constructor_ = java_lang_reflect_Constructor; |
| 284 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 285 | CHECK(java_lang_reflect_Method_ == NULL); |
| 286 | CHECK(java_lang_reflect_Method != NULL); |
| 287 | java_lang_reflect_Method_ = java_lang_reflect_Method; |
| 288 | } |
| 289 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 290 | void Method::ResetClasses() { |
| 291 | CHECK(java_lang_reflect_Constructor_ != NULL); |
| 292 | java_lang_reflect_Constructor_ = NULL; |
| 293 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 294 | CHECK(java_lang_reflect_Method_ != NULL); |
| 295 | java_lang_reflect_Method_ = NULL; |
| 296 | } |
| 297 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 298 | Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) { |
| 299 | if (*p == '[') { |
| 300 | // Something like "[[[Ljava/lang/String;". |
| 301 | const char* start = p; |
| 302 | while (*p == '[') { |
| 303 | ++p; |
| 304 | } |
| 305 | if (*p == 'L') { |
| 306 | while (*p != ';') { |
| 307 | ++p; |
| 308 | } |
| 309 | } |
| 310 | ++p; // Either the ';' or the primitive type. |
| 311 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 312 | std::string descriptor(start, (p - start)); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 313 | return class_linker->FindClass(descriptor, cl); |
| 314 | } else if (*p == 'L') { |
| 315 | const char* start = p; |
| 316 | while (*p != ';') { |
| 317 | ++p; |
| 318 | } |
| 319 | ++p; |
| 320 | StringPiece descriptor(start, (p - start)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 321 | return class_linker->FindClass(descriptor.ToString(), cl); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 322 | } else { |
| 323 | return class_linker->FindPrimitiveClass(*p++); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void Method::InitJavaFieldsLocked() { |
| 328 | // Create the array. |
| 329 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 330 | size_t arg_count = GetShorty()->GetLength() - 1; |
| 331 | Class* array_class = class_linker->FindSystemClass("[Ljava/lang/Class;"); |
| 332 | ObjectArray<Class>* parameters = ObjectArray<Class>::Alloc(array_class, arg_count); |
| 333 | if (parameters == NULL) { |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | // Parse the signature, filling the array. |
| 338 | const ClassLoader* cl = GetDeclaringClass()->GetClassLoader(); |
| 339 | std::string signature(GetSignature()->ToModifiedUtf8()); |
| 340 | const char* p = signature.c_str(); |
| 341 | DCHECK_EQ(*p, '('); |
| 342 | ++p; |
| 343 | for (size_t i = 0; i < arg_count; ++i) { |
| 344 | Class* c = ExtractNextClassFromSignature(class_linker, cl, p); |
| 345 | if (c == NULL) { |
| 346 | return; |
| 347 | } |
| 348 | parameters->Set(i, c); |
| 349 | } |
| 350 | |
| 351 | DCHECK_EQ(*p, ')'); |
| 352 | ++p; |
| 353 | |
| 354 | java_parameter_types_ = parameters; |
| 355 | java_return_type_ = ExtractNextClassFromSignature(class_linker, cl, p); |
| 356 | } |
| 357 | |
| 358 | void Method::InitJavaFields() { |
| 359 | Thread* self = Thread::Current(); |
| 360 | ScopedThreadStateChange tsc(self, Thread::kRunnable); |
| 361 | MonitorEnter(self); |
| 362 | if (java_parameter_types_ == NULL || java_return_type_ == NULL) { |
| 363 | InitJavaFieldsLocked(); |
| 364 | } |
| 365 | MonitorExit(self); |
| 366 | } |
| 367 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 368 | ObjectArray<String>* Method::GetDexCacheStrings() const { |
| 369 | return GetFieldObject<ObjectArray<String>*>( |
| 370 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false); |
| 371 | } |
| 372 | |
| 373 | void Method::SetReturnTypeIdx(uint32_t new_return_type_idx) { |
| 374 | SetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_), |
| 375 | new_return_type_idx, false); |
| 376 | } |
| 377 | |
| 378 | Class* Method::GetReturnType() const { |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 379 | DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous()) |
| 380 | << PrettyMethod(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 381 | // Short-cut |
| 382 | Class* result = GetDexCacheResolvedTypes()->Get(GetReturnTypeIdx()); |
| 383 | if (result == NULL) { |
| 384 | // Do full linkage and set cache value for next call |
| 385 | result = Runtime::Current()->GetClassLinker()->ResolveType(GetReturnTypeIdx(), this); |
| 386 | } |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 387 | CHECK(result != NULL) << PrettyMethod(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 388 | return result; |
| 389 | } |
| 390 | |
| 391 | void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) { |
| 392 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), |
| 393 | new_dex_cache_strings, false); |
| 394 | } |
| 395 | |
| 396 | ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const { |
| 397 | return GetFieldObject<ObjectArray<Class>*>( |
| 398 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false); |
| 399 | } |
| 400 | |
| 401 | void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) { |
| 402 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), |
| 403 | new_dex_cache_classes, false); |
| 404 | } |
| 405 | |
| 406 | ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const { |
| 407 | return GetFieldObject<ObjectArray<Method>*>( |
| 408 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false); |
| 409 | } |
| 410 | |
| 411 | void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) { |
| 412 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), |
| 413 | new_dex_cache_methods, false); |
| 414 | } |
| 415 | |
| 416 | ObjectArray<Field>* Method::GetDexCacheResolvedFields() const { |
| 417 | return GetFieldObject<ObjectArray<Field>*>( |
| 418 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false); |
| 419 | } |
| 420 | |
| 421 | void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) { |
| 422 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), |
| 423 | new_dex_cache_fields, false); |
| 424 | } |
| 425 | |
| 426 | CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const { |
| 427 | return GetFieldPtr<CodeAndDirectMethods*>( |
| 428 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_), |
| 429 | false); |
| 430 | } |
| 431 | |
| 432 | void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) { |
| 433 | SetFieldPtr<CodeAndDirectMethods*>( |
| 434 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_), |
| 435 | new_value, false); |
| 436 | } |
| 437 | |
| 438 | ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const { |
| 439 | return GetFieldObject<ObjectArray<StaticStorageBase>*>( |
| 440 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
| 441 | false); |
| 442 | } |
| 443 | |
| 444 | void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) { |
| 445 | SetFieldObject( |
| 446 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
| 447 | new_value, false); |
| 448 | |
| 449 | } |
| 450 | |
| 451 | size_t Method::NumArgRegisters(const StringPiece& shorty) { |
| 452 | CHECK_LE(1, shorty.length()); |
| 453 | uint32_t num_registers = 0; |
| 454 | for (int i = 1; i < shorty.length(); ++i) { |
| 455 | char ch = shorty[i]; |
| 456 | if (ch == 'D' || ch == 'J') { |
| 457 | num_registers += 2; |
| 458 | } else { |
| 459 | num_registers += 1; |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 462 | return num_registers; |
| 463 | } |
| 464 | |
| 465 | size_t Method::NumArgArrayBytes() const { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 466 | const String* shorty = GetShorty(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 467 | size_t num_bytes = 0; |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 468 | for (int i = 1; i < shorty->GetLength(); ++i) { |
| 469 | char ch = shorty->CharAt(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 470 | if (ch == 'D' || ch == 'J') { |
| 471 | num_bytes += 8; |
| 472 | } else if (ch == 'L') { |
| 473 | // Argument is a reference or an array. The shorty descriptor |
| 474 | // does not distinguish between these types. |
| 475 | num_bytes += sizeof(Object*); |
| 476 | } else { |
| 477 | num_bytes += 4; |
| 478 | } |
| 479 | } |
| 480 | return num_bytes; |
| 481 | } |
| 482 | |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 483 | size_t Method::NumArgs() const { |
| 484 | // "1 +" because the first in Args is the receiver. |
| 485 | // "- 1" because we don't count the return type. |
| 486 | return (IsStatic() ? 0 : 1) + GetShorty()->GetLength() - 1; |
| 487 | } |
| 488 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 489 | // The number of reference arguments to this method including implicit this |
| 490 | // pointer |
| 491 | size_t Method::NumReferenceArgs() const { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 492 | const String* shorty = GetShorty(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 493 | size_t result = IsStatic() ? 0 : 1; // The implicit this pointer. |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 494 | for (int i = 1; i < shorty->GetLength(); i++) { |
| 495 | char ch = shorty->CharAt(i); |
| 496 | if ((ch == 'L') || (ch == '[')) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 497 | result++; |
| 498 | } |
| 499 | } |
| 500 | return result; |
| 501 | } |
| 502 | |
| 503 | // The number of long or double arguments |
| 504 | size_t Method::NumLongOrDoubleArgs() const { |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 505 | const String* shorty = GetShorty(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 506 | size_t result = 0; |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 507 | for (int i = 1; i < shorty->GetLength(); i++) { |
| 508 | char ch = shorty->CharAt(i); |
| 509 | if ((ch == 'D') || (ch == 'J')) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 510 | result++; |
| 511 | } |
| 512 | } |
| 513 | return result; |
| 514 | } |
| 515 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 516 | // Is the given method parameter a reference? |
| 517 | bool Method::IsParamAReference(unsigned int param) const { |
| 518 | CHECK_LT(param, NumArgs()); |
| 519 | if (IsStatic()) { |
| 520 | param++; // 0th argument must skip return value at start of the shorty |
| 521 | } else if (param == 0) { |
| 522 | return true; // this argument |
| 523 | } |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 524 | return GetShorty()->CharAt(param) == 'L'; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // Is the given method parameter a long or double? |
| 528 | bool Method::IsParamALongOrDouble(unsigned int param) const { |
| 529 | CHECK_LT(param, NumArgs()); |
| 530 | if (IsStatic()) { |
| 531 | param++; // 0th argument must skip return value at start of the shorty |
| 532 | } else if (param == 0) { |
| 533 | return false; // this argument |
| 534 | } |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 535 | char ch = GetShorty()->CharAt(param); |
| 536 | return (ch == 'J' || ch == 'D'); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static size_t ShortyCharToSize(char x) { |
| 540 | switch (x) { |
| 541 | case 'V': return 0; |
| 542 | case '[': return kPointerSize; |
| 543 | case 'L': return kPointerSize; |
| 544 | case 'D': return 8; |
| 545 | case 'J': return 8; |
| 546 | default: return 4; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | size_t Method::ParamSize(unsigned int param) const { |
| 551 | CHECK_LT(param, NumArgs()); |
| 552 | if (IsStatic()) { |
| 553 | param++; // 0th argument must skip return value at start of the shorty |
| 554 | } else if (param == 0) { |
| 555 | return kPointerSize; // this argument |
| 556 | } |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 557 | return ShortyCharToSize(GetShorty()->CharAt(param)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | size_t Method::ReturnSize() const { |
Brian Carlstrom | 2ed6739 | 2011-09-09 14:53:28 -0700 | [diff] [blame] | 561 | return ShortyCharToSize(GetShorty()->CharAt(0)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 564 | Method* Method::FindOverriddenMethod() const { |
| 565 | if (IsStatic()) { |
| 566 | return NULL; |
| 567 | } |
| 568 | Class* declaring_class = GetDeclaringClass(); |
| 569 | Class* super_class = declaring_class->GetSuperClass(); |
| 570 | uint16_t method_index = GetMethodIndex(); |
| 571 | ObjectArray<Method>* super_class_vtable = super_class->GetVTable(); |
| 572 | Method* result = NULL; |
| 573 | if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) { |
| 574 | result = super_class_vtable->Get(method_index); |
| 575 | } else { |
| 576 | ObjectArray<Class>* interfaces = declaring_class->GetInterfaces(); |
| 577 | String* name = GetName(); |
| 578 | String* signature = GetSignature(); |
| 579 | for (int32_t i = 0; i < interfaces->GetLength() && result == NULL; i++) { |
| 580 | Class* interface = interfaces->Get(i); |
| 581 | result = interface->FindInterfaceMethod(name, signature); |
| 582 | } |
| 583 | } |
| 584 | DCHECK (result == NULL || HasSameNameAndSignature(result)); |
| 585 | return result; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 588 | uint32_t Method::ToDexPC(const uintptr_t pc) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 589 | const uint32_t* mapping_table = GetMappingTable(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 590 | if (mapping_table == NULL) { |
Brian Carlstrom | 26c935a | 2011-10-16 14:52:35 -0700 | [diff] [blame] | 591 | DCHECK(IsNative() || IsCalleeSaveMethod()) << PrettyMethod(this); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 592 | return DexFile::kDexNoIndex; // Special no mapping case |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 593 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 594 | size_t mapping_table_length = GetMappingTableLength(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 595 | uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetCode()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 596 | uint32_t best_offset = 0; |
| 597 | uint32_t best_dex_offset = 0; |
| 598 | for (size_t i = 0; i < mapping_table_length; i += 2) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 599 | uint32_t map_offset = mapping_table[i]; |
| 600 | uint32_t map_dex_offset = mapping_table[i + 1]; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 601 | if (map_offset == sought_offset) { |
| 602 | best_offset = map_offset; |
| 603 | best_dex_offset = map_dex_offset; |
| 604 | break; |
| 605 | } |
| 606 | if (map_offset < sought_offset && map_offset > best_offset) { |
| 607 | best_offset = map_offset; |
| 608 | best_dex_offset = map_dex_offset; |
| 609 | } |
| 610 | } |
| 611 | return best_dex_offset; |
| 612 | } |
| 613 | |
| 614 | uintptr_t Method::ToNativePC(const uint32_t dex_pc) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 615 | const uint32_t* mapping_table = GetMappingTable(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 616 | if (mapping_table == NULL) { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 617 | DCHECK_EQ(dex_pc, 0U); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 618 | return 0; // Special no mapping/pc == 0 case |
| 619 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 620 | size_t mapping_table_length = GetMappingTableLength(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 621 | for (size_t i = 0; i < mapping_table_length; i += 2) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 622 | uint32_t map_offset = mapping_table[i]; |
| 623 | uint32_t map_dex_offset = mapping_table[i + 1]; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 624 | if (map_dex_offset == dex_pc) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 625 | return reinterpret_cast<uintptr_t>(GetCode()) + map_offset; |
| 626 | } |
| 627 | } |
| 628 | LOG(FATAL) << "Looking up Dex PC not contained in method"; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const { |
| 633 | DexCache* dex_cache = GetDeclaringClass()->GetDexCache(); |
| 634 | const ClassLoader* class_loader = GetDeclaringClass()->GetClassLoader(); |
| 635 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 636 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 637 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(GetCodeItemOffset()); |
| 638 | // Iterate over the catch handlers associated with dex_pc |
| 639 | for (DexFile::CatchHandlerIterator iter = dex_file.dexFindCatchHandler(*code_item, dex_pc); |
| 640 | !iter.HasNext(); iter.Next()) { |
| 641 | uint32_t iter_type_idx = iter.Get().type_idx_; |
| 642 | // Catch all case |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 643 | if (iter_type_idx == DexFile::kDexNoIndex) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 644 | return iter.Get().address_; |
| 645 | } |
| 646 | // Does this catch exception type apply? |
| 647 | Class* iter_exception_type = |
| 648 | class_linker->ResolveType(dex_file, iter_type_idx, dex_cache, class_loader); |
| 649 | if (iter_exception_type->IsAssignableFrom(exception_type)) { |
| 650 | return iter.Get().address_; |
| 651 | } |
| 652 | } |
| 653 | // Handler not found |
| 654 | return DexFile::kDexNoIndex; |
| 655 | } |
| 656 | |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 657 | void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const { |
| 658 | // Push a transition back into managed code onto the linked list in thread. |
| 659 | CHECK_EQ(Thread::kRunnable, self->GetState()); |
| 660 | NativeToManagedRecord record; |
| 661 | self->PushNativeToManagedRecord(&record); |
| 662 | |
| 663 | // Call the invoke stub associated with the method. |
| 664 | // Pass everything as arguments. |
| 665 | const Method::InvokeStub* stub = GetInvokeStub(); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 666 | |
| 667 | bool have_executable_code = (GetCode() != NULL); |
| 668 | #if !defined(__arm__) |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 669 | // Currently we can only compile non-native methods for ARM. |
| 670 | have_executable_code = IsNative(); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 671 | #endif |
| 672 | |
| 673 | if (have_executable_code && stub != NULL) { |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 674 | bool log = false; |
| 675 | if (log) { |
| 676 | LOG(INFO) << "invoking " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub; |
| 677 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 678 | (*stub)(this, receiver, self, args, result); |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 679 | if (log) { |
| 680 | LOG(INFO) << "returned " << PrettyMethod(this) << " code=" << (void*) GetCode() << " stub=" << (void*) stub; |
| 681 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 682 | } else { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 683 | if (Runtime::Current()->IsStarted()) { |
| 684 | LOG(WARNING) << "Not invoking method with no associated code: " << PrettyMethod(this); |
| 685 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 686 | if (result != NULL) { |
| 687 | result->j = 0; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | // Pop transition. |
| 692 | self->PopNativeToManagedRecord(record); |
| 693 | } |
| 694 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 695 | bool Method::IsRegistered() const { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 696 | void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false); |
| 697 | void* jni_stub = Runtime::Current()->GetJniStubArray()->GetData(); |
| 698 | return native_method != jni_stub; |
| 699 | } |
| 700 | |
| 701 | void Method::RegisterNative(const void* native_method) { |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 702 | CHECK(IsNative()) << PrettyMethod(this); |
| 703 | CHECK(native_method != NULL) << PrettyMethod(this); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 704 | SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), |
| 705 | native_method, false); |
| 706 | } |
| 707 | |
| 708 | void Method::UnregisterNative() { |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 709 | CHECK(IsNative()) << PrettyMethod(this); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 710 | // restore stub to lookup native pointer via dlsym |
| 711 | RegisterNative(Runtime::Current()->GetJniStubArray()->GetData()); |
| 712 | } |
| 713 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 714 | void Class::SetStatus(Status new_status) { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 715 | CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted()) |
| 716 | << PrettyClass(this) << " " << GetStatus() << " -> " << new_status; |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 717 | CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this); |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 718 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | DexCache* Class::GetDexCache() const { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 722 | return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void Class::SetDexCache(DexCache* new_dex_cache) { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 726 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 729 | Object* Class::AllocObject() { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 730 | DCHECK(!IsAbstract()) << PrettyClass(this); |
| 731 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 732 | DCHECK(!IsPrimitive()) << PrettyClass(this); |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 733 | DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 734 | return Heap::AllocObject(this, this->object_size_); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 737 | void Class::DumpClass(std::ostream& os, int flags) const { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 738 | if ((flags & kDumpClassFullDetail) == 0) { |
| 739 | os << PrettyClass(this); |
| 740 | if ((flags & kDumpClassClassLoader) != 0) { |
| 741 | os << ' ' << GetClassLoader(); |
| 742 | } |
| 743 | if ((flags & kDumpClassInitialized) != 0) { |
| 744 | os << ' ' << GetStatus(); |
| 745 | } |
| 746 | os << std::endl; |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | Class* super = GetSuperClass(); |
| 751 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
| 752 | << "'" << GetDescriptor()->ToModifiedUtf8() << "' cl=" << GetClassLoader() << " -----\n", |
| 753 | os << " objectSize=" << SizeOf() << " " |
| 754 | << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n", |
| 755 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 756 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
| 757 | if (super != NULL) { |
| 758 | os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n"; |
| 759 | } |
| 760 | if (IsArrayClass()) { |
| 761 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 762 | } |
| 763 | if (NumInterfaces() > 0) { |
| 764 | os << " interfaces (" << NumInterfaces() << "):\n"; |
| 765 | for (size_t i = 0; i < NumInterfaces(); ++i) { |
| 766 | Class* interface = GetInterface(i); |
| 767 | const ClassLoader* cl = interface->GetClassLoader(); |
| 768 | os << StringPrintf(" %2d: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
| 769 | } |
| 770 | } |
| 771 | os << " vtable (" << NumVirtualMethods() << " entries, " |
| 772 | << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n"; |
| 773 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 774 | os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str()); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 775 | } |
| 776 | os << " direct methods (" << NumDirectMethods() << " entries):\n"; |
| 777 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 778 | os << StringPrintf(" %2d: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str()); |
| 779 | } |
| 780 | if (NumStaticFields() > 0) { |
| 781 | os << " static fields (" << NumStaticFields() << " entries):\n"; |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 782 | if (IsResolved() || IsErroneous()) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 783 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 784 | os << StringPrintf(" %2d: %s\n", i, PrettyField(GetStaticField(i)).c_str()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 785 | } |
| 786 | } else { |
| 787 | os << " <not yet available>"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | if (NumInstanceFields() > 0) { |
| 791 | os << " instance fields (" << NumInstanceFields() << " entries):\n"; |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 792 | if (IsResolved() || IsErroneous()) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 793 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 794 | os << StringPrintf(" %2d: %s\n", i, PrettyField(GetInstanceField(i)).c_str()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 795 | } |
| 796 | } else { |
| 797 | os << " <not yet available>"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 802 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 803 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 804 | // Sanity check that the number of bits set in the reference offset bitmap |
| 805 | // agrees with the number of references |
| 806 | Class* cur = this; |
| 807 | size_t cnt = 0; |
| 808 | while (cur) { |
| 809 | cnt += cur->NumReferenceInstanceFieldsDuringLinking(); |
| 810 | cur = cur->GetSuperClass(); |
| 811 | } |
| 812 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), cnt); |
| 813 | } |
| 814 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 815 | new_reference_offsets, false); |
| 816 | } |
| 817 | |
| 818 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 819 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 820 | // Sanity check that the number of bits set in the reference offset bitmap |
| 821 | // agrees with the number of references |
| 822 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 823 | NumReferenceStaticFieldsDuringLinking()); |
| 824 | } |
| 825 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 826 | new_reference_offsets, false); |
| 827 | } |
| 828 | |
| 829 | size_t Class::PrimitiveSize() const { |
| 830 | switch (GetPrimitiveType()) { |
| 831 | case kPrimBoolean: |
| 832 | case kPrimByte: |
| 833 | case kPrimChar: |
| 834 | case kPrimShort: |
| 835 | case kPrimInt: |
| 836 | case kPrimFloat: |
| 837 | return sizeof(int32_t); |
| 838 | case kPrimLong: |
| 839 | case kPrimDouble: |
| 840 | return sizeof(int64_t); |
| 841 | default: |
| 842 | LOG(FATAL) << "Primitive type size calculation on invalid type " << this; |
| 843 | return 0; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | size_t Class::GetTypeSize(const String* descriptor) { |
| 848 | switch (descriptor->CharAt(0)) { |
| 849 | case 'B': return 1; // byte |
| 850 | case 'C': return 2; // char |
| 851 | case 'D': return 8; // double |
| 852 | case 'F': return 4; // float |
| 853 | case 'I': return 4; // int |
| 854 | case 'J': return 8; // long |
| 855 | case 'S': return 2; // short |
| 856 | case 'Z': return 1; // boolean |
| 857 | case 'L': return sizeof(Object*); |
| 858 | case '[': return sizeof(Array*); |
| 859 | default: |
| 860 | LOG(ERROR) << "Unknown type " << descriptor; |
| 861 | return 0; |
| 862 | } |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 865 | bool Class::Implements(const Class* klass) const { |
| 866 | DCHECK(klass != NULL); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 867 | DCHECK(klass->IsInterface()) << PrettyClass(this); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 868 | // All interfaces implemented directly and by our superclass, and |
| 869 | // recursively all super-interfaces of those interfaces, are listed |
| 870 | // in iftable_, so we can just do a linear scan through that. |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 871 | int32_t iftable_count = GetIfTableCount(); |
| 872 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 873 | for (int32_t i = 0; i < iftable_count; i++) { |
| 874 | if (iftable->Get(i)->GetInterface() == klass) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 875 | return true; |
| 876 | } |
| 877 | } |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | // Determine whether "this" is assignable from "klazz", where both of these |
| 882 | // are array classes. |
| 883 | // |
| 884 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 885 | // Y[][] = Y[][] --> true (identity) |
| 886 | // X[][] = Y[][] --> true (element superclass) |
| 887 | // Y = Y[][] --> false |
| 888 | // Y[] = Y[][] --> false |
| 889 | // Object = Y[][] --> true (everything is an object) |
| 890 | // Object[] = Y[][] --> true |
| 891 | // Object[][] = Y[][] --> true |
| 892 | // Object[][][] = Y[][] --> false (too many []s) |
| 893 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 894 | // Serializable[] = Y[][] --> true |
| 895 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 896 | // |
| 897 | // Don't forget about primitive types. |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 898 | // Object[] = int[] --> false |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 899 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 900 | bool Class::IsArrayAssignableFromArray(const Class* src) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 901 | DCHECK(IsArrayClass()) << PrettyClass(this); |
| 902 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 903 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 906 | bool Class::IsAssignableFromArray(const Class* src) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 907 | DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom |
| 908 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 909 | if (!IsArrayClass()) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 910 | // If "this" is not also an array, it must be Object. |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 911 | // src's super should be java_lang_Object, since it is an array. |
| 912 | Class* java_lang_Object = src->GetSuperClass(); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 913 | DCHECK(java_lang_Object != NULL) << PrettyClass(src); |
| 914 | DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 915 | return this == java_lang_Object; |
| 916 | } |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 917 | return IsArrayAssignableFromArray(src); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | bool Class::IsSubClass(const Class* klass) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 921 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 922 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 923 | const Class* current = this; |
| 924 | do { |
| 925 | if (current == klass) { |
| 926 | return true; |
| 927 | } |
| 928 | current = current->GetSuperClass(); |
| 929 | } while (current != NULL); |
| 930 | return false; |
| 931 | } |
| 932 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 933 | bool Class::IsInSamePackage(const String* descriptor_string_1, |
| 934 | const String* descriptor_string_2) { |
| 935 | const std::string descriptor1(descriptor_string_1->ToModifiedUtf8()); |
| 936 | const std::string descriptor2(descriptor_string_2->ToModifiedUtf8()); |
| 937 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 938 | size_t i = 0; |
| 939 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 940 | ++i; |
| 941 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 942 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 943 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 944 | return false; |
| 945 | } else { |
| 946 | return true; |
| 947 | } |
| 948 | } |
| 949 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 950 | #if 0 |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 951 | bool Class::IsInSamePackage(const StringPiece& descriptor1, |
| 952 | const StringPiece& descriptor2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 953 | size_t size = std::min(descriptor1.size(), descriptor2.size()); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 954 | std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 955 | pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size, |
| 956 | descriptor2.begin()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 957 | return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos); |
| 958 | } |
| 959 | #endif |
| 960 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 961 | bool Class::IsInSamePackage(const Class* that) const { |
| 962 | const Class* klass1 = this; |
| 963 | const Class* klass2 = that; |
| 964 | if (klass1 == klass2) { |
| 965 | return true; |
| 966 | } |
| 967 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 968 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 969 | return false; |
| 970 | } |
| 971 | // Arrays are in the same package when their element classes are. |
jeffhao | 4a801a4 | 2011-09-23 13:53:40 -0700 | [diff] [blame] | 972 | while (klass1->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 973 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 974 | } |
jeffhao | 4a801a4 | 2011-09-23 13:53:40 -0700 | [diff] [blame] | 975 | while (klass2->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 976 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 977 | } |
| 978 | // Compare the package part of the descriptor string. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 979 | return IsInSamePackage(klass1->descriptor_, klass2->descriptor_); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 982 | const ClassLoader* Class::GetClassLoader() const { |
| 983 | return GetFieldObject<const ClassLoader*>( |
| 984 | OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 987 | void Class::SetClassLoader(const ClassLoader* new_cl) { |
| 988 | ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl); |
| 989 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), |
| 990 | new_class_loader, false); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Ian Rogers | b04f69f | 2011-10-17 00:40:54 -0700 | [diff] [blame^] | 993 | Method* Class::FindVirtualMethodForInterface(Method* method, bool can_throw) { |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 994 | Class* declaring_class = method->GetDeclaringClass(); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 995 | DCHECK(declaring_class != NULL) << PrettyClass(this); |
| 996 | DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 997 | // TODO cache to improve lookup speed |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 998 | int32_t iftable_count = GetIfTableCount(); |
| 999 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 1000 | for (int32_t i = 0; i < iftable_count; i++) { |
| 1001 | InterfaceEntry* interface_entry = iftable->Get(i); |
| 1002 | if (interface_entry->GetInterface() == declaring_class) { |
| 1003 | return interface_entry->GetMethodArray()->Get(method->GetMethodIndex()); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
Ian Rogers | b04f69f | 2011-10-17 00:40:54 -0700 | [diff] [blame^] | 1006 | if (can_throw) { |
| 1007 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;", |
| 1008 | "Class %s does not implement interface %s", |
| 1009 | PrettyDescriptor(GetDescriptor()).c_str(), |
| 1010 | PrettyDescriptor(declaring_class->GetDescriptor()).c_str()); |
| 1011 | } |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 1012 | return NULL; |
| 1013 | } |
| 1014 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1015 | Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1016 | // Check the current class before checking the interfaces. |
| 1017 | Method* method = FindVirtualMethod(name, signature); |
| 1018 | if (method != NULL) { |
| 1019 | return method; |
| 1020 | } |
| 1021 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 1022 | int32_t iftable_count = GetIfTableCount(); |
| 1023 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 1024 | for (int32_t i = 0; i < iftable_count; i++) { |
| 1025 | method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 1026 | if (method != NULL) { |
| 1027 | return method; |
| 1028 | } |
| 1029 | } |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1033 | Method* Class::FindInterfaceMethod(String* name, String* signature) const { |
| 1034 | // Check the current class before checking the interfaces. |
| 1035 | Method* method = FindVirtualMethod(name, signature); |
| 1036 | if (method != NULL) { |
| 1037 | return method; |
| 1038 | } |
| 1039 | int32_t iftable_count = GetIfTableCount(); |
| 1040 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 1041 | for (int32_t i = 0; i < iftable_count; i++) { |
| 1042 | Class* interface = iftable->Get(i)->GetInterface(); |
| 1043 | method = interface->FindVirtualMethod(name, signature); |
| 1044 | if (method != NULL) { |
| 1045 | return method; |
| 1046 | } |
| 1047 | } |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1051 | Method* Class::FindDeclaredDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1052 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1053 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1054 | Method* method = GetDirectMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1055 | if (method->GetName()->Equals(name) && |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1056 | method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1057 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1060 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1063 | Method* Class::FindDirectMethod(const StringPiece& name, |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1064 | const StringPiece& signature) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1065 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1066 | Method* method = klass->FindDeclaredDirectMethod(name, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1067 | if (method != NULL) { |
| 1068 | return method; |
| 1069 | } |
| 1070 | } |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | |
| 1074 | Method* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1075 | const StringPiece& signature) const { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1076 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1077 | Method* method = GetVirtualMethod(i); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1078 | if (method->GetName()->Equals(name) && method->GetSignature()->Equals(signature)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1079 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1080 | } |
| 1081 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1082 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1085 | Method* Class::FindDeclaredVirtualMethod(String* name, String* signature) const { |
| 1086 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 1087 | Method* method = GetVirtualMethod(i); |
| 1088 | if (method->GetName() == name && method->GetSignature() == signature) { |
| 1089 | return method; |
| 1090 | } else { |
| 1091 | LOG(INFO) << "Find (" << name->ToModifiedUtf8() << ", " << signature->ToModifiedUtf8() |
| 1092 | << ") != " << PrettyMethod(method); |
| 1093 | } |
| 1094 | } |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | |
| 1098 | Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const { |
| 1099 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 1100 | Method* method = klass->FindDeclaredVirtualMethod(name, signature); |
| 1101 | if (method != NULL) { |
| 1102 | return method; |
| 1103 | } |
| 1104 | } |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | |
| 1108 | Method* Class::FindVirtualMethod(String* name, String* signature) const { |
| 1109 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 1110 | Method* method = klass->FindDeclaredVirtualMethod(name, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1111 | if (method != NULL) { |
| 1112 | return method; |
| 1113 | } |
| 1114 | } |
| 1115 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1118 | Field* Class::FindDeclaredInstanceField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1119 | // Is the field in this class? |
| 1120 | // Interfaces are not relevant because they can't contain instance fields. |
| 1121 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 1122 | Field* f = GetInstanceField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1123 | if (f->GetName()->Equals(name) && type == f->GetType()) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1124 | return f; |
| 1125 | } |
| 1126 | } |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1130 | Field* Class::FindInstanceField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1131 | // Is the field in this class, or any of its superclasses? |
| 1132 | // Interfaces are not relevant because they can't contain instance fields. |
| 1133 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1134 | Field* f = c->FindDeclaredInstanceField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1135 | if (f != NULL) { |
| 1136 | return f; |
| 1137 | } |
| 1138 | } |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1142 | Field* Class::FindDeclaredStaticField(const StringPiece& name, Class* type) { |
| 1143 | DCHECK(type != NULL); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1144 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 1145 | Field* f = GetStaticField(i); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1146 | if (f->GetName()->Equals(name) && f->GetType() == type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1147 | return f; |
| 1148 | } |
| 1149 | } |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1153 | Field* Class::FindStaticField(const StringPiece& name, Class* type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1154 | // Is the field in this class (or its interfaces), or any of its |
| 1155 | // superclasses (or their interfaces)? |
| 1156 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 1157 | // Is the field in this class? |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1158 | Field* f = c->FindDeclaredStaticField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1159 | if (f != NULL) { |
| 1160 | return f; |
| 1161 | } |
| 1162 | |
| 1163 | // Is this field in any of this class' interfaces? |
jeffhao | e0cfb6f | 2011-09-22 16:42:56 -0700 | [diff] [blame] | 1164 | for (int32_t i = 0; i < c->GetIfTableCount(); ++i) { |
| 1165 | InterfaceEntry* interface_entry = c->GetIfTable()->Get(i); |
| 1166 | Class* interface = interface_entry->GetInterface(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1167 | f = interface->FindDeclaredStaticField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1168 | if (f != NULL) { |
| 1169 | return f; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | return NULL; |
| 1174 | } |
| 1175 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1176 | Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) { |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 1177 | DCHECK(array_class != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1178 | DCHECK_GE(component_count, 0); |
| 1179 | DCHECK(array_class->IsArrayClass()); |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 1180 | |
| 1181 | size_t header_size = sizeof(Array); |
| 1182 | size_t data_size = component_count * component_size; |
| 1183 | size_t size = header_size + data_size; |
| 1184 | |
| 1185 | // Check for overflow and throw OutOfMemoryError if this was an unreasonable request. |
| 1186 | size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size); |
| 1187 | if (data_size >> component_shift != size_t(component_count) || size < data_size) { |
| 1188 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;", |
| 1189 | "%s of length %zd exceeds the VM limit", |
| 1190 | PrettyDescriptor(array_class->GetDescriptor()).c_str(), component_count); |
| 1191 | return NULL; |
| 1192 | } |
| 1193 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1194 | Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size)); |
| 1195 | if (array != NULL) { |
| 1196 | DCHECK(array->IsArrayInstance()); |
| 1197 | array->SetLength(component_count); |
| 1198 | } |
| 1199 | return array; |
| 1200 | } |
| 1201 | |
| 1202 | Array* Array::Alloc(Class* array_class, int32_t component_count) { |
| 1203 | return Alloc(array_class, component_count, array_class->GetComponentSize()); |
| 1204 | } |
| 1205 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1206 | bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1207 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1208 | "length=%i; index=%i", length_, index); |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | bool Array::ThrowArrayStoreException(Object* object) const { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1213 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1214 | "Can't store an element of type %s into an array of type %s", |
| 1215 | PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str()); |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1219 | template<typename T> |
| 1220 | PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1221 | DCHECK(array_class_ != NULL); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1222 | Array* raw_array = Array::Alloc(array_class_, length, sizeof(T)); |
| 1223 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 1224 | } |
| 1225 | |
| 1226 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 1227 | |
| 1228 | // Explicitly instantiate all the primitive array types. |
| 1229 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 1230 | template class PrimitiveArray<int8_t>; // ByteArray |
| 1231 | template class PrimitiveArray<uint16_t>; // CharArray |
| 1232 | template class PrimitiveArray<double>; // DoubleArray |
| 1233 | template class PrimitiveArray<float>; // FloatArray |
| 1234 | template class PrimitiveArray<int32_t>; // IntArray |
| 1235 | template class PrimitiveArray<int64_t>; // LongArray |
| 1236 | template class PrimitiveArray<int16_t>; // ShortArray |
| 1237 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1238 | // Explicitly instantiate Class[][] |
| 1239 | template class ObjectArray<ObjectArray<Class> >; |
| 1240 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1241 | // TODO: get global references for these |
| 1242 | Class* String::java_lang_String_ = NULL; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1243 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1244 | void String::SetClass(Class* java_lang_String) { |
| 1245 | CHECK(java_lang_String_ == NULL); |
| 1246 | CHECK(java_lang_String != NULL); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1247 | java_lang_String_ = java_lang_String; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1248 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1249 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1250 | void String::ResetClass() { |
| 1251 | CHECK(java_lang_String_ != NULL); |
| 1252 | java_lang_String_ = NULL; |
| 1253 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1254 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 1255 | String* String::Intern() { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1256 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 1257 | } |
| 1258 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 1259 | int32_t String::GetHashCode() { |
| 1260 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 1261 | if (result == 0) { |
| 1262 | ComputeHashCode(); |
| 1263 | } |
| 1264 | result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 1265 | DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0) |
| 1266 | << ToModifiedUtf8() << " " << result; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1267 | return result; |
| 1268 | } |
| 1269 | |
| 1270 | int32_t String::GetLength() const { |
| 1271 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false); |
| 1272 | DCHECK(result >= 0 && result <= GetCharArray()->GetLength()); |
| 1273 | return result; |
| 1274 | } |
| 1275 | |
| 1276 | uint16_t String::CharAt(int32_t index) const { |
| 1277 | // TODO: do we need this? Equals is the only caller, and could |
| 1278 | // bounds check itself. |
| 1279 | if (index < 0 || index >= count_) { |
| 1280 | Thread* self = Thread::Current(); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1281 | self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;", |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1282 | "length=%i; index=%i", count_, index); |
| 1283 | return 0; |
| 1284 | } |
| 1285 | return GetCharArray()->Get(index + GetOffset()); |
| 1286 | } |
| 1287 | |
| 1288 | String* String::AllocFromUtf16(int32_t utf16_length, |
| 1289 | const uint16_t* utf16_data_in, |
| 1290 | int32_t hash_code) { |
| 1291 | String* string = Alloc(GetJavaLangString(), utf16_length); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1292 | if (string == NULL) { |
| 1293 | return NULL; |
| 1294 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1295 | // TODO: use 16-bit wide memset variant |
| 1296 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1297 | if (array == NULL) { |
| 1298 | return NULL; |
| 1299 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1300 | for (int i = 0; i < utf16_length; i++) { |
| 1301 | array->Set(i, utf16_data_in[i]); |
| 1302 | } |
| 1303 | if (hash_code != 0) { |
| 1304 | string->SetHashCode(hash_code); |
| 1305 | } else { |
| 1306 | string->ComputeHashCode(); |
| 1307 | } |
| 1308 | return string; |
| 1309 | } |
| 1310 | |
| 1311 | String* String::AllocFromModifiedUtf8(const char* utf) { |
| 1312 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 1313 | return AllocFromModifiedUtf8(char_count, utf); |
| 1314 | } |
| 1315 | |
| 1316 | String* String::AllocFromModifiedUtf8(int32_t utf16_length, |
| 1317 | const char* utf8_data_in) { |
| 1318 | String* string = Alloc(GetJavaLangString(), utf16_length); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1319 | if (string == NULL) { |
| 1320 | return NULL; |
| 1321 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1322 | uint16_t* utf16_data_out = |
| 1323 | const_cast<uint16_t*>(string->GetCharArray()->GetData()); |
| 1324 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 1325 | string->ComputeHashCode(); |
| 1326 | return string; |
| 1327 | } |
| 1328 | |
| 1329 | String* String::Alloc(Class* java_lang_String, int32_t utf16_length) { |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1330 | CharArray* array = CharArray::Alloc(utf16_length); |
| 1331 | if (array == NULL) { |
| 1332 | return NULL; |
| 1333 | } |
| 1334 | return Alloc(java_lang_String, array); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | String* String::Alloc(Class* java_lang_String, CharArray* array) { |
| 1338 | String* string = down_cast<String*>(java_lang_String->AllocObject()); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1339 | if (string == NULL) { |
| 1340 | return NULL; |
| 1341 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1342 | string->SetArray(array); |
| 1343 | string->SetCount(array->GetLength()); |
| 1344 | return string; |
| 1345 | } |
| 1346 | |
| 1347 | bool String::Equals(const String* that) const { |
| 1348 | if (this == that) { |
| 1349 | // Quick reference equality test |
| 1350 | return true; |
| 1351 | } else if (that == NULL) { |
| 1352 | // Null isn't an instanceof anything |
| 1353 | return false; |
| 1354 | } else if (this->GetLength() != that->GetLength()) { |
| 1355 | // Quick length inequality test |
| 1356 | return false; |
| 1357 | } else { |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1358 | // Note: don't short circuit on hash code as we're presumably here as the |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1359 | // hash code was already equal |
| 1360 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
| 1361 | if (this->CharAt(i) != that->CharAt(i)) { |
| 1362 | return false; |
| 1363 | } |
| 1364 | } |
| 1365 | return true; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | bool String::Equals(const uint16_t* that_chars, int32_t that_offset, |
| 1370 | int32_t that_length) const { |
| 1371 | if (this->GetLength() != that_length) { |
| 1372 | return false; |
| 1373 | } else { |
| 1374 | for (int32_t i = 0; i < that_length; ++i) { |
| 1375 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 1376 | return false; |
| 1377 | } |
| 1378 | } |
| 1379 | return true; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | bool String::Equals(const char* modified_utf8) const { |
| 1384 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1385 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 1386 | if (ch == '\0' || ch != CharAt(i)) { |
| 1387 | return false; |
| 1388 | } |
| 1389 | } |
| 1390 | return *modified_utf8 == '\0'; |
| 1391 | } |
| 1392 | |
| 1393 | bool String::Equals(const StringPiece& modified_utf8) const { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1394 | if (modified_utf8.size() != GetLength()) { |
| 1395 | return false; |
| 1396 | } |
| 1397 | const char* p = modified_utf8.data(); |
| 1398 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1399 | uint16_t ch = GetUtf16FromUtf8(&p); |
| 1400 | if (ch != CharAt(i)) { |
| 1401 | return false; |
| 1402 | } |
| 1403 | } |
| 1404 | return true; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 1408 | std::string String::ToModifiedUtf8() const { |
| 1409 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
| 1410 | size_t byte_count(CountUtf8Bytes(chars, GetLength())); |
| 1411 | std::string result(byte_count, char(0)); |
| 1412 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 1413 | return result; |
| 1414 | } |
| 1415 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1416 | bool Throwable::IsCheckedException() const { |
| 1417 | Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;"); |
| 1418 | if (InstanceOf(error)) { |
| 1419 | return false; |
| 1420 | } |
| 1421 | Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;"); |
| 1422 | return !InstanceOf(jlre); |
| 1423 | } |
| 1424 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1425 | Class* StackTraceElement::java_lang_StackTraceElement_ = NULL; |
| 1426 | |
| 1427 | void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) { |
| 1428 | CHECK(java_lang_StackTraceElement_ == NULL); |
| 1429 | CHECK(java_lang_StackTraceElement != NULL); |
| 1430 | java_lang_StackTraceElement_ = java_lang_StackTraceElement; |
| 1431 | } |
| 1432 | |
| 1433 | void StackTraceElement::ResetClass() { |
| 1434 | CHECK(java_lang_StackTraceElement_ != NULL); |
| 1435 | java_lang_StackTraceElement_ = NULL; |
| 1436 | } |
| 1437 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1438 | StackTraceElement* StackTraceElement::Alloc(const String* declaring_class, |
| 1439 | const String* method_name, |
| 1440 | const String* file_name, |
| 1441 | int32_t line_number) { |
| 1442 | StackTraceElement* trace = |
| 1443 | down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject()); |
| 1444 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), |
| 1445 | const_cast<String*>(declaring_class), false); |
| 1446 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), |
| 1447 | const_cast<String*>(method_name), false); |
| 1448 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), |
| 1449 | const_cast<String*>(file_name), false); |
| 1450 | trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), |
| 1451 | line_number, false); |
| 1452 | return trace; |
| 1453 | } |
| 1454 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1455 | static const char* kClassStatusNames[] = { |
| 1456 | "Error", |
| 1457 | "NotReady", |
| 1458 | "Idx", |
| 1459 | "Loaded", |
| 1460 | "Resolved", |
| 1461 | "Verifying", |
| 1462 | "Verified", |
| 1463 | "Initializing", |
| 1464 | "Initialized" |
| 1465 | }; |
| 1466 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) { |
| 1467 | if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 1468 | os << kClassStatusNames[rhs + 1]; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1469 | } else { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1470 | os << "Class::Status[" << static_cast<int>(rhs) << "]"; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1471 | } |
| 1472 | return os; |
| 1473 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1474 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1475 | } // namespace art |