blob: cebbb2a06d80a60f70f90c8f16b9bcd841c24a4a [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Ian Rogers64b6d142012-10-29 16:34:15 -070033#include "interpreter/interpreter.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070035#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080036#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070037#include "runtime.h"
Ian Rogers60db5ab2012-02-20 17:02:00 -080038#include "runtime_support.h"
Ian Rogers1f539342012-10-03 21:09:42 -070039#include "sirt_ref.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070040#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070041#include "utils.h"
Elliott Hughesa4f94742012-05-29 16:28:38 -070042#include "well_known_classes.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070043
44namespace art {
45
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070046BooleanArray* Object::AsBooleanArray() {
47 DCHECK(GetClass()->IsArrayClass());
48 DCHECK(GetClass()->GetComponentType()->IsPrimitiveBoolean());
49 return down_cast<BooleanArray*>(this);
50}
51
52ByteArray* Object::AsByteArray() {
53 DCHECK(GetClass()->IsArrayClass());
54 DCHECK(GetClass()->GetComponentType()->IsPrimitiveByte());
55 return down_cast<ByteArray*>(this);
56}
57
58CharArray* Object::AsCharArray() {
59 DCHECK(GetClass()->IsArrayClass());
60 DCHECK(GetClass()->GetComponentType()->IsPrimitiveChar());
61 return down_cast<CharArray*>(this);
62}
63
64ShortArray* Object::AsShortArray() {
65 DCHECK(GetClass()->IsArrayClass());
66 DCHECK(GetClass()->GetComponentType()->IsPrimitiveShort());
67 return down_cast<ShortArray*>(this);
68}
69
70IntArray* Object::AsIntArray() {
71 DCHECK(GetClass()->IsArrayClass());
Ian Rogers64b6d142012-10-29 16:34:15 -070072 DCHECK(GetClass()->GetComponentType()->IsPrimitiveInt() ||
73 GetClass()->GetComponentType()->IsPrimitiveFloat());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070074 return down_cast<IntArray*>(this);
75}
76
77LongArray* Object::AsLongArray() {
78 DCHECK(GetClass()->IsArrayClass());
Ian Rogers64b6d142012-10-29 16:34:15 -070079 DCHECK(GetClass()->GetComponentType()->IsPrimitiveLong() ||
80 GetClass()->GetComponentType()->IsPrimitiveDouble());
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070081 return down_cast<LongArray*>(this);
82}
83
Elliott Hughesdbb40792011-11-18 17:05:22 -080084String* Object::AsString() {
85 DCHECK(GetClass()->IsStringClass());
86 return down_cast<String*>(this);
87}
88
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070089Throwable* Object::AsThrowable() {
90 DCHECK(GetClass()->IsThrowableClass());
91 return down_cast<Throwable*>(this);
92}
93
Ian Rogers50b35e22012-10-04 10:09:15 -070094Object* Object::Clone(Thread* self) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070095 Class* c = GetClass();
96 DCHECK(!c->IsClassClass());
97
98 // Object::SizeOf gets the right size even if we're an array.
99 // Using c->AllocObject() here would be wrong.
100 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800101 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -0700102 SirtRef<Object> copy(self, heap->AllocObject(self, c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700103 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -0700104 return NULL;
105 }
106
107 // Copy instance data. We assume memcpy copies by words.
108 // TODO: expose and use move32.
109 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700110 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -0700111 size_t offset = sizeof(Object);
112 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
113
Mathieu Chartier88c95be2012-09-11 14:06:41 -0700114 // Perform write barriers on copied object references.
115 if (c->IsArrayClass()) {
116 if (!c->GetComponentType()->IsPrimitive()) {
117 const ObjectArray<Object>* array = copy->AsObjectArray<Object>();
118 heap->WriteBarrierArray(copy.get(), 0, array->GetLength());
119 }
120 } else {
121 for (const Class* klass = c; klass != NULL; klass = klass->GetSuperClass()) {
122 size_t num_reference_fields = klass->NumReferenceInstanceFields();
123 for (size_t i = 0; i < num_reference_fields; ++i) {
124 Field* field = klass->GetInstanceField(i);
125 MemberOffset field_offset = field->GetOffset();
126 const Object* ref = copy->GetFieldObject<const Object*>(field_offset, false);
127 heap->WriteBarrierField(copy.get(), field_offset, ref);
128 }
129 }
130 }
131
Elliott Hughes20cde902011-10-04 17:37:27 -0700132 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800133 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -0700134 }
Elliott Hughes081be7f2011-09-18 16:50:26 -0700135
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700136 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700137}
138
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700139uint32_t Object::GetThinLockId() {
140 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700141}
142
143void Object::MonitorEnter(Thread* thread) {
144 Monitor::MonitorEnter(thread, this);
145}
146
Ian Rogersff1ed472011-09-20 13:46:24 -0700147bool Object::MonitorExit(Thread* thread) {
148 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700149}
150
151void Object::Notify() {
152 Monitor::Notify(Thread::Current(), this);
153}
154
155void Object::NotifyAll() {
156 Monitor::NotifyAll(Thread::Current(), this);
157}
158
159void Object::Wait(int64_t ms, int32_t ns) {
160 Monitor::Wait(Thread::Current(), this, ms, ns, true);
161}
162
Ian Rogers23435d02012-09-24 11:23:12 -0700163#if VERIFY_OBJECT_ENABLED
164void Object::CheckFieldAssignment(MemberOffset field_offset, const Object* new_value) {
165 const Class* c = GetClass();
166 if (Runtime::Current()->GetClassLinker() == NULL ||
167 !Runtime::Current()->GetHeap()->IsObjectValidationEnabled() ||
168 !c->IsResolved()) {
169 return;
170 }
171 for (const Class* cur = c; cur != NULL; cur = cur->GetSuperClass()) {
172 ObjectArray<Field>* fields = cur->GetIFields();
173 if (fields != NULL) {
174 size_t num_ref_ifields = cur->NumReferenceInstanceFields();
175 for (size_t i = 0; i < num_ref_ifields; ++i) {
176 Field* field = fields->Get(i);
177 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
178 FieldHelper fh(field);
179 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
180 return;
181 }
182 }
183 }
184 }
185 if (c->IsArrayClass()) {
186 // Bounds and assign-ability done in the array setter.
187 return;
188 }
189 if (IsClass()) {
190 ObjectArray<Field>* fields = AsClass()->GetSFields();
191 if (fields != NULL) {
192 size_t num_ref_sfields = AsClass()->NumReferenceStaticFields();
193 for (size_t i = 0; i < num_ref_sfields; ++i) {
194 Field* field = fields->Get(i);
195 if (field->GetOffset().Int32Value() == field_offset.Int32Value()) {
196 FieldHelper fh(field);
197 CHECK(fh.GetType()->IsAssignableFrom(new_value->GetClass()));
198 return;
199 }
200 }
201 }
202 }
203 LOG(FATAL) << "Failed to find field for assignment to " << reinterpret_cast<void*>(this)
204 << " of type " << PrettyDescriptor(c) << " at offset " << field_offset;
205}
206#endif
207
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208// TODO: get global references for these
209Class* Field::java_lang_reflect_Field_ = NULL;
210
211void Field::SetClass(Class* java_lang_reflect_Field) {
212 CHECK(java_lang_reflect_Field_ == NULL);
213 CHECK(java_lang_reflect_Field != NULL);
214 java_lang_reflect_Field_ = java_lang_reflect_Field;
215}
216
217void Field::ResetClass() {
218 CHECK(java_lang_reflect_Field_ != NULL);
219 java_lang_reflect_Field_ = NULL;
220}
221
Ian Rogers0571d352011-11-03 19:51:38 -0700222void Field::SetOffset(MemberOffset num_bytes) {
223 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800224#if 0 // TODO enable later in boot and under !NDEBUG
225 FieldHelper fh(this);
226 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700227 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
228 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
229 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800230#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700231 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
232}
233
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234uint32_t Field::Get32(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700235 DCHECK(object != NULL) << PrettyField(this);
236 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700237 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700238}
239
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700240void Field::Set32(Object* object, uint32_t new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700241 DCHECK(object != NULL) << PrettyField(this);
242 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243 object->SetField32(GetOffset(), new_value, IsVolatile());
244}
245
246uint64_t Field::Get64(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700247 DCHECK(object != NULL) << PrettyField(this);
248 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 return object->GetField64(GetOffset(), IsVolatile());
250}
251
252void Field::Set64(Object* object, uint64_t new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700253 DCHECK(object != NULL) << PrettyField(this);
254 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700255 object->SetField64(GetOffset(), new_value, IsVolatile());
256}
257
258Object* Field::GetObj(const Object* object) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700259 DCHECK(object != NULL) << PrettyField(this);
260 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
262}
263
264void Field::SetObj(Object* object, const Object* new_value) const {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700265 DCHECK(object != NULL) << PrettyField(this);
266 DCHECK(IsStatic() == (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
268}
269
270bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
272 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700273 return Get32(object);
274}
275
276void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
278 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700279 Set32(object, z);
280}
281
282int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
284 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 return Get32(object);
286}
287
288void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
290 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 Set32(object, b);
292}
293
294uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800295 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
296 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297 return Get32(object);
298}
299
300void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800301 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
302 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 Set32(object, c);
304}
305
Ian Rogers466bb252011-10-14 03:29:56 -0700306int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800307 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
308 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700309 return Get32(object);
310}
311
Ian Rogers466bb252011-10-14 03:29:56 -0700312void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
314 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700315 Set32(object, s);
316}
317
318int32_t Field::GetInt(const Object* object) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700319#ifndef NDEBUG
320 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
321 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
322#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 return Get32(object);
324}
325
326void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700327#ifndef NDEBUG
328 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
329 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField(this);
330#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700331 Set32(object, i);
332}
333
334int64_t Field::GetLong(const Object* object) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700335#ifndef NDEBUG
336 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
337 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
338#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339 return Get64(object);
340}
341
342void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers64b6d142012-10-29 16:34:15 -0700343#ifndef NDEBUG
344 Primitive::Type type = FieldHelper(this).GetTypeAsPrimitiveType();
345 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField(this);
346#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 Set64(object, j);
348}
349
Elliott Hughes1d878f32012-04-11 15:17:54 -0700350union Bits {
351 jdouble d;
352 jfloat f;
353 jint i;
354 jlong j;
355};
356
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800358 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
359 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700360 Bits bits;
361 bits.i = Get32(object);
362 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363}
364
365void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800366 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
367 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700368 Bits bits;
369 bits.f = f;
370 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700371}
372
373double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800374 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
375 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700376 Bits bits;
377 bits.j = Get64(object);
378 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379}
380
381void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800382 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
383 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700384 Bits bits;
385 bits.d = d;
386 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387}
388
389Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800390 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
391 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392 return GetObj(object);
393}
394
395void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800396 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
397 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 SetObj(object, l);
399}
400
401// TODO: get global references for these
Mathieu Chartier66f19252012-09-18 08:57:04 -0700402Class* AbstractMethod::java_lang_reflect_Constructor_ = NULL;
403Class* AbstractMethod::java_lang_reflect_Method_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700404
Mathieu Chartier66f19252012-09-18 08:57:04 -0700405InvokeType AbstractMethod::GetInvokeType() const {
Ian Rogers08f753d2012-08-24 14:35:25 -0700406 // TODO: kSuper?
407 if (GetDeclaringClass()->IsInterface()) {
408 return kInterface;
409 } else if (IsStatic()) {
410 return kStatic;
411 } else if (IsDirect()) {
412 return kDirect;
413 } else {
414 return kVirtual;
415 }
416}
417
Mathieu Chartier66f19252012-09-18 08:57:04 -0700418void AbstractMethod::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
Elliott Hughes80609252011-09-23 17:24:51 -0700419 CHECK(java_lang_reflect_Constructor_ == NULL);
420 CHECK(java_lang_reflect_Constructor != NULL);
421 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 CHECK(java_lang_reflect_Method_ == NULL);
424 CHECK(java_lang_reflect_Method != NULL);
425 java_lang_reflect_Method_ = java_lang_reflect_Method;
426}
427
Mathieu Chartier66f19252012-09-18 08:57:04 -0700428void AbstractMethod::ResetClasses() {
Elliott Hughes80609252011-09-23 17:24:51 -0700429 CHECK(java_lang_reflect_Constructor_ != NULL);
430 java_lang_reflect_Constructor_ = NULL;
431
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432 CHECK(java_lang_reflect_Method_ != NULL);
433 java_lang_reflect_Method_ = NULL;
434}
435
Mathieu Chartier66f19252012-09-18 08:57:04 -0700436ObjectArray<String>* AbstractMethod::GetDexCacheStrings() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 return GetFieldObject<ObjectArray<String>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700438 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439}
440
Mathieu Chartier66f19252012-09-18 08:57:04 -0700441void AbstractMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
442 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_strings_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700443 new_dex_cache_strings, false);
444}
445
Mathieu Chartier66f19252012-09-18 08:57:04 -0700446ObjectArray<AbstractMethod>* AbstractMethod::GetDexCacheResolvedMethods() const {
447 return GetFieldObject<ObjectArray<AbstractMethod>*>(
448 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800449}
450
Mathieu Chartier66f19252012-09-18 08:57:04 -0700451void AbstractMethod::SetDexCacheResolvedMethods(ObjectArray<AbstractMethod>* new_dex_cache_methods) {
452 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_methods_),
Ian Rogers19846512012-02-24 11:42:47 -0800453 new_dex_cache_methods, false);
454}
455
Mathieu Chartier66f19252012-09-18 08:57:04 -0700456ObjectArray<Class>* AbstractMethod::GetDexCacheResolvedTypes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 return GetFieldObject<ObjectArray<Class>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700458 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700459}
460
Mathieu Chartier66f19252012-09-18 08:57:04 -0700461void AbstractMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
462 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_resolved_types_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 new_dex_cache_classes, false);
464}
465
Mathieu Chartier66f19252012-09-18 08:57:04 -0700466ObjectArray<StaticStorageBase>* AbstractMethod::GetDexCacheInitializedStaticStorage() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700467 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
Mathieu Chartier66f19252012-09-18 08:57:04 -0700468 OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700469 false);
470}
471
Mathieu Chartier66f19252012-09-18 08:57:04 -0700472void AbstractMethod::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
473 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700475}
476
Mathieu Chartier66f19252012-09-18 08:57:04 -0700477size_t AbstractMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700478 CHECK_LE(1, shorty.length());
479 uint32_t num_registers = 0;
480 for (int i = 1; i < shorty.length(); ++i) {
481 char ch = shorty[i];
482 if (ch == 'D' || ch == 'J') {
483 num_registers += 2;
484 } else {
485 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700486 }
487 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700488 return num_registers;
489}
490
Mathieu Chartier66f19252012-09-18 08:57:04 -0700491bool AbstractMethod::IsProxyMethod() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800492 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700493}
494
Mathieu Chartier66f19252012-09-18 08:57:04 -0700495AbstractMethod* AbstractMethod::FindOverriddenMethod() const {
Ian Rogers466bb252011-10-14 03:29:56 -0700496 if (IsStatic()) {
497 return NULL;
498 }
499 Class* declaring_class = GetDeclaringClass();
500 Class* super_class = declaring_class->GetSuperClass();
501 uint16_t method_index = GetMethodIndex();
Mathieu Chartier66f19252012-09-18 08:57:04 -0700502 ObjectArray<AbstractMethod>* super_class_vtable = super_class->GetVTable();
503 AbstractMethod* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 // Did this method override a super class method? If so load the result from the super class'
505 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700506 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
507 result = super_class_vtable->Get(method_index);
508 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800510 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800511 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
512 CHECK_EQ(result,
513 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800514 } else {
515 MethodHelper mh(this);
516 MethodHelper interface_mh;
Ian Rogers9bc81912012-10-11 21:43:36 -0700517 IfTable* iftable = GetDeclaringClass()->GetIfTable();
518 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
519 Class* interface = iftable->GetInterface(i);
Ian Rogers16f93672012-02-14 12:29:06 -0800520 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700521 AbstractMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers16f93672012-02-14 12:29:06 -0800522 interface_mh.ChangeMethod(interface_method);
523 if (mh.HasSameNameAndSignature(&interface_mh)) {
524 result = interface_method;
525 break;
526 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 }
528 }
Ian Rogers466bb252011-10-14 03:29:56 -0700529 }
530 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800531#ifndef NDEBUG
532 MethodHelper result_mh(result);
533 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
534#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700535 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700536}
537
Mathieu Chartier66f19252012-09-18 08:57:04 -0700538static const void* GetOatCode(const AbstractMethod* m)
539 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800540 Runtime* runtime = Runtime::Current();
541 const void* code = m->GetCode();
542 // Peel off any method tracing trampoline.
543 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
544 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
545 }
546 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800547 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800548 code = runtime->GetClassLinker()->GetOatCodeFor(m);
549 }
550 return code;
551}
552
Mathieu Chartier66f19252012-09-18 08:57:04 -0700553uintptr_t AbstractMethod::NativePcOffset(const uintptr_t pc) const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700554 return pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
555}
556
Bill Buzbeea5b30242012-09-28 07:19:44 -0700557// Find the lowest-address native safepoint pc for a given dex pc
Ian Rogers34065b52012-09-28 16:31:26 -0700558uintptr_t AbstractMethod::ToFirstNativeSafepointPc(const uint32_t dex_pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700559#if !defined(ART_USE_LLVM_COMPILER)
Bill Buzbeea5b30242012-09-28 07:19:44 -0700560 const uint32_t* mapping_table = GetPcToDexMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700561 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800562 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700563 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700564 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700565 size_t mapping_table_length = GetPcToDexMappingTableLength();
566 for (size_t i = 0; i < mapping_table_length; i += 2) {
567 if (mapping_table[i + 1] == dex_pc) {
568 return mapping_table[i] + reinterpret_cast<uintptr_t>(GetOatCode(this));
569 }
570 }
571 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
572 << " in " << PrettyMethod(this);
573 return 0;
574#else
575 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
576 return static_cast<uint32_t>(dex_pc);
577#endif
578}
579
580uint32_t AbstractMethod::ToDexPc(const uintptr_t pc) const {
581#if !defined(ART_USE_LLVM_COMPILER)
582 const uint32_t* mapping_table = GetPcToDexMappingTable();
583 if (mapping_table == NULL) {
584 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
585 return DexFile::kDexNoIndex; // Special no mapping case
586 }
587 size_t mapping_table_length = GetPcToDexMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800588 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700589 for (size_t i = 0; i < mapping_table_length; i += 2) {
buzbee8320f382012-09-11 16:29:42 -0700590 if (mapping_table[i] == sought_offset) {
591 return mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700592 }
593 }
buzbee8320f382012-09-11 16:29:42 -0700594 LOG(FATAL) << "Failed to find Dex offset for PC offset 0x" << std::hex << sought_offset
595 << " in " << PrettyMethod(this);
596 return DexFile::kDexNoIndex;
TDYa127c8dc1012012-04-19 07:03:33 -0700597#else
598 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
599 return static_cast<uint32_t>(pc);
600#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700601}
602
Mathieu Chartier66f19252012-09-18 08:57:04 -0700603uintptr_t AbstractMethod::ToNativePc(const uint32_t dex_pc) const {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700604 const uint32_t* mapping_table = GetDexToPcMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700605 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700606 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700607 return 0; // Special no mapping/pc == 0 case
608 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700609 size_t mapping_table_length = GetDexToPcMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700610 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700611 uint32_t map_offset = mapping_table[i];
612 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700613 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800614 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700615 }
616 }
Bill Buzbeea5b30242012-09-28 07:19:44 -0700617 LOG(FATAL) << "Looking up Dex PC not contained in method, 0x" << std::hex << dex_pc
618 << " in " << PrettyMethod(this);
Ian Rogersbdb03912011-09-14 00:55:44 -0700619 return 0;
620}
621
Mathieu Chartier66f19252012-09-18 08:57:04 -0700622uint32_t AbstractMethod::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800623 MethodHelper mh(this);
624 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700625 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700626 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
627 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700628 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700629 if (iter_type_idx == DexFile::kDexNoIndex16) {
630 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700631 }
632 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800633 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700634 if (iter_exception_type == NULL) {
635 // The verifier should take care of resolving all exception classes early
636 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800637 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700638 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700639 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700640 }
641 }
642 // Handler not found
643 return DexFile::kDexNoIndex;
644}
645
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700646void AbstractMethod::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 if (kIsDebugBuild) {
648 self->AssertThreadSuspensionIsAllowable();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700649 CHECK_EQ(kRunnable, self->GetState());
650 }
TDYa12785321912012-04-01 15:24:56 -0700651
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers0399dde2012-06-06 17:09:28 -0700653 ManagedStack fragment;
654 self->PushManagedStackFragment(&fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700655
656 // Call the invoke stub associated with the method.
657 // Pass everything as arguments.
Mathieu Chartier66f19252012-09-18 08:57:04 -0700658 AbstractMethod::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700659
Elliott Hughes1240dad2011-09-09 16:24:50 -0700660
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700661 if (UNLIKELY(!Runtime::Current()->IsStarted())){
662 LOG(INFO) << "Not invoking " << PrettyMethod(this) << " for a runtime that isn't started.";
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700663 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700664 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700665 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700666 } else {
667 if (GetCode() != NULL && stub != NULL) {
668 bool log = false;
669 if (log) {
670 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
671 PrettyMethod(this).c_str(), GetCode(), stub);
672 }
673 (*stub)(this, receiver, self, args, result);
674 if (log) {
675 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
676 PrettyMethod(this).c_str(), GetCode(), stub);
677 }
678 } else {
679 LOG(INFO) << "Not invoking " << PrettyMethod(this)
680 << " code=" << reinterpret_cast<const void*>(GetCode())
681 << " stub=" << reinterpret_cast<void*>(stub);
682 const bool kInterpretMethodsWithNoCode = false;
683 if (kInterpretMethodsWithNoCode) {
684 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args, result);
685 } else if (result != NULL) {
686 result->SetJ(0);
687 }
688 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700689 }
690
691 // Pop transition.
Ian Rogers0399dde2012-06-06 17:09:28 -0700692 self->PopManagedStackFragment(fragment);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700693}
694
Mathieu Chartier66f19252012-09-18 08:57:04 -0700695bool AbstractMethod::IsRegistered() const {
696 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800697 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800698 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700699 return native_method != jni_stub;
700}
701
Mathieu Chartier66f19252012-09-18 08:57:04 -0700702void AbstractMethod::RegisterNative(Thread* self, const void* native_method) {
Ian Rogers60db5ab2012-02-20 17:02:00 -0800703 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700704 CHECK(IsNative()) << PrettyMethod(this);
705 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700706#if defined(ART_USE_LLVM_COMPILER)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700707 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
TDYa12726467572012-04-17 20:51:22 -0700708 native_method, false);
709#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800710 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700711 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800712 native_method, false);
713 } else {
714 // We've been asked to associate this method with the given native method but are working
715 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
716 // the native method to runtime support and store the target somewhere runtime support will
717 // find it.
718#if defined(__arm__)
Mathieu Chartier66f19252012-09-18 08:57:04 -0700719 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_method_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800720 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
721#else
722 UNIMPLEMENTED(FATAL);
723#endif
Mathieu Chartier66f19252012-09-18 08:57:04 -0700724 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(AbstractMethod, native_gc_map_),
Ian Rogers60db5ab2012-02-20 17:02:00 -0800725 reinterpret_cast<const uint8_t*>(native_method), false);
726 }
TDYa12726467572012-04-17 20:51:22 -0700727#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700728}
729
Mathieu Chartier66f19252012-09-18 08:57:04 -0700730void AbstractMethod::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700731 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700732 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800733 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700734}
735
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700736Class* Class::java_lang_Class_ = NULL;
737
738void Class::SetClassClass(Class* java_lang_Class) {
739 CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class;
740 CHECK(java_lang_Class != NULL);
741 java_lang_Class_ = java_lang_Class;
742}
743
744void Class::ResetClass() {
745 CHECK(java_lang_Class_ != NULL);
746 java_lang_Class_ = NULL;
747}
748
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700749void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700750 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
751 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700752 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogersc8982582012-09-07 16:53:25 -0700753 if (new_status > kStatusResolved) {
754 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
755 }
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800756 if (new_status == kStatusError) {
757 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
758
759 // stash current exception
760 Thread* self = Thread::Current();
Ian Rogers1f539342012-10-03 21:09:42 -0700761 SirtRef<Throwable> exception(self, self->GetException());
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800762 CHECK(exception.get() != NULL);
763
764 // clear exception to call FindSystemClass
765 self->ClearException();
766 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
767 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
768 CHECK(!self->IsExceptionPending());
769
770 // only verification errors, not initialization problems, should set a verify error.
771 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
772 Class* exception_class = exception->GetClass();
773 if (!eiie_class->IsAssignableFrom(exception_class)) {
774 SetVerifyErrorClass(exception_class);
775 }
776
777 // restore exception
778 self->SetException(exception.get());
779 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700780 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700781}
782
783DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700784 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700785}
786
787void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700788 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700789}
790
Ian Rogers50b35e22012-10-04 10:09:15 -0700791Object* Class::AllocObject(Thread* self) {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700792 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700793 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500794 // TODO: decide whether we want this check. It currently fails during bootstrap.
795 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700796 DCHECK_GE(this->object_size_, sizeof(Object));
Ian Rogers50b35e22012-10-04 10:09:15 -0700797 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700798}
799
Ian Rogers0571d352011-11-03 19:51:38 -0700800void Class::SetClassSize(size_t new_class_size) {
801 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
802 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
803}
804
Ian Rogersd418eda2012-01-30 12:14:28 -0800805// Return the class' name. The exact format is bizarre, but it's the specified behavior for
806// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
807// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
808// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
809String* Class::ComputeName() {
810 String* name = GetName();
811 if (name != NULL) {
812 return name;
813 }
814 std::string descriptor(ClassHelper(this).GetDescriptor());
815 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
816 // The descriptor indicates that this is the class for
817 // a primitive type; special-case the return value.
818 const char* c_name = NULL;
819 switch (descriptor[0]) {
820 case 'Z': c_name = "boolean"; break;
821 case 'B': c_name = "byte"; break;
822 case 'C': c_name = "char"; break;
823 case 'S': c_name = "short"; break;
824 case 'I': c_name = "int"; break;
825 case 'J': c_name = "long"; break;
826 case 'F': c_name = "float"; break;
827 case 'D': c_name = "double"; break;
828 case 'V': c_name = "void"; break;
829 default:
830 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
831 }
Ian Rogers50b35e22012-10-04 10:09:15 -0700832 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
Ian Rogersd418eda2012-01-30 12:14:28 -0800833 } else {
834 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
835 // components.
836 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
837 descriptor.erase(0, 1);
838 descriptor.erase(descriptor.size() - 1);
839 }
840 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Ian Rogers50b35e22012-10-04 10:09:15 -0700841 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
Ian Rogersd418eda2012-01-30 12:14:28 -0800842 }
843 SetName(name);
844 return name;
845}
846
Elliott Hughes4681c802011-09-25 18:04:37 -0700847void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700848 if ((flags & kDumpClassFullDetail) == 0) {
849 os << PrettyClass(this);
850 if ((flags & kDumpClassClassLoader) != 0) {
851 os << ' ' << GetClassLoader();
852 }
853 if ((flags & kDumpClassInitialized) != 0) {
854 os << ' ' << GetStatus();
855 }
Elliott Hughese0918552011-10-28 17:18:29 -0700856 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700857 return;
858 }
859
860 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800861 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700862 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800863 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700864 os << " objectSize=" << SizeOf() << " "
865 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
866 os << StringPrintf(" access=0x%04x.%04x\n",
867 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
868 if (super != NULL) {
869 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
870 }
871 if (IsArrayClass()) {
872 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
873 }
Ian Rogersd24e2642012-06-06 21:21:43 -0700874 if (kh.NumDirectInterfaces() > 0) {
875 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
876 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
877 Class* interface = kh.GetDirectInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700878 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800879 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700880 }
881 }
882 os << " vtable (" << NumVirtualMethods() << " entries, "
883 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
884 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800885 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700886 }
887 os << " direct methods (" << NumDirectMethods() << " entries):\n";
888 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800889 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700890 }
891 if (NumStaticFields() > 0) {
892 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700893 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700894 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800895 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700896 }
897 } else {
898 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700899 }
900 }
901 if (NumInstanceFields() > 0) {
902 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700903 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700904 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800905 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700906 }
907 } else {
908 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700909 }
910 }
911}
912
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700913void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
914 if (new_reference_offsets != CLASS_WALK_SUPER) {
915 // Sanity check that the number of bits set in the reference offset bitmap
916 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800917 size_t count = 0;
918 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
919 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700920 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800921 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700922 }
923 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
924 new_reference_offsets, false);
925}
926
927void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
928 if (new_reference_offsets != CLASS_WALK_SUPER) {
929 // Sanity check that the number of bits set in the reference offset bitmap
930 // agrees with the number of references
931 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
932 NumReferenceStaticFieldsDuringLinking());
933 }
934 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
935 new_reference_offsets, false);
936}
937
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700938bool Class::Implements(const Class* klass) const {
939 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700940 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700941 // All interfaces implemented directly and by our superclass, and
942 // recursively all super-interfaces of those interfaces, are listed
943 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700944 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -0700945 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700946 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -0700947 if (iftable->GetInterface(i) == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700948 return true;
949 }
950 }
951 return false;
952}
953
Elliott Hughese84278b2012-03-22 10:06:53 -0700954// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700955// are array classes.
956//
957// Consider an array class, e.g. Y[][], where Y is a subclass of X.
958// Y[][] = Y[][] --> true (identity)
959// X[][] = Y[][] --> true (element superclass)
960// Y = Y[][] --> false
961// Y[] = Y[][] --> false
962// Object = Y[][] --> true (everything is an object)
963// Object[] = Y[][] --> true
964// Object[][] = Y[][] --> true
965// Object[][][] = Y[][] --> false (too many []s)
966// Serializable = Y[][] --> true (all arrays are Serializable)
967// Serializable[] = Y[][] --> true
968// Serializable[][] = Y[][] --> false (unless Y is Serializable)
969//
970// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700971// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700972//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700973bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700974 DCHECK(IsArrayClass()) << PrettyClass(this);
975 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700976 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700977}
978
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700979bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700980 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
981 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700982 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700983 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700984 // src's super should be java_lang_Object, since it is an array.
985 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700986 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
987 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700988 return this == java_lang_Object;
989 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700990 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700991}
992
993bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700994 DCHECK(!IsInterface()) << PrettyClass(this);
995 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700996 const Class* current = this;
997 do {
998 if (current == klass) {
999 return true;
1000 }
1001 current = current->GetSuperClass();
1002 } while (current != NULL);
1003 return false;
1004}
1005
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001006bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001007 size_t i = 0;
1008 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
1009 ++i;
1010 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -07001011 if (descriptor1.find('/', i) != StringPiece::npos ||
1012 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001013 return false;
1014 } else {
1015 return true;
1016 }
1017}
1018
1019bool Class::IsInSamePackage(const Class* that) const {
1020 const Class* klass1 = this;
1021 const Class* klass2 = that;
1022 if (klass1 == klass2) {
1023 return true;
1024 }
1025 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001027 return false;
1028 }
1029 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -07001030 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001032 }
jeffhao4a801a42011-09-23 13:53:40 -07001033 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001034 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001035 }
1036 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001037 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -08001038 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001039 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -08001040 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001041 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001042}
1043
Elliott Hughesdbb40792011-11-18 17:05:22 -08001044bool Class::IsClassClass() const {
1045 Class* java_lang_Class = GetClass()->GetClass();
1046 return this == java_lang_Class;
1047}
1048
1049bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001050 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -08001051}
1052
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001053bool Class::IsThrowableClass() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001054 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001055}
1056
Ian Rogers64b6d142012-10-29 16:34:15 -07001057bool Class::IsFieldClass() const {
1058 Class* java_lang_Class = GetClass();
1059 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
1060 return this == java_lang_reflect_Field;
1061
1062}
1063
1064bool Class::IsMethodClass() const {
1065 return (this == AbstractMethod::GetMethodClass()) ||
1066 (this == AbstractMethod::GetConstructorClass());
1067
1068}
1069
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001070ClassLoader* Class::GetClassLoader() const {
1071 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -07001072}
1073
Ian Rogers365c1022012-06-22 15:05:28 -07001074void Class::SetClassLoader(ClassLoader* new_class_loader) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001076}
1077
Mathieu Chartier66f19252012-09-18 08:57:04 -07001078AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -07001079 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -07001080 DCHECK(declaring_class != NULL) << PrettyClass(this);
1081 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001082 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001083 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001084 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001085 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001086 if (iftable->GetInterface(i) == declaring_class) {
1087 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001088 }
1089 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001090 return NULL;
1091}
1092
Mathieu Chartier66f19252012-09-18 08:57:04 -07001093AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -07001094 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001095 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001096 if (method != NULL) {
1097 return method;
1098 }
1099
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001100 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001101 IfTable* iftable = GetIfTable();
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001102 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001103 method = iftable->GetInterface(i)->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -07001104 if (method != NULL) {
1105 return method;
1106 }
1107 }
1108 return NULL;
1109}
1110
Mathieu Chartier66f19252012-09-18 08:57:04 -07001111AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001112 // Check the current class before checking the interfaces.
Mathieu Chartier66f19252012-09-18 08:57:04 -07001113 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001114 if (method != NULL) {
1115 return method;
1116 }
1117
1118 int32_t iftable_count = GetIfTableCount();
Ian Rogers9bc81912012-10-11 21:43:36 -07001119 IfTable* iftable = GetIfTable();
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001120 for (int32_t i = 0; i < iftable_count; i++) {
Ian Rogers9bc81912012-10-11 21:43:36 -07001121 method = iftable->GetInterface(i)->FindVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001122 if (method != NULL) {
1123 return method;
1124 }
1125 }
1126 return NULL;
1127}
1128
1129
Mathieu Chartier66f19252012-09-18 08:57:04 -07001130AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001131 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001132 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001133 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001134 mh.ChangeMethod(method);
1135 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001136 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001137 }
1138 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001139 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001140}
1141
Mathieu Chartier66f19252012-09-18 08:57:04 -07001142AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001143 if (GetDexCache() == dex_cache) {
1144 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001145 AbstractMethod* method = GetDirectMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001146 if (method->GetDexMethodIndex() == dex_method_idx) {
1147 return method;
1148 }
1149 }
1150 }
1151 return NULL;
1152}
1153
Mathieu Chartier66f19252012-09-18 08:57:04 -07001154AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001155 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001156 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001157 if (method != NULL) {
1158 return method;
1159 }
1160 }
1161 return NULL;
1162}
1163
Mathieu Chartier66f19252012-09-18 08:57:04 -07001164AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001165 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001166 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001167 if (method != NULL) {
1168 return method;
1169 }
1170 }
1171 return NULL;
1172}
1173
Mathieu Chartier66f19252012-09-18 08:57:04 -07001174AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001175 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001176 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001177 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001178 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001179 mh.ChangeMethod(method);
1180 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001181 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001182 }
1183 }
1184 return NULL;
1185}
1186
Mathieu Chartier66f19252012-09-18 08:57:04 -07001187AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001188 if (GetDexCache() == dex_cache) {
1189 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001190 AbstractMethod* method = GetVirtualMethod(i);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001191 if (method->GetDexMethodIndex() == dex_method_idx) {
1192 return method;
1193 }
1194 }
1195 }
1196 return NULL;
1197}
1198
Mathieu Chartier66f19252012-09-18 08:57:04 -07001199AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers466bb252011-10-14 03:29:56 -07001200 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001201 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers466bb252011-10-14 03:29:56 -07001202 if (method != NULL) {
1203 return method;
1204 }
1205 }
1206 return NULL;
1207}
1208
Mathieu Chartier66f19252012-09-18 08:57:04 -07001209AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001210 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001211 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001212 if (method != NULL) {
1213 return method;
1214 }
1215 }
1216 return NULL;
1217}
1218
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001219Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001220 // Is the field in this class?
1221 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001222 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001223 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1224 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001225 fh.ChangeField(f);
1226 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001227 return f;
1228 }
1229 }
1230 return NULL;
1231}
1232
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001233Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1234 if (GetDexCache() == dex_cache) {
1235 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1236 Field* f = GetInstanceField(i);
1237 if (f->GetDexFieldIndex() == dex_field_idx) {
1238 return f;
1239 }
1240 }
1241 }
1242 return NULL;
1243}
1244
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001245Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001246 // Is the field in this class, or any of its superclasses?
1247 // Interfaces are not relevant because they can't contain instance fields.
1248 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001249 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001250 if (f != NULL) {
1251 return f;
1252 }
1253 }
1254 return NULL;
1255}
1256
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001257Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1258 // Is the field in this class, or any of its superclasses?
1259 // Interfaces are not relevant because they can't contain instance fields.
1260 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1261 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1262 if (f != NULL) {
1263 return f;
1264 }
1265 }
1266 return NULL;
1267}
1268
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001269Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1270 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001271 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001272 for (size_t i = 0; i < NumStaticFields(); ++i) {
1273 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001274 fh.ChangeField(f);
1275 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001276 return f;
1277 }
1278 }
1279 return NULL;
1280}
1281
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001282Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1283 if (dex_cache == GetDexCache()) {
1284 for (size_t i = 0; i < NumStaticFields(); ++i) {
1285 Field* f = GetStaticField(i);
1286 if (f->GetDexFieldIndex() == dex_field_idx) {
1287 return f;
1288 }
1289 }
1290 }
1291 return NULL;
1292}
1293
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001294Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1295 // Is the field in this class (or its interfaces), or any of its
1296 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001297 ClassHelper kh;
1298 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001299 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001300 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001301 if (f != NULL) {
1302 return f;
1303 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001304 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001305 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001306 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1307 Class* interface = kh.GetDirectInterface(i);
1308 f = interface->FindStaticField(name, type);
Ian Rogersb067ac22011-12-13 18:05:09 -08001309 if (f != NULL) {
1310 return f;
1311 }
1312 }
1313 }
1314 return NULL;
1315}
1316
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001317Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1318 ClassHelper kh;
1319 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1320 // Is the field in this class?
1321 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1322 if (f != NULL) {
1323 return f;
1324 }
1325 // Is this field in any of this class' interfaces?
1326 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001327 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1328 Class* interface = kh.GetDirectInterface(i);
1329 f = interface->FindStaticField(dex_cache, dex_field_idx);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001330 if (f != NULL) {
1331 return f;
1332 }
1333 }
1334 }
1335 return NULL;
1336}
1337
Ian Rogersb067ac22011-12-13 18:05:09 -08001338Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1339 // Find a field using the JLS field resolution order
1340 ClassHelper kh;
1341 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1342 // Is the field in this class?
1343 Field* f = k->FindDeclaredInstanceField(name, type);
1344 if (f != NULL) {
1345 return f;
1346 }
1347 f = k->FindDeclaredStaticField(name, type);
1348 if (f != NULL) {
1349 return f;
1350 }
1351 // Is this field in any of this class' interfaces?
1352 kh.ChangeClass(k);
Ian Rogersd24e2642012-06-06 21:21:43 -07001353 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
1354 Class* interface = kh.GetDirectInterface(i);
1355 f = interface->FindStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001356 if (f != NULL) {
1357 return f;
1358 }
1359 }
1360 }
1361 return NULL;
1362}
1363
Ian Rogers50b35e22012-10-04 10:09:15 -07001364Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count,
1365 size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001366 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 DCHECK_GE(component_count, 0);
1368 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001369
Ian Rogersa15e67d2012-02-28 13:51:55 -08001370 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001371 size_t data_size = component_count * component_size;
1372 size_t size = header_size + data_size;
1373
1374 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1375 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1376 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001377 self->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001378 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001379 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001380 return NULL;
1381 }
1382
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001383 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001384 Array* array = down_cast<Array*>(heap->AllocObject(self, array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001385 if (array != NULL) {
1386 DCHECK(array->IsArrayInstance());
1387 array->SetLength(component_count);
1388 }
1389 return array;
1390}
1391
Ian Rogers50b35e22012-10-04 10:09:15 -07001392Array* Array::Alloc(Thread* self, Class* array_class, int32_t component_count) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001393 DCHECK(array_class->IsArrayClass());
Ian Rogers50b35e22012-10-04 10:09:15 -07001394 return Alloc(self, array_class, component_count, array_class->GetComponentSize());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001395}
1396
Ian Rogers64b6d142012-10-29 16:34:15 -07001397// Create a multi-dimensional array of Objects or primitive types.
1398//
1399// We have to generate the names for X[], X[][], X[][][], and so on. The
1400// easiest way to deal with that is to create the full name once and then
1401// subtract pieces off. Besides, we want to start with the outermost
1402// piece and work our way in.
1403// Recursively create an array with multiple dimensions. Elements may be
1404// Objects or primitive types.
1405static Array* RecursiveCreateMultiArray(Thread* self, Class* array_class, int current_dimension,
1406 IntArray* dimensions)
1407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1408 int32_t array_length = dimensions->Get(current_dimension);
1409 SirtRef<Array> new_array(self, Array::Alloc(self, array_class, array_length));
1410 if (UNLIKELY(new_array.get() == NULL)) {
1411 CHECK(self->IsExceptionPending());
1412 return NULL;
1413 }
1414 if ((current_dimension + 1) < dimensions->GetLength()) {
1415 // Create a new sub-array in every element of the array.
1416 for (int32_t i = 0; i < array_length; i++) {
1417 Array* sub_array = RecursiveCreateMultiArray(self, array_class->GetComponentType(),
1418 current_dimension + 1, dimensions);
1419 if (UNLIKELY(sub_array == NULL)) {
1420 CHECK(self->IsExceptionPending());
1421 return NULL;
1422 }
1423 new_array->AsObjectArray<Array>()->Set(i, sub_array);
1424 }
1425 }
1426 return new_array.get();
1427}
1428
1429Array* Array::CreateMultiArray(Thread* self, Class* element_class, IntArray* dimensions) {
1430 // Verify dimensions.
1431 //
1432 // The caller is responsible for verifying that "dimArray" is non-null
1433 // and has a length > 0 and <= 255.
1434 int num_dimensions = dimensions->GetLength();
1435 DCHECK_GT(num_dimensions, 0);
1436 DCHECK_LE(num_dimensions, 255);
1437
1438 for (int i = 0; i < num_dimensions; i++) {
1439 int dimension = dimensions->Get(i);
1440 if (UNLIKELY(dimension < 0)) {
1441 self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;",
1442 "Dimension %d: %d", i, dimension);
1443 return NULL;
1444 }
1445 }
1446
1447 // Generate the full name of the array class.
1448 std::string descriptor(num_dimensions, '[');
1449 descriptor += ClassHelper(element_class).GetDescriptor();
1450
1451 // Find/generate the array class.
1452 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1453 Class* array_class = class_linker->FindClass(descriptor.c_str(), element_class->GetClassLoader());
1454 if (UNLIKELY(array_class == NULL)) {
1455 CHECK(self->IsExceptionPending());
1456 return NULL;
1457 }
1458 // create the array
1459 Array* new_array = RecursiveCreateMultiArray(self, array_class, 0, dimensions);
1460 if (UNLIKELY(new_array == NULL)) {
1461 CHECK(self->IsExceptionPending());
1462 return NULL;
1463 }
1464 return new_array;
1465}
1466
Elliott Hughes80609252011-09-23 17:24:51 -07001467bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001468 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001469 "length=%i; index=%i", length_, index);
1470 return false;
1471}
1472
1473bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001474 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001475 "Can't store an element of type %s into an array of type %s",
1476 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1477 return false;
1478}
1479
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001480template<typename T>
Ian Rogers50b35e22012-10-04 10:09:15 -07001481PrimitiveArray<T>* PrimitiveArray<T>::Alloc(Thread* self, size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001482 DCHECK(array_class_ != NULL);
Ian Rogers50b35e22012-10-04 10:09:15 -07001483 Array* raw_array = Array::Alloc(self, array_class_, length, sizeof(T));
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001484 return down_cast<PrimitiveArray<T>*>(raw_array);
1485}
1486
1487template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1488
1489// Explicitly instantiate all the primitive array types.
1490template class PrimitiveArray<uint8_t>; // BooleanArray
1491template class PrimitiveArray<int8_t>; // ByteArray
1492template class PrimitiveArray<uint16_t>; // CharArray
1493template class PrimitiveArray<double>; // DoubleArray
1494template class PrimitiveArray<float>; // FloatArray
1495template class PrimitiveArray<int32_t>; // IntArray
1496template class PrimitiveArray<int64_t>; // LongArray
1497template class PrimitiveArray<int16_t>; // ShortArray
1498
Ian Rogers466bb252011-10-14 03:29:56 -07001499// Explicitly instantiate Class[][]
1500template class ObjectArray<ObjectArray<Class> >;
1501
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001502// TODO: get global references for these
1503Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001504
Brian Carlstroma663ea52011-08-19 23:33:41 -07001505void String::SetClass(Class* java_lang_String) {
1506 CHECK(java_lang_String_ == NULL);
1507 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001508 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001509}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001510
Brian Carlstroma663ea52011-08-19 23:33:41 -07001511void String::ResetClass() {
1512 CHECK(java_lang_String_ != NULL);
1513 java_lang_String_ = NULL;
1514}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001515
Brian Carlstromc74255f2011-09-11 22:47:39 -07001516String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001517 return Runtime::Current()->GetInternTable()->InternWeak(this);
1518}
1519
Brian Carlstrom395520e2011-09-25 19:35:00 -07001520int32_t String::GetHashCode() {
1521 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1522 if (result == 0) {
1523 ComputeHashCode();
1524 }
1525 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1526 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1527 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001528 return result;
1529}
1530
1531int32_t String::GetLength() const {
1532 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1533 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1534 return result;
1535}
1536
1537uint16_t String::CharAt(int32_t index) const {
1538 // TODO: do we need this? Equals is the only caller, and could
1539 // bounds check itself.
1540 if (index < 0 || index >= count_) {
1541 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001542 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001543 "length=%i; index=%i", count_, index);
1544 return 0;
1545 }
1546 return GetCharArray()->Get(index + GetOffset());
1547}
1548
Ian Rogers50b35e22012-10-04 10:09:15 -07001549String* String::AllocFromUtf16(Thread* self,
1550 int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001551 const uint16_t* utf16_data_in,
1552 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001553 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers50b35e22012-10-04 10:09:15 -07001554 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001555 if (string == NULL) {
1556 return NULL;
1557 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001558 // TODO: use 16-bit wide memset variant
1559 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001560 if (array == NULL) {
1561 return NULL;
1562 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001563 for (int i = 0; i < utf16_length; i++) {
1564 array->Set(i, utf16_data_in[i]);
1565 }
1566 if (hash_code != 0) {
1567 string->SetHashCode(hash_code);
1568 } else {
1569 string->ComputeHashCode();
1570 }
1571 return string;
1572}
1573
Ian Rogers50b35e22012-10-04 10:09:15 -07001574 String* String::AllocFromModifiedUtf8(Thread* self, const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001575 if (utf == NULL) {
1576 return NULL;
1577 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001578 size_t char_count = CountModifiedUtf8Chars(utf);
Ian Rogers50b35e22012-10-04 10:09:15 -07001579 return AllocFromModifiedUtf8(self, char_count, utf);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001580}
1581
Ian Rogers50b35e22012-10-04 10:09:15 -07001582String* String::AllocFromModifiedUtf8(Thread* self, int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001583 const char* utf8_data_in) {
Ian Rogers50b35e22012-10-04 10:09:15 -07001584 String* string = Alloc(self, GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001585 if (string == NULL) {
1586 return NULL;
1587 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001588 uint16_t* utf16_data_out =
1589 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1590 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1591 string->ComputeHashCode();
1592 return string;
1593}
1594
Ian Rogers50b35e22012-10-04 10:09:15 -07001595String* String::Alloc(Thread* self, Class* java_lang_String, int32_t utf16_length) {
1596 SirtRef<CharArray> array(self, CharArray::Alloc(self, utf16_length));
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001597 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001598 return NULL;
1599 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001600 return Alloc(self, java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001601}
1602
Ian Rogers50b35e22012-10-04 10:09:15 -07001603String* String::Alloc(Thread* self, Class* java_lang_String, CharArray* array) {
Ian Rogers1f539342012-10-03 21:09:42 -07001604 // Hold reference in case AllocObject causes GC.
Ian Rogers50b35e22012-10-04 10:09:15 -07001605 SirtRef<CharArray> array_ref(self, array);
1606 String* string = down_cast<String*>(java_lang_String->AllocObject(self));
Elliott Hughesb51036c2011-10-12 23:49:11 -07001607 if (string == NULL) {
1608 return NULL;
1609 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001610 string->SetArray(array);
1611 string->SetCount(array->GetLength());
1612 return string;
1613}
1614
1615bool String::Equals(const String* that) const {
1616 if (this == that) {
1617 // Quick reference equality test
1618 return true;
1619 } else if (that == NULL) {
1620 // Null isn't an instanceof anything
1621 return false;
1622 } else if (this->GetLength() != that->GetLength()) {
1623 // Quick length inequality test
1624 return false;
1625 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001626 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001627 // hash code was already equal
1628 for (int32_t i = 0; i < that->GetLength(); ++i) {
1629 if (this->CharAt(i) != that->CharAt(i)) {
1630 return false;
1631 }
1632 }
1633 return true;
1634 }
1635}
1636
Elliott Hughes5d78d392011-12-13 16:53:05 -08001637bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001638 if (this->GetLength() != that_length) {
1639 return false;
1640 } else {
1641 for (int32_t i = 0; i < that_length; ++i) {
1642 if (this->CharAt(i) != that_chars[that_offset + i]) {
1643 return false;
1644 }
1645 }
1646 return true;
1647 }
1648}
1649
1650bool String::Equals(const char* modified_utf8) const {
1651 for (int32_t i = 0; i < GetLength(); ++i) {
1652 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1653 if (ch == '\0' || ch != CharAt(i)) {
1654 return false;
1655 }
1656 }
1657 return *modified_utf8 == '\0';
1658}
1659
1660bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001661 if (modified_utf8.size() != GetLength()) {
1662 return false;
1663 }
1664 const char* p = modified_utf8.data();
1665 for (int32_t i = 0; i < GetLength(); ++i) {
1666 uint16_t ch = GetUtf16FromUtf8(&p);
1667 if (ch != CharAt(i)) {
1668 return false;
1669 }
1670 }
1671 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001672}
1673
1674// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1675std::string String::ToModifiedUtf8() const {
1676 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001677 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001678 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001679 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1680 return result;
1681}
1682
Ian Rogers64b6d142012-10-29 16:34:15 -07001683#ifdef HAVE__MEMCMP16
1684// "count" is in 16-bit units.
1685extern "C" uint32_t __memcmp16(const uint16_t* s0, const uint16_t* s1, size_t count);
1686#define MemCmp16 __memcmp16
1687#else
1688static uint32_t MemCmp16(const uint16_t* s0, const uint16_t* s1, size_t count) {
1689 for (size_t i = 0; i < count; i++) {
1690 if (s0[i] != s1[i]) {
1691 return static_cast<int32_t>(s0[i]) - static_cast<int32_t>(s1[i]);
1692 }
1693 }
1694 return 0;
1695}
1696#endif
1697
1698int32_t String::CompareTo(String* rhs) const {
1699 // Quick test for comparison of a string with itself.
1700 const String* lhs = this;
1701 if (lhs == rhs) {
1702 return 0;
1703 }
1704 // TODO: is this still true?
1705 // The annoying part here is that 0x00e9 - 0xffff != 0x00ea,
1706 // because the interpreter converts the characters to 32-bit integers
1707 // *without* sign extension before it subtracts them (which makes some
1708 // sense since "char" is unsigned). So what we get is the result of
1709 // 0x000000e9 - 0x0000ffff, which is 0xffff00ea.
1710 int lhsCount = lhs->GetLength();
1711 int rhsCount = rhs->GetLength();
1712 int countDiff = lhsCount - rhsCount;
1713 int minCount = (countDiff < 0) ? lhsCount : rhsCount;
1714 const uint16_t* lhsChars = lhs->GetCharArray()->GetData() + lhs->GetOffset();
1715 const uint16_t* rhsChars = rhs->GetCharArray()->GetData() + rhs->GetOffset();
1716 int otherRes = MemCmp16(lhsChars, rhsChars, minCount);
1717 if (otherRes != 0) {
1718 return otherRes;
1719 }
1720 return countDiff;
1721}
1722
Ian Rogers1c5eb702012-02-01 09:18:34 -08001723void Throwable::SetCause(Throwable* cause) {
1724 CHECK(cause != NULL);
1725 CHECK(cause != this);
1726 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1727 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1728}
1729
Ian Rogers466bb252011-10-14 03:29:56 -07001730bool Throwable::IsCheckedException() const {
Elliott Hughesa4f94742012-05-29 16:28:38 -07001731 if (InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_Error))) {
Ian Rogers466bb252011-10-14 03:29:56 -07001732 return false;
1733 }
Elliott Hughesa4f94742012-05-29 16:28:38 -07001734 return !InstanceOf(WellKnownClasses::ToClass(WellKnownClasses::java_lang_RuntimeException));
Ian Rogers466bb252011-10-14 03:29:56 -07001735}
1736
Ian Rogers9074b992011-10-26 17:41:55 -07001737std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001738 std::string result(PrettyTypeOf(this));
1739 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001740 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001741 if (msg != NULL) {
1742 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001743 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001744 result += "\n";
1745 Object* stack_state = GetStackState();
1746 // check stack state isn't missing or corrupt
1747 if (stack_state != NULL && stack_state->IsObjectArray()) {
1748 // Decode the internal stack trace into the depth and method trace
1749 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1750 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001751 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1752 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001753 for (int32_t i = 0; i < depth; ++i) {
Mathieu Chartier66f19252012-09-18 08:57:04 -07001754 AbstractMethod* method = down_cast<AbstractMethod*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001755 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -07001756 uint32_t dex_pc = pc_trace->Get(i);
1757 int32_t line_number = mh.GetLineNumFromDexPC(dex_pc);
Ian Rogers19846512012-02-24 11:42:47 -08001758 const char* source_file = mh.GetDeclaringClassSourceFile();
1759 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1760 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001761 }
Ian Rogers9074b992011-10-26 17:41:55 -07001762 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001763 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001764 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001765 result += "Caused by: ";
1766 result += cause->Dump();
1767 }
Ian Rogers9074b992011-10-26 17:41:55 -07001768 return result;
1769}
1770
Ian Rogers5167c972012-02-03 10:41:20 -08001771
1772Class* Throwable::java_lang_Throwable_ = NULL;
1773
1774void Throwable::SetClass(Class* java_lang_Throwable) {
1775 CHECK(java_lang_Throwable_ == NULL);
1776 CHECK(java_lang_Throwable != NULL);
1777 java_lang_Throwable_ = java_lang_Throwable;
1778}
1779
1780void Throwable::ResetClass() {
1781 CHECK(java_lang_Throwable_ != NULL);
1782 java_lang_Throwable_ = NULL;
1783}
1784
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001785Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1786
1787void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1788 CHECK(java_lang_StackTraceElement_ == NULL);
1789 CHECK(java_lang_StackTraceElement != NULL);
1790 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1791}
1792
1793void StackTraceElement::ResetClass() {
1794 CHECK(java_lang_StackTraceElement_ != NULL);
1795 java_lang_StackTraceElement_ = NULL;
1796}
1797
Ian Rogers50b35e22012-10-04 10:09:15 -07001798StackTraceElement* StackTraceElement::Alloc(Thread* self,
1799 String* declaring_class,
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001800 String* method_name,
1801 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001802 int32_t line_number) {
1803 StackTraceElement* trace =
Ian Rogers50b35e22012-10-04 10:09:15 -07001804 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject(self));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001805 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1806 const_cast<String*>(declaring_class), false);
1807 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1808 const_cast<String*>(method_name), false);
1809 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1810 const_cast<String*>(file_name), false);
1811 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1812 line_number, false);
1813 return trace;
1814}
1815
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001816} // namespace art