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