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