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