blob: 74195f878991ec78d183907590b5e78ca72411e8 [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
251float Field::GetFloat(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800252 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
253 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700254 JValue float_bits;
255 float_bits.i = Get32(object);
256 return float_bits.f;
257}
258
259void Field::SetFloat(Object* object, float f) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType())
261 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700262 JValue float_bits;
263 float_bits.f = f;
264 Set32(object, float_bits.i);
265}
266
267double Field::GetDouble(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
269 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700270 JValue double_bits;
271 double_bits.j = Get64(object);
272 return double_bits.d;
273}
274
275void Field::SetDouble(Object* object, double d) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType())
277 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700278 JValue double_bits;
279 double_bits.d = d;
280 Set64(object, double_bits.j);
281}
282
283Object* Field::GetObject(const Object* object) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
285 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700286 return GetObj(object);
287}
288
289void Field::SetObject(Object* object, const Object* l) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800290 DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType())
291 << PrettyField(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 SetObj(object, l);
293}
294
295// TODO: get global references for these
Elliott Hughes80609252011-09-23 17:24:51 -0700296Class* Method::java_lang_reflect_Constructor_ = NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297Class* Method::java_lang_reflect_Method_ = NULL;
298
Elliott Hughes80609252011-09-23 17:24:51 -0700299void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) {
300 CHECK(java_lang_reflect_Constructor_ == NULL);
301 CHECK(java_lang_reflect_Constructor != NULL);
302 java_lang_reflect_Constructor_ = java_lang_reflect_Constructor;
303
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 CHECK(java_lang_reflect_Method_ == NULL);
305 CHECK(java_lang_reflect_Method != NULL);
306 java_lang_reflect_Method_ = java_lang_reflect_Method;
307}
308
Elliott Hughes80609252011-09-23 17:24:51 -0700309void Method::ResetClasses() {
310 CHECK(java_lang_reflect_Constructor_ != NULL);
311 java_lang_reflect_Constructor_ = NULL;
312
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700313 CHECK(java_lang_reflect_Method_ != NULL);
314 java_lang_reflect_Method_ = NULL;
315}
316
Elliott Hughes418d20f2011-09-22 14:00:39 -0700317Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) {
318 if (*p == '[') {
319 // Something like "[[[Ljava/lang/String;".
320 const char* start = p;
321 while (*p == '[') {
322 ++p;
323 }
324 if (*p == 'L') {
325 while (*p != ';') {
326 ++p;
327 }
328 }
329 ++p; // Either the ';' or the primitive type.
330
Brian Carlstromaded5f72011-10-07 17:15:04 -0700331 std::string descriptor(start, (p - start));
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800332 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700333 } else if (*p == 'L') {
334 const char* start = p;
335 while (*p != ';') {
336 ++p;
337 }
338 ++p;
Elliott Hughesc3b77c72011-12-15 20:56:48 -0800339 std::string descriptor(start, (p - start));
340 return class_linker->FindClass(descriptor.c_str(), cl);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700341 } else {
342 return class_linker->FindPrimitiveClass(*p++);
343 }
344}
345
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346ObjectArray<String>* Method::GetDexCacheStrings() const {
347 return GetFieldObject<ObjectArray<String>*>(
348 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false);
349}
350
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700351void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
352 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_),
353 new_dex_cache_strings, false);
354}
355
Ian Rogers19846512012-02-24 11:42:47 -0800356ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const {
357 return GetFieldObject<ObjectArray<Method>*>(
358 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false);
359}
360
361void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) {
362 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_),
363 new_dex_cache_methods, false);
364}
365
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const {
367 return GetFieldObject<ObjectArray<Class>*>(
368 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false);
369}
370
371void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
372 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_),
373 new_dex_cache_classes, false);
374}
375
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const {
377 return GetFieldObject<ObjectArray<StaticStorageBase>*>(
378 OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
379 false);
380}
381
382void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700383 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 new_value, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385}
386
Logan Chienfca7e872011-12-20 20:08:22 +0800387#if defined(ART_USE_LLVM_COMPILER)
388
389const InferredRegCategoryMap* Method::GetInferredRegCategoryMap() const {
390 const InferredRegCategoryMap* map = GetFieldPtr<const InferredRegCategoryMap*>(
391 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
392 DCHECK(map != NULL) << PrettyMethod(this);
393 return map;
394}
395
396void Method::SetInferredRegCategoryMap(const InferredRegCategoryMap* map) {
Logan Chiena1854662012-02-17 09:52:18 +0800397 const InferredRegCategoryMap* existing_map = GetFieldPtr<const InferredRegCategoryMap*>(
398 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
Logan Chienfca7e872011-12-20 20:08:22 +0800399
400 DCHECK(existing_map == NULL) << PrettyMethod(this);
401 DCHECK(map != NULL) << PrettyMethod(this);
402
403 // TODO: Remove if we won't find any use of InferredRegCategoryMap at runtime.
404 SetFieldPtr<const InferredRegCategoryMap*>(
405 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), map, false);
406}
407
408void Method::ResetInferredRegCategoryMap() {
409 delete GetInferredRegCategoryMap();
410 SetFieldPtr<const InferredRegCategoryMap*>(
Logan Chiena1854662012-02-17 09:52:18 +0800411 OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), NULL, false);
Logan Chienfca7e872011-12-20 20:08:22 +0800412}
413
414#endif
415
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700416size_t Method::NumArgRegisters(const StringPiece& shorty) {
417 CHECK_LE(1, shorty.length());
418 uint32_t num_registers = 0;
419 for (int i = 1; i < shorty.length(); ++i) {
420 char ch = shorty[i];
421 if (ch == 'D' || ch == 'J') {
422 num_registers += 2;
423 } else {
424 num_registers += 1;
Brian Carlstromb63ec392011-08-27 17:38:27 -0700425 }
426 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700427 return num_registers;
428}
429
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800430bool Method::IsProxyMethod() const {
431 return GetDeclaringClass()->IsProxyClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432}
433
Ian Rogers466bb252011-10-14 03:29:56 -0700434Method* Method::FindOverriddenMethod() const {
435 if (IsStatic()) {
436 return NULL;
437 }
438 Class* declaring_class = GetDeclaringClass();
439 Class* super_class = declaring_class->GetSuperClass();
440 uint16_t method_index = GetMethodIndex();
441 ObjectArray<Method>* super_class_vtable = super_class->GetVTable();
442 Method* result = NULL;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 // Did this method override a super class method? If so load the result from the super class'
444 // vtable
Ian Rogers466bb252011-10-14 03:29:56 -0700445 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
446 result = super_class_vtable->Get(method_index);
447 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800448 // Method didn't override superclass method so search interfaces
Ian Rogers16f93672012-02-14 12:29:06 -0800449 if (IsProxyMethod()) {
Ian Rogers19846512012-02-24 11:42:47 -0800450 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
451 CHECK_EQ(result,
452 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Ian Rogers16f93672012-02-14 12:29:06 -0800453 } else {
454 MethodHelper mh(this);
455 MethodHelper interface_mh;
456 ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable();
457 for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) {
458 InterfaceEntry* entry = iftable->Get(i);
459 Class* interface = entry->GetInterface();
460 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
461 Method* interface_method = interface->GetVirtualMethod(j);
462 interface_mh.ChangeMethod(interface_method);
463 if (mh.HasSameNameAndSignature(&interface_mh)) {
464 result = interface_method;
465 break;
466 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800467 }
468 }
Ian Rogers466bb252011-10-14 03:29:56 -0700469 }
470 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800471#ifndef NDEBUG
472 MethodHelper result_mh(result);
473 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
474#endif
Ian Rogers466bb252011-10-14 03:29:56 -0700475 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700476}
477
Elliott Hughes168670b2012-02-29 16:43:26 -0800478static const void* GetOatCode(const Method* m) {
479 Runtime* runtime = Runtime::Current();
480 const void* code = m->GetCode();
481 // Peel off any method tracing trampoline.
482 if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) {
483 code = runtime->GetTracer()->GetSavedCodeFromMap(m);
484 }
485 // Peel off any resolution stub.
Ian Rogersfb6adba2012-03-04 21:51:51 -0800486 if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800487 code = runtime->GetClassLinker()->GetOatCodeFor(m);
488 }
489 return code;
490}
491
Ian Rogersbdb03912011-09-14 00:55:44 -0700492uint32_t Method::ToDexPC(const uintptr_t pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700493 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700494 if (mapping_table == NULL) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800495 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
Ian Rogers67375ac2011-09-14 00:55:44 -0700496 return DexFile::kDexNoIndex; // Special no mapping case
Ian Rogersbdb03912011-09-14 00:55:44 -0700497 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700498 size_t mapping_table_length = GetMappingTableLength();
Elliott Hughes168670b2012-02-29 16:43:26 -0800499 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this));
Ian Rogersbdb03912011-09-14 00:55:44 -0700500 uint32_t best_offset = 0;
501 uint32_t best_dex_offset = 0;
502 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700503 uint32_t map_offset = mapping_table[i];
504 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700505 if (map_offset == sought_offset) {
506 best_offset = map_offset;
507 best_dex_offset = map_dex_offset;
508 break;
509 }
510 if (map_offset < sought_offset && map_offset > best_offset) {
511 best_offset = map_offset;
512 best_dex_offset = map_dex_offset;
513 }
514 }
515 return best_dex_offset;
516}
517
518uintptr_t Method::ToNativePC(const uint32_t dex_pc) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700519 const uint32_t* mapping_table = GetMappingTable();
Ian Rogersbdb03912011-09-14 00:55:44 -0700520 if (mapping_table == NULL) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700521 DCHECK_EQ(dex_pc, 0U);
Ian Rogersbdb03912011-09-14 00:55:44 -0700522 return 0; // Special no mapping/pc == 0 case
523 }
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700524 size_t mapping_table_length = GetMappingTableLength();
Ian Rogersbdb03912011-09-14 00:55:44 -0700525 for (size_t i = 0; i < mapping_table_length; i += 2) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700526 uint32_t map_offset = mapping_table[i];
527 uint32_t map_dex_offset = mapping_table[i + 1];
Ian Rogersbdb03912011-09-14 00:55:44 -0700528 if (map_dex_offset == dex_pc) {
Elliott Hughes168670b2012-02-29 16:43:26 -0800529 return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset;
Ian Rogersbdb03912011-09-14 00:55:44 -0700530 }
531 }
532 LOG(FATAL) << "Looking up Dex PC not contained in method";
533 return 0;
534}
535
536uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800537 MethodHelper mh(this);
538 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Ian Rogersbdb03912011-09-14 00:55:44 -0700539 // Iterate over the catch handlers associated with dex_pc
Ian Rogers0571d352011-11-03 19:51:38 -0700540 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
541 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
Ian Rogersbdb03912011-09-14 00:55:44 -0700542 // Catch all case
Ian Rogers0571d352011-11-03 19:51:38 -0700543 if (iter_type_idx == DexFile::kDexNoIndex16) {
544 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700545 }
546 // Does this catch exception type apply?
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800547 Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700548 if (iter_exception_type == NULL) {
549 // The verifier should take care of resolving all exception classes early
550 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800551 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700552 } else if (iter_exception_type->IsAssignableFrom(exception_type)) {
Ian Rogers0571d352011-11-03 19:51:38 -0700553 return it.GetHandlerAddress();
Ian Rogersbdb03912011-09-14 00:55:44 -0700554 }
555 }
556 // Handler not found
557 return DexFile::kDexNoIndex;
558}
559
Elliott Hughes77405792012-03-15 15:22:12 -0700560void Method::Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const {
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700561 // Push a transition back into managed code onto the linked list in thread.
Elliott Hughes34e06962012-04-09 13:55:55 -0700562 CHECK_EQ(kRunnable, self->GetState());
TDYa12785321912012-04-01 15:24:56 -0700563
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700564#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700565 NativeToManagedRecord record;
566 self->PushNativeToManagedRecord(&record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700567#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700568
569 // Call the invoke stub associated with the method.
570 // Pass everything as arguments.
571 const Method::InvokeStub* stub = GetInvokeStub();
Elliott Hughes1240dad2011-09-09 16:24:50 -0700572
573 bool have_executable_code = (GetCode() != NULL);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700574
TDYa12785321912012-04-01 15:24:56 -0700575#if defined(ART_USE_LLVM_COMPILER)
576 if (stub == NULL && !have_executable_code) {
577 art_ensure_link_from_code(const_cast<Method*>(this));
578 stub = GetInvokeStub();
579 have_executable_code = (GetCode() != NULL);
580 }
581#endif
582
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500583 if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) {
Elliott Hughes9f865372011-10-11 15:04:19 -0700584 bool log = false;
585 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800586 LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p",
587 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700588 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700589 (*stub)(this, receiver, self, args, result);
Elliott Hughes9f865372011-10-11 15:04:19 -0700590 if (log) {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800591 LOG(INFO) << StringPrintf("returned %s code=%p stub=%p",
592 PrettyMethod(this).c_str(), GetCode(), stub);
Elliott Hughes9f865372011-10-11 15:04:19 -0700593 }
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700594 } else {
Elliott Hughesba8eee12012-01-24 20:25:24 -0800595 LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s",
596 PrettyMethod(this).c_str(), GetCode(), stub,
597 Runtime::Current()->IsStarted() ? "true" : "false");
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700598 if (result != NULL) {
599 result->j = 0;
600 }
601 }
602
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700603#if !defined(ART_USE_LLVM_COMPILER)
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700604 // Pop transition.
605 self->PopNativeToManagedRecord(record);
Shih-wei Liao9e0e54d2012-04-02 19:22:28 -0700606#endif
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700607}
608
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700609bool Method::IsRegistered() const {
Brian Carlstrom16192862011-09-12 17:50:06 -0700610 void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false);
Ian Rogers19846512012-02-24 11:42:47 -0800611 CHECK(native_method != NULL);
Ian Rogers169c9a72011-11-13 20:13:17 -0800612 void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
Brian Carlstrom16192862011-09-12 17:50:06 -0700613 return native_method != jni_stub;
614}
615
Ian Rogers60db5ab2012-02-20 17:02:00 -0800616void Method::RegisterNative(Thread* self, const void* native_method) {
617 DCHECK(Thread::Current() == self);
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700618 CHECK(IsNative()) << PrettyMethod(this);
619 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers60db5ab2012-02-20 17:02:00 -0800620 if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) {
621 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
622 native_method, false);
623 } else {
624 // We've been asked to associate this method with the given native method but are working
625 // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct
626 // the native method to runtime support and store the target somewhere runtime support will
627 // find it.
628#if defined(__arm__)
629 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_),
630 reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false);
631#else
632 UNIMPLEMENTED(FATAL);
633#endif
634 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_),
635 reinterpret_cast<const uint8_t*>(native_method), false);
636 }
Brian Carlstrom16192862011-09-12 17:50:06 -0700637}
638
Ian Rogers19846512012-02-24 11:42:47 -0800639void Method::UnregisterNative(Thread* self) {
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700640 CHECK(IsNative()) << PrettyMethod(this);
Brian Carlstrom16192862011-09-12 17:50:06 -0700641 // restore stub to lookup native pointer via dlsym
Ian Rogers19846512012-02-24 11:42:47 -0800642 RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData());
Brian Carlstrom16192862011-09-12 17:50:06 -0700643}
644
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700645void Class::SetStatus(Status new_status) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700646 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
647 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700648 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Brian Carlstrom4d9716c2012-01-30 01:49:33 -0800649 if (new_status == kStatusError) {
650 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
651
652 // stash current exception
653 Thread* self = Thread::Current();
654 SirtRef<Throwable> exception(self->GetException());
655 CHECK(exception.get() != NULL);
656
657 // clear exception to call FindSystemClass
658 self->ClearException();
659 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
660 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
661 CHECK(!self->IsExceptionPending());
662
663 // only verification errors, not initialization problems, should set a verify error.
664 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
665 Class* exception_class = exception->GetClass();
666 if (!eiie_class->IsAssignableFrom(exception_class)) {
667 SetVerifyErrorClass(exception_class);
668 }
669
670 // restore exception
671 self->SetException(exception.get());
672 }
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700673 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700674}
675
676DexCache* Class::GetDexCache() const {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700677 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700678}
679
680void Class::SetDexCache(DexCache* new_dex_cache) {
Elliott Hughesd9cdfe92011-10-06 16:09:04 -0700681 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700682}
683
Brian Carlstrom1f870082011-08-23 16:02:11 -0700684Object* Class::AllocObject() {
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700685 DCHECK(!IsArrayClass()) << PrettyClass(this);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700686 DCHECK(IsInstantiable()) << PrettyClass(this);
Jesse Wilson9a6bae82011-11-14 14:57:30 -0500687 // TODO: decide whether we want this check. It currently fails during bootstrap.
688 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
Brian Carlstrom96a253a2011-10-27 18:38:10 -0700689 DCHECK_GE(this->object_size_, sizeof(Object));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800690 return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700691}
692
Ian Rogers0571d352011-11-03 19:51:38 -0700693void Class::SetClassSize(size_t new_class_size) {
694 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
695 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
696}
697
Ian Rogersd418eda2012-01-30 12:14:28 -0800698// Return the class' name. The exact format is bizarre, but it's the specified behavior for
699// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
700// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
701// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
702String* Class::ComputeName() {
703 String* name = GetName();
704 if (name != NULL) {
705 return name;
706 }
707 std::string descriptor(ClassHelper(this).GetDescriptor());
708 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
709 // The descriptor indicates that this is the class for
710 // a primitive type; special-case the return value.
711 const char* c_name = NULL;
712 switch (descriptor[0]) {
713 case 'Z': c_name = "boolean"; break;
714 case 'B': c_name = "byte"; break;
715 case 'C': c_name = "char"; break;
716 case 'S': c_name = "short"; break;
717 case 'I': c_name = "int"; break;
718 case 'J': c_name = "long"; break;
719 case 'F': c_name = "float"; break;
720 case 'D': c_name = "double"; break;
721 case 'V': c_name = "void"; break;
722 default:
723 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
724 }
725 name = String::AllocFromModifiedUtf8(c_name);
726 } else {
727 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
728 // components.
729 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
730 descriptor.erase(0, 1);
731 descriptor.erase(descriptor.size() - 1);
732 }
733 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
734 name = String::AllocFromModifiedUtf8(descriptor.c_str());
735 }
736 SetName(name);
737 return name;
738}
739
Elliott Hughes4681c802011-09-25 18:04:37 -0700740void Class::DumpClass(std::ostream& os, int flags) const {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700741 if ((flags & kDumpClassFullDetail) == 0) {
742 os << PrettyClass(this);
743 if ((flags & kDumpClassClassLoader) != 0) {
744 os << ' ' << GetClassLoader();
745 }
746 if ((flags & kDumpClassInitialized) != 0) {
747 os << ' ' << GetStatus();
748 }
Elliott Hughese0918552011-10-28 17:18:29 -0700749 os << "\n";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700750 return;
751 }
752
753 Class* super = GetSuperClass();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800754 ClassHelper kh(this);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700755 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800756 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700757 os << " objectSize=" << SizeOf() << " "
758 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
759 os << StringPrintf(" access=0x%04x.%04x\n",
760 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
761 if (super != NULL) {
762 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
763 }
764 if (IsArrayClass()) {
765 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
766 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800767 if (kh.NumInterfaces() > 0) {
768 os << " interfaces (" << kh.NumInterfaces() << "):\n";
769 for (size_t i = 0; i < kh.NumInterfaces(); ++i) {
770 Class* interface = kh.GetInterface(i);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700771 const ClassLoader* cl = interface->GetClassLoader();
Elliott Hughese689d512012-01-18 23:39:47 -0800772 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700773 }
774 }
775 os << " vtable (" << NumVirtualMethods() << " entries, "
776 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
777 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800778 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700779 }
780 os << " direct methods (" << NumDirectMethods() << " entries):\n";
781 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800782 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700783 }
784 if (NumStaticFields() > 0) {
785 os << " static fields (" << NumStaticFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700786 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700787 for (size_t i = 0; i < NumStaticFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800788 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700789 }
790 } else {
791 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700792 }
793 }
794 if (NumInstanceFields() > 0) {
795 os << " instance fields (" << NumInstanceFields() << " entries):\n";
Elliott Hughes03f03492011-09-26 13:38:08 -0700796 if (IsResolved() || IsErroneous()) {
Elliott Hughes4681c802011-09-25 18:04:37 -0700797 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Elliott Hughese689d512012-01-18 23:39:47 -0800798 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
Elliott Hughes4681c802011-09-25 18:04:37 -0700799 }
800 } else {
801 os << " <not yet available>";
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700802 }
803 }
804}
805
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700806void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
807 if (new_reference_offsets != CLASS_WALK_SUPER) {
808 // Sanity check that the number of bits set in the reference offset bitmap
809 // agrees with the number of references
Elliott Hughescccd84f2011-12-05 16:51:54 -0800810 size_t count = 0;
811 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
812 count += c->NumReferenceInstanceFieldsDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700813 }
Elliott Hughescccd84f2011-12-05 16:51:54 -0800814 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700815 }
816 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
817 new_reference_offsets, false);
818}
819
820void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
821 if (new_reference_offsets != CLASS_WALK_SUPER) {
822 // Sanity check that the number of bits set in the reference offset bitmap
823 // agrees with the number of references
824 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
825 NumReferenceStaticFieldsDuringLinking());
826 }
827 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
828 new_reference_offsets, false);
829}
830
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700831bool Class::Implements(const Class* klass) const {
832 DCHECK(klass != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700833 DCHECK(klass->IsInterface()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700834 // All interfaces implemented directly and by our superclass, and
835 // recursively all super-interfaces of those interfaces, are listed
836 // in iftable_, so we can just do a linear scan through that.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700837 int32_t iftable_count = GetIfTableCount();
838 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
839 for (int32_t i = 0; i < iftable_count; i++) {
840 if (iftable->Get(i)->GetInterface() == klass) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700841 return true;
842 }
843 }
844 return false;
845}
846
Elliott Hughese84278b2012-03-22 10:06:53 -0700847// Determine whether "this" is assignable from "src", where both of these
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700848// are array classes.
849//
850// Consider an array class, e.g. Y[][], where Y is a subclass of X.
851// Y[][] = Y[][] --> true (identity)
852// X[][] = Y[][] --> true (element superclass)
853// Y = Y[][] --> false
854// Y[] = Y[][] --> false
855// Object = Y[][] --> true (everything is an object)
856// Object[] = Y[][] --> true
857// Object[][] = Y[][] --> true
858// Object[][][] = Y[][] --> false (too many []s)
859// Serializable = Y[][] --> true (all arrays are Serializable)
860// Serializable[] = Y[][] --> true
861// Serializable[][] = Y[][] --> false (unless Y is Serializable)
862//
863// Don't forget about primitive types.
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700864// Object[] = int[] --> false
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700865//
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700866bool Class::IsArrayAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700867 DCHECK(IsArrayClass()) << PrettyClass(this);
868 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700869 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700870}
871
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700872bool Class::IsAssignableFromArray(const Class* src) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700873 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
874 DCHECK(src->IsArrayClass()) << PrettyClass(src);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700875 if (!IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700876 // If "this" is not also an array, it must be Object.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700877 // src's super should be java_lang_Object, since it is an array.
878 Class* java_lang_Object = src->GetSuperClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700879 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
880 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700881 return this == java_lang_Object;
882 }
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700883 return IsArrayAssignableFromArray(src);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700884}
885
886bool Class::IsSubClass(const Class* klass) const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700887 DCHECK(!IsInterface()) << PrettyClass(this);
888 DCHECK(!IsArrayClass()) << PrettyClass(this);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700889 const Class* current = this;
890 do {
891 if (current == klass) {
892 return true;
893 }
894 current = current->GetSuperClass();
895 } while (current != NULL);
896 return false;
897}
898
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800899bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700900 size_t i = 0;
901 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
902 ++i;
903 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700904 if (descriptor1.find('/', i) != StringPiece::npos ||
905 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700906 return false;
907 } else {
908 return true;
909 }
910}
911
912bool Class::IsInSamePackage(const Class* that) const {
913 const Class* klass1 = this;
914 const Class* klass2 = that;
915 if (klass1 == klass2) {
916 return true;
917 }
918 // Class loaders must match.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700919 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700920 return false;
921 }
922 // Arrays are in the same package when their element classes are.
jeffhao4a801a42011-09-23 13:53:40 -0700923 while (klass1->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700924 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700925 }
jeffhao4a801a42011-09-23 13:53:40 -0700926 while (klass2->IsArrayClass()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700927 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700928 }
929 // Compare the package part of the descriptor string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800930 ClassHelper kh(klass1);
Elliott Hughes95572412011-12-13 18:14:20 -0800931 std::string descriptor1(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800932 kh.ChangeClass(klass2);
Elliott Hughes95572412011-12-13 18:14:20 -0800933 std::string descriptor2(kh.GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800934 return IsInSamePackage(descriptor1, descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700935}
936
Elliott Hughesdbb40792011-11-18 17:05:22 -0800937bool Class::IsClassClass() const {
938 Class* java_lang_Class = GetClass()->GetClass();
939 return this == java_lang_Class;
940}
941
942bool Class::IsStringClass() const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800943 return this == String::GetJavaLangString();
Elliott Hughesdbb40792011-11-18 17:05:22 -0800944}
945
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800946bool Class::IsThrowableClass() const {
947 Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;");
948 return throwable->IsAssignableFrom(this);
949}
950
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800951ClassLoader* Class::GetClassLoader() const {
952 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700953}
954
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700955void Class::SetClassLoader(const ClassLoader* new_cl) {
956 ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl);
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700958}
959
Ian Rogersa32a6fd2012-02-06 20:18:44 -0800960Method* Class::FindVirtualMethodForInterface(Method* method) {
Brian Carlstrom30b94452011-08-25 21:35:26 -0700961 Class* declaring_class = method->GetDeclaringClass();
Brian Carlstrom65ca0772011-09-24 16:03:08 -0700962 DCHECK(declaring_class != NULL) << PrettyClass(this);
963 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -0700964 // TODO cache to improve lookup speed
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700965 int32_t iftable_count = GetIfTableCount();
966 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
967 for (int32_t i = 0; i < iftable_count; i++) {
968 InterfaceEntry* interface_entry = iftable->Get(i);
969 if (interface_entry->GetInterface() == declaring_class) {
970 return interface_entry->GetMethodArray()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -0700971 }
972 }
Brian Carlstrom30b94452011-08-25 21:35:26 -0700973 return NULL;
974}
975
Ian Rogers466bb252011-10-14 03:29:56 -0700976Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
jeffhaobdb76512011-09-07 11:43:16 -0700977 // Check the current class before checking the interfaces.
Ian Rogers94c0e332012-01-18 22:11:47 -0800978 Method* method = FindDeclaredVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700979 if (method != NULL) {
980 return method;
981 }
982
Brian Carlstrom4b620ff2011-09-11 01:11:01 -0700983 int32_t iftable_count = GetIfTableCount();
984 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
985 for (int32_t i = 0; i < iftable_count; i++) {
986 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature);
jeffhaobdb76512011-09-07 11:43:16 -0700987 if (method != NULL) {
988 return method;
989 }
990 }
991 return NULL;
992}
993
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800994Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
995 // Check the current class before checking the interfaces.
996 Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
997 if (method != NULL) {
998 return method;
999 }
1000
1001 int32_t iftable_count = GetIfTableCount();
1002 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1003 for (int32_t i = 0; i < iftable_count; i++) {
1004 method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx);
1005 if (method != NULL) {
1006 return method;
1007 }
1008 }
1009 return NULL;
1010}
1011
1012
1013Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001014 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001015 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001016 Method* method = GetDirectMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001017 mh.ChangeMethod(method);
1018 if (name == mh.GetName() && signature == mh.GetSignature()) {
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001019 return method;
Ian Rogersb033c752011-07-20 12:22:35 -07001020 }
1021 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001022 return NULL;
Ian Rogersb033c752011-07-20 12:22:35 -07001023}
1024
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001025Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1026 if (GetDexCache() == dex_cache) {
1027 for (size_t i = 0; i < NumDirectMethods(); ++i) {
1028 Method* method = GetDirectMethod(i);
1029 if (method->GetDexMethodIndex() == dex_method_idx) {
1030 return method;
1031 }
1032 }
1033 }
1034 return NULL;
1035}
1036
1037Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
1038 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001039 Method* method = klass->FindDeclaredDirectMethod(name, signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001040 if (method != NULL) {
1041 return method;
1042 }
1043 }
1044 return NULL;
1045}
1046
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001047Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1048 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1049 Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
1050 if (method != NULL) {
1051 return method;
1052 }
1053 }
1054 return NULL;
1055}
1056
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001057Method* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers466bb252011-10-14 03:29:56 -07001058 const StringPiece& signature) const {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001059 MethodHelper mh;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001060 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Ian Rogersb033c752011-07-20 12:22:35 -07001061 Method* method = GetVirtualMethod(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001062 mh.ChangeMethod(method);
1063 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers466bb252011-10-14 03:29:56 -07001064 return method;
Ian Rogers466bb252011-10-14 03:29:56 -07001065 }
1066 }
1067 return NULL;
1068}
1069
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001070Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1071 if (GetDexCache() == dex_cache) {
1072 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
1073 Method* method = GetVirtualMethod(i);
1074 if (method->GetDexMethodIndex() == dex_method_idx) {
1075 return method;
1076 }
1077 }
1078 }
1079 return NULL;
1080}
1081
Ian Rogers466bb252011-10-14 03:29:56 -07001082Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
1083 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1084 Method* method = klass->FindDeclaredVirtualMethod(name, signature);
1085 if (method != NULL) {
1086 return method;
1087 }
1088 }
1089 return NULL;
1090}
1091
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001092Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
1093 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
1094 Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
1095 if (method != NULL) {
1096 return method;
1097 }
1098 }
1099 return NULL;
1100}
1101
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001102Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001103 // Is the field in this class?
1104 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001105 FieldHelper fh;
Elliott Hughescdf53122011-08-19 15:46:09 -07001106 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1107 Field* f = GetInstanceField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001108 fh.ChangeField(f);
1109 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001110 return f;
1111 }
1112 }
1113 return NULL;
1114}
1115
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001116Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1117 if (GetDexCache() == dex_cache) {
1118 for (size_t i = 0; i < NumInstanceFields(); ++i) {
1119 Field* f = GetInstanceField(i);
1120 if (f->GetDexFieldIndex() == dex_field_idx) {
1121 return f;
1122 }
1123 }
1124 }
1125 return NULL;
1126}
1127
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001128Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Elliott Hughescdf53122011-08-19 15:46:09 -07001129 // Is the field in this class, or any of its superclasses?
1130 // Interfaces are not relevant because they can't contain instance fields.
1131 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132 Field* f = c->FindDeclaredInstanceField(name, type);
Elliott Hughescdf53122011-08-19 15:46:09 -07001133 if (f != NULL) {
1134 return f;
1135 }
1136 }
1137 return NULL;
1138}
1139
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001140Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1141 // Is the field in this class, or any of its superclasses?
1142 // Interfaces are not relevant because they can't contain instance fields.
1143 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
1144 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
1145 if (f != NULL) {
1146 return f;
1147 }
1148 }
1149 return NULL;
1150}
1151
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001152Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
1153 DCHECK(type != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001154 FieldHelper fh;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001155 for (size_t i = 0; i < NumStaticFields(); ++i) {
1156 Field* f = GetStaticField(i);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001157 fh.ChangeField(f);
1158 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001159 return f;
1160 }
1161 }
1162 return NULL;
1163}
1164
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001165Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1166 if (dex_cache == GetDexCache()) {
1167 for (size_t i = 0; i < NumStaticFields(); ++i) {
1168 Field* f = GetStaticField(i);
1169 if (f->GetDexFieldIndex() == dex_field_idx) {
1170 return f;
1171 }
1172 }
1173 }
1174 return NULL;
1175}
1176
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001177Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
1178 // Is the field in this class (or its interfaces), or any of its
1179 // superclasses (or their interfaces)?
Ian Rogersb067ac22011-12-13 18:05:09 -08001180 ClassHelper kh;
1181 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001182 // Is the field in this class?
Ian Rogersb067ac22011-12-13 18:05:09 -08001183 Field* f = k->FindDeclaredStaticField(name, type);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001184 if (f != NULL) {
1185 return f;
1186 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001187 // Is this field in any of this class' interfaces?
Ian Rogersb067ac22011-12-13 18:05:09 -08001188 kh.ChangeClass(k);
1189 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1190 Class* interface = kh.GetInterface(i);
1191 f = interface->FindDeclaredStaticField(name, type);
1192 if (f != NULL) {
1193 return f;
1194 }
1195 }
1196 }
1197 return NULL;
1198}
1199
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001200Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
1201 ClassHelper kh;
1202 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1203 // Is the field in this class?
1204 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
1205 if (f != NULL) {
1206 return f;
1207 }
1208 // Is this field in any of this class' interfaces?
1209 kh.ChangeClass(k);
1210 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1211 Class* interface = kh.GetInterface(i);
1212 f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx);
1213 if (f != NULL) {
1214 return f;
1215 }
1216 }
1217 }
1218 return NULL;
1219}
1220
Ian Rogersb067ac22011-12-13 18:05:09 -08001221Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
1222 // Find a field using the JLS field resolution order
1223 ClassHelper kh;
1224 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
1225 // Is the field in this class?
1226 Field* f = k->FindDeclaredInstanceField(name, type);
1227 if (f != NULL) {
1228 return f;
1229 }
1230 f = k->FindDeclaredStaticField(name, type);
1231 if (f != NULL) {
1232 return f;
1233 }
1234 // Is this field in any of this class' interfaces?
1235 kh.ChangeClass(k);
1236 for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) {
1237 Class* interface = kh.GetInterface(i);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001238 f = interface->FindDeclaredStaticField(name, type);
1239 if (f != NULL) {
1240 return f;
1241 }
1242 }
1243 }
1244 return NULL;
1245}
1246
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001247Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001248 DCHECK(array_class != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001249 DCHECK_GE(component_count, 0);
1250 DCHECK(array_class->IsArrayClass());
Elliott Hughesb408de72011-10-04 14:35:05 -07001251
Ian Rogersa15e67d2012-02-28 13:51:55 -08001252 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
Elliott Hughesb408de72011-10-04 14:35:05 -07001253 size_t data_size = component_count * component_size;
1254 size_t size = header_size + data_size;
1255
1256 // Check for overflow and throw OutOfMemoryError if this was an unreasonable request.
1257 size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size);
1258 if (data_size >> component_shift != size_t(component_count) || size < data_size) {
1259 Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;",
Elliott Hughes81ff3182012-03-23 20:35:56 -07001260 "%s of length %d would overflow",
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001261 PrettyDescriptor(array_class).c_str(), component_count);
Elliott Hughesb408de72011-10-04 14:35:05 -07001262 return NULL;
1263 }
1264
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08001265 Heap* heap = Runtime::Current()->GetHeap();
1266 Array* array = down_cast<Array*>(heap->AllocObject(array_class, size));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001267 if (array != NULL) {
1268 DCHECK(array->IsArrayInstance());
1269 array->SetLength(component_count);
1270 }
1271 return array;
1272}
1273
1274Array* Array::Alloc(Class* array_class, int32_t component_count) {
1275 return Alloc(array_class, component_count, array_class->GetComponentSize());
1276}
1277
Elliott Hughes80609252011-09-23 17:24:51 -07001278bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001279 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001280 "length=%i; index=%i", length_, index);
1281 return false;
1282}
1283
1284bool Array::ThrowArrayStoreException(Object* object) const {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001285 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes80609252011-09-23 17:24:51 -07001286 "Can't store an element of type %s into an array of type %s",
1287 PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str());
1288 return false;
1289}
1290
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001291template<typename T>
1292PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001293 DCHECK(array_class_ != NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001294 Array* raw_array = Array::Alloc(array_class_, length, sizeof(T));
1295 return down_cast<PrimitiveArray<T>*>(raw_array);
1296}
1297
1298template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL;
1299
1300// Explicitly instantiate all the primitive array types.
1301template class PrimitiveArray<uint8_t>; // BooleanArray
1302template class PrimitiveArray<int8_t>; // ByteArray
1303template class PrimitiveArray<uint16_t>; // CharArray
1304template class PrimitiveArray<double>; // DoubleArray
1305template class PrimitiveArray<float>; // FloatArray
1306template class PrimitiveArray<int32_t>; // IntArray
1307template class PrimitiveArray<int64_t>; // LongArray
1308template class PrimitiveArray<int16_t>; // ShortArray
1309
Ian Rogers466bb252011-10-14 03:29:56 -07001310// Explicitly instantiate Class[][]
1311template class ObjectArray<ObjectArray<Class> >;
1312
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001313// TODO: get global references for these
1314Class* String::java_lang_String_ = NULL;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001315
Brian Carlstroma663ea52011-08-19 23:33:41 -07001316void String::SetClass(Class* java_lang_String) {
1317 CHECK(java_lang_String_ == NULL);
1318 CHECK(java_lang_String != NULL);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001319 java_lang_String_ = java_lang_String;
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001320}
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001321
Brian Carlstroma663ea52011-08-19 23:33:41 -07001322void String::ResetClass() {
1323 CHECK(java_lang_String_ != NULL);
1324 java_lang_String_ = NULL;
1325}
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001326
Brian Carlstromc74255f2011-09-11 22:47:39 -07001327String* String::Intern() {
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001328 return Runtime::Current()->GetInternTable()->InternWeak(this);
1329}
1330
Brian Carlstrom395520e2011-09-25 19:35:00 -07001331int32_t String::GetHashCode() {
1332 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1333 if (result == 0) {
1334 ComputeHashCode();
1335 }
1336 result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false);
1337 DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0)
1338 << ToModifiedUtf8() << " " << result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339 return result;
1340}
1341
1342int32_t String::GetLength() const {
1343 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false);
1344 DCHECK(result >= 0 && result <= GetCharArray()->GetLength());
1345 return result;
1346}
1347
1348uint16_t String::CharAt(int32_t index) const {
1349 // TODO: do we need this? Equals is the only caller, and could
1350 // bounds check itself.
1351 if (index < 0 || index >= count_) {
1352 Thread* self = Thread::Current();
Elliott Hughes5cb5ad22011-10-02 12:13:39 -07001353 self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;",
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001354 "length=%i; index=%i", count_, index);
1355 return 0;
1356 }
1357 return GetCharArray()->Get(index + GetOffset());
1358}
1359
1360String* String::AllocFromUtf16(int32_t utf16_length,
1361 const uint16_t* utf16_data_in,
1362 int32_t hash_code) {
Jesse Wilson25e79a52011-11-18 15:31:58 -05001363 CHECK(utf16_data_in != NULL || utf16_length == 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001364 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001365 if (string == NULL) {
1366 return NULL;
1367 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001368 // TODO: use 16-bit wide memset variant
1369 CharArray* array = const_cast<CharArray*>(string->GetCharArray());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001370 if (array == NULL) {
1371 return NULL;
1372 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001373 for (int i = 0; i < utf16_length; i++) {
1374 array->Set(i, utf16_data_in[i]);
1375 }
1376 if (hash_code != 0) {
1377 string->SetHashCode(hash_code);
1378 } else {
1379 string->ComputeHashCode();
1380 }
1381 return string;
1382}
1383
1384String* String::AllocFromModifiedUtf8(const char* utf) {
Ian Rogers48601312011-12-07 16:45:19 -08001385 if (utf == NULL) {
1386 return NULL;
1387 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001388 size_t char_count = CountModifiedUtf8Chars(utf);
1389 return AllocFromModifiedUtf8(char_count, utf);
1390}
1391
1392String* String::AllocFromModifiedUtf8(int32_t utf16_length,
1393 const char* utf8_data_in) {
1394 String* string = Alloc(GetJavaLangString(), utf16_length);
Elliott Hughesb51036c2011-10-12 23:49:11 -07001395 if (string == NULL) {
1396 return NULL;
1397 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 uint16_t* utf16_data_out =
1399 const_cast<uint16_t*>(string->GetCharArray()->GetData());
1400 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1401 string->ComputeHashCode();
1402 return string;
1403}
1404
1405String* String::Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001406 SirtRef<CharArray> array(CharArray::Alloc(utf16_length));
1407 if (array.get() == NULL) {
Elliott Hughesb51036c2011-10-12 23:49:11 -07001408 return NULL;
1409 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001410 return Alloc(java_lang_String, array.get());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001411}
1412
1413String* String::Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -07001414 SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415 String* string = down_cast<String*>(java_lang_String->AllocObject());
Elliott Hughesb51036c2011-10-12 23:49:11 -07001416 if (string == NULL) {
1417 return NULL;
1418 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001419 string->SetArray(array);
1420 string->SetCount(array->GetLength());
1421 return string;
1422}
1423
1424bool String::Equals(const String* that) const {
1425 if (this == that) {
1426 // Quick reference equality test
1427 return true;
1428 } else if (that == NULL) {
1429 // Null isn't an instanceof anything
1430 return false;
1431 } else if (this->GetLength() != that->GetLength()) {
1432 // Quick length inequality test
1433 return false;
1434 } else {
Elliott Hughes20cde902011-10-04 17:37:27 -07001435 // Note: don't short circuit on hash code as we're presumably here as the
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001436 // hash code was already equal
1437 for (int32_t i = 0; i < that->GetLength(); ++i) {
1438 if (this->CharAt(i) != that->CharAt(i)) {
1439 return false;
1440 }
1441 }
1442 return true;
1443 }
1444}
1445
Elliott Hughes5d78d392011-12-13 16:53:05 -08001446bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001447 if (this->GetLength() != that_length) {
1448 return false;
1449 } else {
1450 for (int32_t i = 0; i < that_length; ++i) {
1451 if (this->CharAt(i) != that_chars[that_offset + i]) {
1452 return false;
1453 }
1454 }
1455 return true;
1456 }
1457}
1458
1459bool String::Equals(const char* modified_utf8) const {
1460 for (int32_t i = 0; i < GetLength(); ++i) {
1461 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1462 if (ch == '\0' || ch != CharAt(i)) {
1463 return false;
1464 }
1465 }
1466 return *modified_utf8 == '\0';
1467}
1468
1469bool String::Equals(const StringPiece& modified_utf8) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001470 if (modified_utf8.size() != GetLength()) {
1471 return false;
1472 }
1473 const char* p = modified_utf8.data();
1474 for (int32_t i = 0; i < GetLength(); ++i) {
1475 uint16_t ch = GetUtf16FromUtf8(&p);
1476 if (ch != CharAt(i)) {
1477 return false;
1478 }
1479 }
1480 return true;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001481}
1482
1483// Create a modified UTF-8 encoded std::string from a java/lang/String object.
1484std::string String::ToModifiedUtf8() const {
1485 const uint16_t* chars = GetCharArray()->GetData() + GetOffset();
jeffhao0ce13152012-03-27 19:45:50 -07001486 size_t byte_count = GetUtfLength();
Elliott Hughes398f64b2012-03-26 18:05:48 -07001487 std::string result(byte_count, static_cast<char>(0));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001488 ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength());
1489 return result;
1490}
1491
Ian Rogers1c5eb702012-02-01 09:18:34 -08001492void Throwable::SetCause(Throwable* cause) {
1493 CHECK(cause != NULL);
1494 CHECK(cause != this);
1495 CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL);
1496 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false);
1497}
1498
Ian Rogers466bb252011-10-14 03:29:56 -07001499bool Throwable::IsCheckedException() const {
1500 Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;");
1501 if (InstanceOf(error)) {
1502 return false;
1503 }
1504 Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;");
1505 return !InstanceOf(jlre);
1506}
1507
Ian Rogers9074b992011-10-26 17:41:55 -07001508std::string Throwable::Dump() const {
Ian Rogers09f6b562012-01-31 21:58:52 -08001509 std::string result(PrettyTypeOf(this));
1510 result += ": ";
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001511 String* msg = GetDetailMessage();
Ian Rogers09f6b562012-01-31 21:58:52 -08001512 if (msg != NULL) {
1513 result += msg->ToModifiedUtf8();
Ian Rogers9074b992011-10-26 17:41:55 -07001514 }
Ian Rogers09f6b562012-01-31 21:58:52 -08001515 result += "\n";
1516 Object* stack_state = GetStackState();
1517 // check stack state isn't missing or corrupt
1518 if (stack_state != NULL && stack_state->IsObjectArray()) {
1519 // Decode the internal stack trace into the depth and method trace
1520 ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state);
1521 int32_t depth = method_trace->GetLength() - 1;
Ian Rogers19846512012-02-24 11:42:47 -08001522 IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth));
1523 MethodHelper mh;
Ian Rogers09f6b562012-01-31 21:58:52 -08001524 for (int32_t i = 0; i < depth; ++i) {
1525 Method* method = down_cast<Method*>(method_trace->Get(i));
Ian Rogers19846512012-02-24 11:42:47 -08001526 mh.ChangeMethod(method);
1527 uint32_t native_pc = pc_trace->Get(i);
1528 int32_t line_number = mh.GetLineNumFromNativePC(native_pc);
1529 const char* source_file = mh.GetDeclaringClassSourceFile();
1530 result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(),
1531 source_file, line_number);
Ian Rogers09f6b562012-01-31 21:58:52 -08001532 }
Ian Rogers9074b992011-10-26 17:41:55 -07001533 }
Ian Rogers1c5eb702012-02-01 09:18:34 -08001534 Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false);
Ian Rogersc8b306f2012-02-17 21:34:44 -08001535 if (cause != NULL && cause != this) { // Constructor makes cause == this by default.
Ian Rogers1c5eb702012-02-01 09:18:34 -08001536 result += "Caused by: ";
1537 result += cause->Dump();
1538 }
Ian Rogers9074b992011-10-26 17:41:55 -07001539 return result;
1540}
1541
Ian Rogers5167c972012-02-03 10:41:20 -08001542
1543Class* Throwable::java_lang_Throwable_ = NULL;
1544
1545void Throwable::SetClass(Class* java_lang_Throwable) {
1546 CHECK(java_lang_Throwable_ == NULL);
1547 CHECK(java_lang_Throwable != NULL);
1548 java_lang_Throwable_ = java_lang_Throwable;
1549}
1550
1551void Throwable::ResetClass() {
1552 CHECK(java_lang_Throwable_ != NULL);
1553 java_lang_Throwable_ = NULL;
1554}
1555
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001556Class* StackTraceElement::java_lang_StackTraceElement_ = NULL;
1557
1558void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) {
1559 CHECK(java_lang_StackTraceElement_ == NULL);
1560 CHECK(java_lang_StackTraceElement != NULL);
1561 java_lang_StackTraceElement_ = java_lang_StackTraceElement;
1562}
1563
1564void StackTraceElement::ResetClass() {
1565 CHECK(java_lang_StackTraceElement_ != NULL);
1566 java_lang_StackTraceElement_ = NULL;
1567}
1568
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001569StackTraceElement* StackTraceElement::Alloc(String* declaring_class,
1570 String* method_name,
1571 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001572 int32_t line_number) {
1573 StackTraceElement* trace =
1574 down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
1575 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_),
1576 const_cast<String*>(declaring_class), false);
1577 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_),
1578 const_cast<String*>(method_name), false);
1579 trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_),
1580 const_cast<String*>(file_name), false);
1581 trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_),
1582 line_number, false);
1583 return trace;
1584}
1585
Elliott Hughes1f359b02011-07-17 14:27:17 -07001586static const char* kClassStatusNames[] = {
1587 "Error",
1588 "NotReady",
1589 "Idx",
1590 "Loaded",
1591 "Resolved",
1592 "Verifying",
1593 "Verified",
1594 "Initializing",
1595 "Initialized"
1596};
1597std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) {
1598 if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) {
Brian Carlstromae3ac012011-07-27 01:30:28 -07001599 os << kClassStatusNames[rhs + 1];
Elliott Hughes1f359b02011-07-17 14:27:17 -07001600 } else {
Ian Rogersb033c752011-07-20 12:22:35 -07001601 os << "Class::Status[" << static_cast<int>(rhs) << "]";
Elliott Hughes1f359b02011-07-17 14:27:17 -07001602 }
1603 return os;
1604}
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001605
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001606} // namespace art