blob: 5413194ef9379e2b30dcdc80610732a20d1d500c [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"
TDYa12785321912012-04-01 15:24:56 -070043#include "compiler_llvm/runtime_support_llvm.h"
Logan Chienfca7e872011-12-20 20:08:22 +080044using art::compiler_llvm::InferredRegCategoryMap;
45#endif
46
Carl Shapiro3ee755d2011-06-28 12:11:04 -070047namespace art {
48
Elliott Hughesdbb40792011-11-18 17:05:22 -080049String* Object::AsString() {
50 DCHECK(GetClass()->IsStringClass());
51 return down_cast<String*>(this);
52}
53
Elliott Hughes081be7f2011-09-18 16:50:26 -070054Object* Object::Clone() {
55 Class* c = GetClass();
56 DCHECK(!c->IsClassClass());
57
58 // Object::SizeOf gets the right size even if we're an array.
59 // Using c->AllocObject() here would be wrong.
60 size_t num_bytes = SizeOf();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080061 Heap* heap = Runtime::Current()->GetHeap();
62 SirtRef<Object> copy(heap->AllocObject(c, num_bytes));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070063 if (copy.get() == NULL) {
Elliott Hughes081be7f2011-09-18 16:50:26 -070064 return NULL;
65 }
66
67 // Copy instance data. We assume memcpy copies by words.
68 // TODO: expose and use move32.
69 byte* src_bytes = reinterpret_cast<byte*>(this);
Brian Carlstrom40381fb2011-10-19 14:13:40 -070070 byte* dst_bytes = reinterpret_cast<byte*>(copy.get());
Elliott Hughes081be7f2011-09-18 16:50:26 -070071 size_t offset = sizeof(Object);
72 memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset);
73
Elliott Hughes20cde902011-10-04 17:37:27 -070074 if (c->IsFinalizable()) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080075 heap->AddFinalizerReference(Thread::Current(), copy.get());
Elliott Hughes20cde902011-10-04 17:37:27 -070076 }
Elliott Hughes081be7f2011-09-18 16:50:26 -070077
Brian Carlstrom40381fb2011-10-19 14:13:40 -070078 return copy.get();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070079}
80
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -070081uint32_t Object::GetThinLockId() {
82 return Monitor::GetThinLockId(monitor_);
Elliott Hughes5f791332011-09-15 17:45:30 -070083}
84
85void Object::MonitorEnter(Thread* thread) {
86 Monitor::MonitorEnter(thread, this);
87}
88
Ian Rogersff1ed472011-09-20 13:46:24 -070089bool Object::MonitorExit(Thread* thread) {
90 return Monitor::MonitorExit(thread, this);
Elliott Hughes5f791332011-09-15 17:45:30 -070091}
92
93void Object::Notify() {
94 Monitor::Notify(Thread::Current(), this);
95}
96
97void Object::NotifyAll() {
98 Monitor::NotifyAll(Thread::Current(), this);
99}
100
101void Object::Wait(int64_t ms, int32_t ns) {
102 Monitor::Wait(Thread::Current(), this, ms, ns, true);
103}
104
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105// TODO: get global references for these
106Class* Field::java_lang_reflect_Field_ = NULL;
107
108void Field::SetClass(Class* java_lang_reflect_Field) {
109 CHECK(java_lang_reflect_Field_ == NULL);
110 CHECK(java_lang_reflect_Field != NULL);
111 java_lang_reflect_Field_ = java_lang_reflect_Field;
112}
113
114void Field::ResetClass() {
115 CHECK(java_lang_reflect_Field_ != NULL);
116 java_lang_reflect_Field_ = NULL;
117}
118
Ian Rogers0571d352011-11-03 19:51:38 -0700119void Field::SetOffset(MemberOffset num_bytes) {
120 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800121#if 0 // TODO enable later in boot and under !NDEBUG
122 FieldHelper fh(this);
123 Primitive::Type type = fh.GetTypeAsPrimitiveType();
Ian Rogers0571d352011-11-03 19:51:38 -0700124 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
125 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
126 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127#endif
Ian Rogers0571d352011-11-03 19:51:38 -0700128 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
129}
130
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700131uint32_t Field::Get32(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700132 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700133 if (IsStatic()) {
134 object = declaring_class_;
135 }
136 return object->GetField32(GetOffset(), IsVolatile());
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700137}
138
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700139void Field::Set32(Object* object, uint32_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700140 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700141 if (IsStatic()) {
142 object = declaring_class_;
143 }
144 object->SetField32(GetOffset(), new_value, IsVolatile());
145}
146
147uint64_t Field::Get64(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700148 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700149 if (IsStatic()) {
150 object = declaring_class_;
151 }
152 return object->GetField64(GetOffset(), IsVolatile());
153}
154
155void Field::Set64(Object* object, uint64_t new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700156 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 if (IsStatic()) {
158 object = declaring_class_;
159 }
160 object->SetField64(GetOffset(), new_value, IsVolatile());
161}
162
163Object* Field::GetObj(const Object* object) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700164 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700165 if (IsStatic()) {
166 object = declaring_class_;
167 }
168 return object->GetFieldObject<Object*>(GetOffset(), IsVolatile());
169}
170
171void Field::SetObj(Object* object, const Object* new_value) const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700172 CHECK((object == NULL) == IsStatic()) << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700173 if (IsStatic()) {
174 object = declaring_class_;
175 }
176 object->SetFieldObject(GetOffset(), new_value, IsVolatile());
177}
178
179bool Field::GetBoolean(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
181 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700182 return Get32(object);
183}
184
185void Field::SetBoolean(Object* object, bool z) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800186 DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType())
187 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188 Set32(object, z);
189}
190
191int8_t Field::GetByte(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800192 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
193 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 return Get32(object);
195}
196
197void Field::SetByte(Object* object, int8_t b) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800198 DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType())
199 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 Set32(object, b);
201}
202
203uint16_t Field::GetChar(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800204 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
205 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 return Get32(object);
207}
208
209void Field::SetChar(Object* object, uint16_t c) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800210 DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType())
211 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 Set32(object, c);
213}
214
Ian Rogers466bb252011-10-14 03:29:56 -0700215int16_t Field::GetShort(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
217 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 return Get32(object);
219}
220
Ian Rogers466bb252011-10-14 03:29:56 -0700221void Field::SetShort(Object* object, int16_t s) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType())
223 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700224 Set32(object, s);
225}
226
227int32_t Field::GetInt(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800228 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
229 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230 return Get32(object);
231}
232
233void Field::SetInt(Object* object, int32_t i) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234 DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType())
235 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700236 Set32(object, i);
237}
238
239int64_t Field::GetLong(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800240 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
241 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700242 return Get64(object);
243}
244
245void Field::SetLong(Object* object, int64_t j) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800246 DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType())
247 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700248 Set64(object, j);
249}
250
Elliott Hughes1d878f32012-04-11 15:17:54 -0700251union Bits {
252 jdouble d;
253 jfloat f;
254 jint i;
255 jlong j;
256};
257
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700258float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800259 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
260 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700261 Bits bits;
262 bits.i = Get32(object);
263 return bits.f;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700264}
265
266void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800267 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
268 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700269 Bits bits;
270 bits.f = f;
271 Set32(object, bits.i);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700272}
273
274double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800275 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
276 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700277 Bits bits;
278 bits.j = Get64(object);
279 return bits.d;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280}
281
282void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800283 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
284 << PrettyField(this);
Elliott Hughes1d878f32012-04-11 15:17:54 -0700285 Bits bits;
286 bits.d = d;
287 Set64(object, bits.j);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700288}
289
290Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
292 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700293 return GetObj(object);
294}
295
296void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
298 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700299 SetObj(object, l);
300}
301
302// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700303Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304Class* Method::java_lang_reflect_Method_ = NULL;
305
Elliott Hughes80609252011-09-23 17:24:51 -0700306void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
307 CHECK(java_lang_reflect_Constructor_ == NULL);
308 CHECK(java_lang_reflect_Constructor != NULL);
309 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
310
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700311 CHECK(java_lang_reflect_Method_ == NULL);
312 CHECK(java_lang_reflect_Method != NULL);
313 java_lang_reflect_Method_ = java_lang_reflect_Method;
314}
315
Elliott Hughes80609252011-09-23 17:24:51 -0700316void Method::ResetClasses() {
317 CHECK(java_lang_reflect_Constructor_ != NULL);
318 java_lang_reflect_Constructor_ = NULL;
319
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 CHECK(java_lang_reflect_Method_ != NULL);
321 java_lang_reflect_Method_ = NULL;
322}
323
Elliott Hughes418d20f2011-09-22 14:00:39 -0700324Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
325 if (*p == '[') {
326 // Something like "[[[Ljava/lang/String;".
327 const char* start = p;
328 while (*p == '[') {
329 ++p;
330 }
331 if (*p == 'L') {
332 while (*p != ';') {
333 ++p;
334 }
335 }
336 ++p; // Either the ';' or the primitive type.
337
Brian Carlstromaded5f72011-10-07 17:15:04 -0700338 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800339 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700340 } else if (*p == 'L') {
341 const char* start = p;
342 while (*p != ';') {
343 ++p;
344 }
345 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800346 std::string descriptor(start, (p - start));
347 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700348 } else {
349 return class_linker->FindPrimitiveClass(*p++);
350 }
351}
352
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353ObjectArray<String>* Method::GetDexCacheStrings() const {
354 return GetFieldObject<ObjectArray<String>*>(
355 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
356}
357
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
359 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
360 new_dex_cache_strings, false);
361}
362
Ian Rogers19846512012-02-24 11:42:47 -0800363ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
364 return GetFieldObject<ObjectArray<Method>*>(
365 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
366}
367
368void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
369 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
370 new_dex_cache_methods, false);
371}
372
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
374 return GetFieldObject<ObjectArray<Class>*>(
375 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
376}
377
378void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
379 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
380 new_dex_cache_classes, false);
381}
382
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
384 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
385 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
386 false);
387}
388
389void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700390 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392}
393
394size_t Method::NumArgRegisters(const StringPiece& shorty) {
395 CHECK_LE(1, shorty.length());
396 uint32_t num_registers = 0;
397 for (int i = 1; i < shorty.length(); ++i) {
398 char ch = shorty[i];
399 if (ch == 'D' || ch == 'J') {
400 num_registers += 2;
401 } else {
402 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700403 }
404 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700405 return num_registers;
406}
407
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408bool Method::IsProxyMethod() const {
409 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700410}
411
Ian Rogers466bb252011-10-14 03:29:56 -0700412Method* Method::FindOverriddenMethod() const {
413 if (IsStatic()) {
414 return NULL;
415 }
416 Class* declaring_class = GetDeclaringClass();
417 Class* super_class = declaring_class->GetSuperClass();
418 uint16_t method_index = GetMethodIndex();
419 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
420 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800421 // Did this method override a super class method? If so load the result from the super class'
422 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700423 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
424 result = super_class_vtable->Get(method_index);
425 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800426 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800427 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800428 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
429 CHECK_EQ(result,
430 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800431 } else {
432 MethodHelper mh(this);
433 MethodHelper interface_mh;
434 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
435 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
436 InterfaceEntry* entry = iftable->Get(i);
437 Class* interface = entry->GetInterface();
438 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
439 Method* interface_method = interface->GetVirtualMethod(j);
440 interface_mh.ChangeMethod(interface_method);
441 if (mh.HasSameNameAndSignature(&interface_mh)) {
442 result = interface_method;
443 break;
444 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 }
446 }
Ian Rogers466bb252011-10-14 03:29:56 -0700447 }
448 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800449#ifndef NDEBUG
450 MethodHelper result_mh(result);
451 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
452#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700453 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700454}
455
Elliott Hughes168670b2012-02-29 16:43:26 -0800456static const void* GetOatCode(const Method* m) {
457 Runtime* runtime = Runtime::Current();
458 const void* code = m->GetCode();
459 // Peel off any method tracing trampoline.
460 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
461 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
462 }
463 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800464 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800465 code = runtime->GetClassLinker()->GetOatCodeFor(m);
466 }
467 return code;
468}
469
Ian Rogersbdb03912011-09-14 00:55:44 -0700470uint32_t Method::ToDexPC(const uintptr_t pc) const {
TDYa127c8dc1012012-04-19 07:03:33 -0700471#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700472 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700473 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700475 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700476 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700477 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800478 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700479 uint32_t best_offset = 0;
480 uint32_t best_dex_offset = 0;
481 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700482 uint32_t map_offset = mapping_table[i];
483 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700484 if (map_offset == sought_offset) {
485 best_offset = map_offset;
486 best_dex_offset = map_dex_offset;
487 break;
488 }
489 if (map_offset < sought_offset && map_offset > best_offset) {
490 best_offset = map_offset;
491 best_dex_offset = map_dex_offset;
492 }
493 }
494 return best_dex_offset;
TDYa127c8dc1012012-04-19 07:03:33 -0700495#else
496 // Compiler LLVM doesn't use the machine pc, we just use dex pc instead.
497 return static_cast<uint32_t>(pc);
498#endif
Ian Rogersbdb03912011-09-14 00:55:44 -0700499}
500
501uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700502 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700503 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700504 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700505 return 0; // Special no mapping/pc == 0 case
506 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700507 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700508 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700509 uint32_t map_offset = mapping_table[i];
510 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700511 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800512 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700513 }
514 }
515 LOG(FATAL) << "Looking up Dex PC not contained in method";
516 return 0;
517}
518
519uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800520 MethodHelper mh(this);
521 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700522 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700523 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
524 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700525 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700526 if (iter_type_idx == DexFile::kDexNoIndex16) {
527 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700528 }
529 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800530 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700531 if (iter_exception_type == NULL) {
532 // The verifier should take care of resolving all exception classes early
533 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800534 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700535 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700536 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700537 }
538 }
539 // Handler not found
540 return DexFile::kDexNoIndex;
541}
542
Elliott Hughes77405792012-03-15 15:22:12 -0700543void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700544 // Push a transition back into managed code onto the linked list in thread.
Elliott Hughes34e06962012-04-09 13:55:55 -0700545 CHECK_EQ(kRunnable, self->GetState());
TDYa12785321912012-04-01 15:24:56 -0700546
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700547#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700548 NativeToManagedRecord record;
549 self->PushNativeToManagedRecord(&record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700550#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700551
TDYa1270b686e52012-04-09 22:43:35 -0700552#if defined(ART_USE_LLVM_COMPILER)
553 art_fix_stub_from_code(const_cast<Method*>(this));
554#endif
555
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700556 // Call the invoke stub associated with the method.
557 // Pass everything as arguments.
558 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700559
560 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700561
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500562 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700563 bool log = false;
564 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800565 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
566 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700567 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700568 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700569 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800570 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
571 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700572 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700573 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800574 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
575 PrettyMethod(this).c_str(), GetCode(), stub,
576 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700577 if (result != NULL) {
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700578 result->SetJ(0);
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700579 }
580 }
581
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700582#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700583 // Pop transition.
584 self->PopNativeToManagedRecord(record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700585#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700586}
587
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700588bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700589 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800590 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800591 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700592 return native_method != jni_stub;
593}
594
Ian Rogers60db5ab2012-02-20 17:02:00 -0800595void Method::RegisterNative(Thread* self, const void* native_method) {
596 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700597 CHECK(IsNative()) << PrettyMethod(this);
598 CHECK(native_method != NULL) << PrettyMethod(this);
TDYa12726467572012-04-17 20:51:22 -0700599#if defined(ART_USE_LLVM_COMPILER)
600 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
601 native_method, false);
602#else
Ian Rogers60db5ab2012-02-20 17:02:00 -0800603 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
604 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
605 native_method, false);
606 } else {
607 // We've been asked to associate this method with the given native method but are working
608 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
609 // the native method to runtime support and store the target somewhere runtime support will
610 // find it.
611#if defined(__arm__)
612 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
613 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
614#else
615 UNIMPLEMENTED(FATAL);
616#endif
617 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
618 reinterpret_cast<const uint8_t*>(native_method), false);
619 }
TDYa12726467572012-04-17 20:51:22 -0700620#endif
Brian Carlstrom16192862011-09-12 17:50:06 -0700621}
622
Ian Rogers19846512012-02-24 11:42:47 -0800623void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700624 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700625 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800626 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700627}
628
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700629void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700630 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
631 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700632 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800633 if (new_status == kStatusError) {
634 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
635
636 // stash current exception
637 Thread* self = Thread::Current();
638 SirtRef<Throwable> exception(self->GetException());
639 CHECK(exception.get() != NULL);
640
641 // clear exception to call FindSystemClass
642 self->ClearException();
643 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
644 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
645 CHECK(!self->IsExceptionPending());
646
647 // only verification errors, not initialization problems, should set a verify error.
648 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
649 Class* exception_class = exception->GetClass();
650 if (!eiie_class->IsAssignableFrom(exception_class)) {
651 SetVerifyErrorClass(exception_class);
652 }
653
654 // restore exception
655 self->SetException(exception.get());
656 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700657 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700658}
659
660DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700661 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700662}
663
664void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700665 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700666}
667
Brian Carlstrom1f870082011-08-23 16:02:11 -0700668Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700669 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700670 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500671 // TODO: decide whether we want this check. It currently fails during bootstrap.
672 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700673 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800674 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700675}
676
Ian Rogers0571d352011-11-03 19:51:38 -0700677void Class::SetClassSize(size_t new_class_size) {
678 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
679 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
680}
681
Ian Rogersd418eda2012-01-30 12:14:28 -0800682// Return the class' name. The exact format is bizarre, but it's the specified behavior for
683// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
684// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
685// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
686String* Class::ComputeName() {
687 String* name = GetName();
688 if (name != NULL) {
689 return name;
690 }
691 std::string descriptor(ClassHelper(this).GetDescriptor());
692 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
693 // The descriptor indicates that this is the class for
694 // a primitive type; special-case the return value.
695 const char* c_name = NULL;
696 switch (descriptor[0]) {
697 case 'Z': c_name = "boolean"; break;
698 case 'B': c_name = "byte"; break;
699 case 'C': c_name = "char"; break;
700 case 'S': c_name = "short"; break;
701 case 'I': c_name = "int"; break;
702 case 'J': c_name = "long"; break;
703 case 'F': c_name = "float"; break;
704 case 'D': c_name = "double"; break;
705 case 'V': c_name = "void"; break;
706 default:
707 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
708 }
709 name = String::AllocFromModifiedUtf8(c_name);
710 } else {
711 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
712 // components.
713 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
714 descriptor.erase(0, 1);
715 descriptor.erase(descriptor.size() - 1);
716 }
717 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
718 name = String::AllocFromModifiedUtf8(descriptor.c_str());
719 }
720 SetName(name);
721 return name;
722}
723
Elliott Hughes4681c802011-09-25 18:04:37 -0700724void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700725 if ((flags & kDumpClassFullDetail) == 0) {
726 os << PrettyClass(this);
727 if ((flags & kDumpClassClassLoader) != 0) {
728 os << ' ' << GetClassLoader();
729 }
730 if ((flags & kDumpClassInitialized) != 0) {
731 os << ' ' << GetStatus();
732 }
Elliott Hughese0918552011-10-28 17:18:29 -0700733 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700734 return;
735 }
736
737 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800738 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700739 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800740 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700741 os << " objectSize=" << SizeOf() << " "
742 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
743 os << StringPrintf(" access=0x%04x.%04x\n",
744 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
745 if (super != NULL) {
746 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
747 }
748 if (IsArrayClass()) {
749 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
750 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800751 if (kh.NumInterfaces() > 0) {
752 os << " interfaces (" << kh.NumInterfaces() << "):\n";
753 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
754 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700755 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800756 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700757 }
758 }
759 os << " vtable (" << NumVirtualMethods() << " entries, "
760 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
761 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800762 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700763 }
764 os << " direct methods (" << NumDirectMethods() << " entries):\n";
765 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800766 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700767 }
768 if (NumStaticFields() > 0) {
769 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700770 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700771 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800772 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700773 }
774 } else {
775 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700776 }
777 }
778 if (NumInstanceFields() > 0) {
779 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700780 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700781 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800782 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700783 }
784 } else {
785 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700786 }
787 }
788}
789
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700790void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
791 if (new_reference_offsets != CLASS_WALK_SUPER) {
792 // Sanity check that the number of bits set in the reference offset bitmap
793 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800794 size_t count = 0;
795 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
796 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700797 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800798 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700799 }
800 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
801 new_reference_offsets, false);
802}
803
804void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
805 if (new_reference_offsets != CLASS_WALK_SUPER) {
806 // Sanity check that the number of bits set in the reference offset bitmap
807 // agrees with the number of references
808 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
809 NumReferenceStaticFieldsDuringLinking());
810 }
811 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
812 new_reference_offsets, false);
813}
814
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700815bool Class::Implements(const Class* klass) const {
816 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700817 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700818 // All interfaces implemented directly and by our superclass, and
819 // recursively all super-interfaces of those interfaces, are listed
820 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700821 int32_t iftable_count = GetIfTableCount();
822 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
823 for (int32_t i = 0; i < iftable_count; i++) {
824 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700825 return true;
826 }
827 }
828 return false;
829}
830
Elliott Hughese84278b2012-03-22 10:06:53 -0700831// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700832// are array classes.
833//
834// Consider an array class, e.g. Y[][], where Y is a subclass of X.
835// Y[][] = Y[][] --> true (identity)
836// X[][] = Y[][] --> true (element superclass)
837// Y = Y[][] --> false
838// Y[] = Y[][] --> false
839// Object = Y[][] --> true (everything is an object)
840// Object[] = Y[][] --> true
841// Object[][] = Y[][] --> true
842// Object[][][] = Y[][] --> false (too many []s)
843// Serializable = Y[][] --> true (all arrays are Serializable)
844// Serializable[] = Y[][] --> true
845// Serializable[][] = Y[][] --> false (unless Y is Serializable)
846//
847// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700848// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700849//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700850bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700851 DCHECK(IsArrayClass()) << PrettyClass(this);
852 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700853 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700854}
855
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700856bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700857 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
858 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700859 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700860 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700861 // src's super should be java_lang_Object, since it is an array.
862 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700863 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
864 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700865 return this == java_lang_Object;
866 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700867 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700868}
869
870bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700871 DCHECK(!IsInterface()) << PrettyClass(this);
872 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700873 const Class* current = this;
874 do {
875 if (current == klass) {
876 return true;
877 }
878 current = current->GetSuperClass();
879 } while (current != NULL);
880 return false;
881}
882
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800883bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700884 size_t i = 0;
885 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
886 ++i;
887 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700888 if (descriptor1.find('/', i) != StringPiece::npos ||
889 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700890 return false;
891 } else {
892 return true;
893 }
894}
895
896bool Class::IsInSamePackage(const Class* that) const {
897 const Class* klass1 = this;
898 const Class* klass2 = that;
899 if (klass1 == klass2) {
900 return true;
901 }
902 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700903 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700904 return false;
905 }
906 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700907 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700908 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700909 }
jeffhao4a801a42011-09-23 13:53:40 -0700910 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700911 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700912 }
913 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800914 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800915 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800916 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800917 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800918 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700919}
920
Elliott Hughesdbb40792011-11-18 17:05:22 -0800921bool Class::IsClassClass() const {
922 Class* java_lang_Class = GetClass()->GetClass();
923 return this == java_lang_Class;
924}
925
926bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800927 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800928}
929
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800930bool Class::IsThrowableClass() const {
931 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
932 return throwable->IsAssignableFrom(this);
933}
934
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800935ClassLoader* Class::GetClassLoader() const {
936 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700937}
938
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700939void Class::SetClassLoader(const ClassLoader* new_cl) {
940 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700942}
943
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800944Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700945 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700946 DCHECK(declaring_class != NULL) << PrettyClass(this);
947 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700948 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700949 int32_t iftable_count = GetIfTableCount();
950 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
951 for (int32_t i = 0; i < iftable_count; i++) {
952 InterfaceEntry* interface_entry = iftable->Get(i);
953 if (interface_entry->GetInterface() == declaring_class) {
954 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700955 }
956 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700957 return NULL;
958}
959
Ian Rogers466bb252011-10-14 03:29:56 -0700960Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700961 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800962 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700963 if (method != NULL) {
964 return method;
965 }
966
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700967 int32_t iftable_count = GetIfTableCount();
968 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
969 for (int32_t i = 0; i < iftable_count; i++) {
970 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700971 if (method != NULL) {
972 return method;
973 }
974 }
975 return NULL;
976}
977
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800978Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
979 // Check the current class before checking the interfaces.
980 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
981 if (method != NULL) {
982 return method;
983 }
984
985 int32_t iftable_count = GetIfTableCount();
986 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
987 for (int32_t i = 0; i < iftable_count; i++) {
988 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
989 if (method != NULL) {
990 return method;
991 }
992 }
993 return NULL;
994}
995
996
997Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800998 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700999 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001000 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001001 mh.ChangeMethod(method);
1002 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001003 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001004 }
1005 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001006 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001007}
1008
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001009Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1010 if (GetDexCache() == dex_cache) {
1011 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1012 Method* method = GetDirectMethod(i);
1013 if (method->GetDexMethodIndex() == dex_method_idx) {
1014 return method;
1015 }
1016 }
1017 }
1018 return NULL;
1019}
1020
1021Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1022 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001023 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001024 if (method != NULL) {
1025 return method;
1026 }
1027 }
1028 return NULL;
1029}
1030
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001031Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1032 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1033 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1034 if (method != NULL) {
1035 return method;
1036 }
1037 }
1038 return NULL;
1039}
1040
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001041Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001042 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001043 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001044 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001045 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001046 mh.ChangeMethod(method);
1047 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001048 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001049 }
1050 }
1051 return NULL;
1052}
1053
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001054Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1055 if (GetDexCache() == dex_cache) {
1056 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1057 Method* method = GetVirtualMethod(i);
1058 if (method->GetDexMethodIndex() == dex_method_idx) {
1059 return method;
1060 }
1061 }
1062 }
1063 return NULL;
1064}
1065
Ian Rogers466bb252011-10-14 03:29:56 -07001066Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1067 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1068 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1069 if (method != NULL) {
1070 return method;
1071 }
1072 }
1073 return NULL;
1074}
1075
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001076Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1077 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1078 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1079 if (method != NULL) {
1080 return method;
1081 }
1082 }
1083 return NULL;
1084}
1085
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001086Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001087 // Is the field in this class?
1088 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001089 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001090 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1091 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001092 fh.ChangeField(f);
1093 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001094 return f;
1095 }
1096 }
1097 return NULL;
1098}
1099
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001100Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1101 if (GetDexCache() == dex_cache) {
1102 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1103 Field* f = GetInstanceField(i);
1104 if (f->GetDexFieldIndex() == dex_field_idx) {
1105 return f;
1106 }
1107 }
1108 }
1109 return NULL;
1110}
1111
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001112Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001113 // Is the field in this class, or any of its superclasses?
1114 // Interfaces are not relevant because they can't contain instance fields.
1115 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001116 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001117 if (f != NULL) {
1118 return f;
1119 }
1120 }
1121 return NULL;
1122}
1123
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001124Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1125 // Is the field in this class, or any of its superclasses?
1126 // Interfaces are not relevant because they can't contain instance fields.
1127 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1128 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1129 if (f != NULL) {
1130 return f;
1131 }
1132 }
1133 return NULL;
1134}
1135
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001136Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1137 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001138 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001139 for (size_t i = 0; i < NumStaticFields(); ++i) {
1140 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001141 fh.ChangeField(f);
1142 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001143 return f;
1144 }
1145 }
1146 return NULL;
1147}
1148
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001149Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1150 if (dex_cache == GetDexCache()) {
1151 for (size_t i = 0; i < NumStaticFields(); ++i) {
1152 Field* f = GetStaticField(i);
1153 if (f->GetDexFieldIndex() == dex_field_idx) {
1154 return f;
1155 }
1156 }
1157 }
1158 return NULL;
1159}
1160
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001161Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1162 // Is the field in this class (or its interfaces), or any of its
1163 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001164 ClassHelper kh;
1165 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001166 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001167 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001168 if (f != NULL) {
1169 return f;
1170 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001171 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001172 kh.ChangeClass(k);
1173 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1174 Class* interface = kh.GetInterface(i);
1175 f = interface->FindDeclaredStaticField(name, type);
1176 if (f != NULL) {
1177 return f;
1178 }
1179 }
1180 }
1181 return NULL;
1182}
1183
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001184Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1185 ClassHelper kh;
1186 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1187 // Is the field in this class?
1188 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1189 if (f != NULL) {
1190 return f;
1191 }
1192 // Is this field in any of this class' interfaces?
1193 kh.ChangeClass(k);
1194 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1195 Class* interface = kh.GetInterface(i);
1196 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1197 if (f != NULL) {
1198 return f;
1199 }
1200 }
1201 }
1202 return NULL;
1203}
1204
Ian Rogersb067ac22011-12-13 18:05:09 -08001205Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1206 // Find a field using the JLS field resolution order
1207 ClassHelper kh;
1208 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1209 // Is the field in this class?
1210 Field* f = k->FindDeclaredInstanceField(name, type);
1211 if (f != NULL) {
1212 return f;
1213 }
1214 f = k->FindDeclaredStaticField(name, type);
1215 if (f != NULL) {
1216 return f;
1217 }
1218 // Is this field in any of this class' interfaces?
1219 kh.ChangeClass(k);
1220 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1221 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001222 f = interface->FindDeclaredStaticField(name, type);
1223 if (f != NULL) {
1224 return f;
1225 }
1226 }
1227 }
1228 return NULL;
1229}
1230
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001231Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001232 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001233 DCHECK_GE(component_count, 0);
1234 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001235
Ian Rogersa15e67d2012-02-28 13:51:55 -08001236 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001237 size_t data_size = component_count * component_size;
1238 size_t size = header_size + data_size;
1239
1240 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1241 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1242 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1243 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001244 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001245 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001246 return NULL;
1247 }
1248
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001249 Heap* heap = Runtime::Current()->GetHeap();
1250 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001251 if (array != NULL) {
1252 DCHECK(array->IsArrayInstance());
1253 array->SetLength(component_count);
1254 }
1255 return array;
1256}
1257
1258Array* Array::Alloc(Class* array_class, int32_t component_count) {
1259 return Alloc(array_class, component_count, array_class->GetComponentSize());
1260}
1261
Elliott Hughes80609252011-09-23 17:24:51 -07001262bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001263 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001264 "length=%i; index=%i", length_, index);
1265 return false;
1266}
1267
1268bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001269 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001270 "Can't store an element of type %s into an array of type %s",
1271 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1272 return false;
1273}
1274
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001275template<typename T>
1276PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001277 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001278 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1279 return down_cast<PrimitiveArray<T>*>(raw_array);
1280}
1281
1282template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1283
1284// Explicitly instantiate all the primitive array types.
1285template class PrimitiveArray<uint8_t>; // BooleanArray
1286template class PrimitiveArray<int8_t>; // ByteArray
1287template class PrimitiveArray<uint16_t>; // CharArray
1288template class PrimitiveArray<double>; // DoubleArray
1289template class PrimitiveArray<float>; // FloatArray
1290template class PrimitiveArray<int32_t>; // IntArray
1291template class PrimitiveArray<int64_t>; // LongArray
1292template class PrimitiveArray<int16_t>; // ShortArray
1293
Ian Rogers466bb252011-10-14 03:29:56 -07001294// Explicitly instantiate Class[][]
1295template class ObjectArray<ObjectArray<Class> >;
1296
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001297// TODO: get global references for these
1298Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001299
Brian Carlstroma663ea52011-08-19 23:33:41 -07001300void String::SetClass(Class* java_lang_String) {
1301 CHECK(java_lang_String_ == NULL);
1302 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001303 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001304}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001305
Brian Carlstroma663ea52011-08-19 23:33:41 -07001306void String::ResetClass() {
1307 CHECK(java_lang_String_ != NULL);
1308 java_lang_String_ = NULL;
1309}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001310
Brian Carlstromc74255f2011-09-11 22:47:39 -07001311String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001312 return Runtime::Current()->GetInternTable()->InternWeak(this);
1313}
1314
Brian Carlstrom395520e2011-09-25 19:35:00 -07001315int32_t String::GetHashCode() {
1316 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1317 if (result == 0) {
1318 ComputeHashCode();
1319 }
1320 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1321 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1322 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001323 return result;
1324}
1325
1326int32_t String::GetLength() const {
1327 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1328 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1329 return result;
1330}
1331
1332uint16_t String::CharAt(int32_t index) const {
1333 // TODO: do we need this? Equals is the only caller, and could
1334 // bounds check itself.
1335 if (index < 0 || index >= count_) {
1336 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001337 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001338 "length=%i; index=%i", count_, index);
1339 return 0;
1340 }
1341 return GetCharArray()->Get(index + GetOffset());
1342}
1343
1344String* String::AllocFromUtf16(int32_t utf16_length,
1345 const uint16_t* utf16_data_in,
1346 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001347 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001348 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001349 if (string == NULL) {
1350 return NULL;
1351 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001352 // TODO: use 16-bit wide memset variant
1353 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001354 if (array == NULL) {
1355 return NULL;
1356 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001357 for (int i = 0; i < utf16_length; i++) {
1358 array->Set(i, utf16_data_in[i]);
1359 }
1360 if (hash_code != 0) {
1361 string->SetHashCode(hash_code);
1362 } else {
1363 string->ComputeHashCode();
1364 }
1365 return string;
1366}
1367
1368String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001369 if (utf == NULL) {
1370 return NULL;
1371 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001372 size_t char_count = CountModifiedUtf8Chars(utf);
1373 return AllocFromModifiedUtf8(char_count, utf);
1374}
1375
1376String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1377 const char* utf8_data_in) {
1378 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001379 if (string == NULL) {
1380 return NULL;
1381 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001382 uint16_t* utf16_data_out =
1383 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1384 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1385 string->ComputeHashCode();
1386 return string;
1387}
1388
1389String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001390 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1391 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001392 return NULL;
1393 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001394 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001395}
1396
1397String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001398 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001399 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001400 if (string == NULL) {
1401 return NULL;
1402 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001403 string->SetArray(array);
1404 string->SetCount(array->GetLength());
1405 return string;
1406}
1407
1408bool String::Equals(const String* that) const {
1409 if (this == that) {
1410 // Quick reference equality test
1411 return true;
1412 } else if (that == NULL) {
1413 // Null isn't an instanceof anything
1414 return false;
1415 } else if (this->GetLength() != that->GetLength()) {
1416 // Quick length inequality test
1417 return false;
1418 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001419 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001420 // hash code was already equal
1421 for (int32_t i = 0; i < that->GetLength(); ++i) {
1422 if (this->CharAt(i) != that->CharAt(i)) {
1423 return false;
1424 }
1425 }
1426 return true;
1427 }
1428}
1429
Elliott Hughes5d78d392011-12-13 16:53:05 -08001430bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431 if (this->GetLength() != that_length) {
1432 return false;
1433 } else {
1434 for (int32_t i = 0; i < that_length; ++i) {
1435 if (this->CharAt(i) != that_chars[that_offset + i]) {
1436 return false;
1437 }
1438 }
1439 return true;
1440 }
1441}
1442
1443bool String::Equals(const char* modified_utf8) const {
1444 for (int32_t i = 0; i < GetLength(); ++i) {
1445 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1446 if (ch == '\0' || ch != CharAt(i)) {
1447 return false;
1448 }
1449 }
1450 return *modified_utf8 == '\0';
1451}
1452
1453bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001454 if (modified_utf8.size() != GetLength()) {
1455 return false;
1456 }
1457 const char* p = modified_utf8.data();
1458 for (int32_t i = 0; i < GetLength(); ++i) {
1459 uint16_t ch = GetUtf16FromUtf8(&p);
1460 if (ch != CharAt(i)) {
1461 return false;
1462 }
1463 }
1464 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001465}
1466
1467// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1468std::string String::ToModifiedUtf8() const {
1469 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001470 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001471 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001472 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1473 return result;
1474}
1475
Ian Rogers1c5eb702012-02-01 09:18:34 -08001476void Throwable::SetCause(Throwable* cause) {
1477 CHECK(cause != NULL);
1478 CHECK(cause != this);
1479 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1480 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1481}
1482
Ian Rogers466bb252011-10-14 03:29:56 -07001483bool Throwable::IsCheckedException() const {
1484 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1485 if (InstanceOf(error)) {
1486 return false;
1487 }
1488 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1489 return !InstanceOf(jlre);
1490}
1491
Ian Rogers9074b992011-10-26 17:41:55 -07001492std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001493 std::string result(PrettyTypeOf(this));
1494 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001495 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001496 if (msg != NULL) {
1497 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001498 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001499 result += "\n";
1500 Object* stack_state = GetStackState();
1501 // check stack state isn't missing or corrupt
1502 if (stack_state != NULL && stack_state->IsObjectArray()) {
1503 // Decode the internal stack trace into the depth and method trace
1504 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1505 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001506 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1507 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001508 for (int32_t i = 0; i < depth; ++i) {
1509 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001510 mh.ChangeMethod(method);
1511 uint32_t native_pc = pc_trace->Get(i);
1512 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1513 const char* source_file = mh.GetDeclaringClassSourceFile();
1514 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1515 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001516 }
Ian Rogers9074b992011-10-26 17:41:55 -07001517 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001518 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001519 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001520 result += "Caused by: ";
1521 result += cause->Dump();
1522 }
Ian Rogers9074b992011-10-26 17:41:55 -07001523 return result;
1524}
1525
Ian Rogers5167c972012-02-03 10:41:20 -08001526
1527Class* Throwable::java_lang_Throwable_ = NULL;
1528
1529void Throwable::SetClass(Class* java_lang_Throwable) {
1530 CHECK(java_lang_Throwable_ == NULL);
1531 CHECK(java_lang_Throwable != NULL);
1532 java_lang_Throwable_ = java_lang_Throwable;
1533}
1534
1535void Throwable::ResetClass() {
1536 CHECK(java_lang_Throwable_ != NULL);
1537 java_lang_Throwable_ = NULL;
1538}
1539
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001540Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1541
1542void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1543 CHECK(java_lang_StackTraceElement_ == NULL);
1544 CHECK(java_lang_StackTraceElement != NULL);
1545 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1546}
1547
1548void StackTraceElement::ResetClass() {
1549 CHECK(java_lang_StackTraceElement_ != NULL);
1550 java_lang_StackTraceElement_ = NULL;
1551}
1552
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001553StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1554 String* method_name,
1555 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001556 int32_t line_number) {
1557 StackTraceElement* trace =
1558 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1559 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1560 const_cast<String*>(declaring_class), false);
1561 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1562 const_cast<String*>(method_name), false);
1563 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1564 const_cast<String*>(file_name), false);
1565 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1566 line_number, false);
1567 return trace;
1568}
1569
Elliott Hughes1f359b02011-07-17 14:27:17 -07001570static const char* kClassStatusNames[] = {
1571 "Error",
1572 "NotReady",
1573 "Idx",
1574 "Loaded",
1575 "Resolved",
1576 "Verifying",
1577 "Verified",
1578 "Initializing",
1579 "Initialized"
1580};
1581std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1582 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001583 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001584 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001585 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001586 }
1587 return os;
1588}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001589
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001590} // namespace art