Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "object.h" |
| 18 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 19 | #include <string.h> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 20 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 22 | #include <iostream> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <utility> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 26 | #include "class_linker.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 27 | #include "class_loader.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 28 | #include "dex_cache.h" |
| 29 | #include "dex_file.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | #include "globals.h" |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 31 | #include "heap.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 32 | #include "intern_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | #include "logging.h" |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 34 | #include "monitor.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 35 | #include "object_utils.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 36 | #include "runtime.h" |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 37 | #include "runtime_support.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 38 | #include "stack.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 39 | #include "utils.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 40 | |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 41 | #if defined(ART_USE_LLVM_COMPILER) |
| 42 | #include "compiler_llvm/inferred_reg_category_map.h" |
| 43 | using art::compiler_llvm::InferredRegCategoryMap; |
| 44 | #endif |
| 45 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 46 | namespace art { |
| 47 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 48 | String* Object::AsString() { |
| 49 | DCHECK(GetClass()->IsStringClass()); |
| 50 | return down_cast<String*>(this); |
| 51 | } |
| 52 | |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 53 | Object* Object::Clone() { |
| 54 | Class* c = GetClass(); |
| 55 | DCHECK(!c->IsClassClass()); |
| 56 | |
| 57 | // Object::SizeOf gets the right size even if we're an array. |
| 58 | // Using c->AllocObject() here would be wrong. |
| 59 | size_t num_bytes = SizeOf(); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 60 | Heap* heap = Runtime::Current()->GetHeap(); |
| 61 | SirtRef<Object> copy(heap->AllocObject(c, num_bytes)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 62 | if (copy.get() == NULL) { |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | // Copy instance data. We assume memcpy copies by words. |
| 67 | // TODO: expose and use move32. |
| 68 | byte* src_bytes = reinterpret_cast<byte*>(this); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 69 | byte* dst_bytes = reinterpret_cast<byte*>(copy.get()); |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 70 | size_t offset = sizeof(Object); |
| 71 | memcpy(dst_bytes + offset, src_bytes + offset, num_bytes - offset); |
| 72 | |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 73 | if (c->IsFinalizable()) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 74 | heap->AddFinalizerReference(Thread::Current(), copy.get()); |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 75 | } |
Elliott Hughes | 081be7f | 2011-09-18 16:50:26 -0700 | [diff] [blame] | 76 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 77 | return copy.get(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 80 | uint32_t Object::GetThinLockId() { |
| 81 | return Monitor::GetThinLockId(monitor_); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void Object::MonitorEnter(Thread* thread) { |
| 85 | Monitor::MonitorEnter(thread, this); |
| 86 | } |
| 87 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 88 | bool Object::MonitorExit(Thread* thread) { |
| 89 | return Monitor::MonitorExit(thread, this); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void Object::Notify() { |
| 93 | Monitor::Notify(Thread::Current(), this); |
| 94 | } |
| 95 | |
| 96 | void Object::NotifyAll() { |
| 97 | Monitor::NotifyAll(Thread::Current(), this); |
| 98 | } |
| 99 | |
| 100 | void Object::Wait(int64_t ms, int32_t ns) { |
| 101 | Monitor::Wait(Thread::Current(), this, ms, ns, true); |
| 102 | } |
| 103 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 104 | // TODO: get global references for these |
| 105 | Class* Field::java_lang_reflect_Field_ = NULL; |
| 106 | |
| 107 | void Field::SetClass(Class* java_lang_reflect_Field) { |
| 108 | CHECK(java_lang_reflect_Field_ == NULL); |
| 109 | CHECK(java_lang_reflect_Field != NULL); |
| 110 | java_lang_reflect_Field_ = java_lang_reflect_Field; |
| 111 | } |
| 112 | |
| 113 | void Field::ResetClass() { |
| 114 | CHECK(java_lang_reflect_Field_ != NULL); |
| 115 | java_lang_reflect_Field_ = NULL; |
| 116 | } |
| 117 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 118 | void Field::SetOffset(MemberOffset num_bytes) { |
| 119 | DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 120 | #if 0 // TODO enable later in boot and under !NDEBUG |
| 121 | FieldHelper fh(this); |
| 122 | Primitive::Type type = fh.GetTypeAsPrimitiveType(); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 123 | if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) { |
| 124 | DCHECK_ALIGNED(num_bytes.Uint32Value(), 8); |
| 125 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 126 | #endif |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 127 | SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false); |
| 128 | } |
| 129 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 130 | uint32_t Field::Get32(const Object* object) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 131 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 132 | if (IsStatic()) { |
| 133 | object = declaring_class_; |
| 134 | } |
| 135 | return object->GetField32(GetOffset(), IsVolatile()); |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 138 | void Field::Set32(Object* object, uint32_t new_value) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 139 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 140 | if (IsStatic()) { |
| 141 | object = declaring_class_; |
| 142 | } |
| 143 | object->SetField32(GetOffset(), new_value, IsVolatile()); |
| 144 | } |
| 145 | |
| 146 | uint64_t Field::Get64(const Object* object) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 147 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 148 | if (IsStatic()) { |
| 149 | object = declaring_class_; |
| 150 | } |
| 151 | return object->GetField64(GetOffset(), IsVolatile()); |
| 152 | } |
| 153 | |
| 154 | void Field::Set64(Object* object, uint64_t new_value) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 155 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 156 | if (IsStatic()) { |
| 157 | object = declaring_class_; |
| 158 | } |
| 159 | object->SetField64(GetOffset(), new_value, IsVolatile()); |
| 160 | } |
| 161 | |
| 162 | Object* Field::GetObj(const Object* object) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 163 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 164 | if (IsStatic()) { |
| 165 | object = declaring_class_; |
| 166 | } |
| 167 | return object->GetFieldObject<Object*>(GetOffset(), IsVolatile()); |
| 168 | } |
| 169 | |
| 170 | void Field::SetObj(Object* object, const Object* new_value) const { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 171 | CHECK((object == NULL) == IsStatic()) << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 172 | if (IsStatic()) { |
| 173 | object = declaring_class_; |
| 174 | } |
| 175 | object->SetFieldObject(GetOffset(), new_value, IsVolatile()); |
| 176 | } |
| 177 | |
| 178 | bool Field::GetBoolean(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 179 | DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 180 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 181 | return Get32(object); |
| 182 | } |
| 183 | |
| 184 | void Field::SetBoolean(Object* object, bool z) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 185 | DCHECK_EQ(Primitive::kPrimBoolean, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 186 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 187 | Set32(object, z); |
| 188 | } |
| 189 | |
| 190 | int8_t Field::GetByte(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 191 | DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 192 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 193 | return Get32(object); |
| 194 | } |
| 195 | |
| 196 | void Field::SetByte(Object* object, int8_t b) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 197 | DCHECK_EQ(Primitive::kPrimByte, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 198 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 199 | Set32(object, b); |
| 200 | } |
| 201 | |
| 202 | uint16_t Field::GetChar(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 203 | DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 204 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 205 | return Get32(object); |
| 206 | } |
| 207 | |
| 208 | void Field::SetChar(Object* object, uint16_t c) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 209 | DCHECK_EQ(Primitive::kPrimChar, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 210 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 211 | Set32(object, c); |
| 212 | } |
| 213 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 214 | int16_t Field::GetShort(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 215 | DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 216 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 217 | return Get32(object); |
| 218 | } |
| 219 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 220 | void Field::SetShort(Object* object, int16_t s) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 221 | DCHECK_EQ(Primitive::kPrimShort, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 222 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 223 | Set32(object, s); |
| 224 | } |
| 225 | |
| 226 | int32_t Field::GetInt(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 227 | DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 228 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 229 | return Get32(object); |
| 230 | } |
| 231 | |
| 232 | void Field::SetInt(Object* object, int32_t i) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 233 | DCHECK_EQ(Primitive::kPrimInt, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 234 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 235 | Set32(object, i); |
| 236 | } |
| 237 | |
| 238 | int64_t Field::GetLong(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 239 | DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 240 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 241 | return Get64(object); |
| 242 | } |
| 243 | |
| 244 | void Field::SetLong(Object* object, int64_t j) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 245 | DCHECK_EQ(Primitive::kPrimLong, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 246 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 247 | Set64(object, j); |
| 248 | } |
| 249 | |
| 250 | float Field::GetFloat(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 251 | DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 252 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 253 | JValue float_bits; |
| 254 | float_bits.i = Get32(object); |
| 255 | return float_bits.f; |
| 256 | } |
| 257 | |
| 258 | void Field::SetFloat(Object* object, float f) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 259 | DCHECK_EQ(Primitive::kPrimFloat, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 260 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 261 | JValue float_bits; |
| 262 | float_bits.f = f; |
| 263 | Set32(object, float_bits.i); |
| 264 | } |
| 265 | |
| 266 | double Field::GetDouble(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 267 | DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 268 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 269 | JValue double_bits; |
| 270 | double_bits.j = Get64(object); |
| 271 | return double_bits.d; |
| 272 | } |
| 273 | |
| 274 | void Field::SetDouble(Object* object, double d) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 275 | DCHECK_EQ(Primitive::kPrimDouble, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 276 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 277 | JValue double_bits; |
| 278 | double_bits.d = d; |
| 279 | Set64(object, double_bits.j); |
| 280 | } |
| 281 | |
| 282 | Object* Field::GetObject(const Object* object) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 283 | DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 284 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 285 | return GetObj(object); |
| 286 | } |
| 287 | |
| 288 | void Field::SetObject(Object* object, const Object* l) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 289 | DCHECK_EQ(Primitive::kPrimNot, FieldHelper(this).GetTypeAsPrimitiveType()) |
| 290 | << PrettyField(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 291 | SetObj(object, l); |
| 292 | } |
| 293 | |
| 294 | // TODO: get global references for these |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 295 | Class* Method::java_lang_reflect_Constructor_ = NULL; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 296 | Class* Method::java_lang_reflect_Method_ = NULL; |
| 297 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 298 | void Method::SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method) { |
| 299 | CHECK(java_lang_reflect_Constructor_ == NULL); |
| 300 | CHECK(java_lang_reflect_Constructor != NULL); |
| 301 | java_lang_reflect_Constructor_ = java_lang_reflect_Constructor; |
| 302 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 303 | CHECK(java_lang_reflect_Method_ == NULL); |
| 304 | CHECK(java_lang_reflect_Method != NULL); |
| 305 | java_lang_reflect_Method_ = java_lang_reflect_Method; |
| 306 | } |
| 307 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 308 | void Method::ResetClasses() { |
| 309 | CHECK(java_lang_reflect_Constructor_ != NULL); |
| 310 | java_lang_reflect_Constructor_ = NULL; |
| 311 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 312 | CHECK(java_lang_reflect_Method_ != NULL); |
| 313 | java_lang_reflect_Method_ = NULL; |
| 314 | } |
| 315 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 316 | Class* ExtractNextClassFromSignature(ClassLinker* class_linker, const ClassLoader* cl, const char*& p) { |
| 317 | if (*p == '[') { |
| 318 | // Something like "[[[Ljava/lang/String;". |
| 319 | const char* start = p; |
| 320 | while (*p == '[') { |
| 321 | ++p; |
| 322 | } |
| 323 | if (*p == 'L') { |
| 324 | while (*p != ';') { |
| 325 | ++p; |
| 326 | } |
| 327 | } |
| 328 | ++p; // Either the ';' or the primitive type. |
| 329 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 330 | std::string descriptor(start, (p - start)); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 331 | return class_linker->FindClass(descriptor.c_str(), cl); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 332 | } else if (*p == 'L') { |
| 333 | const char* start = p; |
| 334 | while (*p != ';') { |
| 335 | ++p; |
| 336 | } |
| 337 | ++p; |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 338 | std::string descriptor(start, (p - start)); |
| 339 | return class_linker->FindClass(descriptor.c_str(), cl); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 340 | } else { |
| 341 | return class_linker->FindPrimitiveClass(*p++); |
| 342 | } |
| 343 | } |
| 344 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 345 | ObjectArray<String>* Method::GetDexCacheStrings() const { |
| 346 | return GetFieldObject<ObjectArray<String>*>( |
| 347 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), false); |
| 348 | } |
| 349 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 350 | void Method::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) { |
| 351 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_), |
| 352 | new_dex_cache_strings, false); |
| 353 | } |
| 354 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 355 | ObjectArray<Method>* Method::GetDexCacheResolvedMethods() const { |
| 356 | return GetFieldObject<ObjectArray<Method>*>( |
| 357 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), false); |
| 358 | } |
| 359 | |
| 360 | void Method::SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods) { |
| 361 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_), |
| 362 | new_dex_cache_methods, false); |
| 363 | } |
| 364 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 365 | ObjectArray<Class>* Method::GetDexCacheResolvedTypes() const { |
| 366 | return GetFieldObject<ObjectArray<Class>*>( |
| 367 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), false); |
| 368 | } |
| 369 | |
| 370 | void Method::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) { |
| 371 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_), |
| 372 | new_dex_cache_classes, false); |
| 373 | } |
| 374 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 375 | ObjectArray<StaticStorageBase>* Method::GetDexCacheInitializedStaticStorage() const { |
| 376 | return GetFieldObject<ObjectArray<StaticStorageBase>*>( |
| 377 | OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
| 378 | false); |
| 379 | } |
| 380 | |
| 381 | void Method::SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 382 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_initialized_static_storage_), |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 383 | new_value, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 386 | #if defined(ART_USE_LLVM_COMPILER) |
| 387 | |
| 388 | const InferredRegCategoryMap* Method::GetInferredRegCategoryMap() const { |
| 389 | const InferredRegCategoryMap* map = GetFieldPtr<const InferredRegCategoryMap*>( |
| 390 | OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false); |
| 391 | DCHECK(map != NULL) << PrettyMethod(this); |
| 392 | return map; |
| 393 | } |
| 394 | |
| 395 | void Method::SetInferredRegCategoryMap(const InferredRegCategoryMap* map) { |
Logan Chien | a185466 | 2012-02-17 09:52:18 +0800 | [diff] [blame] | 396 | const InferredRegCategoryMap* existing_map = GetFieldPtr<const InferredRegCategoryMap*>( |
| 397 | OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false); |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 398 | |
| 399 | DCHECK(existing_map == NULL) << PrettyMethod(this); |
| 400 | DCHECK(map != NULL) << PrettyMethod(this); |
| 401 | |
| 402 | // TODO: Remove if we won't find any use of InferredRegCategoryMap at runtime. |
| 403 | SetFieldPtr<const InferredRegCategoryMap*>( |
| 404 | OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), map, false); |
| 405 | } |
| 406 | |
| 407 | void Method::ResetInferredRegCategoryMap() { |
| 408 | delete GetInferredRegCategoryMap(); |
| 409 | SetFieldPtr<const InferredRegCategoryMap*>( |
Logan Chien | a185466 | 2012-02-17 09:52:18 +0800 | [diff] [blame] | 410 | OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), NULL, false); |
Logan Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | #endif |
| 414 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 415 | size_t Method::NumArgRegisters(const StringPiece& shorty) { |
| 416 | CHECK_LE(1, shorty.length()); |
| 417 | uint32_t num_registers = 0; |
| 418 | for (int i = 1; i < shorty.length(); ++i) { |
| 419 | char ch = shorty[i]; |
| 420 | if (ch == 'D' || ch == 'J') { |
| 421 | num_registers += 2; |
| 422 | } else { |
| 423 | num_registers += 1; |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 426 | return num_registers; |
| 427 | } |
| 428 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 429 | bool Method::IsProxyMethod() const { |
| 430 | return GetDeclaringClass()->IsProxyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 433 | Method* Method::FindOverriddenMethod() const { |
| 434 | if (IsStatic()) { |
| 435 | return NULL; |
| 436 | } |
| 437 | Class* declaring_class = GetDeclaringClass(); |
| 438 | Class* super_class = declaring_class->GetSuperClass(); |
| 439 | uint16_t method_index = GetMethodIndex(); |
| 440 | ObjectArray<Method>* super_class_vtable = super_class->GetVTable(); |
| 441 | Method* result = NULL; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 442 | // Did this method override a super class method? If so load the result from the super class' |
| 443 | // vtable |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 444 | if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) { |
| 445 | result = super_class_vtable->Get(method_index); |
| 446 | } else { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 447 | // Method didn't override superclass method so search interfaces |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 448 | if (IsProxyMethod()) { |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 449 | result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex()); |
| 450 | CHECK_EQ(result, |
| 451 | Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this)); |
Ian Rogers | 16f9367 | 2012-02-14 12:29:06 -0800 | [diff] [blame] | 452 | } else { |
| 453 | MethodHelper mh(this); |
| 454 | MethodHelper interface_mh; |
| 455 | ObjectArray<InterfaceEntry>* iftable = GetDeclaringClass()->GetIfTable(); |
| 456 | for (int32_t i = 0; i < iftable->GetLength() && result == NULL; i++) { |
| 457 | InterfaceEntry* entry = iftable->Get(i); |
| 458 | Class* interface = entry->GetInterface(); |
| 459 | for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) { |
| 460 | Method* interface_method = interface->GetVirtualMethod(j); |
| 461 | interface_mh.ChangeMethod(interface_method); |
| 462 | if (mh.HasSameNameAndSignature(&interface_mh)) { |
| 463 | result = interface_method; |
| 464 | break; |
| 465 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 470 | #ifndef NDEBUG |
| 471 | MethodHelper result_mh(result); |
| 472 | DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh)); |
| 473 | #endif |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 474 | return result; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 477 | static const void* GetOatCode(const Method* m) { |
| 478 | Runtime* runtime = Runtime::Current(); |
| 479 | const void* code = m->GetCode(); |
| 480 | // Peel off any method tracing trampoline. |
| 481 | if (runtime->IsMethodTracingActive() && runtime->GetTracer()->GetSavedCodeFromMap(m) != NULL) { |
| 482 | code = runtime->GetTracer()->GetSavedCodeFromMap(m); |
| 483 | } |
| 484 | // Peel off any resolution stub. |
Ian Rogers | fb6adba | 2012-03-04 21:51:51 -0800 | [diff] [blame] | 485 | if (code == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) { |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 486 | code = runtime->GetClassLinker()->GetOatCodeFor(m); |
| 487 | } |
| 488 | return code; |
| 489 | } |
| 490 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 491 | uint32_t Method::ToDexPC(const uintptr_t pc) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 492 | const uint32_t* mapping_table = GetMappingTable(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 493 | if (mapping_table == NULL) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 494 | DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this); |
Ian Rogers | 67375ac | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 495 | return DexFile::kDexNoIndex; // Special no mapping case |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 496 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 497 | size_t mapping_table_length = GetMappingTableLength(); |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 498 | uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(GetOatCode(this)); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 499 | uint32_t best_offset = 0; |
| 500 | uint32_t best_dex_offset = 0; |
| 501 | for (size_t i = 0; i < mapping_table_length; i += 2) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 502 | uint32_t map_offset = mapping_table[i]; |
| 503 | uint32_t map_dex_offset = mapping_table[i + 1]; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 504 | if (map_offset == sought_offset) { |
| 505 | best_offset = map_offset; |
| 506 | best_dex_offset = map_dex_offset; |
| 507 | break; |
| 508 | } |
| 509 | if (map_offset < sought_offset && map_offset > best_offset) { |
| 510 | best_offset = map_offset; |
| 511 | best_dex_offset = map_dex_offset; |
| 512 | } |
| 513 | } |
| 514 | return best_dex_offset; |
| 515 | } |
| 516 | |
| 517 | uintptr_t Method::ToNativePC(const uint32_t dex_pc) const { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 518 | const uint32_t* mapping_table = GetMappingTable(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 519 | if (mapping_table == NULL) { |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 520 | DCHECK_EQ(dex_pc, 0U); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 521 | return 0; // Special no mapping/pc == 0 case |
| 522 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 523 | size_t mapping_table_length = GetMappingTableLength(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 524 | for (size_t i = 0; i < mapping_table_length; i += 2) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 525 | uint32_t map_offset = mapping_table[i]; |
| 526 | uint32_t map_dex_offset = mapping_table[i + 1]; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 527 | if (map_dex_offset == dex_pc) { |
Elliott Hughes | 168670b | 2012-02-29 16:43:26 -0800 | [diff] [blame] | 528 | return reinterpret_cast<uintptr_t>(GetOatCode(this)) + map_offset; |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | LOG(FATAL) << "Looking up Dex PC not contained in method"; |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | uint32_t Method::FindCatchBlock(Class* exception_type, uint32_t dex_pc) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 536 | MethodHelper mh(this); |
| 537 | const DexFile::CodeItem* code_item = mh.GetCodeItem(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 538 | // Iterate over the catch handlers associated with dex_pc |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 539 | for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) { |
| 540 | uint16_t iter_type_idx = it.GetHandlerTypeIndex(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 541 | // Catch all case |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 542 | if (iter_type_idx == DexFile::kDexNoIndex16) { |
| 543 | return it.GetHandlerAddress(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 544 | } |
| 545 | // Does this catch exception type apply? |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 546 | Class* iter_exception_type = mh.GetDexCacheResolvedType(iter_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 547 | if (iter_exception_type == NULL) { |
| 548 | // The verifier should take care of resolving all exception classes early |
| 549 | LOG(WARNING) << "Unresolved exception class when finding catch block: " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 550 | << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 551 | } else if (iter_exception_type->IsAssignableFrom(exception_type)) { |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 552 | return it.GetHandlerAddress(); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | // Handler not found |
| 556 | return DexFile::kDexNoIndex; |
| 557 | } |
| 558 | |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 559 | void Method::Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const { |
| 560 | // Push a transition back into managed code onto the linked list in thread. |
| 561 | CHECK_EQ(Thread::kRunnable, self->GetState()); |
| 562 | NativeToManagedRecord record; |
| 563 | self->PushNativeToManagedRecord(&record); |
| 564 | |
| 565 | // Call the invoke stub associated with the method. |
| 566 | // Pass everything as arguments. |
| 567 | const Method::InvokeStub* stub = GetInvokeStub(); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 568 | |
| 569 | bool have_executable_code = (GetCode() != NULL); |
| 570 | #if !defined(__arm__) |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 571 | // Currently we can only compile non-native methods for ARM. |
| 572 | have_executable_code = IsNative(); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 573 | #endif |
| 574 | |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 575 | if (Runtime::Current()->IsStarted() && have_executable_code && stub != NULL) { |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 576 | bool log = false; |
| 577 | if (log) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 578 | LOG(INFO) << StringPrintf("invoking %s code=%p stub=%p", |
| 579 | PrettyMethod(this).c_str(), GetCode(), stub); |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 580 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 581 | (*stub)(this, receiver, self, args, result); |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 582 | if (log) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 583 | LOG(INFO) << StringPrintf("returned %s code=%p stub=%p", |
| 584 | PrettyMethod(this).c_str(), GetCode(), stub); |
Elliott Hughes | 9f86537 | 2011-10-11 15:04:19 -0700 | [diff] [blame] | 585 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 586 | } else { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 587 | LOG(INFO) << StringPrintf("not invoking %s code=%p stub=%p started=%s", |
| 588 | PrettyMethod(this).c_str(), GetCode(), stub, |
| 589 | Runtime::Current()->IsStarted() ? "true" : "false"); |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 590 | if (result != NULL) { |
| 591 | result->j = 0; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // Pop transition. |
| 596 | self->PopNativeToManagedRecord(record); |
| 597 | } |
| 598 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 599 | bool Method::IsRegistered() const { |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 600 | void* native_method = GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), false); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 601 | CHECK(native_method != NULL); |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 602 | void* jni_stub = Runtime::Current()->GetJniDlsymLookupStub()->GetData(); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 603 | return native_method != jni_stub; |
| 604 | } |
| 605 | |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 606 | void Method::RegisterNative(Thread* self, const void* native_method) { |
| 607 | DCHECK(Thread::Current() == self); |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 608 | CHECK(IsNative()) << PrettyMethod(this); |
| 609 | CHECK(native_method != NULL) << PrettyMethod(this); |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 610 | if (!self->GetJniEnv()->vm->work_around_app_jni_bugs) { |
| 611 | SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), |
| 612 | native_method, false); |
| 613 | } else { |
| 614 | // We've been asked to associate this method with the given native method but are working |
| 615 | // around JNI bugs, that include not giving Object** SIRT references to native methods. Direct |
| 616 | // the native method to runtime support and store the target somewhere runtime support will |
| 617 | // find it. |
| 618 | #if defined(__arm__) |
| 619 | SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, native_method_), |
| 620 | reinterpret_cast<const void*>(art_work_around_app_jni_bugs), false); |
| 621 | #else |
| 622 | UNIMPLEMENTED(FATAL); |
| 623 | #endif |
| 624 | SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), |
| 625 | reinterpret_cast<const uint8_t*>(native_method), false); |
| 626 | } |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 629 | void Method::UnregisterNative(Thread* self) { |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 630 | CHECK(IsNative()) << PrettyMethod(this); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 631 | // restore stub to lookup native pointer via dlsym |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 632 | RegisterNative(self, Runtime::Current()->GetJniDlsymLookupStub()->GetData()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 635 | void Class::SetStatus(Status new_status) { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 636 | CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted()) |
| 637 | << PrettyClass(this) << " " << GetStatus() << " -> " << new_status; |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 638 | CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this); |
Brian Carlstrom | 4d9716c | 2012-01-30 01:49:33 -0800 | [diff] [blame] | 639 | if (new_status == kStatusError) { |
| 640 | CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this); |
| 641 | |
| 642 | // stash current exception |
| 643 | Thread* self = Thread::Current(); |
| 644 | SirtRef<Throwable> exception(self->GetException()); |
| 645 | CHECK(exception.get() != NULL); |
| 646 | |
| 647 | // clear exception to call FindSystemClass |
| 648 | self->ClearException(); |
| 649 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 650 | Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;"); |
| 651 | CHECK(!self->IsExceptionPending()); |
| 652 | |
| 653 | // only verification errors, not initialization problems, should set a verify error. |
| 654 | // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case. |
| 655 | Class* exception_class = exception->GetClass(); |
| 656 | if (!eiie_class->IsAssignableFrom(exception_class)) { |
| 657 | SetVerifyErrorClass(exception_class); |
| 658 | } |
| 659 | |
| 660 | // restore exception |
| 661 | self->SetException(exception.get()); |
| 662 | } |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 663 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | DexCache* Class::GetDexCache() const { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 667 | return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void Class::SetDexCache(DexCache* new_dex_cache) { |
Elliott Hughes | d9cdfe9 | 2011-10-06 16:09:04 -0700 | [diff] [blame] | 671 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 674 | Object* Class::AllocObject() { |
Brian Carlstrom | 96a253a | 2011-10-27 18:38:10 -0700 | [diff] [blame] | 675 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Ian Rogers | 28ad40d | 2011-10-27 15:19:26 -0700 | [diff] [blame] | 676 | DCHECK(IsInstantiable()) << PrettyClass(this); |
Jesse Wilson | 9a6bae8 | 2011-11-14 14:57:30 -0500 | [diff] [blame] | 677 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 678 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
Brian Carlstrom | 96a253a | 2011-10-27 18:38:10 -0700 | [diff] [blame] | 679 | DCHECK_GE(this->object_size_, sizeof(Object)); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 680 | return Runtime::Current()->GetHeap()->AllocObject(this, this->object_size_); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 683 | void Class::SetClassSize(size_t new_class_size) { |
| 684 | DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); |
| 685 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false); |
| 686 | } |
| 687 | |
Ian Rogers | d418eda | 2012-01-30 12:14:28 -0800 | [diff] [blame] | 688 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 689 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 690 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 691 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
| 692 | String* Class::ComputeName() { |
| 693 | String* name = GetName(); |
| 694 | if (name != NULL) { |
| 695 | return name; |
| 696 | } |
| 697 | std::string descriptor(ClassHelper(this).GetDescriptor()); |
| 698 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 699 | // The descriptor indicates that this is the class for |
| 700 | // a primitive type; special-case the return value. |
| 701 | const char* c_name = NULL; |
| 702 | switch (descriptor[0]) { |
| 703 | case 'Z': c_name = "boolean"; break; |
| 704 | case 'B': c_name = "byte"; break; |
| 705 | case 'C': c_name = "char"; break; |
| 706 | case 'S': c_name = "short"; break; |
| 707 | case 'I': c_name = "int"; break; |
| 708 | case 'J': c_name = "long"; break; |
| 709 | case 'F': c_name = "float"; break; |
| 710 | case 'D': c_name = "double"; break; |
| 711 | case 'V': c_name = "void"; break; |
| 712 | default: |
| 713 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 714 | } |
| 715 | name = String::AllocFromModifiedUtf8(c_name); |
| 716 | } else { |
| 717 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 718 | // components. |
| 719 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 720 | descriptor.erase(0, 1); |
| 721 | descriptor.erase(descriptor.size() - 1); |
| 722 | } |
| 723 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 724 | name = String::AllocFromModifiedUtf8(descriptor.c_str()); |
| 725 | } |
| 726 | SetName(name); |
| 727 | return name; |
| 728 | } |
| 729 | |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 730 | void Class::DumpClass(std::ostream& os, int flags) const { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 731 | if ((flags & kDumpClassFullDetail) == 0) { |
| 732 | os << PrettyClass(this); |
| 733 | if ((flags & kDumpClassClassLoader) != 0) { |
| 734 | os << ' ' << GetClassLoader(); |
| 735 | } |
| 736 | if ((flags & kDumpClassInitialized) != 0) { |
| 737 | os << ' ' << GetStatus(); |
| 738 | } |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 739 | os << "\n"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 740 | return; |
| 741 | } |
| 742 | |
| 743 | Class* super = GetSuperClass(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 744 | ClassHelper kh(this); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 745 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 746 | << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 747 | os << " objectSize=" << SizeOf() << " " |
| 748 | << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n", |
| 749 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 750 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
| 751 | if (super != NULL) { |
| 752 | os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n"; |
| 753 | } |
| 754 | if (IsArrayClass()) { |
| 755 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 756 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 757 | if (kh.NumInterfaces() > 0) { |
| 758 | os << " interfaces (" << kh.NumInterfaces() << "):\n"; |
| 759 | for (size_t i = 0; i < kh.NumInterfaces(); ++i) { |
| 760 | Class* interface = kh.GetInterface(i); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 761 | const ClassLoader* cl = interface->GetClassLoader(); |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 762 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | os << " vtable (" << NumVirtualMethods() << " entries, " |
| 766 | << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n"; |
| 767 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 768 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str()); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 769 | } |
| 770 | os << " direct methods (" << NumDirectMethods() << " entries):\n"; |
| 771 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 772 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str()); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 773 | } |
| 774 | if (NumStaticFields() > 0) { |
| 775 | os << " static fields (" << NumStaticFields() << " entries):\n"; |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 776 | if (IsResolved() || IsErroneous()) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 777 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 778 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 779 | } |
| 780 | } else { |
| 781 | os << " <not yet available>"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | if (NumInstanceFields() > 0) { |
| 785 | os << " instance fields (" << NumInstanceFields() << " entries):\n"; |
Elliott Hughes | 03f0349 | 2011-09-26 13:38:08 -0700 | [diff] [blame] | 786 | if (IsResolved() || IsErroneous()) { |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 787 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 788 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str()); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 789 | } |
| 790 | } else { |
| 791 | os << " <not yet available>"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 796 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 797 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 798 | // Sanity check that the number of bits set in the reference offset bitmap |
| 799 | // agrees with the number of references |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 800 | size_t count = 0; |
| 801 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 802 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 803 | } |
Elliott Hughes | cccd84f | 2011-12-05 16:51:54 -0800 | [diff] [blame] | 804 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 805 | } |
| 806 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 807 | new_reference_offsets, false); |
| 808 | } |
| 809 | |
| 810 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 811 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 812 | // Sanity check that the number of bits set in the reference offset bitmap |
| 813 | // agrees with the number of references |
| 814 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 815 | NumReferenceStaticFieldsDuringLinking()); |
| 816 | } |
| 817 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 818 | new_reference_offsets, false); |
| 819 | } |
| 820 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 821 | bool Class::Implements(const Class* klass) const { |
| 822 | DCHECK(klass != NULL); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 823 | DCHECK(klass->IsInterface()) << PrettyClass(this); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 824 | // All interfaces implemented directly and by our superclass, and |
| 825 | // recursively all super-interfaces of those interfaces, are listed |
| 826 | // in iftable_, so we can just do a linear scan through that. |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 827 | int32_t iftable_count = GetIfTableCount(); |
| 828 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 829 | for (int32_t i = 0; i < iftable_count; i++) { |
| 830 | if (iftable->Get(i)->GetInterface() == klass) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 831 | return true; |
| 832 | } |
| 833 | } |
| 834 | return false; |
| 835 | } |
| 836 | |
| 837 | // Determine whether "this" is assignable from "klazz", where both of these |
| 838 | // are array classes. |
| 839 | // |
| 840 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 841 | // Y[][] = Y[][] --> true (identity) |
| 842 | // X[][] = Y[][] --> true (element superclass) |
| 843 | // Y = Y[][] --> false |
| 844 | // Y[] = Y[][] --> false |
| 845 | // Object = Y[][] --> true (everything is an object) |
| 846 | // Object[] = Y[][] --> true |
| 847 | // Object[][] = Y[][] --> true |
| 848 | // Object[][][] = Y[][] --> false (too many []s) |
| 849 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 850 | // Serializable[] = Y[][] --> true |
| 851 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 852 | // |
| 853 | // Don't forget about primitive types. |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 854 | // Object[] = int[] --> false |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 855 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 856 | bool Class::IsArrayAssignableFromArray(const Class* src) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 857 | DCHECK(IsArrayClass()) << PrettyClass(this); |
| 858 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 859 | return GetComponentType()->IsAssignableFrom(src->GetComponentType()); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 862 | bool Class::IsAssignableFromArray(const Class* src) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 863 | DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom |
| 864 | DCHECK(src->IsArrayClass()) << PrettyClass(src); |
Brian Carlstrom | b63ec39 | 2011-08-27 17:38:27 -0700 | [diff] [blame] | 865 | if (!IsArrayClass()) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 866 | // If "this" is not also an array, it must be Object. |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 867 | // src's super should be java_lang_Object, since it is an array. |
| 868 | Class* java_lang_Object = src->GetSuperClass(); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 869 | DCHECK(java_lang_Object != NULL) << PrettyClass(src); |
| 870 | DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 871 | return this == java_lang_Object; |
| 872 | } |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 873 | return IsArrayAssignableFromArray(src); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | bool Class::IsSubClass(const Class* klass) const { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 877 | DCHECK(!IsInterface()) << PrettyClass(this); |
| 878 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 879 | const Class* current = this; |
| 880 | do { |
| 881 | if (current == klass) { |
| 882 | return true; |
| 883 | } |
| 884 | current = current->GetSuperClass(); |
| 885 | } while (current != NULL); |
| 886 | return false; |
| 887 | } |
| 888 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 889 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 890 | size_t i = 0; |
| 891 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 892 | ++i; |
| 893 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 894 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 895 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 896 | return false; |
| 897 | } else { |
| 898 | return true; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | bool Class::IsInSamePackage(const Class* that) const { |
| 903 | const Class* klass1 = this; |
| 904 | const Class* klass2 = that; |
| 905 | if (klass1 == klass2) { |
| 906 | return true; |
| 907 | } |
| 908 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 909 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 910 | return false; |
| 911 | } |
| 912 | // Arrays are in the same package when their element classes are. |
jeffhao | 4a801a4 | 2011-09-23 13:53:40 -0700 | [diff] [blame] | 913 | while (klass1->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 914 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 915 | } |
jeffhao | 4a801a4 | 2011-09-23 13:53:40 -0700 | [diff] [blame] | 916 | while (klass2->IsArrayClass()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 917 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 918 | } |
| 919 | // Compare the package part of the descriptor string. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 920 | ClassHelper kh(klass1); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 921 | std::string descriptor1(kh.GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 922 | kh.ChangeClass(klass2); |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 923 | std::string descriptor2(kh.GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 924 | return IsInSamePackage(descriptor1, descriptor2); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 927 | bool Class::IsClassClass() const { |
| 928 | Class* java_lang_Class = GetClass()->GetClass(); |
| 929 | return this == java_lang_Class; |
| 930 | } |
| 931 | |
| 932 | bool Class::IsStringClass() const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 933 | return this == String::GetJavaLangString(); |
Elliott Hughes | dbb4079 | 2011-11-18 17:05:22 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Ian Rogers | 6f1dfe4 | 2011-12-08 17:28:34 -0800 | [diff] [blame] | 936 | bool Class::IsThrowableClass() const { |
| 937 | Class* throwable = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Throwable;"); |
| 938 | return throwable->IsAssignableFrom(this); |
| 939 | } |
| 940 | |
Elliott Hughes | 1bba14f | 2011-12-01 18:00:36 -0800 | [diff] [blame] | 941 | ClassLoader* Class::GetClassLoader() const { |
| 942 | return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
Brian Carlstrom | b9edb84 | 2011-08-28 16:31:06 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 945 | void Class::SetClassLoader(const ClassLoader* new_cl) { |
| 946 | ClassLoader* new_class_loader = const_cast<ClassLoader*>(new_cl); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 947 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 950 | Method* Class::FindVirtualMethodForInterface(Method* method) { |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 951 | Class* declaring_class = method->GetDeclaringClass(); |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 952 | DCHECK(declaring_class != NULL) << PrettyClass(this); |
| 953 | DCHECK(declaring_class->IsInterface()) << PrettyMethod(method); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 954 | // TODO cache to improve lookup speed |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 955 | int32_t iftable_count = GetIfTableCount(); |
| 956 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 957 | for (int32_t i = 0; i < iftable_count; i++) { |
| 958 | InterfaceEntry* interface_entry = iftable->Get(i); |
| 959 | if (interface_entry->GetInterface() == declaring_class) { |
| 960 | return interface_entry->GetMethodArray()->Get(method->GetMethodIndex()); |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 961 | } |
| 962 | } |
Brian Carlstrom | 30b9445 | 2011-08-25 21:35:26 -0700 | [diff] [blame] | 963 | return NULL; |
| 964 | } |
| 965 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 966 | Method* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const { |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 967 | // Check the current class before checking the interfaces. |
Ian Rogers | 94c0e33 | 2012-01-18 22:11:47 -0800 | [diff] [blame] | 968 | Method* method = FindDeclaredVirtualMethod(name, signature); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 969 | if (method != NULL) { |
| 970 | return method; |
| 971 | } |
| 972 | |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 973 | int32_t iftable_count = GetIfTableCount(); |
| 974 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 975 | for (int32_t i = 0; i < iftable_count; i++) { |
| 976 | method = iftable->Get(i)->GetInterface()->FindVirtualMethod(name, signature); |
jeffhao | bdb7651 | 2011-09-07 11:43:16 -0700 | [diff] [blame] | 977 | if (method != NULL) { |
| 978 | return method; |
| 979 | } |
| 980 | } |
| 981 | return NULL; |
| 982 | } |
| 983 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 984 | Method* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 985 | // Check the current class before checking the interfaces. |
| 986 | Method* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 987 | if (method != NULL) { |
| 988 | return method; |
| 989 | } |
| 990 | |
| 991 | int32_t iftable_count = GetIfTableCount(); |
| 992 | ObjectArray<InterfaceEntry>* iftable = GetIfTable(); |
| 993 | for (int32_t i = 0; i < iftable_count; i++) { |
| 994 | method = iftable->Get(i)->GetInterface()->FindVirtualMethod(dex_cache, dex_method_idx); |
| 995 | if (method != NULL) { |
| 996 | return method; |
| 997 | } |
| 998 | } |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | Method* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1004 | MethodHelper mh; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1005 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1006 | Method* method = GetDirectMethod(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1007 | mh.ChangeMethod(method); |
| 1008 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1009 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1010 | } |
| 1011 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1012 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1015 | Method* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 1016 | if (GetDexCache() == dex_cache) { |
| 1017 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 1018 | Method* method = GetDirectMethod(i); |
| 1019 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 1020 | return method; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | Method* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
| 1028 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1029 | Method* method = klass->FindDeclaredDirectMethod(name, signature); |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1030 | if (method != NULL) { |
| 1031 | return method; |
| 1032 | } |
| 1033 | } |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1037 | Method* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 1038 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 1039 | Method* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); |
| 1040 | if (method != NULL) { |
| 1041 | return method; |
| 1042 | } |
| 1043 | } |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1047 | Method* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1048 | const StringPiece& signature) const { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1049 | MethodHelper mh; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 1050 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1051 | Method* method = GetVirtualMethod(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1052 | mh.ChangeMethod(method); |
| 1053 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1054 | return method; |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1060 | Method* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 1061 | if (GetDexCache() == dex_cache) { |
| 1062 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 1063 | Method* method = GetVirtualMethod(i); |
| 1064 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 1065 | return method; |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1072 | Method* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const { |
| 1073 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 1074 | Method* method = klass->FindDeclaredVirtualMethod(name, signature); |
| 1075 | if (method != NULL) { |
| 1076 | return method; |
| 1077 | } |
| 1078 | } |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1082 | Method* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 1083 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 1084 | Method* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 1085 | if (method != NULL) { |
| 1086 | return method; |
| 1087 | } |
| 1088 | } |
| 1089 | return NULL; |
| 1090 | } |
| 1091 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1092 | Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1093 | // Is the field in this class? |
| 1094 | // Interfaces are not relevant because they can't contain instance fields. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1095 | FieldHelper fh; |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1096 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 1097 | Field* f = GetInstanceField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1098 | fh.ChangeField(f); |
| 1099 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1100 | return f; |
| 1101 | } |
| 1102 | } |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1106 | Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1107 | if (GetDexCache() == dex_cache) { |
| 1108 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 1109 | Field* f = GetInstanceField(i); |
| 1110 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 1111 | return f; |
| 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1118 | Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1119 | // Is the field in this class, or any of its superclasses? |
| 1120 | // Interfaces are not relevant because they can't contain instance fields. |
| 1121 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1122 | Field* f = c->FindDeclaredInstanceField(name, type); |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1123 | if (f != NULL) { |
| 1124 | return f; |
| 1125 | } |
| 1126 | } |
| 1127 | return NULL; |
| 1128 | } |
| 1129 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1130 | Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1131 | // Is the field in this class, or any of its superclasses? |
| 1132 | // Interfaces are not relevant because they can't contain instance fields. |
| 1133 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 1134 | Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
| 1135 | if (f != NULL) { |
| 1136 | return f; |
| 1137 | } |
| 1138 | } |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1142 | Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
| 1143 | DCHECK(type != NULL); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1144 | FieldHelper fh; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1145 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 1146 | Field* f = GetStaticField(i); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1147 | fh.ChangeField(f); |
| 1148 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1149 | return f; |
| 1150 | } |
| 1151 | } |
| 1152 | return NULL; |
| 1153 | } |
| 1154 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1155 | Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1156 | if (dex_cache == GetDexCache()) { |
| 1157 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 1158 | Field* f = GetStaticField(i); |
| 1159 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 1160 | return f; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1167 | Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) { |
| 1168 | // Is the field in this class (or its interfaces), or any of its |
| 1169 | // superclasses (or their interfaces)? |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1170 | ClassHelper kh; |
| 1171 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1172 | // Is the field in this class? |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1173 | Field* f = k->FindDeclaredStaticField(name, type); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1174 | if (f != NULL) { |
| 1175 | return f; |
| 1176 | } |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1177 | // Is this field in any of this class' interfaces? |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1178 | kh.ChangeClass(k); |
| 1179 | for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) { |
| 1180 | Class* interface = kh.GetInterface(i); |
| 1181 | f = interface->FindDeclaredStaticField(name, type); |
| 1182 | if (f != NULL) { |
| 1183 | return f; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 1190 | Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 1191 | ClassHelper kh; |
| 1192 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 1193 | // Is the field in this class? |
| 1194 | Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
| 1195 | if (f != NULL) { |
| 1196 | return f; |
| 1197 | } |
| 1198 | // Is this field in any of this class' interfaces? |
| 1199 | kh.ChangeClass(k); |
| 1200 | for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) { |
| 1201 | Class* interface = kh.GetInterface(i); |
| 1202 | f = interface->FindDeclaredStaticField(dex_cache, dex_field_idx); |
| 1203 | if (f != NULL) { |
| 1204 | return f; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | return NULL; |
| 1209 | } |
| 1210 | |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 1211 | Field* Class::FindField(const StringPiece& name, const StringPiece& type) { |
| 1212 | // Find a field using the JLS field resolution order |
| 1213 | ClassHelper kh; |
| 1214 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 1215 | // Is the field in this class? |
| 1216 | Field* f = k->FindDeclaredInstanceField(name, type); |
| 1217 | if (f != NULL) { |
| 1218 | return f; |
| 1219 | } |
| 1220 | f = k->FindDeclaredStaticField(name, type); |
| 1221 | if (f != NULL) { |
| 1222 | return f; |
| 1223 | } |
| 1224 | // Is this field in any of this class' interfaces? |
| 1225 | kh.ChangeClass(k); |
| 1226 | for (uint32_t i = 0; i < kh.NumInterfaces(); ++i) { |
| 1227 | Class* interface = kh.GetInterface(i); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 1228 | f = interface->FindDeclaredStaticField(name, type); |
| 1229 | if (f != NULL) { |
| 1230 | return f; |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | return NULL; |
| 1235 | } |
| 1236 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1237 | Array* Array::Alloc(Class* array_class, int32_t component_count, size_t component_size) { |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 1238 | DCHECK(array_class != NULL); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1239 | DCHECK_GE(component_count, 0); |
| 1240 | DCHECK(array_class->IsArrayClass()); |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 1241 | |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 1242 | size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4); |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 1243 | size_t data_size = component_count * component_size; |
| 1244 | size_t size = header_size + data_size; |
| 1245 | |
| 1246 | // Check for overflow and throw OutOfMemoryError if this was an unreasonable request. |
| 1247 | size_t component_shift = sizeof(size_t) * 8 - 1 - CLZ(component_size); |
| 1248 | if (data_size >> component_shift != size_t(component_count) || size < data_size) { |
| 1249 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/OutOfMemoryError;", |
Elliott Hughes | e689d51 | 2012-01-18 23:39:47 -0800 | [diff] [blame] | 1250 | "%s of length %d exceeds the VM limit", |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1251 | PrettyDescriptor(array_class).c_str(), component_count); |
Elliott Hughes | b408de7 | 2011-10-04 14:35:05 -0700 | [diff] [blame] | 1252 | return NULL; |
| 1253 | } |
| 1254 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 1255 | Heap* heap = Runtime::Current()->GetHeap(); |
| 1256 | Array* array = down_cast<Array*>(heap->AllocObject(array_class, size)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1257 | if (array != NULL) { |
| 1258 | DCHECK(array->IsArrayInstance()); |
| 1259 | array->SetLength(component_count); |
| 1260 | } |
| 1261 | return array; |
| 1262 | } |
| 1263 | |
| 1264 | Array* Array::Alloc(Class* array_class, int32_t component_count) { |
| 1265 | return Alloc(array_class, component_count, array_class->GetComponentSize()); |
| 1266 | } |
| 1267 | |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1268 | bool Array::ThrowArrayIndexOutOfBoundsException(int32_t index) const { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1269 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1270 | "length=%i; index=%i", length_, index); |
| 1271 | return false; |
| 1272 | } |
| 1273 | |
| 1274 | bool Array::ThrowArrayStoreException(Object* object) const { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1275 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | 8060925 | 2011-09-23 17:24:51 -0700 | [diff] [blame] | 1276 | "Can't store an element of type %s into an array of type %s", |
| 1277 | PrettyTypeOf(object).c_str(), PrettyTypeOf(this).c_str()); |
| 1278 | return false; |
| 1279 | } |
| 1280 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1281 | template<typename T> |
| 1282 | PrimitiveArray<T>* PrimitiveArray<T>::Alloc(size_t length) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 1283 | DCHECK(array_class_ != NULL); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1284 | Array* raw_array = Array::Alloc(array_class_, length, sizeof(T)); |
| 1285 | return down_cast<PrimitiveArray<T>*>(raw_array); |
| 1286 | } |
| 1287 | |
| 1288 | template <typename T> Class* PrimitiveArray<T>::array_class_ = NULL; |
| 1289 | |
| 1290 | // Explicitly instantiate all the primitive array types. |
| 1291 | template class PrimitiveArray<uint8_t>; // BooleanArray |
| 1292 | template class PrimitiveArray<int8_t>; // ByteArray |
| 1293 | template class PrimitiveArray<uint16_t>; // CharArray |
| 1294 | template class PrimitiveArray<double>; // DoubleArray |
| 1295 | template class PrimitiveArray<float>; // FloatArray |
| 1296 | template class PrimitiveArray<int32_t>; // IntArray |
| 1297 | template class PrimitiveArray<int64_t>; // LongArray |
| 1298 | template class PrimitiveArray<int16_t>; // ShortArray |
| 1299 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1300 | // Explicitly instantiate Class[][] |
| 1301 | template class ObjectArray<ObjectArray<Class> >; |
| 1302 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1303 | // TODO: get global references for these |
| 1304 | Class* String::java_lang_String_ = NULL; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1305 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1306 | void String::SetClass(Class* java_lang_String) { |
| 1307 | CHECK(java_lang_String_ == NULL); |
| 1308 | CHECK(java_lang_String != NULL); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1309 | java_lang_String_ = java_lang_String; |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1310 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1311 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1312 | void String::ResetClass() { |
| 1313 | CHECK(java_lang_String_ != NULL); |
| 1314 | java_lang_String_ = NULL; |
| 1315 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1316 | |
Brian Carlstrom | c74255f | 2011-09-11 22:47:39 -0700 | [diff] [blame] | 1317 | String* String::Intern() { |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 1318 | return Runtime::Current()->GetInternTable()->InternWeak(this); |
| 1319 | } |
| 1320 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 1321 | int32_t String::GetHashCode() { |
| 1322 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 1323 | if (result == 0) { |
| 1324 | ComputeHashCode(); |
| 1325 | } |
| 1326 | result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false); |
| 1327 | DCHECK(result != 0 || ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()) == 0) |
| 1328 | << ToModifiedUtf8() << " " << result; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1329 | return result; |
| 1330 | } |
| 1331 | |
| 1332 | int32_t String::GetLength() const { |
| 1333 | int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), false); |
| 1334 | DCHECK(result >= 0 && result <= GetCharArray()->GetLength()); |
| 1335 | return result; |
| 1336 | } |
| 1337 | |
| 1338 | uint16_t String::CharAt(int32_t index) const { |
| 1339 | // TODO: do we need this? Equals is the only caller, and could |
| 1340 | // bounds check itself. |
| 1341 | if (index < 0 || index >= count_) { |
| 1342 | Thread* self = Thread::Current(); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 1343 | self->ThrowNewExceptionF("Ljava/lang/StringIndexOutOfBoundsException;", |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1344 | "length=%i; index=%i", count_, index); |
| 1345 | return 0; |
| 1346 | } |
| 1347 | return GetCharArray()->Get(index + GetOffset()); |
| 1348 | } |
| 1349 | |
| 1350 | String* String::AllocFromUtf16(int32_t utf16_length, |
| 1351 | const uint16_t* utf16_data_in, |
| 1352 | int32_t hash_code) { |
Jesse Wilson | 25e79a5 | 2011-11-18 15:31:58 -0500 | [diff] [blame] | 1353 | CHECK(utf16_data_in != NULL || utf16_length == 0); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1354 | String* string = Alloc(GetJavaLangString(), utf16_length); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1355 | if (string == NULL) { |
| 1356 | return NULL; |
| 1357 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1358 | // TODO: use 16-bit wide memset variant |
| 1359 | CharArray* array = const_cast<CharArray*>(string->GetCharArray()); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1360 | if (array == NULL) { |
| 1361 | return NULL; |
| 1362 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1363 | for (int i = 0; i < utf16_length; i++) { |
| 1364 | array->Set(i, utf16_data_in[i]); |
| 1365 | } |
| 1366 | if (hash_code != 0) { |
| 1367 | string->SetHashCode(hash_code); |
| 1368 | } else { |
| 1369 | string->ComputeHashCode(); |
| 1370 | } |
| 1371 | return string; |
| 1372 | } |
| 1373 | |
| 1374 | String* String::AllocFromModifiedUtf8(const char* utf) { |
Ian Rogers | 4860131 | 2011-12-07 16:45:19 -0800 | [diff] [blame] | 1375 | if (utf == NULL) { |
| 1376 | return NULL; |
| 1377 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1378 | size_t char_count = CountModifiedUtf8Chars(utf); |
| 1379 | return AllocFromModifiedUtf8(char_count, utf); |
| 1380 | } |
| 1381 | |
| 1382 | String* String::AllocFromModifiedUtf8(int32_t utf16_length, |
| 1383 | const char* utf8_data_in) { |
| 1384 | String* string = Alloc(GetJavaLangString(), utf16_length); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1385 | if (string == NULL) { |
| 1386 | return NULL; |
| 1387 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1388 | uint16_t* utf16_data_out = |
| 1389 | const_cast<uint16_t*>(string->GetCharArray()->GetData()); |
| 1390 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 1391 | string->ComputeHashCode(); |
| 1392 | return string; |
| 1393 | } |
| 1394 | |
| 1395 | String* String::Alloc(Class* java_lang_String, int32_t utf16_length) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1396 | SirtRef<CharArray> array(CharArray::Alloc(utf16_length)); |
| 1397 | if (array.get() == NULL) { |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1398 | return NULL; |
| 1399 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1400 | return Alloc(java_lang_String, array.get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | String* String::Alloc(Class* java_lang_String, CharArray* array) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 1404 | SirtRef<CharArray> array_ref(array); // hold reference in case AllocObject causes GC |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1405 | String* string = down_cast<String*>(java_lang_String->AllocObject()); |
Elliott Hughes | b51036c | 2011-10-12 23:49:11 -0700 | [diff] [blame] | 1406 | if (string == NULL) { |
| 1407 | return NULL; |
| 1408 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1409 | string->SetArray(array); |
| 1410 | string->SetCount(array->GetLength()); |
| 1411 | return string; |
| 1412 | } |
| 1413 | |
| 1414 | bool String::Equals(const String* that) const { |
| 1415 | if (this == that) { |
| 1416 | // Quick reference equality test |
| 1417 | return true; |
| 1418 | } else if (that == NULL) { |
| 1419 | // Null isn't an instanceof anything |
| 1420 | return false; |
| 1421 | } else if (this->GetLength() != that->GetLength()) { |
| 1422 | // Quick length inequality test |
| 1423 | return false; |
| 1424 | } else { |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1425 | // Note: don't short circuit on hash code as we're presumably here as the |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1426 | // hash code was already equal |
| 1427 | for (int32_t i = 0; i < that->GetLength(); ++i) { |
| 1428 | if (this->CharAt(i) != that->CharAt(i)) { |
| 1429 | return false; |
| 1430 | } |
| 1431 | } |
| 1432 | return true; |
| 1433 | } |
| 1434 | } |
| 1435 | |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 1436 | bool String::Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1437 | if (this->GetLength() != that_length) { |
| 1438 | return false; |
| 1439 | } else { |
| 1440 | for (int32_t i = 0; i < that_length; ++i) { |
| 1441 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 1442 | return false; |
| 1443 | } |
| 1444 | } |
| 1445 | return true; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | bool String::Equals(const char* modified_utf8) const { |
| 1450 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1451 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 1452 | if (ch == '\0' || ch != CharAt(i)) { |
| 1453 | return false; |
| 1454 | } |
| 1455 | } |
| 1456 | return *modified_utf8 == '\0'; |
| 1457 | } |
| 1458 | |
| 1459 | bool String::Equals(const StringPiece& modified_utf8) const { |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1460 | if (modified_utf8.size() != GetLength()) { |
| 1461 | return false; |
| 1462 | } |
| 1463 | const char* p = modified_utf8.data(); |
| 1464 | for (int32_t i = 0; i < GetLength(); ++i) { |
| 1465 | uint16_t ch = GetUtf16FromUtf8(&p); |
| 1466 | if (ch != CharAt(i)) { |
| 1467 | return false; |
| 1468 | } |
| 1469 | } |
| 1470 | return true; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 1474 | std::string String::ToModifiedUtf8() const { |
| 1475 | const uint16_t* chars = GetCharArray()->GetData() + GetOffset(); |
| 1476 | size_t byte_count(CountUtf8Bytes(chars, GetLength())); |
| 1477 | std::string result(byte_count, char(0)); |
| 1478 | ConvertUtf16ToModifiedUtf8(&result[0], chars, GetLength()); |
| 1479 | return result; |
| 1480 | } |
| 1481 | |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1482 | void Throwable::SetCause(Throwable* cause) { |
| 1483 | CHECK(cause != NULL); |
| 1484 | CHECK(cause != this); |
| 1485 | CHECK(GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false) == NULL); |
| 1486 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), cause, false); |
| 1487 | } |
| 1488 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 1489 | bool Throwable::IsCheckedException() const { |
| 1490 | Class* error = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Error;"); |
| 1491 | if (InstanceOf(error)) { |
| 1492 | return false; |
| 1493 | } |
| 1494 | Class* jlre = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/RuntimeException;"); |
| 1495 | return !InstanceOf(jlre); |
| 1496 | } |
| 1497 | |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1498 | std::string Throwable::Dump() const { |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1499 | std::string result(PrettyTypeOf(this)); |
| 1500 | result += ": "; |
Ian Rogers | a32a6fd | 2012-02-06 20:18:44 -0800 | [diff] [blame] | 1501 | String* msg = GetDetailMessage(); |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1502 | if (msg != NULL) { |
| 1503 | result += msg->ToModifiedUtf8(); |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1504 | } |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1505 | result += "\n"; |
| 1506 | Object* stack_state = GetStackState(); |
| 1507 | // check stack state isn't missing or corrupt |
| 1508 | if (stack_state != NULL && stack_state->IsObjectArray()) { |
| 1509 | // Decode the internal stack trace into the depth and method trace |
| 1510 | ObjectArray<Object>* method_trace = down_cast<ObjectArray<Object>*>(stack_state); |
| 1511 | int32_t depth = method_trace->GetLength() - 1; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1512 | IntArray* pc_trace = down_cast<IntArray*>(method_trace->Get(depth)); |
| 1513 | MethodHelper mh; |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1514 | for (int32_t i = 0; i < depth; ++i) { |
| 1515 | Method* method = down_cast<Method*>(method_trace->Get(i)); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 1516 | mh.ChangeMethod(method); |
| 1517 | uint32_t native_pc = pc_trace->Get(i); |
| 1518 | int32_t line_number = mh.GetLineNumFromNativePC(native_pc); |
| 1519 | const char* source_file = mh.GetDeclaringClassSourceFile(); |
| 1520 | result += StringPrintf(" at %s (%s:%d)\n", PrettyMethod(method, true).c_str(), |
| 1521 | source_file, line_number); |
Ian Rogers | 09f6b56 | 2012-01-31 21:58:52 -0800 | [diff] [blame] | 1522 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1523 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1524 | Throwable* cause = GetFieldObject<Throwable*>(OFFSET_OF_OBJECT_MEMBER(Throwable, cause_), false); |
Ian Rogers | c8b306f | 2012-02-17 21:34:44 -0800 | [diff] [blame] | 1525 | if (cause != NULL && cause != this) { // Constructor makes cause == this by default. |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 1526 | result += "Caused by: "; |
| 1527 | result += cause->Dump(); |
| 1528 | } |
Ian Rogers | 9074b99 | 2011-10-26 17:41:55 -0700 | [diff] [blame] | 1529 | return result; |
| 1530 | } |
| 1531 | |
Ian Rogers | 5167c97 | 2012-02-03 10:41:20 -0800 | [diff] [blame] | 1532 | |
| 1533 | Class* Throwable::java_lang_Throwable_ = NULL; |
| 1534 | |
| 1535 | void Throwable::SetClass(Class* java_lang_Throwable) { |
| 1536 | CHECK(java_lang_Throwable_ == NULL); |
| 1537 | CHECK(java_lang_Throwable != NULL); |
| 1538 | java_lang_Throwable_ = java_lang_Throwable; |
| 1539 | } |
| 1540 | |
| 1541 | void Throwable::ResetClass() { |
| 1542 | CHECK(java_lang_Throwable_ != NULL); |
| 1543 | java_lang_Throwable_ = NULL; |
| 1544 | } |
| 1545 | |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 1546 | Class* StackTraceElement::java_lang_StackTraceElement_ = NULL; |
| 1547 | |
| 1548 | void StackTraceElement::SetClass(Class* java_lang_StackTraceElement) { |
| 1549 | CHECK(java_lang_StackTraceElement_ == NULL); |
| 1550 | CHECK(java_lang_StackTraceElement != NULL); |
| 1551 | java_lang_StackTraceElement_ = java_lang_StackTraceElement; |
| 1552 | } |
| 1553 | |
| 1554 | void StackTraceElement::ResetClass() { |
| 1555 | CHECK(java_lang_StackTraceElement_ != NULL); |
| 1556 | java_lang_StackTraceElement_ = NULL; |
| 1557 | } |
| 1558 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1559 | StackTraceElement* StackTraceElement::Alloc(String* declaring_class, |
| 1560 | String* method_name, |
| 1561 | String* file_name, |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1562 | int32_t line_number) { |
| 1563 | StackTraceElement* trace = |
| 1564 | down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject()); |
| 1565 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), |
| 1566 | const_cast<String*>(declaring_class), false); |
| 1567 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), |
| 1568 | const_cast<String*>(method_name), false); |
| 1569 | trace->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), |
| 1570 | const_cast<String*>(file_name), false); |
| 1571 | trace->SetField32(OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), |
| 1572 | line_number, false); |
| 1573 | return trace; |
| 1574 | } |
| 1575 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1576 | static const char* kClassStatusNames[] = { |
| 1577 | "Error", |
| 1578 | "NotReady", |
| 1579 | "Idx", |
| 1580 | "Loaded", |
| 1581 | "Resolved", |
| 1582 | "Verifying", |
| 1583 | "Verified", |
| 1584 | "Initializing", |
| 1585 | "Initialized" |
| 1586 | }; |
| 1587 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) { |
| 1588 | if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 1589 | os << kClassStatusNames[rhs + 1]; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1590 | } else { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1591 | os << "Class::Status[" << static_cast<int>(rhs) << "]"; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1592 | } |
| 1593 | return os; |
| 1594 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1595 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1596 | } // namespace art |