blob: 7ead4ab7a31e17e378e6b0cf7536edfcfe5348a0 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070034#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070037#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070038#include "utils.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070039
40namespace art {
41
Elliott Hughesdbb40792011-11-18 17:05:22 -080042String* Object::AsString() {
43 DCHECK(GetClass()->IsStringClass());
44 return down_cast<String*>(this);
45}
46
Elliott Hughes081be7f2011-09-18 16:50:26 -070047Object* Object::Clone() {
48 Class* c = GetClass();
49 DCHECK(!c->IsClassClass());
50
51 // Object::SizeOf gets the right size even if we're an array.
52 // Using c->AllocObject() here would be wrong.
53 size_t num_bytes = SizeOf();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070054 SirtRef<Object> copy(Heap::AllocObject(c, num_bytes));
55 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070056 return NULL;
57 }
58
59 // Copy instance data. We assume memcpy copies by words.
60 // TODO: expose and use move32.
61 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070062 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070063 size_t offset = sizeof(Object);
64 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
65
Elliott Hughes20cde902011-10-04 17:37:27 -070066 if (c->IsFinalizable()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070067 Heap::AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070068 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070069
Brian Carlstrom40381fb2011-10-19 14:13:40 -070070 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070071}
72
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070073uint32_t Object::GetThinLockId() {
74 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070075}
76
77void Object::MonitorEnter(Thread* thread) {
78 Monitor::MonitorEnter(thread, this);
79}
80
Ian Rogersff1ed472011-09-20 13:46:24 -070081bool Object::MonitorExit(Thread* thread) {
82 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070083}
84
85void Object::Notify() {
86 Monitor::Notify(Thread::Current(), this);
87}
88
89void Object::NotifyAll() {
90 Monitor::NotifyAll(Thread::Current(), this);
91}
92
93void Object::Wait(int64_t ms, int32_t ns) {
94 Monitor::Wait(Thread::Current(), this, ms, ns, true);
95}
96
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070097// TODO: get global references for these
98Class* Field::java_lang_reflect_Field_ = NULL;
99
100void Field::SetClass(Class* java_lang_reflect_Field) {
101 CHECK(java_lang_reflect_Field_ == NULL);
102 CHECK(java_lang_reflect_Field != NULL);
103 java_lang_reflect_Field_ = java_lang_reflect_Field;
104}
105
106void Field::ResetClass() {
107 CHECK(java_lang_reflect_Field_ != NULL);
108 java_lang_reflect_Field_ = NULL;
109}
110
Ian Rogers0571d352011-11-03 19:51:38 -0700111void Field::SetOffset(MemberOffset num_bytes) {
112 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800113#if 0 // TODO enable later in boot and under !NDEBUG
114 FieldHelper fh(this);
115 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700116 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
117 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
118 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800119#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700120 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
121}
122
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700123uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700124 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700125 if (IsStatic()) {
126 object = declaring_class_;
127 }
128 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700129}
130
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700131void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700132 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700133 if (IsStatic()) {
134 object = declaring_class_;
135 }
136 object->SetField32(GetOffset(), new_value, IsVolatile());
137}
138
139uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700140 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700141 if (IsStatic()) {
142 object = declaring_class_;
143 }
144 return object->GetField64(GetOffset(), IsVolatile());
145}
146
147void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700148 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700149 if (IsStatic()) {
150 object = declaring_class_;
151 }
152 object->SetField64(GetOffset(), new_value, IsVolatile());
153}
154
155Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 if (IsStatic()) {
158 object = declaring_class_;
159 }
160 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
161}
162
163void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700164 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700165 if (IsStatic()) {
166 object = declaring_class_;
167 }
168 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
169}
170
171bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
173 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700174 return Get32(object);
175}
176
177void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800178 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
179 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700180 Set32(object, z);
181}
182
183int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800184 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
185 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186 return Get32(object);
187}
188
189void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800190 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
191 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 Set32(object, b);
193}
194
195uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800196 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
197 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 return Get32(object);
199}
200
201void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800202 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
203 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 Set32(object, c);
205}
206
Ian Rogers466bb252011-10-14 03:29:56 -0700207int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800208 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
209 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 return Get32(object);
211}
212
Ian Rogers466bb252011-10-14 03:29:56 -0700213void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800214 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
215 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700216 Set32(object, s);
217}
218
219int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800220 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
221 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700222 return Get32(object);
223}
224
225void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800226 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
227 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228 Set32(object, i);
229}
230
231int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800232 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
233 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234 return Get64(object);
235}
236
237void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800238 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
239 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240 Set64(object, j);
241}
242
243float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
245 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700246 JValue float_bits;
247 float_bits.i = Get32(object);
248 return float_bits.f;
249}
250
251void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800252 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
253 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700254 JValue float_bits;
255 float_bits.f = f;
256 Set32(object, float_bits.i);
257}
258
259double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
261 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 JValue double_bits;
263 double_bits.j = Get64(object);
264 return double_bits.d;
265}
266
267void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
269 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700270 JValue double_bits;
271 double_bits.d = d;
272 Set64(object, double_bits.j);
273}
274
275Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
277 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700278 return GetObj(object);
279}
280
281void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800282 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
283 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700284 SetObj(object, l);
285}
286
287// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700288Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289Class* Method::java_lang_reflect_Method_ = NULL;
290
Elliott Hughes80609252011-09-23 17:24:51 -0700291void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
292 CHECK(java_lang_reflect_Constructor_ == NULL);
293 CHECK(java_lang_reflect_Constructor != NULL);
294 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
295
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296 CHECK(java_lang_reflect_Method_ == NULL);
297 CHECK(java_lang_reflect_Method != NULL);
298 java_lang_reflect_Method_ = java_lang_reflect_Method;
299}
300
Elliott Hughes80609252011-09-23 17:24:51 -0700301void Method::ResetClasses() {
302 CHECK(java_lang_reflect_Constructor_ != NULL);
303 java_lang_reflect_Constructor_ = NULL;
304
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305 CHECK(java_lang_reflect_Method_ != NULL);
306 java_lang_reflect_Method_ = NULL;
307}
308
Elliott Hughes418d20f2011-09-22 14:00:39 -0700309Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
310 if (*p == '[') {
311 // Something like "[[[Ljava/lang/String;".
312 const char* start = p;
313 while (*p == '[') {
314 ++p;
315 }
316 if (*p == 'L') {
317 while (*p != ';') {
318 ++p;
319 }
320 }
321 ++p; // Either the ';' or the primitive type.
322
Brian Carlstromaded5f72011-10-07 17:15:04 -0700323 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800324 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700325 } else if (*p == 'L') {
326 const char* start = p;
327 while (*p != ';') {
328 ++p;
329 }
330 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800331 std::string descriptor(start, (p - start));
332 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700333 } else {
334 return class_linker->FindPrimitiveClass(*p++);
335 }
336}
337
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338ObjectArray<String>* Method::GetDexCacheStrings() const {
339 return GetFieldObject<ObjectArray<String>*>(
340 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
341}
342
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
344 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
345 new_dex_cache_strings, false);
346}
347
348ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
349 return GetFieldObject<ObjectArray<Class>*>(
350 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
351}
352
353void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
354 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
355 new_dex_cache_classes, false);
356}
357
358ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
359 return GetFieldObject<ObjectArray<Method>*>(
360 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
361}
362
363void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
364 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
365 new_dex_cache_methods, false);
366}
367
368ObjectArray<Field>* Method::GetDexCacheResolvedFields() const {
369 return GetFieldObject<ObjectArray<Field>*>(
370 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_), false);
371}
372
373void Method::SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields) {
374 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_),
375 new_dex_cache_fields, false);
376}
377
378CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
379 return GetFieldPtr<CodeAndDirectMethods*>(
380 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
381 false);
382}
383
384void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
385 SetFieldPtr<CodeAndDirectMethods*>(
386 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
387 new_value, false);
388}
389
390ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
391 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
392 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
393 false);
394}
395
396void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700397 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399}
400
401size_t Method::NumArgRegisters(const StringPiece& shorty) {
402 CHECK_LE(1, shorty.length());
403 uint32_t num_registers = 0;
404 for (int i = 1; i < shorty.length(); ++i) {
405 char ch = shorty[i];
406 if (ch == 'D' || ch == 'J') {
407 num_registers += 2;
408 } else {
409 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700410 }
411 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700412 return num_registers;
413}
414
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800415bool Method::IsProxyMethod() const {
416 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417}
418
Ian Rogers466bb252011-10-14 03:29:56 -0700419Method* Method::FindOverriddenMethod() const {
420 if (IsStatic()) {
421 return NULL;
422 }
423 Class* declaring_class = GetDeclaringClass();
424 Class* super_class = declaring_class->GetSuperClass();
425 uint16_t method_index = GetMethodIndex();
426 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
427 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428 // Did this method override a super class method? If so load the result from the super class'
429 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700430 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
431 result = super_class_vtable->Get(method_index);
432 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800433 // Method didn't override superclass method so search interfaces
434 MethodHelper mh(this);
435 MethodHelper interface_mh;
436 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
437 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
438 InterfaceEntry* entry = iftable->Get(i);
439 Class* interface = entry->GetInterface();
440 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
441 Method* interface_method = interface->GetVirtualMethod(j);
442 interface_mh.ChangeMethod(interface_method);
443 if (mh.HasSameNameAndSignature(&interface_mh)) {
444 result = interface_method;
445 break;
446 }
447 }
Ian Rogers466bb252011-10-14 03:29:56 -0700448 }
449 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450#ifndef NDEBUG
451 MethodHelper result_mh(result);
452 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
453#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700454 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455}
456
Ian Rogersbdb03912011-09-14 00:55:44 -0700457uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700458 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700459 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800460 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700461 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700462 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700463 size_t mapping_table_length = GetMappingTableLength();
jeffhao26c0a1a2012-01-17 16:28:33 -0800464 const void* code_offset;
465 if (Runtime::Current()->IsMethodTracingActive() &&
466 Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this) != NULL) {
467 code_offset = Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this);
468 } else {
469 code_offset = GetCode();
470 }
jeffhaoe343b762011-12-05 16:36:44 -0800471 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code_offset);
Ian Rogersbdb03912011-09-14 00:55:44 -0700472 uint32_t best_offset = 0;
473 uint32_t best_dex_offset = 0;
474 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475 uint32_t map_offset = mapping_table[i];
476 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700477 if (map_offset == sought_offset) {
478 best_offset = map_offset;
479 best_dex_offset = map_dex_offset;
480 break;
481 }
482 if (map_offset < sought_offset && map_offset > best_offset) {
483 best_offset = map_offset;
484 best_dex_offset = map_dex_offset;
485 }
486 }
487 return best_dex_offset;
488}
489
490uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700491 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700492 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700493 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700494 return 0; // Special no mapping/pc == 0 case
495 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700496 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700497 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700498 uint32_t map_offset = mapping_table[i];
499 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700500 if (map_dex_offset == dex_pc) {
jeffhao2692b572011-12-16 15:42:28 -0800501 if (Runtime::Current()->IsMethodTracingActive()) {
502 Trace* tracer = Runtime::Current()->GetTracer();
503 return reinterpret_cast<uintptr_t>(tracer->GetSavedCodeFromMap(this)) + map_offset;
jeffhaoe343b762011-12-05 16:36:44 -0800504 } else {
505 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
506 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700507 }
508 }
509 LOG(FATAL) << "Looking up Dex PC not contained in method";
510 return 0;
511}
512
513uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800514 MethodHelper mh(this);
515 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700516 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700517 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
518 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700519 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700520 if (iter_type_idx == DexFile::kDexNoIndex16) {
521 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700522 }
523 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800524 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700525 if (iter_exception_type == NULL) {
526 // The verifier should take care of resolving all exception classes early
527 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700529 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700530 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700531 }
532 }
533 // Handler not found
534 return DexFile::kDexNoIndex;
535}
536
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700537void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
538 // Push a transition back into managed code onto the linked list in thread.
539 CHECK_EQ(Thread::kRunnable, self->GetState());
540 NativeToManagedRecord record;
541 self->PushNativeToManagedRecord(&record);
542
543 // Call the invoke stub associated with the method.
544 // Pass everything as arguments.
545 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700546
547 bool have_executable_code = (GetCode() != NULL);
548#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700549 // Currently we can only compile non-native methods for ARM.
550 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700551#endif
552
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500553 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700554 bool log = false;
555 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800556 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
557 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700558 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700559 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700560 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800561 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
562 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700563 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700564 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800565 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
566 PrettyMethod(this).c_str(), GetCode(), stub,
567 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700568 if (result != NULL) {
569 result->j = 0;
570 }
571 }
572
573 // Pop transition.
574 self->PopNativeToManagedRecord(record);
575}
576
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700577bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700578 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800579 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700580 return native_method != jni_stub;
581}
582
583void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700584 CHECK(IsNative()) << PrettyMethod(this);
585 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700586 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
587 native_method, false);
588}
589
590void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700591 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700592 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800593 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700594}
595
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700596void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700597 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
598 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700599 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700600 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700601}
602
603DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700604 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700605}
606
607void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700608 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700609}
610
Brian Carlstrom1f870082011-08-23 16:02:11 -0700611Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700612 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700613 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500614 // TODO: decide whether we want this check. It currently fails during bootstrap.
615 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700616 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700617 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700618}
619
Ian Rogers0571d352011-11-03 19:51:38 -0700620void Class::SetClassSize(size_t new_class_size) {
621 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
622 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
623}
624
Ian Rogersd418eda2012-01-30 12:14:28 -0800625// Return the class' name. The exact format is bizarre, but it's the specified behavior for
626// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
627// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
628// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
629String* Class::ComputeName() {
630 String* name = GetName();
631 if (name != NULL) {
632 return name;
633 }
634 std::string descriptor(ClassHelper(this).GetDescriptor());
635 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
636 // The descriptor indicates that this is the class for
637 // a primitive type; special-case the return value.
638 const char* c_name = NULL;
639 switch (descriptor[0]) {
640 case 'Z': c_name = "boolean"; break;
641 case 'B': c_name = "byte"; break;
642 case 'C': c_name = "char"; break;
643 case 'S': c_name = "short"; break;
644 case 'I': c_name = "int"; break;
645 case 'J': c_name = "long"; break;
646 case 'F': c_name = "float"; break;
647 case 'D': c_name = "double"; break;
648 case 'V': c_name = "void"; break;
649 default:
650 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
651 }
652 name = String::AllocFromModifiedUtf8(c_name);
653 } else {
654 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
655 // components.
656 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
657 descriptor.erase(0, 1);
658 descriptor.erase(descriptor.size() - 1);
659 }
660 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
661 name = String::AllocFromModifiedUtf8(descriptor.c_str());
662 }
663 SetName(name);
664 return name;
665}
666
Elliott Hughes4681c802011-09-25 18:04:37 -0700667void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700668 if ((flags & kDumpClassFullDetail) == 0) {
669 os << PrettyClass(this);
670 if ((flags & kDumpClassClassLoader) != 0) {
671 os << ' ' << GetClassLoader();
672 }
673 if ((flags & kDumpClassInitialized) != 0) {
674 os << ' ' << GetStatus();
675 }
Elliott Hughese0918552011-10-28 17:18:29 -0700676 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700677 return;
678 }
679
680 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800681 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700682 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800683 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700684 os << " objectSize=" << SizeOf() << " "
685 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
686 os << StringPrintf(" access=0x%04x.%04x\n",
687 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
688 if (super != NULL) {
689 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
690 }
691 if (IsArrayClass()) {
692 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
693 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800694 if (kh.NumInterfaces() > 0) {
695 os << " interfaces (" << kh.NumInterfaces() << "):\n";
696 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
697 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700698 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800699 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700700 }
701 }
702 os << " vtable (" << NumVirtualMethods() << " entries, "
703 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
704 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800705 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700706 }
707 os << " direct methods (" << NumDirectMethods() << " entries):\n";
708 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800709 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700710 }
711 if (NumStaticFields() > 0) {
712 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700713 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700714 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800715 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700716 }
717 } else {
718 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700719 }
720 }
721 if (NumInstanceFields() > 0) {
722 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700723 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700724 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800725 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700726 }
727 } else {
728 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700729 }
730 }
731}
732
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700733void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
734 if (new_reference_offsets != CLASS_WALK_SUPER) {
735 // Sanity check that the number of bits set in the reference offset bitmap
736 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800737 size_t count = 0;
738 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
739 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700740 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800741 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700742 }
743 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
744 new_reference_offsets, false);
745}
746
747void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
748 if (new_reference_offsets != CLASS_WALK_SUPER) {
749 // Sanity check that the number of bits set in the reference offset bitmap
750 // agrees with the number of references
751 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
752 NumReferenceStaticFieldsDuringLinking());
753 }
754 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
755 new_reference_offsets, false);
756}
757
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700758bool Class::Implements(const Class* klass) const {
759 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700760 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700761 // All interfaces implemented directly and by our superclass, and
762 // recursively all super-interfaces of those interfaces, are listed
763 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700764 int32_t iftable_count = GetIfTableCount();
765 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
766 for (int32_t i = 0; i < iftable_count; i++) {
767 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700768 return true;
769 }
770 }
771 return false;
772}
773
774// Determine whether "this" is assignable from "klazz", where both of these
775// are array classes.
776//
777// Consider an array class, e.g. Y[][], where Y is a subclass of X.
778// Y[][] = Y[][] --> true (identity)
779// X[][] = Y[][] --> true (element superclass)
780// Y = Y[][] --> false
781// Y[] = Y[][] --> false
782// Object = Y[][] --> true (everything is an object)
783// Object[] = Y[][] --> true
784// Object[][] = Y[][] --> true
785// Object[][][] = Y[][] --> false (too many []s)
786// Serializable = Y[][] --> true (all arrays are Serializable)
787// Serializable[] = Y[][] --> true
788// Serializable[][] = Y[][] --> false (unless Y is Serializable)
789//
790// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700791// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700792//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700793bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700794 DCHECK(IsArrayClass()) << PrettyClass(this);
795 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700796 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700797}
798
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700799bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700800 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
801 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700802 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700803 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700804 // src's super should be java_lang_Object, since it is an array.
805 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700806 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
807 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700808 return this == java_lang_Object;
809 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700810 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700811}
812
813bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700814 DCHECK(!IsInterface()) << PrettyClass(this);
815 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700816 const Class* current = this;
817 do {
818 if (current == klass) {
819 return true;
820 }
821 current = current->GetSuperClass();
822 } while (current != NULL);
823 return false;
824}
825
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800826bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700827 size_t i = 0;
828 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
829 ++i;
830 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700831 if (descriptor1.find('/', i) != StringPiece::npos ||
832 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700833 return false;
834 } else {
835 return true;
836 }
837}
838
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700839#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700840bool Class::IsInSamePackage(const StringPiece& descriptor1,
841 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700842 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700843 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700844 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
845 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700846 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
847}
848#endif
849
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700850bool Class::IsInSamePackage(const Class* that) const {
851 const Class* klass1 = this;
852 const Class* klass2 = that;
853 if (klass1 == klass2) {
854 return true;
855 }
856 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700857 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700858 return false;
859 }
860 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700861 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700862 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700863 }
jeffhao4a801a42011-09-23 13:53:40 -0700864 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700865 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700866 }
867 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800868 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800869 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800870 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800871 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800872 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700873}
874
Elliott Hughesdbb40792011-11-18 17:05:22 -0800875bool Class::IsClassClass() const {
876 Class* java_lang_Class = GetClass()->GetClass();
877 return this == java_lang_Class;
878}
879
880bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800881 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800882}
883
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800884bool Class::IsThrowableClass() const {
885 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
886 return throwable->IsAssignableFrom(this);
887}
888
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800889ClassLoader* Class::GetClassLoader() const {
890 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700891}
892
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700893void Class::SetClassLoader(const ClassLoader* new_cl) {
894 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700896}
897
Ian Rogersb04f69f2011-10-17 00:40:54 -0700898Method* Class::FindVirtualMethodForInterface(Method* method, bool can_throw) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700899 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700900 DCHECK(declaring_class != NULL) << PrettyClass(this);
901 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700902 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700903 int32_t iftable_count = GetIfTableCount();
904 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
905 for (int32_t i = 0; i < iftable_count; i++) {
906 InterfaceEntry* interface_entry = iftable->Get(i);
907 if (interface_entry->GetInterface() == declaring_class) {
908 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700909 }
910 }
Ian Rogersb04f69f2011-10-17 00:40:54 -0700911 if (can_throw) {
912 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;",
913 "Class %s does not implement interface %s",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800914 PrettyDescriptor(this).c_str(), PrettyDescriptor(declaring_class).c_str());
Ian Rogersb04f69f2011-10-17 00:40:54 -0700915 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700916 return NULL;
917}
918
Ian Rogers466bb252011-10-14 03:29:56 -0700919Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700920 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800921 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700922 if (method != NULL) {
923 return method;
924 }
925
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700926 int32_t iftable_count = GetIfTableCount();
927 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
928 for (int32_t i = 0; i < iftable_count; i++) {
929 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700930 if (method != NULL) {
931 return method;
932 }
933 }
934 return NULL;
935}
936
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700937Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700938 const StringPiece& signature) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800939 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700940 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700941 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800942 mh.ChangeMethod(method);
943 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700944 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700945 }
946 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700947 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700948}
949
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700950Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700951 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700952 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700953 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700954 if (method != NULL) {
955 return method;
956 }
957 }
958 return NULL;
959}
960
961Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -0700962 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800963 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700964 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700965 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800966 mh.ChangeMethod(method);
967 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -0700968 return method;
Ian Rogers466bb252011-10-14 03:29:56 -0700969 }
970 }
971 return NULL;
972}
973
974Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
975 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
976 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
977 if (method != NULL) {
978 return method;
979 }
980 }
981 return NULL;
982}
983
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700984Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700985 // Is the field in this class?
986 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800987 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 for (size_t i = 0; i < NumInstanceFields(); ++i) {
989 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800990 fh.ChangeField(f);
991 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700992 return f;
993 }
994 }
995 return NULL;
996}
997
998Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700999 // Is the field in this class, or any of its superclasses?
1000 // Interfaces are not relevant because they can't contain instance fields.
1001 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001002 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001003 if (f != NULL) {
1004 return f;
1005 }
1006 }
1007 return NULL;
1008}
1009
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001010Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1011 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001012 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001013 for (size_t i = 0; i < NumStaticFields(); ++i) {
1014 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001015 fh.ChangeField(f);
1016 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001017 return f;
1018 }
1019 }
1020 return NULL;
1021}
1022
1023Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1024 // Is the field in this class (or its interfaces), or any of its
1025 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001026 ClassHelper kh;
1027 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001028 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001029 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001030 if (f != NULL) {
1031 return f;
1032 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001033 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001034 kh.ChangeClass(k);
1035 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1036 Class* interface = kh.GetInterface(i);
1037 f = interface->FindDeclaredStaticField(name, type);
1038 if (f != NULL) {
1039 return f;
1040 }
1041 }
1042 }
1043 return NULL;
1044}
1045
1046Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1047 // Find a field using the JLS field resolution order
1048 ClassHelper kh;
1049 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1050 // Is the field in this class?
1051 Field* f = k->FindDeclaredInstanceField(name, type);
1052 if (f != NULL) {
1053 return f;
1054 }
1055 f = k->FindDeclaredStaticField(name, type);
1056 if (f != NULL) {
1057 return f;
1058 }
1059 // Is this field in any of this class' interfaces?
1060 kh.ChangeClass(k);
1061 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1062 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001063 f = interface->FindDeclaredStaticField(name, type);
1064 if (f != NULL) {
1065 return f;
1066 }
1067 }
1068 }
1069 return NULL;
1070}
1071
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001072Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001073 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001074 DCHECK_GE(component_count, 0);
1075 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001076
1077 size_t header_size = sizeof(Array);
1078 size_t data_size = component_count * component_size;
1079 size_t size = header_size + data_size;
1080
1081 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1082 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1083 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1084 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001085 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001086 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001087 return NULL;
1088 }
1089
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001090 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1091 if (array != NULL) {
1092 DCHECK(array->IsArrayInstance());
1093 array->SetLength(component_count);
1094 }
1095 return array;
1096}
1097
1098Array* Array::Alloc(Class* array_class, int32_t component_count) {
1099 return Alloc(array_class, component_count, array_class->GetComponentSize());
1100}
1101
Elliott Hughes80609252011-09-23 17:24:51 -07001102bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001103 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001104 "length=%i; index=%i", length_, index);
1105 return false;
1106}
1107
1108bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001109 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001110 "Can't store an element of type %s into an array of type %s",
1111 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1112 return false;
1113}
1114
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001115template<typename T>
1116PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001117 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001118 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1119 return down_cast<PrimitiveArray<T>*>(raw_array);
1120}
1121
1122template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1123
1124// Explicitly instantiate all the primitive array types.
1125template class PrimitiveArray<uint8_t>; // BooleanArray
1126template class PrimitiveArray<int8_t>; // ByteArray
1127template class PrimitiveArray<uint16_t>; // CharArray
1128template class PrimitiveArray<double>; // DoubleArray
1129template class PrimitiveArray<float>; // FloatArray
1130template class PrimitiveArray<int32_t>; // IntArray
1131template class PrimitiveArray<int64_t>; // LongArray
1132template class PrimitiveArray<int16_t>; // ShortArray
1133
Ian Rogers466bb252011-10-14 03:29:56 -07001134// Explicitly instantiate Class[][]
1135template class ObjectArray<ObjectArray<Class> >;
1136
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001137// TODO: get global references for these
1138Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001139
Brian Carlstroma663ea52011-08-19 23:33:41 -07001140void String::SetClass(Class* java_lang_String) {
1141 CHECK(java_lang_String_ == NULL);
1142 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001143 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001144}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001145
Brian Carlstroma663ea52011-08-19 23:33:41 -07001146void String::ResetClass() {
1147 CHECK(java_lang_String_ != NULL);
1148 java_lang_String_ = NULL;
1149}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001150
Brian Carlstromc74255f2011-09-11 22:47:39 -07001151String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001152 return Runtime::Current()->GetInternTable()->InternWeak(this);
1153}
1154
Brian Carlstrom395520e2011-09-25 19:35:00 -07001155int32_t String::GetHashCode() {
1156 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1157 if (result == 0) {
1158 ComputeHashCode();
1159 }
1160 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1161 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1162 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001163 return result;
1164}
1165
1166int32_t String::GetLength() const {
1167 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1168 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1169 return result;
1170}
1171
1172uint16_t String::CharAt(int32_t index) const {
1173 // TODO: do we need this? Equals is the only caller, and could
1174 // bounds check itself.
1175 if (index < 0 || index >= count_) {
1176 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001177 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001178 "length=%i; index=%i", count_, index);
1179 return 0;
1180 }
1181 return GetCharArray()->Get(index + GetOffset());
1182}
1183
1184String* String::AllocFromUtf16(int32_t utf16_length,
1185 const uint16_t* utf16_data_in,
1186 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001187 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001188 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001189 if (string == NULL) {
1190 return NULL;
1191 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001192 // TODO: use 16-bit wide memset variant
1193 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001194 if (array == NULL) {
1195 return NULL;
1196 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001197 for (int i = 0; i < utf16_length; i++) {
1198 array->Set(i, utf16_data_in[i]);
1199 }
1200 if (hash_code != 0) {
1201 string->SetHashCode(hash_code);
1202 } else {
1203 string->ComputeHashCode();
1204 }
1205 return string;
1206}
1207
1208String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001209 if (utf == NULL) {
1210 return NULL;
1211 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001212 size_t char_count = CountModifiedUtf8Chars(utf);
1213 return AllocFromModifiedUtf8(char_count, utf);
1214}
1215
1216String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1217 const char* utf8_data_in) {
1218 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001219 if (string == NULL) {
1220 return NULL;
1221 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001222 uint16_t* utf16_data_out =
1223 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1224 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1225 string->ComputeHashCode();
1226 return string;
1227}
1228
1229String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001230 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1231 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001232 return NULL;
1233 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001234 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001235}
1236
1237String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001238 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001239 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001240 if (string == NULL) {
1241 return NULL;
1242 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001243 string->SetArray(array);
1244 string->SetCount(array->GetLength());
1245 return string;
1246}
1247
1248bool String::Equals(const String* that) const {
1249 if (this == that) {
1250 // Quick reference equality test
1251 return true;
1252 } else if (that == NULL) {
1253 // Null isn't an instanceof anything
1254 return false;
1255 } else if (this->GetLength() != that->GetLength()) {
1256 // Quick length inequality test
1257 return false;
1258 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001259 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001260 // hash code was already equal
1261 for (int32_t i = 0; i < that->GetLength(); ++i) {
1262 if (this->CharAt(i) != that->CharAt(i)) {
1263 return false;
1264 }
1265 }
1266 return true;
1267 }
1268}
1269
Elliott Hughes5d78d392011-12-13 16:53:05 -08001270bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001271 if (this->GetLength() != that_length) {
1272 return false;
1273 } else {
1274 for (int32_t i = 0; i < that_length; ++i) {
1275 if (this->CharAt(i) != that_chars[that_offset + i]) {
1276 return false;
1277 }
1278 }
1279 return true;
1280 }
1281}
1282
1283bool String::Equals(const char* modified_utf8) const {
1284 for (int32_t i = 0; i < GetLength(); ++i) {
1285 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1286 if (ch == '\0' || ch != CharAt(i)) {
1287 return false;
1288 }
1289 }
1290 return *modified_utf8 == '\0';
1291}
1292
1293bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001294 if (modified_utf8.size() != GetLength()) {
1295 return false;
1296 }
1297 const char* p = modified_utf8.data();
1298 for (int32_t i = 0; i < GetLength(); ++i) {
1299 uint16_t ch = GetUtf16FromUtf8(&p);
1300 if (ch != CharAt(i)) {
1301 return false;
1302 }
1303 }
1304 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001305}
1306
1307// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1308std::string String::ToModifiedUtf8() const {
1309 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1310 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1311 std::string result(byte_count, char(0));
1312 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1313 return result;
1314}
1315
Ian Rogers466bb252011-10-14 03:29:56 -07001316bool Throwable::IsCheckedException() const {
1317 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1318 if (InstanceOf(error)) {
1319 return false;
1320 }
1321 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1322 return !InstanceOf(jlre);
1323}
1324
Ian Rogers9074b992011-10-26 17:41:55 -07001325std::string Throwable::Dump() const {
1326 Object* stack_state = GetStackState();
1327 if (stack_state == NULL || !stack_state->IsObjectArray()) {
1328 // missing or corrupt stack state
1329 return "";
1330 }
1331 // Decode the internal stack trace into the depth and method trace
1332 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1333 int32_t depth = method_trace->GetLength() - 1;
1334 std::string result;
1335 for (int32_t i = 0; i < depth; ++i) {
1336 Method* method = down_cast<Method*>(method_trace->Get(i));
1337 result += " at ";
1338 result += PrettyMethod(method, true);
1339 result += "\n";
1340 }
1341 return result;
1342}
1343
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001344Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1345
1346void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1347 CHECK(java_lang_StackTraceElement_ == NULL);
1348 CHECK(java_lang_StackTraceElement != NULL);
1349 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1350}
1351
1352void StackTraceElement::ResetClass() {
1353 CHECK(java_lang_StackTraceElement_ != NULL);
1354 java_lang_StackTraceElement_ = NULL;
1355}
1356
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001357StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1358 String* method_name,
1359 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001360 int32_t line_number) {
1361 StackTraceElement* trace =
1362 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1363 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1364 const_cast<String*>(declaring_class), false);
1365 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1366 const_cast<String*>(method_name), false);
1367 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1368 const_cast<String*>(file_name), false);
1369 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1370 line_number, false);
1371 return trace;
1372}
1373
Elliott Hughes1f359b02011-07-17 14:27:17 -07001374static const char* kClassStatusNames[] = {
1375 "Error",
1376 "NotReady",
1377 "Idx",
1378 "Loaded",
1379 "Resolved",
1380 "Verifying",
1381 "Verified",
1382 "Initializing",
1383 "Initialized"
1384};
1385std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1386 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001387 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001388 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001389 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001390 }
1391 return os;
1392}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001393
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001394} // namespace art