blob: 210c42d54f63134a679414848787feec24d0408f [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);
jeffhaod7521322012-11-21 15:38:24 -0800236 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);
jeffhaod7521322012-11-21 15:38:24 -0800242 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);
jeffhaod7521322012-11-21 15:38:24 -0800248 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);
jeffhaod7521322012-11-21 15:38:24 -0800254 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);
jeffhaod7521322012-11-21 15:38:24 -0800260 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);
jeffhaod7521322012-11-21 15:38:24 -0800266 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.
jeffhao725a9572012-11-13 18:20:12 -0800543 if (runtime->IsMethodTracingActive() && runtime->GetInstrumentation()->GetSavedCodeFromMap(m) != NULL) {
544 code = runtime->GetInstrumentation()->GetSavedCodeFromMap(m);
Elliott Hughes168670b2012-02-29 16:43:26 -0800545 }
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
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700660 if (UNLIKELY(!Runtime::Current()->IsStarted())){
Ian Rogers50ffee22012-11-20 11:47:44 -0800661 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started";
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700662 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700663 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700664 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700665 } else {
jeffhaod7521322012-11-21 15:38:24 -0800666 bool interpret = self->ReadFlag(kEnterInterpreter) && !IsNative() && !IsProxyMethod();
Ian Rogers50ffee22012-11-20 11:47:44 -0800667 const bool kLogInvocationStartAndReturn = false;
668 if (!interpret && GetCode() != NULL && stub != NULL) {
669 if (kLogInvocationStartAndReturn) {
670 LOG(INFO) << StringPrintf("Invoking '%s' code=%p stub=%p",
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700671 PrettyMethod(this).c_str(), GetCode(), stub);
672 }
673 (*stub)(this, receiver, self, args, result);
Ian Rogers50ffee22012-11-20 11:47:44 -0800674 if (kLogInvocationStartAndReturn) {
675 LOG(INFO) << StringPrintf("Returned '%s' code=%p stub=%p",
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700676 PrettyMethod(this).c_str(), GetCode(), stub);
677 }
678 } else {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700679 const bool kInterpretMethodsWithNoCode = false;
Ian Rogers50ffee22012-11-20 11:47:44 -0800680 if (interpret || kInterpretMethodsWithNoCode) {
681 if (kLogInvocationStartAndReturn) {
682 LOG(INFO) << "Interpreting " << PrettyMethod(this) << "'";
683 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700684 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args, result);
Ian Rogers50ffee22012-11-20 11:47:44 -0800685 if (kLogInvocationStartAndReturn) {
686 LOG(INFO) << "Returned '" << PrettyMethod(this) << "'";
687 }
688 } else {
689 LOG(INFO) << "Not invoking '" << PrettyMethod(this)
690 << "' code=" << reinterpret_cast<const void*>(GetCode())
691 << " stub=" << reinterpret_cast<void*>(stub);
692 if (result != NULL) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700693 result->SetJ(0);
Ian Rogers50ffee22012-11-20 11:47:44 -0800694 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700695 }
696 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700697 }
698
699 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700700 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700701}
702
Mathieu Chartier66f19252012-09-18 08:57:04 -0700703bool AbstractMethod::IsRegistered() const {
704 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800705 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800706 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700707 return native_method != jni_stub;
708}
709
Mathieu Chartier66f19252012-09-18 08:57:04 -0700710void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800711 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700712 CHECK(IsNative()) << PrettyMethod(this);
713 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700714#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700715 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700716 native_method, false);
717#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800718 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
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 native_method, false);
721 } else {
722 // We've been asked to associate this method with the given native method but are working
723 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
724 // the native method to runtime support and store the target somewhere runtime support will
725 // find it.
726#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700727 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800728 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
729#else
730 UNIMPLEMENTED(FATAL);
731#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700732 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800733 reinterpret_cast<const uint8_t*>(native_method), false);
734 }
TDYa12726467572012-04-17 20:51:22 -0700735#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700736}
737
Mathieu Chartier66f19252012-09-18 08:57:04 -0700738void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700739 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700740 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800741 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700742}
743
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700744Class* Class::java_lang_Class_ = NULL;
745
746void Class::SetClassClass(Class* java_lang_Class) {
747 CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class;
748 CHECK(java_lang_Class != NULL);
749 java_lang_Class_ = java_lang_Class;
750}
751
752void Class::ResetClass() {
753 CHECK(java_lang_Class_ != NULL);
754 java_lang_Class_ = NULL;
755}
756
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700757void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700758 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
759 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700760 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700761 if (new_status > kStatusResolved) {
762 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
763 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800764 if (new_status == kStatusError) {
765 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
766
767 // stash current exception
768 Thread* self = Thread::Current();
Ian Rogers1f539342012-10-03 21:09:42 -0700769 SirtRef<Throwable> exception(self, self->GetException());
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800770 CHECK(exception.get() != NULL);
771
772 // clear exception to call FindSystemClass
773 self->ClearException();
774 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
775 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
776 CHECK(!self->IsExceptionPending());
777
778 // only verification errors, not initialization problems, should set a verify error.
779 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
780 Class* exception_class = exception->GetClass();
781 if (!eiie_class->IsAssignableFrom(exception_class)) {
782 SetVerifyErrorClass(exception_class);
783 }
784
785 // restore exception
786 self->SetException(exception.get());
787 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700788 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700789}
790
791DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700792 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700793}
794
795void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700796 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700797}
798
Ian Rogers50b35e22012-10-04 10:09:15 -0700799Object* Class::AllocObject(Thread* self) {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700800 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700801 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500802 // TODO: decide whether we want this check. It currently fails during bootstrap.
803 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700804 DCHECK_GE(this->object_size_, sizeof(Object));
Ian Rogers50b35e22012-10-04 10:09:15 -0700805 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700806}
807
Ian Rogers0571d352011-11-03 19:51:38 -0700808void Class::SetClassSize(size_t new_class_size) {
809 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
810 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
811}
812
Ian Rogersd418eda2012-01-30 12:14:28 -0800813// Return the class' name. The exact format is bizarre, but it's the specified behavior for
814// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
815// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
816// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
817String* Class::ComputeName() {
818 String* name = GetName();
819 if (name != NULL) {
820 return name;
821 }
822 std::string descriptor(ClassHelper(this).GetDescriptor());
823 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
824 // The descriptor indicates that this is the class for
825 // a primitive type; special-case the return value.
826 const char* c_name = NULL;
827 switch (descriptor[0]) {
828 case 'Z': c_name = "boolean"; break;
829 case 'B': c_name = "byte"; break;
830 case 'C': c_name = "char"; break;
831 case 'S': c_name = "short"; break;
832 case 'I': c_name = "int"; break;
833 case 'J': c_name = "long"; break;
834 case 'F': c_name = "float"; break;
835 case 'D': c_name = "double"; break;
836 case 'V': c_name = "void"; break;
837 default:
838 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
839 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700840 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
Ian Rogersd418eda2012-01-30 12:14:28 -0800841 } else {
842 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
843 // components.
844 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
845 descriptor.erase(0, 1);
846 descriptor.erase(descriptor.size() - 1);
847 }
848 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Ian Rogers50b35e22012-10-04 10:09:15 -0700849 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
Ian Rogersd418eda2012-01-30 12:14:28 -0800850 }
851 SetName(name);
852 return name;
853}
854
Elliott Hughes4681c802011-09-25 18:04:37 -0700855void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700856 if ((flags & kDumpClassFullDetail) == 0) {
857 os << PrettyClass(this);
858 if ((flags & kDumpClassClassLoader) != 0) {
859 os << ' ' << GetClassLoader();
860 }
861 if ((flags & kDumpClassInitialized) != 0) {
862 os << ' ' << GetStatus();
863 }
Elliott Hughese0918552011-10-28 17:18:29 -0700864 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700865 return;
866 }
867
868 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800869 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700870 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800871 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700872 os << " objectSize=" << SizeOf() << " "
873 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
874 os << StringPrintf(" access=0x%04x.%04x\n",
875 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
876 if (super != NULL) {
877 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
878 }
879 if (IsArrayClass()) {
880 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
881 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700882 if (kh.NumDirectInterfaces() > 0) {
883 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
884 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
885 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700886 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800887 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700888 }
889 }
890 os << " vtable (" << NumVirtualMethods() << " entries, "
891 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
892 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800893 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700894 }
895 os << " direct methods (" << NumDirectMethods() << " entries):\n";
896 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800897 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700898 }
899 if (NumStaticFields() > 0) {
900 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700901 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700902 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800903 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700904 }
905 } else {
906 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700907 }
908 }
909 if (NumInstanceFields() > 0) {
910 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700911 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700912 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800913 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700914 }
915 } else {
916 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700917 }
918 }
919}
920
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700921void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
922 if (new_reference_offsets != CLASS_WALK_SUPER) {
923 // Sanity check that the number of bits set in the reference offset bitmap
924 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800925 size_t count = 0;
926 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
927 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700928 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800929 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700930 }
931 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
932 new_reference_offsets, false);
933}
934
935void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
936 if (new_reference_offsets != CLASS_WALK_SUPER) {
937 // Sanity check that the number of bits set in the reference offset bitmap
938 // agrees with the number of references
939 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
940 NumReferenceStaticFieldsDuringLinking());
941 }
942 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
943 new_reference_offsets, false);
944}
945
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700946bool Class::Implements(const Class* klass) const {
947 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700948 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700949 // All interfaces implemented directly and by our superclass, and
950 // recursively all super-interfaces of those interfaces, are listed
951 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700952 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -0700953 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700954 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700955 if (iftable->GetInterface(i) == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700956 return true;
957 }
958 }
959 return false;
960}
961
Elliott Hughese84278b2012-03-22 10:06:53 -0700962// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700963// are array classes.
964//
965// Consider an array class, e.g. Y[][], where Y is a subclass of X.
966// Y[][] = Y[][] --> true (identity)
967// X[][] = Y[][] --> true (element superclass)
968// Y = Y[][] --> false
969// Y[] = Y[][] --> false
970// Object = Y[][] --> true (everything is an object)
971// Object[] = Y[][] --> true
972// Object[][] = Y[][] --> true
973// Object[][][] = Y[][] --> false (too many []s)
974// Serializable = Y[][] --> true (all arrays are Serializable)
975// Serializable[] = Y[][] --> true
976// Serializable[][] = Y[][] --> false (unless Y is Serializable)
977//
978// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700979// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700980//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700981bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700982 DCHECK(IsArrayClass()) << PrettyClass(this);
983 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700984 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700985}
986
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700987bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700988 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
989 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700990 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700991 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700992 // src's super should be java_lang_Object, since it is an array.
993 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700994 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
995 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700996 return this == java_lang_Object;
997 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700998 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700999}
1000
1001bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001002 DCHECK(!IsInterface()) << PrettyClass(this);
1003 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001004 const Class* current = this;
1005 do {
1006 if (current == klass) {
1007 return true;
1008 }
1009 current = current->GetSuperClass();
1010 } while (current != NULL);
1011 return false;
1012}
1013
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001014bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001015 size_t i = 0;
1016 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
1017 ++i;
1018 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001019 if (descriptor1.find('/', i) != StringPiece::npos ||
1020 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001021 return false;
1022 } else {
1023 return true;
1024 }
1025}
1026
1027bool Class::IsInSamePackage(const Class* that) const {
1028 const Class* klass1 = this;
1029 const Class* klass2 = that;
1030 if (klass1 == klass2) {
1031 return true;
1032 }
1033 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001034 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001035 return false;
1036 }
1037 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -07001038 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001039 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001040 }
jeffhao4a801a42011-09-23 13:53:40 -07001041 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001043 }
1044 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001045 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -08001046 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001047 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -08001048 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001049 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001050}
1051
Elliott Hughesdbb40792011-11-18 17:05:22 -08001052bool Class::IsClassClass() const {
1053 Class* java_lang_Class = GetClass()->GetClass();
1054 return this == java_lang_Class;
1055}
1056
1057bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001058 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001059}
1060
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001061bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001062 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001063}
1064
Ian Rogers64b6d142012-10-29 16:34:15 -07001065bool Class::IsFieldClass() const {
1066 Class* java_lang_Class = GetClass();
1067 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
1068 return this == java_lang_reflect_Field;
1069
1070}
1071
1072bool Class::IsMethodClass() const {
1073 return (this == AbstractMethod::GetMethodClass()) ||
1074 (this == AbstractMethod::GetConstructorClass());
1075
1076}
1077
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001078ClassLoader* Class::GetClassLoader() const {
1079 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -07001080}
1081
Ian Rogers365c1022012-06-22 15:05:28 -07001082void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001084}
1085
Mathieu Chartier66f19252012-09-18 08:57:04 -07001086AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001087 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001088 DCHECK(declaring_class != NULL) << PrettyClass(this);
1089 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001090 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001091 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001092 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001093 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001094 if (iftable->GetInterface(i) == declaring_class) {
1095 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001096 }
1097 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001098 return NULL;
1099}
1100
Mathieu Chartier66f19252012-09-18 08:57:04 -07001101AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001102 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001103 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001104 if (method != NULL) {
1105 return method;
1106 }
1107
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001108 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001109 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001110 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001111 method = iftable->GetInterface(i)->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001112 if (method != NULL) {
1113 return method;
1114 }
1115 }
1116 return NULL;
1117}
1118
Mathieu Chartier66f19252012-09-18 08:57:04 -07001119AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001120 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001121 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001122 if (method != NULL) {
1123 return method;
1124 }
1125
1126 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001127 IfTable* iftable = GetIfTable();
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001128 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001129 method = iftable->GetInterface(i)->FindVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001130 if (method != NULL) {
1131 return method;
1132 }
1133 }
1134 return NULL;
1135}
1136
1137
Mathieu Chartier66f19252012-09-18 08:57:04 -07001138AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001139 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001140 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001141 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001142 mh.ChangeMethod(method);
1143 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001144 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001145 }
1146 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001147 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001148}
1149
Mathieu Chartier66f19252012-09-18 08:57:04 -07001150AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001151 if (GetDexCache() == dex_cache) {
1152 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001153 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001154 if (method->GetDexMethodIndex() == dex_method_idx) {
1155 return method;
1156 }
1157 }
1158 }
1159 return NULL;
1160}
1161
Mathieu Chartier66f19252012-09-18 08:57:04 -07001162AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001163 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001164 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001165 if (method != NULL) {
1166 return method;
1167 }
1168 }
1169 return NULL;
1170}
1171
Mathieu Chartier66f19252012-09-18 08:57:04 -07001172AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001173 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001174 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001175 if (method != NULL) {
1176 return method;
1177 }
1178 }
1179 return NULL;
1180}
1181
Mathieu Chartier66f19252012-09-18 08:57:04 -07001182AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001183 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001184 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001185 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001186 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001187 mh.ChangeMethod(method);
1188 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001189 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001190 }
1191 }
1192 return NULL;
1193}
1194
Mathieu Chartier66f19252012-09-18 08:57:04 -07001195AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001196 if (GetDexCache() == dex_cache) {
1197 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001198 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001199 if (method->GetDexMethodIndex() == dex_method_idx) {
1200 return method;
1201 }
1202 }
1203 }
1204 return NULL;
1205}
1206
Mathieu Chartier66f19252012-09-18 08:57:04 -07001207AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001208 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001209 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001210 if (method != NULL) {
1211 return method;
1212 }
1213 }
1214 return NULL;
1215}
1216
Mathieu Chartier66f19252012-09-18 08:57:04 -07001217AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001218 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001219 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001220 if (method != NULL) {
1221 return method;
1222 }
1223 }
1224 return NULL;
1225}
1226
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001227Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001228 // Is the field in this class?
1229 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001230 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001231 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1232 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001233 fh.ChangeField(f);
1234 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001235 return f;
1236 }
1237 }
1238 return NULL;
1239}
1240
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001241Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1242 if (GetDexCache() == dex_cache) {
1243 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1244 Field* f = GetInstanceField(i);
1245 if (f->GetDexFieldIndex() == dex_field_idx) {
1246 return f;
1247 }
1248 }
1249 }
1250 return NULL;
1251}
1252
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001253Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001254 // Is the field in this class, or any of its superclasses?
1255 // Interfaces are not relevant because they can't contain instance fields.
1256 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001257 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001258 if (f != NULL) {
1259 return f;
1260 }
1261 }
1262 return NULL;
1263}
1264
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001265Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1266 // Is the field in this class, or any of its superclasses?
1267 // Interfaces are not relevant because they can't contain instance fields.
1268 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1269 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1270 if (f != NULL) {
1271 return f;
1272 }
1273 }
1274 return NULL;
1275}
1276
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001277Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1278 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001279 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001280 for (size_t i = 0; i < NumStaticFields(); ++i) {
1281 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001282 fh.ChangeField(f);
1283 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001284 return f;
1285 }
1286 }
1287 return NULL;
1288}
1289
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001290Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1291 if (dex_cache == GetDexCache()) {
1292 for (size_t i = 0; i < NumStaticFields(); ++i) {
1293 Field* f = GetStaticField(i);
1294 if (f->GetDexFieldIndex() == dex_field_idx) {
1295 return f;
1296 }
1297 }
1298 }
1299 return NULL;
1300}
1301
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001302Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1303 // Is the field in this class (or its interfaces), or any of its
1304 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001305 ClassHelper kh;
1306 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001307 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001308 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001309 if (f != NULL) {
1310 return f;
1311 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001312 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001313 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(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001317 if (f != NULL) {
1318 return f;
1319 }
1320 }
1321 }
1322 return NULL;
1323}
1324
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001325Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1326 ClassHelper kh;
1327 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1328 // Is the field in this class?
1329 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1330 if (f != NULL) {
1331 return f;
1332 }
1333 // Is this field in any of this class' interfaces?
1334 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001335 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1336 Class* interface = kh.GetDirectInterface(i);
1337 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001338 if (f != NULL) {
1339 return f;
1340 }
1341 }
1342 }
1343 return NULL;
1344}
1345
Ian Rogersb067ac22011-12-13 18:05:09 -08001346Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1347 // Find a field using the JLS field resolution order
1348 ClassHelper kh;
1349 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1350 // Is the field in this class?
1351 Field* f = k->FindDeclaredInstanceField(name, type);
1352 if (f != NULL) {
1353 return f;
1354 }
1355 f = k->FindDeclaredStaticField(name, type);
1356 if (f != NULL) {
1357 return f;
1358 }
1359 // Is this field in any of this class' interfaces?
1360 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001361 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1362 Class* interface = kh.GetDirectInterface(i);
1363 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001364 if (f != NULL) {
1365 return f;
1366 }
1367 }
1368 }
1369 return NULL;
1370}
1371
Ian Rogers50b35e22012-10-04 10:09:15 -07001372Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
1373 size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001374 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001375 DCHECK_GE(component_count, 0);
1376 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001377
Ian Rogersa15e67d2012-02-28 13:51:55 -08001378 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001379 size_t data_size = component_count * component_size;
1380 size_t size = header_size + data_size;
1381
1382 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1383 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1384 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001385 self->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001386 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001387 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001388 return NULL;
1389 }
1390
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001391 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001392 Array* array = down_cast<Array*>(heap->AllocObject(self, array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001393 if (array != NULL) {
1394 DCHECK(array->IsArrayInstance());
1395 array->SetLength(component_count);
1396 }
1397 return array;
1398}
1399
Ian Rogers50b35e22012-10-04 10:09:15 -07001400Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001401 DCHECK(array_class->IsArrayClass());
Ian Rogers50b35e22012-10-04 10:09:15 -07001402 return Alloc(self, array_class, component_count, array_class->GetComponentSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001403}
1404
Ian Rogers64b6d142012-10-29 16:34:15 -07001405// Create a multi-dimensional array of Objects or primitive types.
1406//
1407// We have to generate the names for X[], X[][], X[][][], and so on. The
1408// easiest way to deal with that is to create the full name once and then
1409// subtract pieces off. Besides, we want to start with the outermost
1410// piece and work our way in.
1411// Recursively create an array with multiple dimensions. Elements may be
1412// Objects or primitive types.
1413static Array* RecursiveCreateMultiArray(Thread* self, Class* array_class, int current_dimension,
1414 IntArray* dimensions)
1415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1416 int32_t array_length = dimensions->Get(current_dimension);
1417 SirtRef<Array> new_array(self, Array::Alloc(self, array_class, array_length));
1418 if (UNLIKELY(new_array.get() == NULL)) {
1419 CHECK(self->IsExceptionPending());
1420 return NULL;
1421 }
1422 if ((current_dimension + 1) < dimensions->GetLength()) {
1423 // Create a new sub-array in every element of the array.
1424 for (int32_t i = 0; i < array_length; i++) {
1425 Array* sub_array = RecursiveCreateMultiArray(self, array_class->GetComponentType(),
1426 current_dimension + 1, dimensions);
1427 if (UNLIKELY(sub_array == NULL)) {
1428 CHECK(self->IsExceptionPending());
1429 return NULL;
1430 }
1431 new_array->AsObjectArray<Array>()->Set(i, sub_array);
1432 }
1433 }
1434 return new_array.get();
1435}
1436
1437Array* Array::CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions) {
1438 // Verify dimensions.
1439 //
1440 // The caller is responsible for verifying that "dimArray" is non-null
1441 // and has a length > 0 and <= 255.
1442 int num_dimensions = dimensions->GetLength();
1443 DCHECK_GT(num_dimensions, 0);
1444 DCHECK_LE(num_dimensions, 255);
1445
1446 for (int i = 0; i < num_dimensions; i++) {
1447 int dimension = dimensions->Get(i);
1448 if (UNLIKELY(dimension < 0)) {
1449 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
1450 "Dimension %d: %d", i, dimension);
1451 return NULL;
1452 }
1453 }
1454
1455 // Generate the full name of the array class.
1456 std::string descriptor(num_dimensions, '[');
1457 descriptor += ClassHelper(element_class).GetDescriptor();
1458
1459 // Find/generate the array class.
1460 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1461 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
1462 if (UNLIKELY(array_class == NULL)) {
1463 CHECK(self->IsExceptionPending());
1464 return NULL;
1465 }
1466 // create the array
1467 Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
1468 if (UNLIKELY(new_array == NULL)) {
1469 CHECK(self->IsExceptionPending());
1470 return NULL;
1471 }
1472 return new_array;
1473}
1474
Elliott Hughes80609252011-09-23 17:24:51 -07001475bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001476 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001477 "length=%i; index=%i", length_, index);
1478 return false;
1479}
1480
1481bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001482 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
jeffhao94d6df42012-11-26 16:02:12 -08001483 "%s cannot be stored in an array of type %s",
Elliott Hughes80609252011-09-23 17:24:51 -07001484 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1485 return false;
1486}
1487
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001488template<typename T>
Ian Rogers50b35e22012-10-04 10:09:15 -07001489PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001490 DCHECK(array_class_ != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -07001491 Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001492 return down_cast<PrimitiveArray<T>*>(raw_array);
1493}
1494
1495template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1496
1497// Explicitly instantiate all the primitive array types.
1498template class PrimitiveArray<uint8_t>; // BooleanArray
1499template class PrimitiveArray<int8_t>; // ByteArray
1500template class PrimitiveArray<uint16_t>; // CharArray
1501template class PrimitiveArray<double>; // DoubleArray
1502template class PrimitiveArray<float>; // FloatArray
1503template class PrimitiveArray<int32_t>; // IntArray
1504template class PrimitiveArray<int64_t>; // LongArray
1505template class PrimitiveArray<int16_t>; // ShortArray
1506
Ian Rogers466bb252011-10-14 03:29:56 -07001507// Explicitly instantiate Class[][]
1508template class ObjectArray<ObjectArray<Class> >;
1509
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001510// TODO: get global references for these
1511Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001512
Brian Carlstroma663ea52011-08-19 23:33:41 -07001513void String::SetClass(Class* java_lang_String) {
1514 CHECK(java_lang_String_ == NULL);
1515 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001516 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001517}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001518
Brian Carlstroma663ea52011-08-19 23:33:41 -07001519void String::ResetClass() {
1520 CHECK(java_lang_String_ != NULL);
1521 java_lang_String_ = NULL;
1522}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001523
Brian Carlstromc74255f2011-09-11 22:47:39 -07001524String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001525 return Runtime::Current()->GetInternTable()->InternWeak(this);
1526}
1527
Brian Carlstrom395520e2011-09-25 19:35:00 -07001528int32_t String::GetHashCode() {
1529 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1530 if (result == 0) {
1531 ComputeHashCode();
1532 }
1533 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1534 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1535 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001536 return result;
1537}
1538
1539int32_t String::GetLength() const {
1540 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1541 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1542 return result;
1543}
1544
1545uint16_t String::CharAt(int32_t index) const {
1546 // TODO: do we need this? Equals is the only caller, and could
1547 // bounds check itself.
1548 if (index < 0 || index >= count_) {
1549 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001550 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001551 "length=%i; index=%i", count_, index);
1552 return 0;
1553 }
1554 return GetCharArray()->Get(index + GetOffset());
1555}
1556
Ian Rogers50b35e22012-10-04 10:09:15 -07001557String* String::AllocFromUtf16(Thread* self,
1558 int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001559 const uint16_t* utf16_data_in,
1560 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001561 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers50b35e22012-10-04 10:09:15 -07001562 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001563 if (string == NULL) {
1564 return NULL;
1565 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001566 // TODO: use 16-bit wide memset variant
1567 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001568 if (array == NULL) {
1569 return NULL;
1570 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001571 for (int i = 0; i < utf16_length; i++) {
1572 array->Set(i, utf16_data_in[i]);
1573 }
1574 if (hash_code != 0) {
1575 string->SetHashCode(hash_code);
1576 } else {
1577 string->ComputeHashCode();
1578 }
1579 return string;
1580}
1581
Ian Rogers50b35e22012-10-04 10:09:15 -07001582 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001583 if (utf == NULL) {
1584 return NULL;
1585 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001586 size_t char_count = CountModifiedUtf8Chars(utf);
Ian Rogers50b35e22012-10-04 10:09:15 -07001587 return AllocFromModifiedUtf8(self, char_count, utf);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001588}
1589
Ian Rogers50b35e22012-10-04 10:09:15 -07001590String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001591 const char* utf8_data_in) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001592 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001593 if (string == NULL) {
1594 return NULL;
1595 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001596 uint16_t* utf16_data_out =
1597 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1598 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1599 string->ComputeHashCode();
1600 return string;
1601}
1602
Ian Rogers50b35e22012-10-04 10:09:15 -07001603String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) {
1604 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001605 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001606 return NULL;
1607 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001608 return Alloc(self, java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001609}
1610
Ian Rogers50b35e22012-10-04 10:09:15 -07001611String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) {
Ian Rogers1f539342012-10-03 21:09:42 -07001612 // Hold reference in case AllocObject causes GC.
Ian Rogers50b35e22012-10-04 10:09:15 -07001613 SirtRef<CharArray> array_ref(self, array);
1614 String* string = down_cast<String*>(java_lang_String->AllocObject(self));
Elliott Hughesb51036c2011-10-12 23:49:11 -07001615 if (string == NULL) {
1616 return NULL;
1617 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001618 string->SetArray(array);
1619 string->SetCount(array->GetLength());
1620 return string;
1621}
1622
1623bool String::Equals(const String* that) const {
1624 if (this == that) {
1625 // Quick reference equality test
1626 return true;
1627 } else if (that == NULL) {
1628 // Null isn't an instanceof anything
1629 return false;
1630 } else if (this->GetLength() != that->GetLength()) {
1631 // Quick length inequality test
1632 return false;
1633 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001634 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001635 // hash code was already equal
1636 for (int32_t i = 0; i < that->GetLength(); ++i) {
1637 if (this->CharAt(i) != that->CharAt(i)) {
1638 return false;
1639 }
1640 }
1641 return true;
1642 }
1643}
1644
Elliott Hughes5d78d392011-12-13 16:53:05 -08001645bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001646 if (this->GetLength() != that_length) {
1647 return false;
1648 } else {
1649 for (int32_t i = 0; i < that_length; ++i) {
1650 if (this->CharAt(i) != that_chars[that_offset + i]) {
1651 return false;
1652 }
1653 }
1654 return true;
1655 }
1656}
1657
1658bool String::Equals(const char* modified_utf8) const {
1659 for (int32_t i = 0; i < GetLength(); ++i) {
1660 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1661 if (ch == '\0' || ch != CharAt(i)) {
1662 return false;
1663 }
1664 }
1665 return *modified_utf8 == '\0';
1666}
1667
1668bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001669 if (modified_utf8.size() != GetLength()) {
1670 return false;
1671 }
1672 const char* p = modified_utf8.data();
1673 for (int32_t i = 0; i < GetLength(); ++i) {
1674 uint16_t ch = GetUtf16FromUtf8(&p);
1675 if (ch != CharAt(i)) {
1676 return false;
1677 }
1678 }
1679 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001680}
1681
1682// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1683std::string String::ToModifiedUtf8() const {
1684 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001685 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001686 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001687 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1688 return result;
1689}
1690
Ian Rogers64b6d142012-10-29 16:34:15 -07001691#ifdef HAVE__MEMCMP16
1692// "count" is in 16-bit units.
1693extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
1694#define MemCmp16 __memcmp16
1695#else
1696static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
1697 for (size_t i = 0; i < count; i++) {
1698 if (s0[i] != s1[i]) {
1699 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
1700 }
1701 }
1702 return 0;
1703}
1704#endif
1705
1706int32_t String::CompareTo(String* rhs) const {
1707 // Quick test for comparison of a string with itself.
1708 const String* lhs = this;
1709 if (lhs == rhs) {
1710 return 0;
1711 }
1712 // TODO: is this still true?
1713 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
1714 // because the interpreter converts the characters to 32-bit integers
1715 // *without* sign extension before it subtracts them (which makes some
1716 // sense since "char" is unsigned). So what we get is the result of
1717 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
1718 int lhsCount = lhs->GetLength();
1719 int rhsCount = rhs->GetLength();
1720 int countDiff = lhsCount - rhsCount;
1721 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
1722 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
1723 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
1724 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
1725 if (otherRes != 0) {
1726 return otherRes;
1727 }
1728 return countDiff;
1729}
1730
Ian Rogers1c5eb702012-02-01 09:18:34 -08001731void Throwable::SetCause(Throwable* cause) {
1732 CHECK(cause != NULL);
1733 CHECK(cause != this);
1734 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1735 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1736}
1737
Ian Rogers466bb252011-10-14 03:29:56 -07001738bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001739 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001740 return false;
1741 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001742 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001743}
1744
Ian Rogers9074b992011-10-26 17:41:55 -07001745std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001746 std::string result(PrettyTypeOf(this));
1747 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001748 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001749 if (msg != NULL) {
1750 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001751 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001752 result += "\n";
1753 Object* stack_state = GetStackState();
1754 // check stack state isn't missing or corrupt
1755 if (stack_state != NULL && stack_state->IsObjectArray()) {
1756 // Decode the internal stack trace into the depth and method trace
1757 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1758 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001759 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1760 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001761 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001762 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001763 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001764 uint32_t dex_pc = pc_trace->Get(i);
1765 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001766 const char* source_file = mh.GetDeclaringClassSourceFile();
1767 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1768 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001769 }
Ian Rogers9074b992011-10-26 17:41:55 -07001770 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001771 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001772 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001773 result += "Caused by: ";
1774 result += cause->Dump();
1775 }
Ian Rogers9074b992011-10-26 17:41:55 -07001776 return result;
1777}
1778
Ian Rogers5167c972012-02-03 10:41:20 -08001779
1780Class* Throwable::java_lang_Throwable_ = NULL;
1781
1782void Throwable::SetClass(Class* java_lang_Throwable) {
1783 CHECK(java_lang_Throwable_ == NULL);
1784 CHECK(java_lang_Throwable != NULL);
1785 java_lang_Throwable_ = java_lang_Throwable;
1786}
1787
1788void Throwable::ResetClass() {
1789 CHECK(java_lang_Throwable_ != NULL);
1790 java_lang_Throwable_ = NULL;
1791}
1792
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001793Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1794
1795void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1796 CHECK(java_lang_StackTraceElement_ == NULL);
1797 CHECK(java_lang_StackTraceElement != NULL);
1798 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1799}
1800
1801void StackTraceElement::ResetClass() {
1802 CHECK(java_lang_StackTraceElement_ != NULL);
1803 java_lang_StackTraceElement_ = NULL;
1804}
1805
Ian Rogers50b35e22012-10-04 10:09:15 -07001806StackTraceElement* StackTraceElement::Alloc(Thread* self,
1807 String* declaring_class,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001808 String* method_name,
1809 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001810 int32_t line_number) {
1811 StackTraceElement* trace =
Ian Rogers50b35e22012-10-04 10:09:15 -07001812 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject(self));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001813 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1814 const_cast<String*>(declaring_class), false);
1815 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1816 const_cast<String*>(method_name), false);
1817 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1818 const_cast<String*>(file_name), false);
1819 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1820 line_number, false);
1821 return trace;
1822}
1823
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001824} // namespace art