blob: 22ee74297e38041fb410bf019d862fbeb07d939b [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
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
359 return GetFieldPtr<CodeAndDirectMethods*>(
360 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
361 false);
362}
363
364void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
365 SetFieldPtr<CodeAndDirectMethods*>(
366 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
367 new_value, false);
368}
369
370ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
371 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
372 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
373 false);
374}
375
376void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700377 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700378 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379}
380
381size_t Method::NumArgRegisters(const StringPiece& shorty) {
382 CHECK_LE(1, shorty.length());
383 uint32_t num_registers = 0;
384 for (int i = 1; i < shorty.length(); ++i) {
385 char ch = shorty[i];
386 if (ch == 'D' || ch == 'J') {
387 num_registers += 2;
388 } else {
389 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700390 }
391 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 return num_registers;
393}
394
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800395bool Method::IsProxyMethod() const {
396 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700397}
398
Ian Rogers466bb252011-10-14 03:29:56 -0700399Method* Method::FindOverriddenMethod() const {
400 if (IsStatic()) {
401 return NULL;
402 }
403 Class* declaring_class = GetDeclaringClass();
404 Class* super_class = declaring_class->GetSuperClass();
405 uint16_t method_index = GetMethodIndex();
406 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
407 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 // Did this method override a super class method? If so load the result from the super class'
409 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700410 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
411 result = super_class_vtable->Get(method_index);
412 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800413 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800414 if (IsProxyMethod()) {
415 result = Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this);
416 } else {
417 MethodHelper mh(this);
418 MethodHelper interface_mh;
419 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
420 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
421 InterfaceEntry* entry = iftable->Get(i);
422 Class* interface = entry->GetInterface();
423 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
424 Method* interface_method = interface->GetVirtualMethod(j);
425 interface_mh.ChangeMethod(interface_method);
426 if (mh.HasSameNameAndSignature(&interface_mh)) {
427 result = interface_method;
428 break;
429 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800430 }
431 }
Ian Rogers466bb252011-10-14 03:29:56 -0700432 }
433 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434#ifndef NDEBUG
435 MethodHelper result_mh(result);
436 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
437#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700438 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439}
440
Ian Rogersbdb03912011-09-14 00:55:44 -0700441uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700442 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700443 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800444 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700445 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700446 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700447 size_t mapping_table_length = GetMappingTableLength();
jeffhao26c0a1a2012-01-17 16:28:33 -0800448 const void* code_offset;
449 if (Runtime::Current()->IsMethodTracingActive() &&
450 Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this) != NULL) {
451 code_offset = Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this);
452 } else {
453 code_offset = GetCode();
454 }
jeffhaoe343b762011-12-05 16:36:44 -0800455 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code_offset);
Ian Rogersbdb03912011-09-14 00:55:44 -0700456 uint32_t best_offset = 0;
457 uint32_t best_dex_offset = 0;
458 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700459 uint32_t map_offset = mapping_table[i];
460 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700461 if (map_offset == sought_offset) {
462 best_offset = map_offset;
463 best_dex_offset = map_dex_offset;
464 break;
465 }
466 if (map_offset < sought_offset && map_offset > best_offset) {
467 best_offset = map_offset;
468 best_dex_offset = map_dex_offset;
469 }
470 }
471 return best_dex_offset;
472}
473
474uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700476 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700477 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700478 return 0; // Special no mapping/pc == 0 case
479 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700481 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 uint32_t map_offset = mapping_table[i];
483 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700484 if (map_dex_offset == dex_pc) {
jeffhao2692b572011-12-16 15:42:28 -0800485 if (Runtime::Current()->IsMethodTracingActive()) {
486 Trace* tracer = Runtime::Current()->GetTracer();
487 return reinterpret_cast<uintptr_t>(tracer->GetSavedCodeFromMap(this)) + map_offset;
jeffhaoe343b762011-12-05 16:36:44 -0800488 } else {
489 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
490 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700491 }
492 }
493 LOG(FATAL) << "Looking up Dex PC not contained in method";
494 return 0;
495}
496
497uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800498 MethodHelper mh(this);
499 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700500 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700501 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
502 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700503 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700504 if (iter_type_idx == DexFile::kDexNoIndex16) {
505 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700506 }
507 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700509 if (iter_exception_type == NULL) {
510 // The verifier should take care of resolving all exception classes early
511 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800512 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700513 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700514 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700515 }
516 }
517 // Handler not found
518 return DexFile::kDexNoIndex;
519}
520
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700521void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
522 // Push a transition back into managed code onto the linked list in thread.
523 CHECK_EQ(Thread::kRunnable, self->GetState());
524 NativeToManagedRecord record;
525 self->PushNativeToManagedRecord(&record);
526
527 // Call the invoke stub associated with the method.
528 // Pass everything as arguments.
529 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700530
531 bool have_executable_code = (GetCode() != NULL);
532#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700533 // Currently we can only compile non-native methods for ARM.
534 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700535#endif
536
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500537 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700538 bool log = false;
539 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800540 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
541 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700542 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700543 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700544 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800545 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
546 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700547 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700548 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800549 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
550 PrettyMethod(this).c_str(), GetCode(), stub,
551 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700552 if (result != NULL) {
553 result->j = 0;
554 }
555 }
556
557 // Pop transition.
558 self->PopNativeToManagedRecord(record);
559}
560
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700561bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700562 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800563 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700564 return native_method != jni_stub;
565}
566
567void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700568 CHECK(IsNative()) << PrettyMethod(this);
569 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700570 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
571 native_method, false);
572}
573
574void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700575 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700576 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800577 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700578}
579
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700580void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700581 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
582 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700583 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800584 if (new_status == kStatusError) {
585 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
586
587 // stash current exception
588 Thread* self = Thread::Current();
589 SirtRef<Throwable> exception(self->GetException());
590 CHECK(exception.get() != NULL);
591
592 // clear exception to call FindSystemClass
593 self->ClearException();
594 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
595 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
596 CHECK(!self->IsExceptionPending());
597
598 // only verification errors, not initialization problems, should set a verify error.
599 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
600 Class* exception_class = exception->GetClass();
601 if (!eiie_class->IsAssignableFrom(exception_class)) {
602 SetVerifyErrorClass(exception_class);
603 }
604
605 // restore exception
606 self->SetException(exception.get());
607 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700608 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700609}
610
611DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700612 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700613}
614
615void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700616 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700617}
618
Brian Carlstrom1f870082011-08-23 16:02:11 -0700619Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700620 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700621 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500622 // TODO: decide whether we want this check. It currently fails during bootstrap.
623 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700624 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700625 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700626}
627
Ian Rogers0571d352011-11-03 19:51:38 -0700628void Class::SetClassSize(size_t new_class_size) {
629 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
630 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
631}
632
Ian Rogersd418eda2012-01-30 12:14:28 -0800633// Return the class' name. The exact format is bizarre, but it's the specified behavior for
634// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
635// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
636// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
637String* Class::ComputeName() {
638 String* name = GetName();
639 if (name != NULL) {
640 return name;
641 }
642 std::string descriptor(ClassHelper(this).GetDescriptor());
643 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
644 // The descriptor indicates that this is the class for
645 // a primitive type; special-case the return value.
646 const char* c_name = NULL;
647 switch (descriptor[0]) {
648 case 'Z': c_name = "boolean"; break;
649 case 'B': c_name = "byte"; break;
650 case 'C': c_name = "char"; break;
651 case 'S': c_name = "short"; break;
652 case 'I': c_name = "int"; break;
653 case 'J': c_name = "long"; break;
654 case 'F': c_name = "float"; break;
655 case 'D': c_name = "double"; break;
656 case 'V': c_name = "void"; break;
657 default:
658 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
659 }
660 name = String::AllocFromModifiedUtf8(c_name);
661 } else {
662 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
663 // components.
664 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
665 descriptor.erase(0, 1);
666 descriptor.erase(descriptor.size() - 1);
667 }
668 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
669 name = String::AllocFromModifiedUtf8(descriptor.c_str());
670 }
671 SetName(name);
672 return name;
673}
674
Elliott Hughes4681c802011-09-25 18:04:37 -0700675void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700676 if ((flags & kDumpClassFullDetail) == 0) {
677 os << PrettyClass(this);
678 if ((flags & kDumpClassClassLoader) != 0) {
679 os << ' ' << GetClassLoader();
680 }
681 if ((flags & kDumpClassInitialized) != 0) {
682 os << ' ' << GetStatus();
683 }
Elliott Hughese0918552011-10-28 17:18:29 -0700684 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700685 return;
686 }
687
688 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800689 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700690 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800691 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700692 os << " objectSize=" << SizeOf() << " "
693 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
694 os << StringPrintf(" access=0x%04x.%04x\n",
695 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
696 if (super != NULL) {
697 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
698 }
699 if (IsArrayClass()) {
700 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
701 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800702 if (kh.NumInterfaces() > 0) {
703 os << " interfaces (" << kh.NumInterfaces() << "):\n";
704 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
705 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700706 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800707 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700708 }
709 }
710 os << " vtable (" << NumVirtualMethods() << " entries, "
711 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
712 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800713 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700714 }
715 os << " direct methods (" << NumDirectMethods() << " entries):\n";
716 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800717 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700718 }
719 if (NumStaticFields() > 0) {
720 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700721 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700722 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800723 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700724 }
725 } else {
726 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700727 }
728 }
729 if (NumInstanceFields() > 0) {
730 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700731 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700732 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800733 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700734 }
735 } else {
736 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700737 }
738 }
739}
740
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700741void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
742 if (new_reference_offsets != CLASS_WALK_SUPER) {
743 // Sanity check that the number of bits set in the reference offset bitmap
744 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800745 size_t count = 0;
746 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
747 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700748 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800749 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700750 }
751 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
752 new_reference_offsets, false);
753}
754
755void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
756 if (new_reference_offsets != CLASS_WALK_SUPER) {
757 // Sanity check that the number of bits set in the reference offset bitmap
758 // agrees with the number of references
759 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
760 NumReferenceStaticFieldsDuringLinking());
761 }
762 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
763 new_reference_offsets, false);
764}
765
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700766bool Class::Implements(const Class* klass) const {
767 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700768 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700769 // All interfaces implemented directly and by our superclass, and
770 // recursively all super-interfaces of those interfaces, are listed
771 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700772 int32_t iftable_count = GetIfTableCount();
773 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
774 for (int32_t i = 0; i < iftable_count; i++) {
775 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700776 return true;
777 }
778 }
779 return false;
780}
781
782// Determine whether "this" is assignable from "klazz", where both of these
783// are array classes.
784//
785// Consider an array class, e.g. Y[][], where Y is a subclass of X.
786// Y[][] = Y[][] --> true (identity)
787// X[][] = Y[][] --> true (element superclass)
788// Y = Y[][] --> false
789// Y[] = Y[][] --> false
790// Object = Y[][] --> true (everything is an object)
791// Object[] = Y[][] --> true
792// Object[][] = Y[][] --> true
793// Object[][][] = Y[][] --> false (too many []s)
794// Serializable = Y[][] --> true (all arrays are Serializable)
795// Serializable[] = Y[][] --> true
796// Serializable[][] = Y[][] --> false (unless Y is Serializable)
797//
798// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700799// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700800//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700801bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700802 DCHECK(IsArrayClass()) << PrettyClass(this);
803 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700804 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700805}
806
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700807bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700808 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
809 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700810 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700811 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700812 // src's super should be java_lang_Object, since it is an array.
813 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700814 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
815 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700816 return this == java_lang_Object;
817 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700818 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700819}
820
821bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700822 DCHECK(!IsInterface()) << PrettyClass(this);
823 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700824 const Class* current = this;
825 do {
826 if (current == klass) {
827 return true;
828 }
829 current = current->GetSuperClass();
830 } while (current != NULL);
831 return false;
832}
833
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800834bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700835 size_t i = 0;
836 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
837 ++i;
838 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700839 if (descriptor1.find('/', i) != StringPiece::npos ||
840 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700841 return false;
842 } else {
843 return true;
844 }
845}
846
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700847#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700848bool Class::IsInSamePackage(const StringPiece& descriptor1,
849 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700850 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700851 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700852 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
853 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700854 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
855}
856#endif
857
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700858bool Class::IsInSamePackage(const Class* that) const {
859 const Class* klass1 = this;
860 const Class* klass2 = that;
861 if (klass1 == klass2) {
862 return true;
863 }
864 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700865 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700866 return false;
867 }
868 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700869 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700870 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700871 }
jeffhao4a801a42011-09-23 13:53:40 -0700872 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700873 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700874 }
875 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800876 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800877 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800878 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800879 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800880 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700881}
882
Elliott Hughesdbb40792011-11-18 17:05:22 -0800883bool Class::IsClassClass() const {
884 Class* java_lang_Class = GetClass()->GetClass();
885 return this == java_lang_Class;
886}
887
888bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800889 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800890}
891
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800892bool Class::IsThrowableClass() const {
893 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
894 return throwable->IsAssignableFrom(this);
895}
896
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800897ClassLoader* Class::GetClassLoader() const {
898 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700899}
900
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700901void Class::SetClassLoader(const ClassLoader* new_cl) {
902 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700904}
905
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800906Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700907 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700908 DCHECK(declaring_class != NULL) << PrettyClass(this);
909 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700910 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700911 int32_t iftable_count = GetIfTableCount();
912 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
913 for (int32_t i = 0; i < iftable_count; i++) {
914 InterfaceEntry* interface_entry = iftable->Get(i);
915 if (interface_entry->GetInterface() == declaring_class) {
916 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700917 }
918 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700919 return NULL;
920}
921
Ian Rogers466bb252011-10-14 03:29:56 -0700922Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700923 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800924 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700925 if (method != NULL) {
926 return method;
927 }
928
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700929 int32_t iftable_count = GetIfTableCount();
930 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
931 for (int32_t i = 0; i < iftable_count; i++) {
932 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700933 if (method != NULL) {
934 return method;
935 }
936 }
937 return NULL;
938}
939
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700940Method* Class::FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700941 const StringPiece& signature) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800942 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700943 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700944 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800945 mh.ChangeMethod(method);
946 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700947 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700948 }
949 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700950 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -0700951}
952
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700953Method* Class::FindDirectMethod(const StringPiece& name,
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700954 const StringPiece& signature) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700955 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700956 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700957 if (method != NULL) {
958 return method;
959 }
960 }
961 return NULL;
962}
963
964Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -0700965 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800966 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700967 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700968 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800969 mh.ChangeMethod(method);
970 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -0700971 return method;
Ian Rogers466bb252011-10-14 03:29:56 -0700972 }
973 }
974 return NULL;
975}
976
977Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
978 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
979 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
980 if (method != NULL) {
981 return method;
982 }
983 }
984 return NULL;
985}
986
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700987Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -0700988 // Is the field in this class?
989 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800990 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -0700991 for (size_t i = 0; i < NumInstanceFields(); ++i) {
992 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800993 fh.ChangeField(f);
994 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700995 return f;
996 }
997 }
998 return NULL;
999}
1000
1001Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001002 // Is the field in this class, or any of its superclasses?
1003 // Interfaces are not relevant because they can't contain instance fields.
1004 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001005 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001006 if (f != NULL) {
1007 return f;
1008 }
1009 }
1010 return NULL;
1011}
1012
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001013Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1014 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001015 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001016 for (size_t i = 0; i < NumStaticFields(); ++i) {
1017 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001018 fh.ChangeField(f);
1019 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001020 return f;
1021 }
1022 }
1023 return NULL;
1024}
1025
1026Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1027 // Is the field in this class (or its interfaces), or any of its
1028 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001029 ClassHelper kh;
1030 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001031 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001032 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001033 if (f != NULL) {
1034 return f;
1035 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001036 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001037 kh.ChangeClass(k);
1038 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1039 Class* interface = kh.GetInterface(i);
1040 f = interface->FindDeclaredStaticField(name, type);
1041 if (f != NULL) {
1042 return f;
1043 }
1044 }
1045 }
1046 return NULL;
1047}
1048
1049Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1050 // Find a field using the JLS field resolution order
1051 ClassHelper kh;
1052 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1053 // Is the field in this class?
1054 Field* f = k->FindDeclaredInstanceField(name, type);
1055 if (f != NULL) {
1056 return f;
1057 }
1058 f = k->FindDeclaredStaticField(name, type);
1059 if (f != NULL) {
1060 return f;
1061 }
1062 // Is this field in any of this class' interfaces?
1063 kh.ChangeClass(k);
1064 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1065 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001066 f = interface->FindDeclaredStaticField(name, type);
1067 if (f != NULL) {
1068 return f;
1069 }
1070 }
1071 }
1072 return NULL;
1073}
1074
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001075Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001076 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001077 DCHECK_GE(component_count, 0);
1078 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001079
1080 size_t header_size = sizeof(Array);
1081 size_t data_size = component_count * component_size;
1082 size_t size = header_size + data_size;
1083
1084 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1085 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1086 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1087 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001088 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001089 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001090 return NULL;
1091 }
1092
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001093 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1094 if (array != NULL) {
1095 DCHECK(array->IsArrayInstance());
1096 array->SetLength(component_count);
1097 }
1098 return array;
1099}
1100
1101Array* Array::Alloc(Class* array_class, int32_t component_count) {
1102 return Alloc(array_class, component_count, array_class->GetComponentSize());
1103}
1104
Elliott Hughes80609252011-09-23 17:24:51 -07001105bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001106 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001107 "length=%i; index=%i", length_, index);
1108 return false;
1109}
1110
1111bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001112 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001113 "Can't store an element of type %s into an array of type %s",
1114 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1115 return false;
1116}
1117
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001118template<typename T>
1119PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001120 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001121 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1122 return down_cast<PrimitiveArray<T>*>(raw_array);
1123}
1124
1125template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1126
1127// Explicitly instantiate all the primitive array types.
1128template class PrimitiveArray<uint8_t>; // BooleanArray
1129template class PrimitiveArray<int8_t>; // ByteArray
1130template class PrimitiveArray<uint16_t>; // CharArray
1131template class PrimitiveArray<double>; // DoubleArray
1132template class PrimitiveArray<float>; // FloatArray
1133template class PrimitiveArray<int32_t>; // IntArray
1134template class PrimitiveArray<int64_t>; // LongArray
1135template class PrimitiveArray<int16_t>; // ShortArray
1136
Ian Rogers466bb252011-10-14 03:29:56 -07001137// Explicitly instantiate Class[][]
1138template class ObjectArray<ObjectArray<Class> >;
1139
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001140// TODO: get global references for these
1141Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001142
Brian Carlstroma663ea52011-08-19 23:33:41 -07001143void String::SetClass(Class* java_lang_String) {
1144 CHECK(java_lang_String_ == NULL);
1145 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001146 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001147}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001148
Brian Carlstroma663ea52011-08-19 23:33:41 -07001149void String::ResetClass() {
1150 CHECK(java_lang_String_ != NULL);
1151 java_lang_String_ = NULL;
1152}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001153
Brian Carlstromc74255f2011-09-11 22:47:39 -07001154String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001155 return Runtime::Current()->GetInternTable()->InternWeak(this);
1156}
1157
Brian Carlstrom395520e2011-09-25 19:35:00 -07001158int32_t String::GetHashCode() {
1159 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1160 if (result == 0) {
1161 ComputeHashCode();
1162 }
1163 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1164 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1165 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001166 return result;
1167}
1168
1169int32_t String::GetLength() const {
1170 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1171 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1172 return result;
1173}
1174
1175uint16_t String::CharAt(int32_t index) const {
1176 // TODO: do we need this? Equals is the only caller, and could
1177 // bounds check itself.
1178 if (index < 0 || index >= count_) {
1179 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001180 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001181 "length=%i; index=%i", count_, index);
1182 return 0;
1183 }
1184 return GetCharArray()->Get(index + GetOffset());
1185}
1186
1187String* String::AllocFromUtf16(int32_t utf16_length,
1188 const uint16_t* utf16_data_in,
1189 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001190 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001191 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001192 if (string == NULL) {
1193 return NULL;
1194 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001195 // TODO: use 16-bit wide memset variant
1196 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001197 if (array == NULL) {
1198 return NULL;
1199 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001200 for (int i = 0; i < utf16_length; i++) {
1201 array->Set(i, utf16_data_in[i]);
1202 }
1203 if (hash_code != 0) {
1204 string->SetHashCode(hash_code);
1205 } else {
1206 string->ComputeHashCode();
1207 }
1208 return string;
1209}
1210
1211String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001212 if (utf == NULL) {
1213 return NULL;
1214 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001215 size_t char_count = CountModifiedUtf8Chars(utf);
1216 return AllocFromModifiedUtf8(char_count, utf);
1217}
1218
1219String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1220 const char* utf8_data_in) {
1221 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001222 if (string == NULL) {
1223 return NULL;
1224 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001225 uint16_t* utf16_data_out =
1226 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1227 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1228 string->ComputeHashCode();
1229 return string;
1230}
1231
1232String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001233 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1234 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001235 return NULL;
1236 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001237 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001238}
1239
1240String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001241 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001242 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001243 if (string == NULL) {
1244 return NULL;
1245 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001246 string->SetArray(array);
1247 string->SetCount(array->GetLength());
1248 return string;
1249}
1250
1251bool String::Equals(const String* that) const {
1252 if (this == that) {
1253 // Quick reference equality test
1254 return true;
1255 } else if (that == NULL) {
1256 // Null isn't an instanceof anything
1257 return false;
1258 } else if (this->GetLength() != that->GetLength()) {
1259 // Quick length inequality test
1260 return false;
1261 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001262 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001263 // hash code was already equal
1264 for (int32_t i = 0; i < that->GetLength(); ++i) {
1265 if (this->CharAt(i) != that->CharAt(i)) {
1266 return false;
1267 }
1268 }
1269 return true;
1270 }
1271}
1272
Elliott Hughes5d78d392011-12-13 16:53:05 -08001273bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001274 if (this->GetLength() != that_length) {
1275 return false;
1276 } else {
1277 for (int32_t i = 0; i < that_length; ++i) {
1278 if (this->CharAt(i) != that_chars[that_offset + i]) {
1279 return false;
1280 }
1281 }
1282 return true;
1283 }
1284}
1285
1286bool String::Equals(const char* modified_utf8) const {
1287 for (int32_t i = 0; i < GetLength(); ++i) {
1288 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1289 if (ch == '\0' || ch != CharAt(i)) {
1290 return false;
1291 }
1292 }
1293 return *modified_utf8 == '\0';
1294}
1295
1296bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001297 if (modified_utf8.size() != GetLength()) {
1298 return false;
1299 }
1300 const char* p = modified_utf8.data();
1301 for (int32_t i = 0; i < GetLength(); ++i) {
1302 uint16_t ch = GetUtf16FromUtf8(&p);
1303 if (ch != CharAt(i)) {
1304 return false;
1305 }
1306 }
1307 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001308}
1309
1310// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1311std::string String::ToModifiedUtf8() const {
1312 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1313 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1314 std::string result(byte_count, char(0));
1315 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1316 return result;
1317}
1318
Ian Rogers1c5eb702012-02-01 09:18:34 -08001319void Throwable::SetCause(Throwable* cause) {
1320 CHECK(cause != NULL);
1321 CHECK(cause != this);
1322 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1323 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1324}
1325
Ian Rogers466bb252011-10-14 03:29:56 -07001326bool Throwable::IsCheckedException() const {
1327 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1328 if (InstanceOf(error)) {
1329 return false;
1330 }
1331 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1332 return !InstanceOf(jlre);
1333}
1334
Ian Rogers9074b992011-10-26 17:41:55 -07001335std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001336 std::string result(PrettyTypeOf(this));
1337 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001338 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001339 if (msg != NULL) {
1340 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001341 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001342 result += "\n";
1343 Object* stack_state = GetStackState();
1344 // check stack state isn't missing or corrupt
1345 if (stack_state != NULL && stack_state->IsObjectArray()) {
1346 // Decode the internal stack trace into the depth and method trace
1347 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1348 int32_t depth = method_trace->GetLength() - 1;
1349 for (int32_t i = 0; i < depth; ++i) {
1350 Method* method = down_cast<Method*>(method_trace->Get(i));
1351 result += " at ";
1352 result += PrettyMethod(method, true);
1353 result += "\n";
1354 }
Ian Rogers9074b992011-10-26 17:41:55 -07001355 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001356 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
1357 if (cause != NULL) {
1358 result += "Caused by: ";
1359 result += cause->Dump();
1360 }
Ian Rogers9074b992011-10-26 17:41:55 -07001361 return result;
1362}
1363
Ian Rogers5167c972012-02-03 10:41:20 -08001364
1365Class* Throwable::java_lang_Throwable_ = NULL;
1366
1367void Throwable::SetClass(Class* java_lang_Throwable) {
1368 CHECK(java_lang_Throwable_ == NULL);
1369 CHECK(java_lang_Throwable != NULL);
1370 java_lang_Throwable_ = java_lang_Throwable;
1371}
1372
1373void Throwable::ResetClass() {
1374 CHECK(java_lang_Throwable_ != NULL);
1375 java_lang_Throwable_ = NULL;
1376}
1377
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001378Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1379
1380void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1381 CHECK(java_lang_StackTraceElement_ == NULL);
1382 CHECK(java_lang_StackTraceElement != NULL);
1383 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1384}
1385
1386void StackTraceElement::ResetClass() {
1387 CHECK(java_lang_StackTraceElement_ != NULL);
1388 java_lang_StackTraceElement_ = NULL;
1389}
1390
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001391StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1392 String* method_name,
1393 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394 int32_t line_number) {
1395 StackTraceElement* trace =
1396 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1397 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1398 const_cast<String*>(declaring_class), false);
1399 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1400 const_cast<String*>(method_name), false);
1401 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1402 const_cast<String*>(file_name), false);
1403 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1404 line_number, false);
1405 return trace;
1406}
1407
Elliott Hughes1f359b02011-07-17 14:27:17 -07001408static const char* kClassStatusNames[] = {
1409 "Error",
1410 "NotReady",
1411 "Idx",
1412 "Loaded",
1413 "Resolved",
1414 "Verifying",
1415 "Verified",
1416 "Initializing",
1417 "Initialized"
1418};
1419std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1420 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001421 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001422 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001423 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001424 }
1425 return os;
1426}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001427
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001428} // namespace art