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