blob: 97634d550a41ae1f3a32c876548f0a2cc5a35487 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro3ee755d2011-06-28 12:11:04 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "object.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <string.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020
Ian Rogersdf20fe02011-07-20 20:34:16 -070021#include <algorithm>
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070022#include <iostream>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070023#include <string>
24#include <utility>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070026#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_loader.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070028#include "dex_cache.h"
29#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070031#include "heap.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070032#include "intern_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "logging.h"
Elliott Hughes54e7df12011-09-16 11:47:04 -070034#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080035#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070036#include "runtime.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070037#include "stack.h"
Ian Rogers0571d352011-11-03 19:51:38 -070038#include "utils.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070039
Logan Chienfca7e872011-12-20 20:08:22 +080040#if defined(ART_USE_LLVM_COMPILER)
41#include "compiler_llvm/inferred_reg_category_map.h"
42using art::compiler_llvm::InferredRegCategoryMap;
43#endif
44
Carl Shapiro3ee755d2011-06-28 12:11:04 -070045namespace art {
46
Elliott Hughesdbb40792011-11-18 17:05:22 -080047String* Object::AsString() {
48 DCHECK(GetClass()->IsStringClass());
49 return down_cast<String*>(this);
50}
51
Elliott Hughes081be7f2011-09-18 16:50:26 -070052Object* Object::Clone() {
53 Class* c = GetClass();
54 DCHECK(!c->IsClassClass());
55
56 // Object::SizeOf gets the right size even if we're an array.
57 // Using c->AllocObject() here would be wrong.
58 size_t num_bytes = SizeOf();
Brian Carlstrom40381fb2011-10-19 14:13:40 -070059 SirtRef<Object> copy(Heap::AllocObject(c, num_bytes));
60 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070061 return NULL;
62 }
63
64 // Copy instance data. We assume memcpy copies by words.
65 // TODO: expose and use move32.
66 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070067 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070068 size_t offset = sizeof(Object);
69 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
70
Elliott Hughes20cde902011-10-04 17:37:27 -070071 if (c->IsFinalizable()) {
Ian Rogers5d4bdc22011-11-02 22:15:43 -070072 Heap::AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070073 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070074
Brian Carlstrom40381fb2011-10-19 14:13:40 -070075 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070076}
77
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070078uint32_t Object::GetThinLockId() {
79 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070080}
81
82void Object::MonitorEnter(Thread* thread) {
83 Monitor::MonitorEnter(thread, this);
84}
85
Ian Rogersff1ed472011-09-20 13:46:24 -070086bool Object::MonitorExit(Thread* thread) {
87 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070088}
89
90void Object::Notify() {
91 Monitor::Notify(Thread::Current(), this);
92}
93
94void Object::NotifyAll() {
95 Monitor::NotifyAll(Thread::Current(), this);
96}
97
98void Object::Wait(int64_t ms, int32_t ns) {
99 Monitor::Wait(Thread::Current(), this, ms, ns, true);
100}
101
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700102// TODO: get global references for these
103Class* Field::java_lang_reflect_Field_ = NULL;
104
105void Field::SetClass(Class* java_lang_reflect_Field) {
106 CHECK(java_lang_reflect_Field_ == NULL);
107 CHECK(java_lang_reflect_Field != NULL);
108 java_lang_reflect_Field_ = java_lang_reflect_Field;
109}
110
111void Field::ResetClass() {
112 CHECK(java_lang_reflect_Field_ != NULL);
113 java_lang_reflect_Field_ = NULL;
114}
115
Ian Rogers0571d352011-11-03 19:51:38 -0700116void Field::SetOffset(MemberOffset num_bytes) {
117 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800118#if 0 // TODO enable later in boot and under !NDEBUG
119 FieldHelper fh(this);
120 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700121 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
122 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
123 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800124#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700125 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
126}
127
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700128uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700129 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130 if (IsStatic()) {
131 object = declaring_class_;
132 }
133 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700134}
135
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700136void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700137 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700138 if (IsStatic()) {
139 object = declaring_class_;
140 }
141 object->SetField32(GetOffset(), new_value, IsVolatile());
142}
143
144uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700145 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700146 if (IsStatic()) {
147 object = declaring_class_;
148 }
149 return object->GetField64(GetOffset(), IsVolatile());
150}
151
152void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700153 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700154 if (IsStatic()) {
155 object = declaring_class_;
156 }
157 object->SetField64(GetOffset(), new_value, IsVolatile());
158}
159
160Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700161 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162 if (IsStatic()) {
163 object = declaring_class_;
164 }
165 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
166}
167
168void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700169 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 if (IsStatic()) {
171 object = declaring_class_;
172 }
173 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
174}
175
176bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800177 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
178 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700179 return Get32(object);
180}
181
182void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800183 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
184 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700185 Set32(object, z);
186}
187
188int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800189 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
190 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191 return Get32(object);
192}
193
194void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800195 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
196 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700197 Set32(object, b);
198}
199
200uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
202 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700203 return Get32(object);
204}
205
206void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
208 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 Set32(object, c);
210}
211
Ian Rogers466bb252011-10-14 03:29:56 -0700212int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800213 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
214 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700215 return Get32(object);
216}
217
Ian Rogers466bb252011-10-14 03:29:56 -0700218void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800219 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
220 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221 Set32(object, s);
222}
223
224int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
226 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 return Get32(object);
228}
229
230void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800231 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
232 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700233 Set32(object, i);
234}
235
236int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800237 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
238 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239 return Get64(object);
240}
241
242void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800243 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
244 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700245 Set64(object, j);
246}
247
248float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
250 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700251 JValue float_bits;
252 float_bits.i = Get32(object);
253 return float_bits.f;
254}
255
256void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800257 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
258 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259 JValue float_bits;
260 float_bits.f = f;
261 Set32(object, float_bits.i);
262}
263
264double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
266 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 JValue double_bits;
268 double_bits.j = Get64(object);
269 return double_bits.d;
270}
271
272void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800273 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
274 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 JValue double_bits;
276 double_bits.d = d;
277 Set64(object, double_bits.j);
278}
279
280Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
282 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 return GetObj(object);
284}
285
286void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800287 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
288 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700289 SetObj(object, l);
290}
291
292// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700293Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700294Class* Method::java_lang_reflect_Method_ = NULL;
295
Elliott Hughes80609252011-09-23 17:24:51 -0700296void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
297 CHECK(java_lang_reflect_Constructor_ == NULL);
298 CHECK(java_lang_reflect_Constructor != NULL);
299 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
300
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 CHECK(java_lang_reflect_Method_ == NULL);
302 CHECK(java_lang_reflect_Method != NULL);
303 java_lang_reflect_Method_ = java_lang_reflect_Method;
304}
305
Elliott Hughes80609252011-09-23 17:24:51 -0700306void Method::ResetClasses() {
307 CHECK(java_lang_reflect_Constructor_ != NULL);
308 java_lang_reflect_Constructor_ = NULL;
309
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700310 CHECK(java_lang_reflect_Method_ != NULL);
311 java_lang_reflect_Method_ = NULL;
312}
313
Elliott Hughes418d20f2011-09-22 14:00:39 -0700314Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
315 if (*p == '[') {
316 // Something like "[[[Ljava/lang/String;".
317 const char* start = p;
318 while (*p == '[') {
319 ++p;
320 }
321 if (*p == 'L') {
322 while (*p != ';') {
323 ++p;
324 }
325 }
326 ++p; // Either the ';' or the primitive type.
327
Brian Carlstromaded5f72011-10-07 17:15:04 -0700328 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800329 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700330 } else if (*p == 'L') {
331 const char* start = p;
332 while (*p != ';') {
333 ++p;
334 }
335 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800336 std::string descriptor(start, (p - start));
337 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700338 } else {
339 return class_linker->FindPrimitiveClass(*p++);
340 }
341}
342
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343ObjectArray<String>* Method::GetDexCacheStrings() const {
344 return GetFieldObject<ObjectArray<String>*>(
345 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
346}
347
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
349 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
350 new_dex_cache_strings, false);
351}
352
353ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
354 return GetFieldObject<ObjectArray<Class>*>(
355 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
356}
357
358void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
359 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
360 new_dex_cache_classes, false);
361}
362
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363CodeAndDirectMethods* Method::GetDexCacheCodeAndDirectMethods() const {
364 return GetFieldPtr<CodeAndDirectMethods*>(
365 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
366 false);
367}
368
369void Method::SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value) {
370 SetFieldPtr<CodeAndDirectMethods*>(
371 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_),
372 new_value, false);
373}
374
375ObjectArray<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) {
396 const InferredRegCategoryMap* existing_map = GetInferredRegCategoryMap();
397
398 DCHECK(existing_map == NULL) << PrettyMethod(this);
399 DCHECK(map != NULL) << PrettyMethod(this);
400
401 // TODO: Remove if we won't find any use of InferredRegCategoryMap at runtime.
402 SetFieldPtr<const InferredRegCategoryMap*>(
403 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), map, false);
404}
405
406void Method::ResetInferredRegCategoryMap() {
407 delete GetInferredRegCategoryMap();
408 SetFieldPtr<const InferredRegCategoryMap*>(
409 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), NULL, false);
410}
411
412#endif
413
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414size_t Method::NumArgRegisters(const StringPiece& shorty) {
415 CHECK_LE(1, shorty.length());
416 uint32_t num_registers = 0;
417 for (int i = 1; i < shorty.length(); ++i) {
418 char ch = shorty[i];
419 if (ch == 'D' || ch == 'J') {
420 num_registers += 2;
421 } else {
422 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700423 }
424 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 return num_registers;
426}
427
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800428bool Method::IsProxyMethod() const {
429 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430}
431
Ian Rogers466bb252011-10-14 03:29:56 -0700432Method* Method::FindOverriddenMethod() const {
433 if (IsStatic()) {
434 return NULL;
435 }
436 Class* declaring_class = GetDeclaringClass();
437 Class* super_class = declaring_class->GetSuperClass();
438 uint16_t method_index = GetMethodIndex();
439 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
440 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800441 // Did this method override a super class method? If so load the result from the super class'
442 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700443 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
444 result = super_class_vtable->Get(method_index);
445 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800447 if (IsProxyMethod()) {
448 result = Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this);
449 } else {
450 MethodHelper mh(this);
451 MethodHelper interface_mh;
452 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
453 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
454 InterfaceEntry* entry = iftable->Get(i);
455 Class* interface = entry->GetInterface();
456 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
457 Method* interface_method = interface->GetVirtualMethod(j);
458 interface_mh.ChangeMethod(interface_method);
459 if (mh.HasSameNameAndSignature(&interface_mh)) {
460 result = interface_method;
461 break;
462 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800463 }
464 }
Ian Rogers466bb252011-10-14 03:29:56 -0700465 }
466 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800467#ifndef NDEBUG
468 MethodHelper result_mh(result);
469 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
470#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700471 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700472}
473
Ian Rogersbdb03912011-09-14 00:55:44 -0700474uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700475 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700476 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800477 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700478 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700479 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700480 size_t mapping_table_length = GetMappingTableLength();
jeffhao26c0a1a2012-01-17 16:28:33 -0800481 const void* code_offset;
482 if (Runtime::Current()->IsMethodTracingActive() &&
483 Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this) != NULL) {
484 code_offset = Runtime::Current()->GetTracer()->GetSavedCodeFromMap(this);
485 } else {
486 code_offset = GetCode();
487 }
jeffhaoe343b762011-12-05 16:36:44 -0800488 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code_offset);
Ian Rogersbdb03912011-09-14 00:55:44 -0700489 uint32_t best_offset = 0;
490 uint32_t best_dex_offset = 0;
491 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700492 uint32_t map_offset = mapping_table[i];
493 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700494 if (map_offset == sought_offset) {
495 best_offset = map_offset;
496 best_dex_offset = map_dex_offset;
497 break;
498 }
499 if (map_offset < sought_offset && map_offset > best_offset) {
500 best_offset = map_offset;
501 best_dex_offset = map_dex_offset;
502 }
503 }
504 return best_dex_offset;
505}
506
507uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700508 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700509 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700510 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700511 return 0; // Special no mapping/pc == 0 case
512 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700513 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700514 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700515 uint32_t map_offset = mapping_table[i];
516 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700517 if (map_dex_offset == dex_pc) {
jeffhao2692b572011-12-16 15:42:28 -0800518 if (Runtime::Current()->IsMethodTracingActive()) {
519 Trace* tracer = Runtime::Current()->GetTracer();
520 return reinterpret_cast<uintptr_t>(tracer->GetSavedCodeFromMap(this)) + map_offset;
jeffhaoe343b762011-12-05 16:36:44 -0800521 } else {
522 return reinterpret_cast<uintptr_t>(GetCode()) + map_offset;
523 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700524 }
525 }
526 LOG(FATAL) << "Looking up Dex PC not contained in method";
527 return 0;
528}
529
530uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800531 MethodHelper mh(this);
532 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700533 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700534 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
535 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700536 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700537 if (iter_type_idx == DexFile::kDexNoIndex16) {
538 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700539 }
540 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800541 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700542 if (iter_exception_type == NULL) {
543 // The verifier should take care of resolving all exception classes early
544 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700546 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700547 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700548 }
549 }
550 // Handler not found
551 return DexFile::kDexNoIndex;
552}
553
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700554void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const {
555 // Push a transition back into managed code onto the linked list in thread.
556 CHECK_EQ(Thread::kRunnable, self->GetState());
557 NativeToManagedRecord record;
558 self->PushNativeToManagedRecord(&record);
559
560 // Call the invoke stub associated with the method.
561 // Pass everything as arguments.
562 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700563
564 bool have_executable_code = (GetCode() != NULL);
565#if !defined(__arm__)
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700566 // Currently we can only compile non-native methods for ARM.
567 have_executable_code = IsNative();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700568#endif
569
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500570 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700571 bool log = false;
572 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800573 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
574 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700575 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700576 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700577 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800578 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
579 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700580 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700581 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800582 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
583 PrettyMethod(this).c_str(), GetCode(), stub,
584 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700585 if (result != NULL) {
586 result->j = 0;
587 }
588 }
589
590 // Pop transition.
591 self->PopNativeToManagedRecord(record);
592}
593
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700594bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700595 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers169c9a72011-11-13 20:13:17 -0800596 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700597 return native_method != jni_stub;
598}
599
600void Method::RegisterNative(const void* native_method) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700601 CHECK(IsNative()) << PrettyMethod(this);
602 CHECK(native_method != NULL) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700603 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
604 native_method, false);
605}
606
607void Method::UnregisterNative() {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700608 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700609 // restore stub to lookup native pointer via dlsym
Ian Rogers169c9a72011-11-13 20:13:17 -0800610 RegisterNative(Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700611}
612
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700613void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700614 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
615 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700616 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800617 if (new_status == kStatusError) {
618 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
619
620 // stash current exception
621 Thread* self = Thread::Current();
622 SirtRef<Throwable> exception(self->GetException());
623 CHECK(exception.get() != NULL);
624
625 // clear exception to call FindSystemClass
626 self->ClearException();
627 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
628 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
629 CHECK(!self->IsExceptionPending());
630
631 // only verification errors, not initialization problems, should set a verify error.
632 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
633 Class* exception_class = exception->GetClass();
634 if (!eiie_class->IsAssignableFrom(exception_class)) {
635 SetVerifyErrorClass(exception_class);
636 }
637
638 // restore exception
639 self->SetException(exception.get());
640 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700641 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700642}
643
644DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700645 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700646}
647
648void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700649 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700650}
651
Brian Carlstrom1f870082011-08-23 16:02:11 -0700652Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700653 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700654 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500655 // TODO: decide whether we want this check. It currently fails during bootstrap.
656 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700657 DCHECK_GE(this->object_size_, sizeof(Object));
Brian Carlstrom1f870082011-08-23 16:02:11 -0700658 return Heap::AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700659}
660
Ian Rogers0571d352011-11-03 19:51:38 -0700661void Class::SetClassSize(size_t new_class_size) {
662 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
663 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
664}
665
Ian Rogersd418eda2012-01-30 12:14:28 -0800666// Return the class' name. The exact format is bizarre, but it's the specified behavior for
667// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
668// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
669// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
670String* Class::ComputeName() {
671 String* name = GetName();
672 if (name != NULL) {
673 return name;
674 }
675 std::string descriptor(ClassHelper(this).GetDescriptor());
676 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
677 // The descriptor indicates that this is the class for
678 // a primitive type; special-case the return value.
679 const char* c_name = NULL;
680 switch (descriptor[0]) {
681 case 'Z': c_name = "boolean"; break;
682 case 'B': c_name = "byte"; break;
683 case 'C': c_name = "char"; break;
684 case 'S': c_name = "short"; break;
685 case 'I': c_name = "int"; break;
686 case 'J': c_name = "long"; break;
687 case 'F': c_name = "float"; break;
688 case 'D': c_name = "double"; break;
689 case 'V': c_name = "void"; break;
690 default:
691 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
692 }
693 name = String::AllocFromModifiedUtf8(c_name);
694 } else {
695 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
696 // components.
697 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
698 descriptor.erase(0, 1);
699 descriptor.erase(descriptor.size() - 1);
700 }
701 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
702 name = String::AllocFromModifiedUtf8(descriptor.c_str());
703 }
704 SetName(name);
705 return name;
706}
707
Elliott Hughes4681c802011-09-25 18:04:37 -0700708void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700709 if ((flags & kDumpClassFullDetail) == 0) {
710 os << PrettyClass(this);
711 if ((flags & kDumpClassClassLoader) != 0) {
712 os << ' ' << GetClassLoader();
713 }
714 if ((flags & kDumpClassInitialized) != 0) {
715 os << ' ' << GetStatus();
716 }
Elliott Hughese0918552011-10-28 17:18:29 -0700717 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700718 return;
719 }
720
721 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800722 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700723 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800724 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700725 os << " objectSize=" << SizeOf() << " "
726 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
727 os << StringPrintf(" access=0x%04x.%04x\n",
728 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
729 if (super != NULL) {
730 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
731 }
732 if (IsArrayClass()) {
733 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
734 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800735 if (kh.NumInterfaces() > 0) {
736 os << " interfaces (" << kh.NumInterfaces() << "):\n";
737 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
738 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700739 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800740 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700741 }
742 }
743 os << " vtable (" << NumVirtualMethods() << " entries, "
744 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
745 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800746 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700747 }
748 os << " direct methods (" << NumDirectMethods() << " entries):\n";
749 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800750 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700751 }
752 if (NumStaticFields() > 0) {
753 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700754 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700755 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800756 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700757 }
758 } else {
759 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700760 }
761 }
762 if (NumInstanceFields() > 0) {
763 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700764 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700765 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800766 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700767 }
768 } else {
769 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700770 }
771 }
772}
773
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700774void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
775 if (new_reference_offsets != CLASS_WALK_SUPER) {
776 // Sanity check that the number of bits set in the reference offset bitmap
777 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800778 size_t count = 0;
779 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
780 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700781 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800782 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700783 }
784 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
785 new_reference_offsets, false);
786}
787
788void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
789 if (new_reference_offsets != CLASS_WALK_SUPER) {
790 // Sanity check that the number of bits set in the reference offset bitmap
791 // agrees with the number of references
792 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
793 NumReferenceStaticFieldsDuringLinking());
794 }
795 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
796 new_reference_offsets, false);
797}
798
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700799bool Class::Implements(const Class* klass) const {
800 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700801 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700802 // All interfaces implemented directly and by our superclass, and
803 // recursively all super-interfaces of those interfaces, are listed
804 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700805 int32_t iftable_count = GetIfTableCount();
806 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
807 for (int32_t i = 0; i < iftable_count; i++) {
808 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700809 return true;
810 }
811 }
812 return false;
813}
814
815// Determine whether "this" is assignable from "klazz", where both of these
816// are array classes.
817//
818// Consider an array class, e.g. Y[][], where Y is a subclass of X.
819// Y[][] = Y[][] --> true (identity)
820// X[][] = Y[][] --> true (element superclass)
821// Y = Y[][] --> false
822// Y[] = Y[][] --> false
823// Object = Y[][] --> true (everything is an object)
824// Object[] = Y[][] --> true
825// Object[][] = Y[][] --> true
826// Object[][][] = Y[][] --> false (too many []s)
827// Serializable = Y[][] --> true (all arrays are Serializable)
828// Serializable[] = Y[][] --> true
829// Serializable[][] = Y[][] --> false (unless Y is Serializable)
830//
831// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700832// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700833//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700834bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700835 DCHECK(IsArrayClass()) << PrettyClass(this);
836 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700837 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700838}
839
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700840bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700841 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
842 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700843 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700844 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700845 // src's super should be java_lang_Object, since it is an array.
846 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700847 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
848 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700849 return this == java_lang_Object;
850 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700851 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700852}
853
854bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700855 DCHECK(!IsInterface()) << PrettyClass(this);
856 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700857 const Class* current = this;
858 do {
859 if (current == klass) {
860 return true;
861 }
862 current = current->GetSuperClass();
863 } while (current != NULL);
864 return false;
865}
866
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800867bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700868 size_t i = 0;
869 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
870 ++i;
871 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700872 if (descriptor1.find('/', i) != StringPiece::npos ||
873 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700874 return false;
875 } else {
876 return true;
877 }
878}
879
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700880#if 0
Ian Rogersb033c752011-07-20 12:22:35 -0700881bool Class::IsInSamePackage(const StringPiece& descriptor1,
882 const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700883 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700884 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
Ian Rogersb033c752011-07-20 12:22:35 -0700885 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size,
886 descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700887 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
888}
889#endif
890
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700891bool Class::IsInSamePackage(const Class* that) const {
892 const Class* klass1 = this;
893 const Class* klass2 = that;
894 if (klass1 == klass2) {
895 return true;
896 }
897 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700898 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700899 return false;
900 }
901 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700902 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700903 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700904 }
jeffhao4a801a42011-09-23 13:53:40 -0700905 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700907 }
908 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800909 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800910 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800911 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800912 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800913 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700914}
915
Elliott Hughesdbb40792011-11-18 17:05:22 -0800916bool Class::IsClassClass() const {
917 Class* java_lang_Class = GetClass()->GetClass();
918 return this == java_lang_Class;
919}
920
921bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800922 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800923}
924
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800925bool Class::IsThrowableClass() const {
926 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
927 return throwable->IsAssignableFrom(this);
928}
929
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800930ClassLoader* Class::GetClassLoader() const {
931 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700932}
933
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700934void Class::SetClassLoader(const ClassLoader* new_cl) {
935 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700937}
938
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800939Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700940 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700941 DCHECK(declaring_class != NULL) << PrettyClass(this);
942 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700943 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700944 int32_t iftable_count = GetIfTableCount();
945 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
946 for (int32_t i = 0; i < iftable_count; i++) {
947 InterfaceEntry* interface_entry = iftable->Get(i);
948 if (interface_entry->GetInterface() == declaring_class) {
949 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700950 }
951 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700952 return NULL;
953}
954
Ian Rogers466bb252011-10-14 03:29:56 -0700955Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700956 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800957 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700958 if (method != NULL) {
959 return method;
960 }
961
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700962 int32_t iftable_count = GetIfTableCount();
963 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
964 for (int32_t i = 0; i < iftable_count; i++) {
965 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700966 if (method != NULL) {
967 return method;
968 }
969 }
970 return NULL;
971}
972
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800973Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
974 // Check the current class before checking the interfaces.
975 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
976 if (method != NULL) {
977 return method;
978 }
979
980 int32_t iftable_count = GetIfTableCount();
981 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
982 for (int32_t i = 0; i < iftable_count; i++) {
983 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
984 if (method != NULL) {
985 return method;
986 }
987 }
988 return NULL;
989}
990
991
992Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800993 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700994 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -0700995 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800996 mh.ChangeMethod(method);
997 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700998 return method;
Ian Rogersb033c752011-07-20 12:22:35 -0700999 }
1000 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001001 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001002}
1003
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001004Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1005 if (GetDexCache() == dex_cache) {
1006 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1007 Method* method = GetDirectMethod(i);
1008 if (method->GetDexMethodIndex() == dex_method_idx) {
1009 return method;
1010 }
1011 }
1012 }
1013 return NULL;
1014}
1015
1016Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1017 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001018 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001019 if (method != NULL) {
1020 return method;
1021 }
1022 }
1023 return NULL;
1024}
1025
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001026Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1027 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1028 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1029 if (method != NULL) {
1030 return method;
1031 }
1032 }
1033 return NULL;
1034}
1035
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001036Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001037 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001038 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001039 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001040 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001041 mh.ChangeMethod(method);
1042 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001043 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001044 }
1045 }
1046 return NULL;
1047}
1048
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001049Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1050 if (GetDexCache() == dex_cache) {
1051 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1052 Method* method = GetVirtualMethod(i);
1053 if (method->GetDexMethodIndex() == dex_method_idx) {
1054 return method;
1055 }
1056 }
1057 }
1058 return NULL;
1059}
1060
Ian Rogers466bb252011-10-14 03:29:56 -07001061Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1062 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1063 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1064 if (method != NULL) {
1065 return method;
1066 }
1067 }
1068 return NULL;
1069}
1070
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001071Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1072 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1073 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1074 if (method != NULL) {
1075 return method;
1076 }
1077 }
1078 return NULL;
1079}
1080
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001081Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001082 // Is the field in this class?
1083 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001084 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001085 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1086 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001087 fh.ChangeField(f);
1088 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001089 return f;
1090 }
1091 }
1092 return NULL;
1093}
1094
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001095Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1096 if (GetDexCache() == dex_cache) {
1097 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1098 Field* f = GetInstanceField(i);
1099 if (f->GetDexFieldIndex() == dex_field_idx) {
1100 return f;
1101 }
1102 }
1103 }
1104 return NULL;
1105}
1106
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001107Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001108 // Is the field in this class, or any of its superclasses?
1109 // Interfaces are not relevant because they can't contain instance fields.
1110 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001111 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001112 if (f != NULL) {
1113 return f;
1114 }
1115 }
1116 return NULL;
1117}
1118
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001119Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1120 // Is the field in this class, or any of its superclasses?
1121 // Interfaces are not relevant because they can't contain instance fields.
1122 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1123 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1124 if (f != NULL) {
1125 return f;
1126 }
1127 }
1128 return NULL;
1129}
1130
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001131Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1132 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001133 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001134 for (size_t i = 0; i < NumStaticFields(); ++i) {
1135 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001136 fh.ChangeField(f);
1137 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001138 return f;
1139 }
1140 }
1141 return NULL;
1142}
1143
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001144Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1145 if (dex_cache == GetDexCache()) {
1146 for (size_t i = 0; i < NumStaticFields(); ++i) {
1147 Field* f = GetStaticField(i);
1148 if (f->GetDexFieldIndex() == dex_field_idx) {
1149 return f;
1150 }
1151 }
1152 }
1153 return NULL;
1154}
1155
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001156Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1157 // Is the field in this class (or its interfaces), or any of its
1158 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001159 ClassHelper kh;
1160 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001161 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001162 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001163 if (f != NULL) {
1164 return f;
1165 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001166 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001167 kh.ChangeClass(k);
1168 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1169 Class* interface = kh.GetInterface(i);
1170 f = interface->FindDeclaredStaticField(name, type);
1171 if (f != NULL) {
1172 return f;
1173 }
1174 }
1175 }
1176 return NULL;
1177}
1178
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001179Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1180 ClassHelper kh;
1181 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1182 // Is the field in this class?
1183 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1184 if (f != NULL) {
1185 return f;
1186 }
1187 // Is this field in any of this class' interfaces?
1188 kh.ChangeClass(k);
1189 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1190 Class* interface = kh.GetInterface(i);
1191 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1192 if (f != NULL) {
1193 return f;
1194 }
1195 }
1196 }
1197 return NULL;
1198}
1199
Ian Rogersb067ac22011-12-13 18:05:09 -08001200Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1201 // Find a field using the JLS field resolution order
1202 ClassHelper kh;
1203 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1204 // Is the field in this class?
1205 Field* f = k->FindDeclaredInstanceField(name, type);
1206 if (f != NULL) {
1207 return f;
1208 }
1209 f = k->FindDeclaredStaticField(name, type);
1210 if (f != NULL) {
1211 return f;
1212 }
1213 // Is this field in any of this class' interfaces?
1214 kh.ChangeClass(k);
1215 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1216 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001217 f = interface->FindDeclaredStaticField(name, type);
1218 if (f != NULL) {
1219 return f;
1220 }
1221 }
1222 }
1223 return NULL;
1224}
1225
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001226Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001227 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001228 DCHECK_GE(component_count, 0);
1229 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001230
1231 size_t header_size = sizeof(Array);
1232 size_t data_size = component_count * component_size;
1233 size_t size = header_size + data_size;
1234
1235 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1236 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1237 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1238 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughese689d512012-01-18 23:39:47 -08001239 "%s of length %d exceeds the VM limit",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001240 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001241 return NULL;
1242 }
1243
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001244 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
1245 if (array != NULL) {
1246 DCHECK(array->IsArrayInstance());
1247 array->SetLength(component_count);
1248 }
1249 return array;
1250}
1251
1252Array* Array::Alloc(Class* array_class, int32_t component_count) {
1253 return Alloc(array_class, component_count, array_class->GetComponentSize());
1254}
1255
Elliott Hughes80609252011-09-23 17:24:51 -07001256bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001257 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001258 "length=%i; index=%i", length_, index);
1259 return false;
1260}
1261
1262bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001263 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001264 "Can't store an element of type %s into an array of type %s",
1265 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1266 return false;
1267}
1268
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001269template<typename T>
1270PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001271 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001272 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1273 return down_cast<PrimitiveArray<T>*>(raw_array);
1274}
1275
1276template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1277
1278// Explicitly instantiate all the primitive array types.
1279template class PrimitiveArray<uint8_t>; // BooleanArray
1280template class PrimitiveArray<int8_t>; // ByteArray
1281template class PrimitiveArray<uint16_t>; // CharArray
1282template class PrimitiveArray<double>; // DoubleArray
1283template class PrimitiveArray<float>; // FloatArray
1284template class PrimitiveArray<int32_t>; // IntArray
1285template class PrimitiveArray<int64_t>; // LongArray
1286template class PrimitiveArray<int16_t>; // ShortArray
1287
Ian Rogers466bb252011-10-14 03:29:56 -07001288// Explicitly instantiate Class[][]
1289template class ObjectArray<ObjectArray<Class> >;
1290
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001291// TODO: get global references for these
1292Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001293
Brian Carlstroma663ea52011-08-19 23:33:41 -07001294void String::SetClass(Class* java_lang_String) {
1295 CHECK(java_lang_String_ == NULL);
1296 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001297 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001298}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001299
Brian Carlstroma663ea52011-08-19 23:33:41 -07001300void String::ResetClass() {
1301 CHECK(java_lang_String_ != NULL);
1302 java_lang_String_ = NULL;
1303}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001304
Brian Carlstromc74255f2011-09-11 22:47:39 -07001305String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001306 return Runtime::Current()->GetInternTable()->InternWeak(this);
1307}
1308
Brian Carlstrom395520e2011-09-25 19:35:00 -07001309int32_t String::GetHashCode() {
1310 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1311 if (result == 0) {
1312 ComputeHashCode();
1313 }
1314 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1315 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1316 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001317 return result;
1318}
1319
1320int32_t String::GetLength() const {
1321 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1322 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1323 return result;
1324}
1325
1326uint16_t String::CharAt(int32_t index) const {
1327 // TODO: do we need this? Equals is the only caller, and could
1328 // bounds check itself.
1329 if (index < 0 || index >= count_) {
1330 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001331 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001332 "length=%i; index=%i", count_, index);
1333 return 0;
1334 }
1335 return GetCharArray()->Get(index + GetOffset());
1336}
1337
1338String* String::AllocFromUtf16(int32_t utf16_length,
1339 const uint16_t* utf16_data_in,
1340 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001341 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001342 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001343 if (string == NULL) {
1344 return NULL;
1345 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001346 // TODO: use 16-bit wide memset variant
1347 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001348 if (array == NULL) {
1349 return NULL;
1350 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001351 for (int i = 0; i < utf16_length; i++) {
1352 array->Set(i, utf16_data_in[i]);
1353 }
1354 if (hash_code != 0) {
1355 string->SetHashCode(hash_code);
1356 } else {
1357 string->ComputeHashCode();
1358 }
1359 return string;
1360}
1361
1362String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001363 if (utf == NULL) {
1364 return NULL;
1365 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001366 size_t char_count = CountModifiedUtf8Chars(utf);
1367 return AllocFromModifiedUtf8(char_count, utf);
1368}
1369
1370String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1371 const char* utf8_data_in) {
1372 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001373 if (string == NULL) {
1374 return NULL;
1375 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 uint16_t* utf16_data_out =
1377 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1378 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1379 string->ComputeHashCode();
1380 return string;
1381}
1382
1383String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001384 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1385 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001386 return NULL;
1387 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001388 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001389}
1390
1391String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001392 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001393 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001394 if (string == NULL) {
1395 return NULL;
1396 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001397 string->SetArray(array);
1398 string->SetCount(array->GetLength());
1399 return string;
1400}
1401
1402bool String::Equals(const String* that) const {
1403 if (this == that) {
1404 // Quick reference equality test
1405 return true;
1406 } else if (that == NULL) {
1407 // Null isn't an instanceof anything
1408 return false;
1409 } else if (this->GetLength() != that->GetLength()) {
1410 // Quick length inequality test
1411 return false;
1412 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001413 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001414 // hash code was already equal
1415 for (int32_t i = 0; i < that->GetLength(); ++i) {
1416 if (this->CharAt(i) != that->CharAt(i)) {
1417 return false;
1418 }
1419 }
1420 return true;
1421 }
1422}
1423
Elliott Hughes5d78d392011-12-13 16:53:05 -08001424bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001425 if (this->GetLength() != that_length) {
1426 return false;
1427 } else {
1428 for (int32_t i = 0; i < that_length; ++i) {
1429 if (this->CharAt(i) != that_chars[that_offset + i]) {
1430 return false;
1431 }
1432 }
1433 return true;
1434 }
1435}
1436
1437bool String::Equals(const char* modified_utf8) const {
1438 for (int32_t i = 0; i < GetLength(); ++i) {
1439 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1440 if (ch == '\0' || ch != CharAt(i)) {
1441 return false;
1442 }
1443 }
1444 return *modified_utf8 == '\0';
1445}
1446
1447bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001448 if (modified_utf8.size() != GetLength()) {
1449 return false;
1450 }
1451 const char* p = modified_utf8.data();
1452 for (int32_t i = 0; i < GetLength(); ++i) {
1453 uint16_t ch = GetUtf16FromUtf8(&p);
1454 if (ch != CharAt(i)) {
1455 return false;
1456 }
1457 }
1458 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459}
1460
1461// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1462std::string String::ToModifiedUtf8() const {
1463 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
1464 size_t byte_count(CountUtf8Bytes(chars, GetLength()));
1465 std::string result(byte_count, char(0));
1466 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1467 return result;
1468}
1469
Ian Rogers1c5eb702012-02-01 09:18:34 -08001470void Throwable::SetCause(Throwable* cause) {
1471 CHECK(cause != NULL);
1472 CHECK(cause != this);
1473 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1474 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1475}
1476
Ian Rogers466bb252011-10-14 03:29:56 -07001477bool Throwable::IsCheckedException() const {
1478 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1479 if (InstanceOf(error)) {
1480 return false;
1481 }
1482 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1483 return !InstanceOf(jlre);
1484}
1485
Ian Rogers9074b992011-10-26 17:41:55 -07001486std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001487 std::string result(PrettyTypeOf(this));
1488 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001489 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001490 if (msg != NULL) {
1491 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001492 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001493 result += "\n";
1494 Object* stack_state = GetStackState();
1495 // check stack state isn't missing or corrupt
1496 if (stack_state != NULL && stack_state->IsObjectArray()) {
1497 // Decode the internal stack trace into the depth and method trace
1498 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1499 int32_t depth = method_trace->GetLength() - 1;
1500 for (int32_t i = 0; i < depth; ++i) {
1501 Method* method = down_cast<Method*>(method_trace->Get(i));
1502 result += " at ";
1503 result += PrettyMethod(method, true);
1504 result += "\n";
1505 }
Ian Rogers9074b992011-10-26 17:41:55 -07001506 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001507 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
1508 if (cause != NULL) {
1509 result += "Caused by: ";
1510 result += cause->Dump();
1511 }
Ian Rogers9074b992011-10-26 17:41:55 -07001512 return result;
1513}
1514
Ian Rogers5167c972012-02-03 10:41:20 -08001515
1516Class* Throwable::java_lang_Throwable_ = NULL;
1517
1518void Throwable::SetClass(Class* java_lang_Throwable) {
1519 CHECK(java_lang_Throwable_ == NULL);
1520 CHECK(java_lang_Throwable != NULL);
1521 java_lang_Throwable_ = java_lang_Throwable;
1522}
1523
1524void Throwable::ResetClass() {
1525 CHECK(java_lang_Throwable_ != NULL);
1526 java_lang_Throwable_ = NULL;
1527}
1528
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001529Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1530
1531void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1532 CHECK(java_lang_StackTraceElement_ == NULL);
1533 CHECK(java_lang_StackTraceElement != NULL);
1534 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1535}
1536
1537void StackTraceElement::ResetClass() {
1538 CHECK(java_lang_StackTraceElement_ != NULL);
1539 java_lang_StackTraceElement_ = NULL;
1540}
1541
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001542StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1543 String* method_name,
1544 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001545 int32_t line_number) {
1546 StackTraceElement* trace =
1547 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1548 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1549 const_cast<String*>(declaring_class), false);
1550 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1551 const_cast<String*>(method_name), false);
1552 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1553 const_cast<String*>(file_name), false);
1554 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1555 line_number, false);
1556 return trace;
1557}
1558
Elliott Hughes1f359b02011-07-17 14:27:17 -07001559static const char* kClassStatusNames[] = {
1560 "Error",
1561 "NotReady",
1562 "Idx",
1563 "Loaded",
1564 "Resolved",
1565 "Verifying",
1566 "Verified",
1567 "Initializing",
1568 "Initialized"
1569};
1570std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1571 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001572 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001573 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001574 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001575 }
1576 return os;
1577}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001578
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001579} // namespace art