blob: 9a4588a74b87e42cbac511d49f7d327ca86df0aa [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"
Ian Rogers64b6d142012-10-29 16:34:15 -070033#include "interpreter/interpreter.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070035#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080036#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070037#include "runtime.h"
Ian Rogers60db5ab2012-02-20 17:02:00 -080038#include "runtime_support.h"
Ian Rogers1f539342012-10-03 21:09:42 -070039#include "sirt_ref.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070040#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070041#include "utils.h"
Elliott Hughesa4f94742012-05-29 16:28:38 -070042#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070043
44namespace art {
45
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070046BooleanArray* Object::AsBooleanArray() {
47 DCHECK(GetClass()->IsArrayClass());
48 DCHECK(GetClass()->GetComponentType()->IsPrimitiveBoolean());
49 return down_cast<BooleanArray*>(this);
50}
51
52ByteArray* Object::AsByteArray() {
53 DCHECK(GetClass()->IsArrayClass());
54 DCHECK(GetClass()->GetComponentType()->IsPrimitiveByte());
55 return down_cast<ByteArray*>(this);
56}
57
58CharArray* Object::AsCharArray() {
59 DCHECK(GetClass()->IsArrayClass());
60 DCHECK(GetClass()->GetComponentType()->IsPrimitiveChar());
61 return down_cast<CharArray*>(this);
62}
63
64ShortArray* Object::AsShortArray() {
65 DCHECK(GetClass()->IsArrayClass());
66 DCHECK(GetClass()->GetComponentType()->IsPrimitiveShort());
67 return down_cast<ShortArray*>(this);
68}
69
70IntArray* Object::AsIntArray() {
71 DCHECK(GetClass()->IsArrayClass());
Ian Rogers64b6d142012-10-29 16:34:15 -070072 DCHECK(GetClass()->GetComponentType()->IsPrimitiveInt() ||
73 GetClass()->GetComponentType()->IsPrimitiveFloat());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070074 return down_cast<IntArray*>(this);
75}
76
77LongArray* Object::AsLongArray() {
78 DCHECK(GetClass()->IsArrayClass());
Ian Rogers64b6d142012-10-29 16:34:15 -070079 DCHECK(GetClass()->GetComponentType()->IsPrimitiveLong() ||
80 GetClass()->GetComponentType()->IsPrimitiveDouble());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070081 return down_cast<LongArray*>(this);
82}
83
Elliott Hughesdbb40792011-11-18 17:05:22 -080084String* Object::AsString() {
85 DCHECK(GetClass()->IsStringClass());
86 return down_cast<String*>(this);
87}
88
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070089Throwable* Object::AsThrowable() {
90 DCHECK(GetClass()->IsThrowableClass());
91 return down_cast<Throwable*>(this);
92}
93
Ian Rogers50b35e22012-10-04 10:09:15 -070094Object* Object::Clone(Thread* self) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070095 Class* c = GetClass();
96 DCHECK(!c->IsClassClass());
97
98 // Object::SizeOf gets the right size even if we're an array.
99 // Using c->AllocObject() here would be wrong.
100 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800101 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 SirtRef<Object> copy(self, heap->AllocObject(self, c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700103 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -0700104 return NULL;
105 }
106
107 // Copy instance data. We assume memcpy copies by words.
108 // TODO: expose and use move32.
109 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700110 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -0700111 size_t offset = sizeof(Object);
112 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
113
Mathieu Chartier88c95be2012-09-11 14:06:41 -0700114 // Perform write barriers on copied object references.
115 if (c->IsArrayClass()) {
116 if (!c->GetComponentType()->IsPrimitive()) {
117 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
118 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
119 }
120 } else {
121 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
122 size_t num_reference_fields = klass->NumReferenceInstanceFields();
123 for (size_t i = 0; i < num_reference_fields; ++i) {
124 Field* field = klass->GetInstanceField(i);
125 MemberOffset field_offset = field->GetOffset();
126 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
127 heap->WriteBarrierField(copy.get(), field_offset, ref);
128 }
129 }
130 }
131
Elliott Hughes20cde902011-10-04 17:37:27 -0700132 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800133 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -0700134 }
Elliott Hughes081be7f2011-09-18 16:50:26 -0700135
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700136 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700137}
138
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700139uint32_t Object::GetThinLockId() {
140 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700141}
142
143void Object::MonitorEnter(Thread* thread) {
144 Monitor::MonitorEnter(thread, this);
145}
146
Ian Rogersff1ed472011-09-20 13:46:24 -0700147bool Object::MonitorExit(Thread* thread) {
148 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700149}
150
151void Object::Notify() {
152 Monitor::Notify(Thread::Current(), this);
153}
154
155void Object::NotifyAll() {
156 Monitor::NotifyAll(Thread::Current(), this);
157}
158
159void Object::Wait(int64_t ms, int32_t ns) {
160 Monitor::Wait(Thread::Current(), this, ms, ns, true);
161}
162
Ian Rogers23435d02012-09-24 11:23:12 -0700163#if VERIFY_OBJECT_ENABLED
164void Object::CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) {
165 const Class* c = GetClass();
166 if (Runtime::Current()->GetClassLinker() == NULL ||
167 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
168 !c->IsResolved()) {
169 return;
170 }
171 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
172 ObjectArray<Field>* fields = cur->GetIFields();
173 if (fields != NULL) {
174 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
175 for (size_t i = 0; i < num_ref_ifields; ++i) {
176 Field* field = fields->Get(i);
177 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
178 FieldHelper fh(field);
179 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
180 return;
181 }
182 }
183 }
184 }
185 if (c->IsArrayClass()) {
186 // Bounds and assign-ability done in the array setter.
187 return;
188 }
189 if (IsClass()) {
190 ObjectArray<Field>* fields = AsClass()->GetSFields();
191 if (fields != NULL) {
192 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
193 for (size_t i = 0; i < num_ref_sfields; ++i) {
194 Field* field = fields->Get(i);
195 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
196 FieldHelper fh(field);
197 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
198 return;
199 }
200 }
201 }
202 }
203 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
204 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
205}
206#endif
207
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208// TODO: get global references for these
209Class* Field::java_lang_reflect_Field_ = NULL;
210
211void Field::SetClass(Class* java_lang_reflect_Field) {
212 CHECK(java_lang_reflect_Field_ == NULL);
213 CHECK(java_lang_reflect_Field != NULL);
214 java_lang_reflect_Field_ = java_lang_reflect_Field;
215}
216
217void Field::ResetClass() {
218 CHECK(java_lang_reflect_Field_ != NULL);
219 java_lang_reflect_Field_ = NULL;
220}
221
Ian Rogers0571d352011-11-03 19:51:38 -0700222void Field::SetOffset(MemberOffset num_bytes) {
223 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800224#if 0 // TODO enable later in boot and under !NDEBUG
225 FieldHelper fh(this);
226 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700227 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
228 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
229 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800230#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700231 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
232}
233
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234uint32_t Field::Get32(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700235 DCHECK(object != NULL) << PrettyField(this);
236 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700238}
239
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240void Field::Set32(Object* object, uint32_t new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700241 DCHECK(object != NULL) << PrettyField(this);
242 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 object->SetField32(GetOffset(), new_value, IsVolatile());
244}
245
246uint64_t Field::Get64(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700247 DCHECK(object != NULL) << PrettyField(this);
248 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 return object->GetField64(GetOffset(), IsVolatile());
250}
251
252void Field::Set64(Object* object, uint64_t new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700253 DCHECK(object != NULL) << PrettyField(this);
254 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700255 object->SetField64(GetOffset(), new_value, IsVolatile());
256}
257
258Object* Field::GetObj(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700259 DCHECK(object != NULL) << PrettyField(this);
260 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
262}
263
264void Field::SetObj(Object* object, const Object* new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700265 DCHECK(object != NULL) << PrettyField(this);
266 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
268}
269
270bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
272 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 return Get32(object);
274}
275
276void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
278 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700279 Set32(object, z);
280}
281
282int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
284 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 return Get32(object);
286}
287
288void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
290 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 Set32(object, b);
292}
293
294uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800295 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
296 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297 return Get32(object);
298}
299
300void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800301 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
302 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 Set32(object, c);
304}
305
Ian Rogers466bb252011-10-14 03:29:56 -0700306int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
308 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700309 return Get32(object);
310}
311
Ian Rogers466bb252011-10-14 03:29:56 -0700312void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
314 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700315 Set32(object, s);
316}
317
318int32_t Field::GetInt(const Object* object) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700319#ifndef NDEBUG
320 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
321 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
322#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 return Get32(object);
324}
325
326void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700327#ifndef NDEBUG
328 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
329 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
330#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 Set32(object, i);
332}
333
334int64_t Field::GetLong(const Object* object) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700335#ifndef NDEBUG
336 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
337 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
338#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339 return Get64(object);
340}
341
342void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700343#ifndef NDEBUG
344 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
345 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
346#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 Set64(object, j);
348}
349
Elliott Hughes1d878f32012-04-11 15:17:54 -0700350union Bits {
351 jdouble d;
352 jfloat f;
353 jint i;
354 jlong j;
355};
356
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800358 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
359 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700360 Bits bits;
361 bits.i = Get32(object);
362 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363}
364
365void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800366 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
367 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700368 Bits bits;
369 bits.f = f;
370 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371}
372
373double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800374 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
375 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700376 Bits bits;
377 bits.j = Get64(object);
378 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379}
380
381void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
383 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700384 Bits bits;
385 bits.d = d;
386 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387}
388
389Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800390 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
391 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 return GetObj(object);
393}
394
395void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800396 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
397 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 SetObj(object, l);
399}
400
401// TODO: get global references for these
Mathieu Chartier66f19252012-09-18 08:57:04 -0700402Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
403Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700404
Mathieu Chartier66f19252012-09-18 08:57:04 -0700405InvokeType AbstractMethod::GetInvokeType() const {
Ian Rogers08f753d2012-08-24 14:35:25 -0700406 // TODO: kSuper?
407 if (GetDeclaringClass()->IsInterface()) {
408 return kInterface;
409 } else if (IsStatic()) {
410 return kStatic;
411 } else if (IsDirect()) {
412 return kDirect;
413 } else {
414 return kVirtual;
415 }
416}
417
Mathieu Chartier66f19252012-09-18 08:57:04 -0700418void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
Elliott Hughes80609252011-09-23 17:24:51 -0700419 CHECK(java_lang_reflect_Constructor_ == NULL);
420 CHECK(java_lang_reflect_Constructor != NULL);
421 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 CHECK(java_lang_reflect_Method_ == NULL);
424 CHECK(java_lang_reflect_Method != NULL);
425 java_lang_reflect_Method_ = java_lang_reflect_Method;
426}
427
Mathieu Chartier66f19252012-09-18 08:57:04 -0700428void AbstractMethod::ResetClasses() {
Elliott Hughes80609252011-09-23 17:24:51 -0700429 CHECK(java_lang_reflect_Constructor_ != NULL);
430 java_lang_reflect_Constructor_ = NULL;
431
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432 CHECK(java_lang_reflect_Method_ != NULL);
433 java_lang_reflect_Method_ = NULL;
434}
435
Mathieu Chartier66f19252012-09-18 08:57:04 -0700436ObjectArray<String>* AbstractMethod::GetDexCacheStrings() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 return GetFieldObject<ObjectArray<String>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700438 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439}
440
Mathieu Chartier66f19252012-09-18 08:57:04 -0700441void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
442 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700443 new_dex_cache_strings, false);
444}
445
Mathieu Chartier66f19252012-09-18 08:57:04 -0700446ObjectArray<AbstractMethod>* AbstractMethod::GetDexCacheResolvedMethods() const {
447 return GetFieldObject<ObjectArray<AbstractMethod>*>(
448 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800449}
450
Mathieu Chartier66f19252012-09-18 08:57:04 -0700451void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
452 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
Ian Rogers19846512012-02-24 11:42:47 -0800453 new_dex_cache_methods, false);
454}
455
Mathieu Chartier66f19252012-09-18 08:57:04 -0700456ObjectArray<Class>* AbstractMethod::GetDexCacheResolvedTypes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 return GetFieldObject<ObjectArray<Class>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700458 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700459}
460
Mathieu Chartier66f19252012-09-18 08:57:04 -0700461void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
462 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 new_dex_cache_classes, false);
464}
465
Mathieu Chartier66f19252012-09-18 08:57:04 -0700466ObjectArray<StaticStorageBase>* AbstractMethod::GetDexCacheInitializedStaticStorage() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700467 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700468 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700469 false);
470}
471
Mathieu Chartier66f19252012-09-18 08:57:04 -0700472void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
473 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700475}
476
Mathieu Chartier66f19252012-09-18 08:57:04 -0700477size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700478 CHECK_LE(1, shorty.length());
479 uint32_t num_registers = 0;
480 for (int i = 1; i < shorty.length(); ++i) {
481 char ch = shorty[i];
482 if (ch == 'D' || ch == 'J') {
483 num_registers += 2;
484 } else {
485 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700486 }
487 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700488 return num_registers;
489}
490
Mathieu Chartier66f19252012-09-18 08:57:04 -0700491bool AbstractMethod::IsProxyMethod() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800492 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700493}
494
Mathieu Chartier66f19252012-09-18 08:57:04 -0700495AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
Ian Rogers466bb252011-10-14 03:29:56 -0700496 if (IsStatic()) {
497 return NULL;
498 }
499 Class* declaring_class = GetDeclaringClass();
500 Class* super_class = declaring_class->GetSuperClass();
501 uint16_t method_index = GetMethodIndex();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700502 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
503 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 // Did this method override a super class method? If so load the result from the super class'
505 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700506 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
507 result = super_class_vtable->Get(method_index);
508 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800510 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800511 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
512 CHECK_EQ(result,
513 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800514 } else {
515 MethodHelper mh(this);
516 MethodHelper interface_mh;
Ian Rogers9bc81912012-10-11 21:43:36 -0700517 IfTable* iftable = GetDeclaringClass()->GetIfTable();
518 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
519 Class* interface = iftable->GetInterface(i);
Ian Rogers16f93672012-02-14 12:29:06 -0800520 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700521 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers16f93672012-02-14 12:29:06 -0800522 interface_mh.ChangeMethod(interface_method);
523 if (mh.HasSameNameAndSignature(&interface_mh)) {
524 result = interface_method;
525 break;
526 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 }
528 }
Ian Rogers466bb252011-10-14 03:29:56 -0700529 }
530 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800531#ifndef NDEBUG
532 MethodHelper result_mh(result);
533 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
534#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700535 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700536}
537
Mathieu Chartier66f19252012-09-18 08:57:04 -0700538static const void* GetOatCode(const AbstractMethod* m)
539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800540 Runtime* runtime = Runtime::Current();
541 const void* code = m->GetCode();
542 // Peel off any method tracing trampoline.
543 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
544 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
545 }
546 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800547 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800548 code = runtime->GetClassLinker()->GetOatCodeFor(m);
549 }
550 return code;
551}
552
Mathieu Chartier66f19252012-09-18 08:57:04 -0700553uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700554 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
555}
556
Bill Buzbeea5b30242012-09-28 07:19:44 -0700557// Find the lowest-address native safepoint pc for a given dex pc
Ian Rogers34065b52012-09-28 16:31:26 -0700558uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700559#if !defined(ART_USE_LLVM_COMPILER)
Bill Buzbeea5b30242012-09-28 07:19:44 -0700560 const uint32_t* mapping_table = GetPcToDexMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700561 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800562 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700563 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700564 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700565 size_t mapping_table_length = GetPcToDexMappingTableLength();
566 for (size_t i = 0; i < mapping_table_length; i += 2) {
567 if (mapping_table[i + 1] == dex_pc) {
568 return mapping_table[i] + reinterpret_cast<uintptr_t>(GetOatCode(this));
569 }
570 }
571 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
572 << " in " << PrettyMethod(this);
573 return 0;
574#else
575 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
576 return static_cast<uint32_t>(dex_pc);
577#endif
578}
579
580uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
581#if !defined(ART_USE_LLVM_COMPILER)
582 const uint32_t* mapping_table = GetPcToDexMappingTable();
583 if (mapping_table == NULL) {
584 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
585 return DexFile::kDexNoIndex; // Special no mapping case
586 }
587 size_t mapping_table_length = GetPcToDexMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800588 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700589 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700590 if (mapping_table[i] == sought_offset) {
591 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700592 }
593 }
buzbee8320f382012-09-11 16:29:42 -0700594 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
595 << " in " << PrettyMethod(this);
596 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700597#else
598 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
599 return static_cast<uint32_t>(pc);
600#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700601}
602
Mathieu Chartier66f19252012-09-18 08:57:04 -0700603uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700604 const uint32_t* mapping_table = GetDexToPcMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700605 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700606 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700607 return 0; // Special no mapping/pc == 0 case
608 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700609 size_t mapping_table_length = GetDexToPcMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700610 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700611 uint32_t map_offset = mapping_table[i];
612 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700613 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800614 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700615 }
616 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700617 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
618 << " in " << PrettyMethod(this);
Ian Rogersbdb03912011-09-14 00:55:44 -0700619 return 0;
620}
621
Mathieu Chartier66f19252012-09-18 08:57:04 -0700622uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800623 MethodHelper mh(this);
624 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700625 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700626 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
627 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700628 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700629 if (iter_type_idx == DexFile::kDexNoIndex16) {
630 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700631 }
632 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800633 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700634 if (iter_exception_type == NULL) {
635 // The verifier should take care of resolving all exception classes early
636 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800637 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700638 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700639 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700640 }
641 }
642 // Handler not found
643 return DexFile::kDexNoIndex;
644}
645
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700646void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 if (kIsDebugBuild) {
648 self->AssertThreadSuspensionIsAllowable();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 CHECK_EQ(kRunnable, self->GetState());
650 }
TDYa12785321912012-04-01 15:24:56 -0700651
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700653 ManagedStack fragment;
654 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700655
656 // Call the invoke stub associated with the method.
657 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700658 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700659
Elliott Hughes1240dad2011-09-09 16:24:50 -0700660
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700661 if (UNLIKELY(!Runtime::Current()->IsStarted())){
662 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started.";
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700663 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700664 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700665 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700666 } else {
667 if (GetCode() != NULL && stub != NULL) {
668 bool log = false;
669 if (log) {
670 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
671 PrettyMethod(this).c_str(), GetCode(), stub);
672 }
673 (*stub)(this, receiver, self, args, result);
674 if (log) {
675 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
676 PrettyMethod(this).c_str(), GetCode(), stub);
677 }
678 } else {
679 LOG(INFO) << "Not invoking " << PrettyMethod(this)
680 << " code=" << reinterpret_cast<const void*>(GetCode())
681 << " stub=" << reinterpret_cast<void*>(stub);
682 const bool kInterpretMethodsWithNoCode = false;
683 if (kInterpretMethodsWithNoCode) {
684 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args, result);
685 } else if (result != NULL) {
686 result->SetJ(0);
687 }
688 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700689 }
690
691 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700692 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700693}
694
Mathieu Chartier66f19252012-09-18 08:57:04 -0700695bool AbstractMethod::IsRegistered() const {
696 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800697 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800698 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700699 return native_method != jni_stub;
700}
701
Mathieu Chartier66f19252012-09-18 08:57:04 -0700702void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800703 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700704 CHECK(IsNative()) << PrettyMethod(this);
705 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700706#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700707 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700708 native_method, false);
709#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800710 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700711 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800712 native_method, false);
713 } else {
714 // We've been asked to associate this method with the given native method but are working
715 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
716 // the native method to runtime support and store the target somewhere runtime support will
717 // find it.
718#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700719 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800720 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
721#else
722 UNIMPLEMENTED(FATAL);
723#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700724 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800725 reinterpret_cast<const uint8_t*>(native_method), false);
726 }
TDYa12726467572012-04-17 20:51:22 -0700727#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700728}
729
Mathieu Chartier66f19252012-09-18 08:57:04 -0700730void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700731 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700732 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800733 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700734}
735
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700736void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700737 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
738 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700739 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700740 if (new_status > kStatusResolved) {
741 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
742 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800743 if (new_status == kStatusError) {
744 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
745
746 // stash current exception
747 Thread* self = Thread::Current();
Ian Rogers1f539342012-10-03 21:09:42 -0700748 SirtRef<Throwable> exception(self, self->GetException());
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800749 CHECK(exception.get() != NULL);
750
751 // clear exception to call FindSystemClass
752 self->ClearException();
753 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
754 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
755 CHECK(!self->IsExceptionPending());
756
757 // only verification errors, not initialization problems, should set a verify error.
758 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
759 Class* exception_class = exception->GetClass();
760 if (!eiie_class->IsAssignableFrom(exception_class)) {
761 SetVerifyErrorClass(exception_class);
762 }
763
764 // restore exception
765 self->SetException(exception.get());
766 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700767 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700768}
769
770DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700771 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700772}
773
774void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700775 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700776}
777
Ian Rogers50b35e22012-10-04 10:09:15 -0700778Object* Class::AllocObject(Thread* self) {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700779 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700780 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500781 // TODO: decide whether we want this check. It currently fails during bootstrap.
782 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700783 DCHECK_GE(this->object_size_, sizeof(Object));
Ian Rogers50b35e22012-10-04 10:09:15 -0700784 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700785}
786
Ian Rogers0571d352011-11-03 19:51:38 -0700787void Class::SetClassSize(size_t new_class_size) {
788 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
789 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
790}
791
Ian Rogersd418eda2012-01-30 12:14:28 -0800792// Return the class' name. The exact format is bizarre, but it's the specified behavior for
793// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
794// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
795// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
796String* Class::ComputeName() {
797 String* name = GetName();
798 if (name != NULL) {
799 return name;
800 }
801 std::string descriptor(ClassHelper(this).GetDescriptor());
802 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
803 // The descriptor indicates that this is the class for
804 // a primitive type; special-case the return value.
805 const char* c_name = NULL;
806 switch (descriptor[0]) {
807 case 'Z': c_name = "boolean"; break;
808 case 'B': c_name = "byte"; break;
809 case 'C': c_name = "char"; break;
810 case 'S': c_name = "short"; break;
811 case 'I': c_name = "int"; break;
812 case 'J': c_name = "long"; break;
813 case 'F': c_name = "float"; break;
814 case 'D': c_name = "double"; break;
815 case 'V': c_name = "void"; break;
816 default:
817 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
818 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700819 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
Ian Rogersd418eda2012-01-30 12:14:28 -0800820 } else {
821 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
822 // components.
823 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
824 descriptor.erase(0, 1);
825 descriptor.erase(descriptor.size() - 1);
826 }
827 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Ian Rogers50b35e22012-10-04 10:09:15 -0700828 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
Ian Rogersd418eda2012-01-30 12:14:28 -0800829 }
830 SetName(name);
831 return name;
832}
833
Elliott Hughes4681c802011-09-25 18:04:37 -0700834void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700835 if ((flags & kDumpClassFullDetail) == 0) {
836 os << PrettyClass(this);
837 if ((flags & kDumpClassClassLoader) != 0) {
838 os << ' ' << GetClassLoader();
839 }
840 if ((flags & kDumpClassInitialized) != 0) {
841 os << ' ' << GetStatus();
842 }
Elliott Hughese0918552011-10-28 17:18:29 -0700843 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700844 return;
845 }
846
847 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800848 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700849 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800850 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700851 os << " objectSize=" << SizeOf() << " "
852 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
853 os << StringPrintf(" access=0x%04x.%04x\n",
854 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
855 if (super != NULL) {
856 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
857 }
858 if (IsArrayClass()) {
859 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
860 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700861 if (kh.NumDirectInterfaces() > 0) {
862 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
863 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
864 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700865 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800866 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700867 }
868 }
869 os << " vtable (" << NumVirtualMethods() << " entries, "
870 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
871 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800872 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700873 }
874 os << " direct methods (" << NumDirectMethods() << " entries):\n";
875 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800876 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700877 }
878 if (NumStaticFields() > 0) {
879 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700880 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700881 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800882 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700883 }
884 } else {
885 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700886 }
887 }
888 if (NumInstanceFields() > 0) {
889 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700890 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700891 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800892 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700893 }
894 } else {
895 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700896 }
897 }
898}
899
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700900void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
901 if (new_reference_offsets != CLASS_WALK_SUPER) {
902 // Sanity check that the number of bits set in the reference offset bitmap
903 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800904 size_t count = 0;
905 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
906 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700907 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800908 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700909 }
910 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
911 new_reference_offsets, false);
912}
913
914void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
915 if (new_reference_offsets != CLASS_WALK_SUPER) {
916 // Sanity check that the number of bits set in the reference offset bitmap
917 // agrees with the number of references
918 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
919 NumReferenceStaticFieldsDuringLinking());
920 }
921 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
922 new_reference_offsets, false);
923}
924
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700925bool Class::Implements(const Class* klass) const {
926 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700927 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700928 // All interfaces implemented directly and by our superclass, and
929 // recursively all super-interfaces of those interfaces, are listed
930 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700931 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -0700932 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700933 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700934 if (iftable->GetInterface(i) == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700935 return true;
936 }
937 }
938 return false;
939}
940
Elliott Hughese84278b2012-03-22 10:06:53 -0700941// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700942// are array classes.
943//
944// Consider an array class, e.g. Y[][], where Y is a subclass of X.
945// Y[][] = Y[][] --> true (identity)
946// X[][] = Y[][] --> true (element superclass)
947// Y = Y[][] --> false
948// Y[] = Y[][] --> false
949// Object = Y[][] --> true (everything is an object)
950// Object[] = Y[][] --> true
951// Object[][] = Y[][] --> true
952// Object[][][] = Y[][] --> false (too many []s)
953// Serializable = Y[][] --> true (all arrays are Serializable)
954// Serializable[] = Y[][] --> true
955// Serializable[][] = Y[][] --> false (unless Y is Serializable)
956//
957// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700958// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700959//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700960bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700961 DCHECK(IsArrayClass()) << PrettyClass(this);
962 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700963 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700964}
965
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700966bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700967 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
968 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700969 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700970 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700971 // src's super should be java_lang_Object, since it is an array.
972 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700973 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
974 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700975 return this == java_lang_Object;
976 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700977 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700978}
979
980bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700981 DCHECK(!IsInterface()) << PrettyClass(this);
982 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700983 const Class* current = this;
984 do {
985 if (current == klass) {
986 return true;
987 }
988 current = current->GetSuperClass();
989 } while (current != NULL);
990 return false;
991}
992
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800993bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700994 size_t i = 0;
995 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
996 ++i;
997 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700998 if (descriptor1.find('/', i) != StringPiece::npos ||
999 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001000 return false;
1001 } else {
1002 return true;
1003 }
1004}
1005
1006bool Class::IsInSamePackage(const Class* that) const {
1007 const Class* klass1 = this;
1008 const Class* klass2 = that;
1009 if (klass1 == klass2) {
1010 return true;
1011 }
1012 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001013 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001014 return false;
1015 }
1016 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -07001017 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001018 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001019 }
jeffhao4a801a42011-09-23 13:53:40 -07001020 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001021 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001022 }
1023 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001024 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -08001025 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001026 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -08001027 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001028 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001029}
1030
Elliott Hughesdbb40792011-11-18 17:05:22 -08001031bool Class::IsClassClass() const {
1032 Class* java_lang_Class = GetClass()->GetClass();
1033 return this == java_lang_Class;
1034}
1035
1036bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001037 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001038}
1039
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001040bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001041 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001042}
1043
Ian Rogers64b6d142012-10-29 16:34:15 -07001044bool Class::IsFieldClass() const {
1045 Class* java_lang_Class = GetClass();
1046 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
1047 return this == java_lang_reflect_Field;
1048
1049}
1050
1051bool Class::IsMethodClass() const {
1052 return (this == AbstractMethod::GetMethodClass()) ||
1053 (this == AbstractMethod::GetConstructorClass());
1054
1055}
1056
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001057ClassLoader* Class::GetClassLoader() const {
1058 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -07001059}
1060
Ian Rogers365c1022012-06-22 15:05:28 -07001061void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001063}
1064
Mathieu Chartier66f19252012-09-18 08:57:04 -07001065AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001066 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001067 DCHECK(declaring_class != NULL) << PrettyClass(this);
1068 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001069 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001070 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001071 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001072 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001073 if (iftable->GetInterface(i) == declaring_class) {
1074 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001075 }
1076 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001077 return NULL;
1078}
1079
Mathieu Chartier66f19252012-09-18 08:57:04 -07001080AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001081 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001082 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001083 if (method != NULL) {
1084 return method;
1085 }
1086
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001087 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001088 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001089 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001090 method = iftable->GetInterface(i)->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001091 if (method != NULL) {
1092 return method;
1093 }
1094 }
1095 return NULL;
1096}
1097
Mathieu Chartier66f19252012-09-18 08:57:04 -07001098AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001099 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001100 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001101 if (method != NULL) {
1102 return method;
1103 }
1104
1105 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001106 IfTable* iftable = GetIfTable();
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001107 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001108 method = iftable->GetInterface(i)->FindVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001109 if (method != NULL) {
1110 return method;
1111 }
1112 }
1113 return NULL;
1114}
1115
1116
Mathieu Chartier66f19252012-09-18 08:57:04 -07001117AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001118 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001119 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001120 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001121 mh.ChangeMethod(method);
1122 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001123 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001124 }
1125 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001126 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001127}
1128
Mathieu Chartier66f19252012-09-18 08:57:04 -07001129AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001130 if (GetDexCache() == dex_cache) {
1131 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001132 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001133 if (method->GetDexMethodIndex() == dex_method_idx) {
1134 return method;
1135 }
1136 }
1137 }
1138 return NULL;
1139}
1140
Mathieu Chartier66f19252012-09-18 08:57:04 -07001141AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001142 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001143 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001144 if (method != NULL) {
1145 return method;
1146 }
1147 }
1148 return NULL;
1149}
1150
Mathieu Chartier66f19252012-09-18 08:57:04 -07001151AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001152 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001153 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001154 if (method != NULL) {
1155 return method;
1156 }
1157 }
1158 return NULL;
1159}
1160
Mathieu Chartier66f19252012-09-18 08:57:04 -07001161AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001162 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001163 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001164 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001165 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001166 mh.ChangeMethod(method);
1167 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001168 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001169 }
1170 }
1171 return NULL;
1172}
1173
Mathieu Chartier66f19252012-09-18 08:57:04 -07001174AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001175 if (GetDexCache() == dex_cache) {
1176 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001177 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001178 if (method->GetDexMethodIndex() == dex_method_idx) {
1179 return method;
1180 }
1181 }
1182 }
1183 return NULL;
1184}
1185
Mathieu Chartier66f19252012-09-18 08:57:04 -07001186AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001187 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001188 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001189 if (method != NULL) {
1190 return method;
1191 }
1192 }
1193 return NULL;
1194}
1195
Mathieu Chartier66f19252012-09-18 08:57:04 -07001196AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001197 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001198 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001199 if (method != NULL) {
1200 return method;
1201 }
1202 }
1203 return NULL;
1204}
1205
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001206Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001207 // Is the field in this class?
1208 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001209 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001210 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1211 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001212 fh.ChangeField(f);
1213 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001214 return f;
1215 }
1216 }
1217 return NULL;
1218}
1219
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001220Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1221 if (GetDexCache() == dex_cache) {
1222 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1223 Field* f = GetInstanceField(i);
1224 if (f->GetDexFieldIndex() == dex_field_idx) {
1225 return f;
1226 }
1227 }
1228 }
1229 return NULL;
1230}
1231
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001232Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001233 // Is the field in this class, or any of its superclasses?
1234 // Interfaces are not relevant because they can't contain instance fields.
1235 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001236 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001237 if (f != NULL) {
1238 return f;
1239 }
1240 }
1241 return NULL;
1242}
1243
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001244Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1245 // Is the field in this class, or any of its superclasses?
1246 // Interfaces are not relevant because they can't contain instance fields.
1247 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1248 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1249 if (f != NULL) {
1250 return f;
1251 }
1252 }
1253 return NULL;
1254}
1255
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001256Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1257 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001258 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001259 for (size_t i = 0; i < NumStaticFields(); ++i) {
1260 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001261 fh.ChangeField(f);
1262 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001263 return f;
1264 }
1265 }
1266 return NULL;
1267}
1268
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001269Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1270 if (dex_cache == GetDexCache()) {
1271 for (size_t i = 0; i < NumStaticFields(); ++i) {
1272 Field* f = GetStaticField(i);
1273 if (f->GetDexFieldIndex() == dex_field_idx) {
1274 return f;
1275 }
1276 }
1277 }
1278 return NULL;
1279}
1280
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001281Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1282 // Is the field in this class (or its interfaces), or any of its
1283 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001284 ClassHelper kh;
1285 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001286 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001287 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001288 if (f != NULL) {
1289 return f;
1290 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001291 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001292 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001293 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1294 Class* interface = kh.GetDirectInterface(i);
1295 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001296 if (f != NULL) {
1297 return f;
1298 }
1299 }
1300 }
1301 return NULL;
1302}
1303
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001304Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1305 ClassHelper kh;
1306 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1307 // Is the field in this class?
1308 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1309 if (f != NULL) {
1310 return f;
1311 }
1312 // Is this field in any of this class' interfaces?
1313 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001314 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1315 Class* interface = kh.GetDirectInterface(i);
1316 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001317 if (f != NULL) {
1318 return f;
1319 }
1320 }
1321 }
1322 return NULL;
1323}
1324
Ian Rogersb067ac22011-12-13 18:05:09 -08001325Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1326 // Find a field using the JLS field resolution order
1327 ClassHelper kh;
1328 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1329 // Is the field in this class?
1330 Field* f = k->FindDeclaredInstanceField(name, type);
1331 if (f != NULL) {
1332 return f;
1333 }
1334 f = k->FindDeclaredStaticField(name, type);
1335 if (f != NULL) {
1336 return f;
1337 }
1338 // Is this field in any of this class' interfaces?
1339 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001340 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1341 Class* interface = kh.GetDirectInterface(i);
1342 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001343 if (f != NULL) {
1344 return f;
1345 }
1346 }
1347 }
1348 return NULL;
1349}
1350
Ian Rogers50b35e22012-10-04 10:09:15 -07001351Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
1352 size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001353 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001354 DCHECK_GE(component_count, 0);
1355 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001356
Ian Rogersa15e67d2012-02-28 13:51:55 -08001357 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001358 size_t data_size = component_count * component_size;
1359 size_t size = header_size + data_size;
1360
1361 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1362 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1363 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001364 self->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001365 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001366 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001367 return NULL;
1368 }
1369
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001370 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001371 Array* array = down_cast<Array*>(heap->AllocObject(self, array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001372 if (array != NULL) {
1373 DCHECK(array->IsArrayInstance());
1374 array->SetLength(component_count);
1375 }
1376 return array;
1377}
1378
Ian Rogers50b35e22012-10-04 10:09:15 -07001379Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001380 DCHECK(array_class->IsArrayClass());
Ian Rogers50b35e22012-10-04 10:09:15 -07001381 return Alloc(self, array_class, component_count, array_class->GetComponentSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001382}
1383
Ian Rogers64b6d142012-10-29 16:34:15 -07001384// Create a multi-dimensional array of Objects or primitive types.
1385//
1386// We have to generate the names for X[], X[][], X[][][], and so on. The
1387// easiest way to deal with that is to create the full name once and then
1388// subtract pieces off. Besides, we want to start with the outermost
1389// piece and work our way in.
1390// Recursively create an array with multiple dimensions. Elements may be
1391// Objects or primitive types.
1392static Array* RecursiveCreateMultiArray(Thread* self, Class* array_class, int current_dimension,
1393 IntArray* dimensions)
1394 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1395 int32_t array_length = dimensions->Get(current_dimension);
1396 SirtRef<Array> new_array(self, Array::Alloc(self, array_class, array_length));
1397 if (UNLIKELY(new_array.get() == NULL)) {
1398 CHECK(self->IsExceptionPending());
1399 return NULL;
1400 }
1401 if ((current_dimension + 1) < dimensions->GetLength()) {
1402 // Create a new sub-array in every element of the array.
1403 for (int32_t i = 0; i < array_length; i++) {
1404 Array* sub_array = RecursiveCreateMultiArray(self, array_class->GetComponentType(),
1405 current_dimension + 1, dimensions);
1406 if (UNLIKELY(sub_array == NULL)) {
1407 CHECK(self->IsExceptionPending());
1408 return NULL;
1409 }
1410 new_array->AsObjectArray<Array>()->Set(i, sub_array);
1411 }
1412 }
1413 return new_array.get();
1414}
1415
1416Array* Array::CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions) {
1417 // Verify dimensions.
1418 //
1419 // The caller is responsible for verifying that "dimArray" is non-null
1420 // and has a length > 0 and <= 255.
1421 int num_dimensions = dimensions->GetLength();
1422 DCHECK_GT(num_dimensions, 0);
1423 DCHECK_LE(num_dimensions, 255);
1424
1425 for (int i = 0; i < num_dimensions; i++) {
1426 int dimension = dimensions->Get(i);
1427 if (UNLIKELY(dimension < 0)) {
1428 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
1429 "Dimension %d: %d", i, dimension);
1430 return NULL;
1431 }
1432 }
1433
1434 // Generate the full name of the array class.
1435 std::string descriptor(num_dimensions, '[');
1436 descriptor += ClassHelper(element_class).GetDescriptor();
1437
1438 // Find/generate the array class.
1439 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1440 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
1441 if (UNLIKELY(array_class == NULL)) {
1442 CHECK(self->IsExceptionPending());
1443 return NULL;
1444 }
1445 // create the array
1446 Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
1447 if (UNLIKELY(new_array == NULL)) {
1448 CHECK(self->IsExceptionPending());
1449 return NULL;
1450 }
1451 return new_array;
1452}
1453
Elliott Hughes80609252011-09-23 17:24:51 -07001454bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001455 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001456 "length=%i; index=%i", length_, index);
1457 return false;
1458}
1459
1460bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001461 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001462 "Can't store an element of type %s into an array of type %s",
1463 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1464 return false;
1465}
1466
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001467template<typename T>
Ian Rogers50b35e22012-10-04 10:09:15 -07001468PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001469 DCHECK(array_class_ != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -07001470 Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001471 return down_cast<PrimitiveArray<T>*>(raw_array);
1472}
1473
1474template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1475
1476// Explicitly instantiate all the primitive array types.
1477template class PrimitiveArray<uint8_t>; // BooleanArray
1478template class PrimitiveArray<int8_t>; // ByteArray
1479template class PrimitiveArray<uint16_t>; // CharArray
1480template class PrimitiveArray<double>; // DoubleArray
1481template class PrimitiveArray<float>; // FloatArray
1482template class PrimitiveArray<int32_t>; // IntArray
1483template class PrimitiveArray<int64_t>; // LongArray
1484template class PrimitiveArray<int16_t>; // ShortArray
1485
Ian Rogers466bb252011-10-14 03:29:56 -07001486// Explicitly instantiate Class[][]
1487template class ObjectArray<ObjectArray<Class> >;
1488
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001489// TODO: get global references for these
1490Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001491
Brian Carlstroma663ea52011-08-19 23:33:41 -07001492void String::SetClass(Class* java_lang_String) {
1493 CHECK(java_lang_String_ == NULL);
1494 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001495 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001496}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001497
Brian Carlstroma663ea52011-08-19 23:33:41 -07001498void String::ResetClass() {
1499 CHECK(java_lang_String_ != NULL);
1500 java_lang_String_ = NULL;
1501}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001502
Brian Carlstromc74255f2011-09-11 22:47:39 -07001503String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001504 return Runtime::Current()->GetInternTable()->InternWeak(this);
1505}
1506
Brian Carlstrom395520e2011-09-25 19:35:00 -07001507int32_t String::GetHashCode() {
1508 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1509 if (result == 0) {
1510 ComputeHashCode();
1511 }
1512 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1513 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1514 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001515 return result;
1516}
1517
1518int32_t String::GetLength() const {
1519 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1520 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1521 return result;
1522}
1523
1524uint16_t String::CharAt(int32_t index) const {
1525 // TODO: do we need this? Equals is the only caller, and could
1526 // bounds check itself.
1527 if (index < 0 || index >= count_) {
1528 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001529 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001530 "length=%i; index=%i", count_, index);
1531 return 0;
1532 }
1533 return GetCharArray()->Get(index + GetOffset());
1534}
1535
Ian Rogers50b35e22012-10-04 10:09:15 -07001536String* String::AllocFromUtf16(Thread* self,
1537 int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001538 const uint16_t* utf16_data_in,
1539 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001540 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers50b35e22012-10-04 10:09:15 -07001541 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001542 if (string == NULL) {
1543 return NULL;
1544 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001545 // TODO: use 16-bit wide memset variant
1546 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001547 if (array == NULL) {
1548 return NULL;
1549 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001550 for (int i = 0; i < utf16_length; i++) {
1551 array->Set(i, utf16_data_in[i]);
1552 }
1553 if (hash_code != 0) {
1554 string->SetHashCode(hash_code);
1555 } else {
1556 string->ComputeHashCode();
1557 }
1558 return string;
1559}
1560
Ian Rogers50b35e22012-10-04 10:09:15 -07001561 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001562 if (utf == NULL) {
1563 return NULL;
1564 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001565 size_t char_count = CountModifiedUtf8Chars(utf);
Ian Rogers50b35e22012-10-04 10:09:15 -07001566 return AllocFromModifiedUtf8(self, char_count, utf);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001567}
1568
Ian Rogers50b35e22012-10-04 10:09:15 -07001569String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001570 const char* utf8_data_in) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001571 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001572 if (string == NULL) {
1573 return NULL;
1574 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001575 uint16_t* utf16_data_out =
1576 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1577 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1578 string->ComputeHashCode();
1579 return string;
1580}
1581
Ian Rogers50b35e22012-10-04 10:09:15 -07001582String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) {
1583 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001584 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001585 return NULL;
1586 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001587 return Alloc(self, java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001588}
1589
Ian Rogers50b35e22012-10-04 10:09:15 -07001590String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) {
Ian Rogers1f539342012-10-03 21:09:42 -07001591 // Hold reference in case AllocObject causes GC.
Ian Rogers50b35e22012-10-04 10:09:15 -07001592 SirtRef<CharArray> array_ref(self, array);
1593 String* string = down_cast<String*>(java_lang_String->AllocObject(self));
Elliott Hughesb51036c2011-10-12 23:49:11 -07001594 if (string == NULL) {
1595 return NULL;
1596 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001597 string->SetArray(array);
1598 string->SetCount(array->GetLength());
1599 return string;
1600}
1601
1602bool String::Equals(const String* that) const {
1603 if (this == that) {
1604 // Quick reference equality test
1605 return true;
1606 } else if (that == NULL) {
1607 // Null isn't an instanceof anything
1608 return false;
1609 } else if (this->GetLength() != that->GetLength()) {
1610 // Quick length inequality test
1611 return false;
1612 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001613 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001614 // hash code was already equal
1615 for (int32_t i = 0; i < that->GetLength(); ++i) {
1616 if (this->CharAt(i) != that->CharAt(i)) {
1617 return false;
1618 }
1619 }
1620 return true;
1621 }
1622}
1623
Elliott Hughes5d78d392011-12-13 16:53:05 -08001624bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001625 if (this->GetLength() != that_length) {
1626 return false;
1627 } else {
1628 for (int32_t i = 0; i < that_length; ++i) {
1629 if (this->CharAt(i) != that_chars[that_offset + i]) {
1630 return false;
1631 }
1632 }
1633 return true;
1634 }
1635}
1636
1637bool String::Equals(const char* modified_utf8) const {
1638 for (int32_t i = 0; i < GetLength(); ++i) {
1639 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1640 if (ch == '\0' || ch != CharAt(i)) {
1641 return false;
1642 }
1643 }
1644 return *modified_utf8 == '\0';
1645}
1646
1647bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001648 if (modified_utf8.size() != GetLength()) {
1649 return false;
1650 }
1651 const char* p = modified_utf8.data();
1652 for (int32_t i = 0; i < GetLength(); ++i) {
1653 uint16_t ch = GetUtf16FromUtf8(&p);
1654 if (ch != CharAt(i)) {
1655 return false;
1656 }
1657 }
1658 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001659}
1660
1661// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1662std::string String::ToModifiedUtf8() const {
1663 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001664 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001665 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001666 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1667 return result;
1668}
1669
Ian Rogers64b6d142012-10-29 16:34:15 -07001670#ifdef HAVE__MEMCMP16
1671// "count" is in 16-bit units.
1672extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
1673#define MemCmp16 __memcmp16
1674#else
1675static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
1676 for (size_t i = 0; i < count; i++) {
1677 if (s0[i] != s1[i]) {
1678 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
1679 }
1680 }
1681 return 0;
1682}
1683#endif
1684
1685int32_t String::CompareTo(String* rhs) const {
1686 // Quick test for comparison of a string with itself.
1687 const String* lhs = this;
1688 if (lhs == rhs) {
1689 return 0;
1690 }
1691 // TODO: is this still true?
1692 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
1693 // because the interpreter converts the characters to 32-bit integers
1694 // *without* sign extension before it subtracts them (which makes some
1695 // sense since "char" is unsigned). So what we get is the result of
1696 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
1697 int lhsCount = lhs->GetLength();
1698 int rhsCount = rhs->GetLength();
1699 int countDiff = lhsCount - rhsCount;
1700 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
1701 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
1702 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
1703 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
1704 if (otherRes != 0) {
1705 return otherRes;
1706 }
1707 return countDiff;
1708}
1709
Ian Rogers1c5eb702012-02-01 09:18:34 -08001710void Throwable::SetCause(Throwable* cause) {
1711 CHECK(cause != NULL);
1712 CHECK(cause != this);
1713 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1714 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1715}
1716
Ian Rogers466bb252011-10-14 03:29:56 -07001717bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001718 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001719 return false;
1720 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001721 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001722}
1723
Ian Rogers9074b992011-10-26 17:41:55 -07001724std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001725 std::string result(PrettyTypeOf(this));
1726 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001727 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001728 if (msg != NULL) {
1729 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001730 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001731 result += "\n";
1732 Object* stack_state = GetStackState();
1733 // check stack state isn't missing or corrupt
1734 if (stack_state != NULL && stack_state->IsObjectArray()) {
1735 // Decode the internal stack trace into the depth and method trace
1736 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1737 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001738 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1739 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001740 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001741 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001742 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001743 uint32_t dex_pc = pc_trace->Get(i);
1744 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001745 const char* source_file = mh.GetDeclaringClassSourceFile();
1746 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1747 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001748 }
Ian Rogers9074b992011-10-26 17:41:55 -07001749 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001750 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001751 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001752 result += "Caused by: ";
1753 result += cause->Dump();
1754 }
Ian Rogers9074b992011-10-26 17:41:55 -07001755 return result;
1756}
1757
Ian Rogers5167c972012-02-03 10:41:20 -08001758
1759Class* Throwable::java_lang_Throwable_ = NULL;
1760
1761void Throwable::SetClass(Class* java_lang_Throwable) {
1762 CHECK(java_lang_Throwable_ == NULL);
1763 CHECK(java_lang_Throwable != NULL);
1764 java_lang_Throwable_ = java_lang_Throwable;
1765}
1766
1767void Throwable::ResetClass() {
1768 CHECK(java_lang_Throwable_ != NULL);
1769 java_lang_Throwable_ = NULL;
1770}
1771
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001772Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1773
1774void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1775 CHECK(java_lang_StackTraceElement_ == NULL);
1776 CHECK(java_lang_StackTraceElement != NULL);
1777 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1778}
1779
1780void StackTraceElement::ResetClass() {
1781 CHECK(java_lang_StackTraceElement_ != NULL);
1782 java_lang_StackTraceElement_ = NULL;
1783}
1784
Ian Rogers50b35e22012-10-04 10:09:15 -07001785StackTraceElement* StackTraceElement::Alloc(Thread* self,
1786 String* declaring_class,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001787 String* method_name,
1788 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001789 int32_t line_number) {
1790 StackTraceElement* trace =
Ian Rogers50b35e22012-10-04 10:09:15 -07001791 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject(self));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001792 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1793 const_cast<String*>(declaring_class), false);
1794 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1795 const_cast<String*>(method_name), false);
1796 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1797 const_cast<String*>(file_name), false);
1798 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1799 line_number, false);
1800 return trace;
1801}
1802
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001803} // namespace art