blob: 13e47321315e432e602e5b1171804693cd3c31dc [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070034#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Ian Rogers60db5ab2012-02-20 17:02:00 -080037#include "runtime_support.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070038#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070039#include "utils.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070040
Logan Chienfca7e872011-12-20 20:08:22 +080041#if defined(ART_USE_LLVM_COMPILER)
42#include "compiler_llvm/inferred_reg_category_map.h"
43using art::compiler_llvm::InferredRegCategoryMap;
44#endif
45
Carl Shapiro3ee755d2011-06-28 12:11:04 -070046namespace art {
47
Elliott Hughesdbb40792011-11-18 17:05:22 -080048String* Object::AsString() {
49 DCHECK(GetClass()->IsStringClass());
50 return down_cast<String*>(this);
51}
52
Elliott Hughes081be7f2011-09-18 16:50:26 -070053Object* Object::Clone() {
54 Class* c = GetClass();
55 DCHECK(!c->IsClassClass());
56
57 // Object::SizeOf gets the right size even if we're an array.
58 // Using c->AllocObject() here would be wrong.
59 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080060 Heap* heap = Runtime::Current()->GetHeap();
61 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070062 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070063 return NULL;
64 }
65
66 // Copy instance data. We assume memcpy copies by words.
67 // TODO: expose and use move32.
68 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070069 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070070 size_t offset = sizeof(Object);
71 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
72
Elliott Hughes20cde902011-10-04 17:37:27 -070073 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080074 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070075 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070076
Brian Carlstrom40381fb2011-10-19 14:13:40 -070077 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070078}
79
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070080uint32_t Object::GetThinLockId() {
81 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070082}
83
84void Object::MonitorEnter(Thread* thread) {
85 Monitor::MonitorEnter(thread, this);
86}
87
Ian Rogersff1ed472011-09-20 13:46:24 -070088bool Object::MonitorExit(Thread* thread) {
89 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070090}
91
92void Object::Notify() {
93 Monitor::Notify(Thread::Current(), this);
94}
95
96void Object::NotifyAll() {
97 Monitor::NotifyAll(Thread::Current(), this);
98}
99
100void Object::Wait(int64_t ms, int32_t ns) {
101 Monitor::Wait(Thread::Current(), this, ms, ns, true);
102}
103
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700104// TODO: get global references for these
105Class* Field::java_lang_reflect_Field_ = NULL;
106
107void Field::SetClass(Class* java_lang_reflect_Field) {
108 CHECK(java_lang_reflect_Field_ == NULL);
109 CHECK(java_lang_reflect_Field != NULL);
110 java_lang_reflect_Field_ = java_lang_reflect_Field;
111}
112
113void Field::ResetClass() {
114 CHECK(java_lang_reflect_Field_ != NULL);
115 java_lang_reflect_Field_ = NULL;
116}
117
Ian Rogers0571d352011-11-03 19:51:38 -0700118void Field::SetOffset(MemberOffset num_bytes) {
119 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120#if 0 // TODO enable later in boot and under !NDEBUG
121 FieldHelper fh(this);
122 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700123 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
124 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
125 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700127 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
128}
129
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700131 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700132 if (IsStatic()) {
133 object = declaring_class_;
134 }
135 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700136}
137
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700138void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700139 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700140 if (IsStatic()) {
141 object = declaring_class_;
142 }
143 object->SetField32(GetOffset(), new_value, IsVolatile());
144}
145
146uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700147 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700148 if (IsStatic()) {
149 object = declaring_class_;
150 }
151 return object->GetField64(GetOffset(), IsVolatile());
152}
153
154void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700155 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700156 if (IsStatic()) {
157 object = declaring_class_;
158 }
159 object->SetField64(GetOffset(), new_value, IsVolatile());
160}
161
162Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700163 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700164 if (IsStatic()) {
165 object = declaring_class_;
166 }
167 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
168}
169
170void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700171 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700172 if (IsStatic()) {
173 object = declaring_class_;
174 }
175 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
176}
177
178bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800179 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
180 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700181 return Get32(object);
182}
183
184void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
186 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 Set32(object, z);
188}
189
190int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800191 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
192 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193 return Get32(object);
194}
195
196void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800197 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
198 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199 Set32(object, b);
200}
201
202uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
204 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 return Get32(object);
206}
207
208void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800209 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
210 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 Set32(object, c);
212}
213
Ian Rogers466bb252011-10-14 03:29:56 -0700214int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800215 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
216 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 return Get32(object);
218}
219
Ian Rogers466bb252011-10-14 03:29:56 -0700220void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800221 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
222 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 Set32(object, s);
224}
225
226int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
228 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229 return Get32(object);
230}
231
232void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
234 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700235 Set32(object, i);
236}
237
238int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800239 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
240 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700241 return Get64(object);
242}
243
244void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800245 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
246 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700247 Set64(object, j);
248}
249
250float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800251 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
252 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700253 JValue float_bits;
254 float_bits.i = Get32(object);
255 return float_bits.f;
256}
257
258void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
260 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 JValue float_bits;
262 float_bits.f = f;
263 Set32(object, float_bits.i);
264}
265
266double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
268 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700269 JValue double_bits;
270 double_bits.j = Get64(object);
271 return double_bits.d;
272}
273
274void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
276 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700277 JValue double_bits;
278 double_bits.d = d;
279 Set64(object, double_bits.j);
280}
281
282Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
284 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 return GetObj(object);
286}
287
288void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
290 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 SetObj(object, l);
292}
293
294// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700295Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296Class* Method::java_lang_reflect_Method_ = NULL;
297
Elliott Hughes80609252011-09-23 17:24:51 -0700298void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
299 CHECK(java_lang_reflect_Constructor_ == NULL);
300 CHECK(java_lang_reflect_Constructor != NULL);
301 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
302
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 CHECK(java_lang_reflect_Method_ == NULL);
304 CHECK(java_lang_reflect_Method != NULL);
305 java_lang_reflect_Method_ = java_lang_reflect_Method;
306}
307
Elliott Hughes80609252011-09-23 17:24:51 -0700308void Method::ResetClasses() {
309 CHECK(java_lang_reflect_Constructor_ != NULL);
310 java_lang_reflect_Constructor_ = NULL;
311
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 CHECK(java_lang_reflect_Method_ != NULL);
313 java_lang_reflect_Method_ = NULL;
314}
315
Elliott Hughes418d20f2011-09-22 14:00:39 -0700316Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
317 if (*p == '[') {
318 // Something like "[[[Ljava/lang/String;".
319 const char* start = p;
320 while (*p == '[') {
321 ++p;
322 }
323 if (*p == 'L') {
324 while (*p != ';') {
325 ++p;
326 }
327 }
328 ++p; // Either the ';' or the primitive type.
329
Brian Carlstromaded5f72011-10-07 17:15:04 -0700330 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800331 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700332 } else if (*p == 'L') {
333 const char* start = p;
334 while (*p != ';') {
335 ++p;
336 }
337 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800338 std::string descriptor(start, (p - start));
339 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 } else {
341 return class_linker->FindPrimitiveClass(*p++);
342 }
343}
344
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345ObjectArray<String>* Method::GetDexCacheStrings() const {
346 return GetFieldObject<ObjectArray<String>*>(
347 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
348}
349
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
351 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
352 new_dex_cache_strings, false);
353}
354
Ian Rogers19846512012-02-24 11:42:47 -0800355ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
356 return GetFieldObject<ObjectArray<Method>*>(
357 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
358}
359
360void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
361 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
362 new_dex_cache_methods, false);
363}
364
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700365ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
366 return GetFieldObject<ObjectArray<Class>*>(
367 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
368}
369
370void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
371 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
372 new_dex_cache_classes, false);
373}
374
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700375ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
376 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
377 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
378 false);
379}
380
381void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700382 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384}
385
Logan Chienfca7e872011-12-20 20:08:22 +0800386#if defined(ART_USE_LLVM_COMPILER)
387
388const InferredRegCategoryMap* Method::GetInferredRegCategoryMap() const {
389 const InferredRegCategoryMap* map = GetFieldPtr<const InferredRegCategoryMap*>(
390 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
391 DCHECK(map != NULL) << PrettyMethod(this);
392 return map;
393}
394
395void Method::SetInferredRegCategoryMap(const InferredRegCategoryMap* map) {
Logan Chiena1854662012-02-17 09:52:18 +0800396 const InferredRegCategoryMap* existing_map = GetFieldPtr<const InferredRegCategoryMap*>(
397 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
Logan Chienfca7e872011-12-20 20:08:22 +0800398
399 DCHECK(existing_map == NULL) << PrettyMethod(this);
400 DCHECK(map != NULL) << PrettyMethod(this);
401
402 // TODO: Remove if we won't find any use of InferredRegCategoryMap at runtime.
403 SetFieldPtr<const InferredRegCategoryMap*>(
404 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), map, false);
405}
406
407void Method::ResetInferredRegCategoryMap() {
408 delete GetInferredRegCategoryMap();
409 SetFieldPtr<const InferredRegCategoryMap*>(
Logan Chiena1854662012-02-17 09:52:18 +0800410 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), NULL, false);
Logan Chienfca7e872011-12-20 20:08:22 +0800411}
412
413#endif
414
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700415size_t Method::NumArgRegisters(const StringPiece& shorty) {
416 CHECK_LE(1, shorty.length());
417 uint32_t num_registers = 0;
418 for (int i = 1; i < shorty.length(); ++i) {
419 char ch = shorty[i];
420 if (ch == 'D' || ch == 'J') {
421 num_registers += 2;
422 } else {
423 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700424 }
425 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426 return num_registers;
427}
428
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429bool Method::IsProxyMethod() const {
430 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700431}
432
Ian Rogers466bb252011-10-14 03:29:56 -0700433Method* Method::FindOverriddenMethod() const {
434 if (IsStatic()) {
435 return NULL;
436 }
437 Class* declaring_class = GetDeclaringClass();
438 Class* super_class = declaring_class->GetSuperClass();
439 uint16_t method_index = GetMethodIndex();
440 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
441 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 // Did this method override a super class method? If so load the result from the super class'
443 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700444 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
445 result = super_class_vtable->Get(method_index);
446 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800447 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800448 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800449 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
450 CHECK_EQ(result,
451 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800452 } else {
453 MethodHelper mh(this);
454 MethodHelper interface_mh;
455 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
456 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
457 InterfaceEntry* entry = iftable->Get(i);
458 Class* interface = entry->GetInterface();
459 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
460 Method* interface_method = interface->GetVirtualMethod(j);
461 interface_mh.ChangeMethod(interface_method);
462 if (mh.HasSameNameAndSignature(&interface_mh)) {
463 result = interface_method;
464 break;
465 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800466 }
467 }
Ian Rogers466bb252011-10-14 03:29:56 -0700468 }
469 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800470#ifndef NDEBUG
471 MethodHelper result_mh(result);
472 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
473#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700474 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700475}
476
Elliott Hughes168670b2012-02-29 16:43:26 -0800477static const void* GetOatCode(const Method* m) {
478 Runtime* runtime = Runtime::Current();
479 const void* code = m->GetCode();
480 // Peel off any method tracing trampoline.
481 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
482 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
483 }
484 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800485 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800486 code = runtime->GetClassLinker()->GetOatCodeFor(m);
487 }
488 return code;
489}
490
Ian Rogersbdb03912011-09-14 00:55:44 -0700491uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700493 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700495 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700496 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700497 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800498 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700499 uint32_t best_offset = 0;
500 uint32_t best_dex_offset = 0;
501 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 uint32_t map_offset = mapping_table[i];
503 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700504 if (map_offset == sought_offset) {
505 best_offset = map_offset;
506 best_dex_offset = map_dex_offset;
507 break;
508 }
509 if (map_offset < sought_offset && map_offset > best_offset) {
510 best_offset = map_offset;
511 best_dex_offset = map_dex_offset;
512 }
513 }
514 return best_dex_offset;
515}
516
517uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700518 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700519 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700520 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700521 return 0; // Special no mapping/pc == 0 case
522 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700523 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700524 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700525 uint32_t map_offset = mapping_table[i];
526 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700527 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800528 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700529 }
530 }
531 LOG(FATAL) << "Looking up Dex PC not contained in method";
532 return 0;
533}
534
535uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800536 MethodHelper mh(this);
537 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700538 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700539 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
540 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700541 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700542 if (iter_type_idx == DexFile::kDexNoIndex16) {
543 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700544 }
545 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800546 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700547 if (iter_exception_type == NULL) {
548 // The verifier should take care of resolving all exception classes early
549 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800550 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700551 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700552 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700553 }
554 }
555 // Handler not found
556 return DexFile::kDexNoIndex;
557}
558
Elliott Hughes77405792012-03-15 15:22:12 -0700559void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700560 // Push a transition back into managed code onto the linked list in thread.
561 CHECK_EQ(Thread::kRunnable, self->GetState());
562 NativeToManagedRecord record;
563 self->PushNativeToManagedRecord(&record);
564
565 // Call the invoke stub associated with the method.
566 // Pass everything as arguments.
567 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700568
569 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700570
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500571 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700572 bool log = false;
573 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800574 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
575 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700576 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700577 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700578 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800579 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
580 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700581 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700582 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800583 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
584 PrettyMethod(this).c_str(), GetCode(), stub,
585 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700586 if (result != NULL) {
587 result->j = 0;
588 }
589 }
590
591 // Pop transition.
592 self->PopNativeToManagedRecord(record);
593}
594
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700595bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700596 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800597 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800598 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700599 return native_method != jni_stub;
600}
601
Ian Rogers60db5ab2012-02-20 17:02:00 -0800602void Method::RegisterNative(Thread* self, const void* native_method) {
603 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700604 CHECK(IsNative()) << PrettyMethod(this);
605 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers60db5ab2012-02-20 17:02:00 -0800606 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
607 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
608 native_method, false);
609 } else {
610 // We've been asked to associate this method with the given native method but are working
611 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
612 // the native method to runtime support and store the target somewhere runtime support will
613 // find it.
614#if defined(__arm__)
615 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
616 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
617#else
618 UNIMPLEMENTED(FATAL);
619#endif
620 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
621 reinterpret_cast<const uint8_t*>(native_method), false);
622 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700623}
624
Ian Rogers19846512012-02-24 11:42:47 -0800625void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700626 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700627 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800628 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700629}
630
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700632 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
633 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700634 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800635 if (new_status == kStatusError) {
636 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
637
638 // stash current exception
639 Thread* self = Thread::Current();
640 SirtRef<Throwable> exception(self->GetException());
641 CHECK(exception.get() != NULL);
642
643 // clear exception to call FindSystemClass
644 self->ClearException();
645 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
646 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
647 CHECK(!self->IsExceptionPending());
648
649 // only verification errors, not initialization problems, should set a verify error.
650 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
651 Class* exception_class = exception->GetClass();
652 if (!eiie_class->IsAssignableFrom(exception_class)) {
653 SetVerifyErrorClass(exception_class);
654 }
655
656 // restore exception
657 self->SetException(exception.get());
658 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700659 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700660}
661
662DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700663 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664}
665
666void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700667 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700668}
669
Brian Carlstrom1f870082011-08-23 16:02:11 -0700670Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700671 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700672 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500673 // TODO: decide whether we want this check. It currently fails during bootstrap.
674 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700675 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800676 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700677}
678
Ian Rogers0571d352011-11-03 19:51:38 -0700679void Class::SetClassSize(size_t new_class_size) {
680 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
681 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
682}
683
Ian Rogersd418eda2012-01-30 12:14:28 -0800684// Return the class' name. The exact format is bizarre, but it's the specified behavior for
685// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
686// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
687// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
688String* Class::ComputeName() {
689 String* name = GetName();
690 if (name != NULL) {
691 return name;
692 }
693 std::string descriptor(ClassHelper(this).GetDescriptor());
694 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
695 // The descriptor indicates that this is the class for
696 // a primitive type; special-case the return value.
697 const char* c_name = NULL;
698 switch (descriptor[0]) {
699 case 'Z': c_name = "boolean"; break;
700 case 'B': c_name = "byte"; break;
701 case 'C': c_name = "char"; break;
702 case 'S': c_name = "short"; break;
703 case 'I': c_name = "int"; break;
704 case 'J': c_name = "long"; break;
705 case 'F': c_name = "float"; break;
706 case 'D': c_name = "double"; break;
707 case 'V': c_name = "void"; break;
708 default:
709 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
710 }
711 name = String::AllocFromModifiedUtf8(c_name);
712 } else {
713 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
714 // components.
715 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
716 descriptor.erase(0, 1);
717 descriptor.erase(descriptor.size() - 1);
718 }
719 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
720 name = String::AllocFromModifiedUtf8(descriptor.c_str());
721 }
722 SetName(name);
723 return name;
724}
725
Elliott Hughes4681c802011-09-25 18:04:37 -0700726void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700727 if ((flags & kDumpClassFullDetail) == 0) {
728 os << PrettyClass(this);
729 if ((flags & kDumpClassClassLoader) != 0) {
730 os << ' ' << GetClassLoader();
731 }
732 if ((flags & kDumpClassInitialized) != 0) {
733 os << ' ' << GetStatus();
734 }
Elliott Hughese0918552011-10-28 17:18:29 -0700735 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700736 return;
737 }
738
739 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800740 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700741 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800742 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700743 os << " objectSize=" << SizeOf() << " "
744 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
745 os << StringPrintf(" access=0x%04x.%04x\n",
746 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
747 if (super != NULL) {
748 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
749 }
750 if (IsArrayClass()) {
751 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
752 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800753 if (kh.NumInterfaces() > 0) {
754 os << " interfaces (" << kh.NumInterfaces() << "):\n";
755 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
756 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700757 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800758 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700759 }
760 }
761 os << " vtable (" << NumVirtualMethods() << " entries, "
762 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
763 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800764 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700765 }
766 os << " direct methods (" << NumDirectMethods() << " entries):\n";
767 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800768 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700769 }
770 if (NumStaticFields() > 0) {
771 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700772 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700773 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800774 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700775 }
776 } else {
777 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700778 }
779 }
780 if (NumInstanceFields() > 0) {
781 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700782 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700783 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800784 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700785 }
786 } else {
787 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700788 }
789 }
790}
791
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700792void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
793 if (new_reference_offsets != CLASS_WALK_SUPER) {
794 // Sanity check that the number of bits set in the reference offset bitmap
795 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800796 size_t count = 0;
797 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
798 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700799 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800800 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801 }
802 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
803 new_reference_offsets, false);
804}
805
806void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
807 if (new_reference_offsets != CLASS_WALK_SUPER) {
808 // Sanity check that the number of bits set in the reference offset bitmap
809 // agrees with the number of references
810 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
811 NumReferenceStaticFieldsDuringLinking());
812 }
813 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
814 new_reference_offsets, false);
815}
816
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700817bool Class::Implements(const Class* klass) const {
818 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700819 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700820 // All interfaces implemented directly and by our superclass, and
821 // recursively all super-interfaces of those interfaces, are listed
822 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700823 int32_t iftable_count = GetIfTableCount();
824 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
825 for (int32_t i = 0; i < iftable_count; i++) {
826 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700827 return true;
828 }
829 }
830 return false;
831}
832
Elliott Hughese84278b2012-03-22 10:06:53 -0700833// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700834// are array classes.
835//
836// Consider an array class, e.g. Y[][], where Y is a subclass of X.
837// Y[][] = Y[][] --> true (identity)
838// X[][] = Y[][] --> true (element superclass)
839// Y = Y[][] --> false
840// Y[] = Y[][] --> false
841// Object = Y[][] --> true (everything is an object)
842// Object[] = Y[][] --> true
843// Object[][] = Y[][] --> true
844// Object[][][] = Y[][] --> false (too many []s)
845// Serializable = Y[][] --> true (all arrays are Serializable)
846// Serializable[] = Y[][] --> true
847// Serializable[][] = Y[][] --> false (unless Y is Serializable)
848//
849// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700850// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700851//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700852bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700853 DCHECK(IsArrayClass()) << PrettyClass(this);
854 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700855 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700856}
857
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700858bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700859 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
860 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700861 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700862 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700863 // src's super should be java_lang_Object, since it is an array.
864 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700865 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
866 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700867 return this == java_lang_Object;
868 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700869 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700870}
871
872bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700873 DCHECK(!IsInterface()) << PrettyClass(this);
874 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700875 const Class* current = this;
876 do {
877 if (current == klass) {
878 return true;
879 }
880 current = current->GetSuperClass();
881 } while (current != NULL);
882 return false;
883}
884
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800885bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700886 size_t i = 0;
887 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
888 ++i;
889 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700890 if (descriptor1.find('/', i) != StringPiece::npos ||
891 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700892 return false;
893 } else {
894 return true;
895 }
896}
897
898bool Class::IsInSamePackage(const Class* that) const {
899 const Class* klass1 = this;
900 const Class* klass2 = that;
901 if (klass1 == klass2) {
902 return true;
903 }
904 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700905 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700906 return false;
907 }
908 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700909 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700910 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700911 }
jeffhao4a801a42011-09-23 13:53:40 -0700912 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700913 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700914 }
915 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800916 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800917 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800918 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800919 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800920 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700921}
922
Elliott Hughesdbb40792011-11-18 17:05:22 -0800923bool Class::IsClassClass() const {
924 Class* java_lang_Class = GetClass()->GetClass();
925 return this == java_lang_Class;
926}
927
928bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800929 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800930}
931
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800932bool Class::IsThrowableClass() const {
933 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
934 return throwable->IsAssignableFrom(this);
935}
936
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800937ClassLoader* Class::GetClassLoader() const {
938 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700939}
940
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700941void Class::SetClassLoader(const ClassLoader* new_cl) {
942 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700944}
945
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800946Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700947 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700948 DCHECK(declaring_class != NULL) << PrettyClass(this);
949 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700950 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700951 int32_t iftable_count = GetIfTableCount();
952 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
953 for (int32_t i = 0; i < iftable_count; i++) {
954 InterfaceEntry* interface_entry = iftable->Get(i);
955 if (interface_entry->GetInterface() == declaring_class) {
956 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700957 }
958 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700959 return NULL;
960}
961
Ian Rogers466bb252011-10-14 03:29:56 -0700962Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700963 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800964 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700965 if (method != NULL) {
966 return method;
967 }
968
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700969 int32_t iftable_count = GetIfTableCount();
970 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
971 for (int32_t i = 0; i < iftable_count; i++) {
972 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700973 if (method != NULL) {
974 return method;
975 }
976 }
977 return NULL;
978}
979
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800980Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
981 // Check the current class before checking the interfaces.
982 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
983 if (method != NULL) {
984 return method;
985 }
986
987 int32_t iftable_count = GetIfTableCount();
988 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
989 for (int32_t i = 0; i < iftable_count; i++) {
990 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
991 if (method != NULL) {
992 return method;
993 }
994 }
995 return NULL;
996}
997
998
999Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001000 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001001 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001002 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001003 mh.ChangeMethod(method);
1004 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001005 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001006 }
1007 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001008 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001009}
1010
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001011Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1012 if (GetDexCache() == dex_cache) {
1013 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1014 Method* method = GetDirectMethod(i);
1015 if (method->GetDexMethodIndex() == dex_method_idx) {
1016 return method;
1017 }
1018 }
1019 }
1020 return NULL;
1021}
1022
1023Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1024 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001025 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001026 if (method != NULL) {
1027 return method;
1028 }
1029 }
1030 return NULL;
1031}
1032
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001033Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1034 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1035 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1036 if (method != NULL) {
1037 return method;
1038 }
1039 }
1040 return NULL;
1041}
1042
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001043Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001044 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001045 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001046 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001047 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001048 mh.ChangeMethod(method);
1049 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001050 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001051 }
1052 }
1053 return NULL;
1054}
1055
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001056Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1057 if (GetDexCache() == dex_cache) {
1058 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1059 Method* method = GetVirtualMethod(i);
1060 if (method->GetDexMethodIndex() == dex_method_idx) {
1061 return method;
1062 }
1063 }
1064 }
1065 return NULL;
1066}
1067
Ian Rogers466bb252011-10-14 03:29:56 -07001068Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1069 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1070 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1071 if (method != NULL) {
1072 return method;
1073 }
1074 }
1075 return NULL;
1076}
1077
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001078Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1079 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1080 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1081 if (method != NULL) {
1082 return method;
1083 }
1084 }
1085 return NULL;
1086}
1087
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001088Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001089 // Is the field in this class?
1090 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001091 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001092 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1093 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001094 fh.ChangeField(f);
1095 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001096 return f;
1097 }
1098 }
1099 return NULL;
1100}
1101
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001102Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1103 if (GetDexCache() == dex_cache) {
1104 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1105 Field* f = GetInstanceField(i);
1106 if (f->GetDexFieldIndex() == dex_field_idx) {
1107 return f;
1108 }
1109 }
1110 }
1111 return NULL;
1112}
1113
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001114Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001115 // Is the field in this class, or any of its superclasses?
1116 // Interfaces are not relevant because they can't contain instance fields.
1117 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001118 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001119 if (f != NULL) {
1120 return f;
1121 }
1122 }
1123 return NULL;
1124}
1125
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001126Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1127 // Is the field in this class, or any of its superclasses?
1128 // Interfaces are not relevant because they can't contain instance fields.
1129 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1130 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1131 if (f != NULL) {
1132 return f;
1133 }
1134 }
1135 return NULL;
1136}
1137
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001138Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1139 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001140 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001141 for (size_t i = 0; i < NumStaticFields(); ++i) {
1142 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001143 fh.ChangeField(f);
1144 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001145 return f;
1146 }
1147 }
1148 return NULL;
1149}
1150
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001151Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1152 if (dex_cache == GetDexCache()) {
1153 for (size_t i = 0; i < NumStaticFields(); ++i) {
1154 Field* f = GetStaticField(i);
1155 if (f->GetDexFieldIndex() == dex_field_idx) {
1156 return f;
1157 }
1158 }
1159 }
1160 return NULL;
1161}
1162
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001163Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1164 // Is the field in this class (or its interfaces), or any of its
1165 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001166 ClassHelper kh;
1167 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001168 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001169 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001170 if (f != NULL) {
1171 return f;
1172 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001173 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001174 kh.ChangeClass(k);
1175 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1176 Class* interface = kh.GetInterface(i);
1177 f = interface->FindDeclaredStaticField(name, type);
1178 if (f != NULL) {
1179 return f;
1180 }
1181 }
1182 }
1183 return NULL;
1184}
1185
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001186Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1187 ClassHelper kh;
1188 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1189 // Is the field in this class?
1190 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1191 if (f != NULL) {
1192 return f;
1193 }
1194 // Is this field in any of this class' interfaces?
1195 kh.ChangeClass(k);
1196 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1197 Class* interface = kh.GetInterface(i);
1198 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1199 if (f != NULL) {
1200 return f;
1201 }
1202 }
1203 }
1204 return NULL;
1205}
1206
Ian Rogersb067ac22011-12-13 18:05:09 -08001207Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1208 // Find a field using the JLS field resolution order
1209 ClassHelper kh;
1210 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1211 // Is the field in this class?
1212 Field* f = k->FindDeclaredInstanceField(name, type);
1213 if (f != NULL) {
1214 return f;
1215 }
1216 f = k->FindDeclaredStaticField(name, type);
1217 if (f != NULL) {
1218 return f;
1219 }
1220 // Is this field in any of this class' interfaces?
1221 kh.ChangeClass(k);
1222 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1223 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001224 f = interface->FindDeclaredStaticField(name, type);
1225 if (f != NULL) {
1226 return f;
1227 }
1228 }
1229 }
1230 return NULL;
1231}
1232
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001233Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001234 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001235 DCHECK_GE(component_count, 0);
1236 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001237
Ian Rogersa15e67d2012-02-28 13:51:55 -08001238 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001239 size_t data_size = component_count * component_size;
1240 size_t size = header_size + data_size;
1241
1242 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1243 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1244 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1245 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001246 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001247 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001248 return NULL;
1249 }
1250
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001251 Heap* heap = Runtime::Current()->GetHeap();
1252 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001253 if (array != NULL) {
1254 DCHECK(array->IsArrayInstance());
1255 array->SetLength(component_count);
1256 }
1257 return array;
1258}
1259
1260Array* Array::Alloc(Class* array_class, int32_t component_count) {
1261 return Alloc(array_class, component_count, array_class->GetComponentSize());
1262}
1263
Elliott Hughes80609252011-09-23 17:24:51 -07001264bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001265 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001266 "length=%i; index=%i", length_, index);
1267 return false;
1268}
1269
1270bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001271 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001272 "Can't store an element of type %s into an array of type %s",
1273 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1274 return false;
1275}
1276
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001277template<typename T>
1278PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001279 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001280 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1281 return down_cast<PrimitiveArray<T>*>(raw_array);
1282}
1283
1284template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1285
1286// Explicitly instantiate all the primitive array types.
1287template class PrimitiveArray<uint8_t>; // BooleanArray
1288template class PrimitiveArray<int8_t>; // ByteArray
1289template class PrimitiveArray<uint16_t>; // CharArray
1290template class PrimitiveArray<double>; // DoubleArray
1291template class PrimitiveArray<float>; // FloatArray
1292template class PrimitiveArray<int32_t>; // IntArray
1293template class PrimitiveArray<int64_t>; // LongArray
1294template class PrimitiveArray<int16_t>; // ShortArray
1295
Ian Rogers466bb252011-10-14 03:29:56 -07001296// Explicitly instantiate Class[][]
1297template class ObjectArray<ObjectArray<Class> >;
1298
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001299// TODO: get global references for these
1300Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001301
Brian Carlstroma663ea52011-08-19 23:33:41 -07001302void String::SetClass(Class* java_lang_String) {
1303 CHECK(java_lang_String_ == NULL);
1304 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001305 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001306}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001307
Brian Carlstroma663ea52011-08-19 23:33:41 -07001308void String::ResetClass() {
1309 CHECK(java_lang_String_ != NULL);
1310 java_lang_String_ = NULL;
1311}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001312
Brian Carlstromc74255f2011-09-11 22:47:39 -07001313String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001314 return Runtime::Current()->GetInternTable()->InternWeak(this);
1315}
1316
Brian Carlstrom395520e2011-09-25 19:35:00 -07001317int32_t String::GetHashCode() {
1318 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1319 if (result == 0) {
1320 ComputeHashCode();
1321 }
1322 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1323 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1324 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001325 return result;
1326}
1327
1328int32_t String::GetLength() const {
1329 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1330 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1331 return result;
1332}
1333
1334uint16_t String::CharAt(int32_t index) const {
1335 // TODO: do we need this? Equals is the only caller, and could
1336 // bounds check itself.
1337 if (index < 0 || index >= count_) {
1338 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001339 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001340 "length=%i; index=%i", count_, index);
1341 return 0;
1342 }
1343 return GetCharArray()->Get(index + GetOffset());
1344}
1345
1346String* String::AllocFromUtf16(int32_t utf16_length,
1347 const uint16_t* utf16_data_in,
1348 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001349 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001350 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001351 if (string == NULL) {
1352 return NULL;
1353 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001354 // TODO: use 16-bit wide memset variant
1355 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001356 if (array == NULL) {
1357 return NULL;
1358 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001359 for (int i = 0; i < utf16_length; i++) {
1360 array->Set(i, utf16_data_in[i]);
1361 }
1362 if (hash_code != 0) {
1363 string->SetHashCode(hash_code);
1364 } else {
1365 string->ComputeHashCode();
1366 }
1367 return string;
1368}
1369
1370String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001371 if (utf == NULL) {
1372 return NULL;
1373 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 size_t char_count = CountModifiedUtf8Chars(utf);
1375 return AllocFromModifiedUtf8(char_count, utf);
1376}
1377
1378String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1379 const char* utf8_data_in) {
1380 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001381 if (string == NULL) {
1382 return NULL;
1383 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001384 uint16_t* utf16_data_out =
1385 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1386 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1387 string->ComputeHashCode();
1388 return string;
1389}
1390
1391String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001392 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1393 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001394 return NULL;
1395 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001396 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001397}
1398
1399String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001400 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001401 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001402 if (string == NULL) {
1403 return NULL;
1404 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001405 string->SetArray(array);
1406 string->SetCount(array->GetLength());
1407 return string;
1408}
1409
1410bool String::Equals(const String* that) const {
1411 if (this == that) {
1412 // Quick reference equality test
1413 return true;
1414 } else if (that == NULL) {
1415 // Null isn't an instanceof anything
1416 return false;
1417 } else if (this->GetLength() != that->GetLength()) {
1418 // Quick length inequality test
1419 return false;
1420 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001421 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001422 // hash code was already equal
1423 for (int32_t i = 0; i < that->GetLength(); ++i) {
1424 if (this->CharAt(i) != that->CharAt(i)) {
1425 return false;
1426 }
1427 }
1428 return true;
1429 }
1430}
1431
Elliott Hughes5d78d392011-12-13 16:53:05 -08001432bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001433 if (this->GetLength() != that_length) {
1434 return false;
1435 } else {
1436 for (int32_t i = 0; i < that_length; ++i) {
1437 if (this->CharAt(i) != that_chars[that_offset + i]) {
1438 return false;
1439 }
1440 }
1441 return true;
1442 }
1443}
1444
1445bool String::Equals(const char* modified_utf8) const {
1446 for (int32_t i = 0; i < GetLength(); ++i) {
1447 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1448 if (ch == '\0' || ch != CharAt(i)) {
1449 return false;
1450 }
1451 }
1452 return *modified_utf8 == '\0';
1453}
1454
1455bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001456 if (modified_utf8.size() != GetLength()) {
1457 return false;
1458 }
1459 const char* p = modified_utf8.data();
1460 for (int32_t i = 0; i < GetLength(); ++i) {
1461 uint16_t ch = GetUtf16FromUtf8(&p);
1462 if (ch != CharAt(i)) {
1463 return false;
1464 }
1465 }
1466 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001467}
1468
1469// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1470std::string String::ToModifiedUtf8() const {
1471 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1472 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1473 std::string result(byte_count, char(0));
1474 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1475 return result;
1476}
1477
Ian Rogers1c5eb702012-02-01 09:18:34 -08001478void Throwable::SetCause(Throwable* cause) {
1479 CHECK(cause != NULL);
1480 CHECK(cause != this);
1481 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1482 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1483}
1484
Ian Rogers466bb252011-10-14 03:29:56 -07001485bool Throwable::IsCheckedException() const {
1486 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1487 if (InstanceOf(error)) {
1488 return false;
1489 }
1490 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1491 return !InstanceOf(jlre);
1492}
1493
Ian Rogers9074b992011-10-26 17:41:55 -07001494std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001495 std::string result(PrettyTypeOf(this));
1496 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001497 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001498 if (msg != NULL) {
1499 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001500 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001501 result += "\n";
1502 Object* stack_state = GetStackState();
1503 // check stack state isn't missing or corrupt
1504 if (stack_state != NULL && stack_state->IsObjectArray()) {
1505 // Decode the internal stack trace into the depth and method trace
1506 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1507 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001508 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1509 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001510 for (int32_t i = 0; i < depth; ++i) {
1511 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001512 mh.ChangeMethod(method);
1513 uint32_t native_pc = pc_trace->Get(i);
1514 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1515 const char* source_file = mh.GetDeclaringClassSourceFile();
1516 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1517 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001518 }
Ian Rogers9074b992011-10-26 17:41:55 -07001519 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001520 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001521 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001522 result += "Caused by: ";
1523 result += cause->Dump();
1524 }
Ian Rogers9074b992011-10-26 17:41:55 -07001525 return result;
1526}
1527
Ian Rogers5167c972012-02-03 10:41:20 -08001528
1529Class* Throwable::java_lang_Throwable_ = NULL;
1530
1531void Throwable::SetClass(Class* java_lang_Throwable) {
1532 CHECK(java_lang_Throwable_ == NULL);
1533 CHECK(java_lang_Throwable != NULL);
1534 java_lang_Throwable_ = java_lang_Throwable;
1535}
1536
1537void Throwable::ResetClass() {
1538 CHECK(java_lang_Throwable_ != NULL);
1539 java_lang_Throwable_ = NULL;
1540}
1541
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001542Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1543
1544void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1545 CHECK(java_lang_StackTraceElement_ == NULL);
1546 CHECK(java_lang_StackTraceElement != NULL);
1547 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1548}
1549
1550void StackTraceElement::ResetClass() {
1551 CHECK(java_lang_StackTraceElement_ != NULL);
1552 java_lang_StackTraceElement_ = NULL;
1553}
1554
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001555StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1556 String* method_name,
1557 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001558 int32_t line_number) {
1559 StackTraceElement* trace =
1560 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1561 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1562 const_cast<String*>(declaring_class), false);
1563 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1564 const_cast<String*>(method_name), false);
1565 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1566 const_cast<String*>(file_name), false);
1567 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1568 line_number, false);
1569 return trace;
1570}
1571
Elliott Hughes1f359b02011-07-17 14:27:17 -07001572static const char* kClassStatusNames[] = {
1573 "Error",
1574 "NotReady",
1575 "Idx",
1576 "Loaded",
1577 "Resolved",
1578 "Verifying",
1579 "Verified",
1580 "Initializing",
1581 "Initialized"
1582};
1583std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1584 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001585 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001586 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001587 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001588 }
1589 return os;
1590}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001591
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001592} // namespace art