Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "class.h" |
| 18 | |
| 19 | #include "abstract_method-inl.h" |
| 20 | #include "class-inl.h" |
| 21 | #include "class_linker.h" |
| 22 | #include "class_loader.h" |
| 23 | #include "dex_cache.h" |
| 24 | #include "field-inl.h" |
| 25 | #include "gc/card_table-inl.h" |
| 26 | #include "object-inl.h" |
| 27 | #include "object_array-inl.h" |
| 28 | #include "object_utils.h" |
| 29 | #include "runtime.h" |
| 30 | #include "sirt_ref.h" |
| 31 | #include "thread.h" |
| 32 | #include "throwable.h" |
| 33 | #include "utils.h" |
| 34 | #include "well_known_classes.h" |
| 35 | |
| 36 | namespace art { |
| 37 | namespace mirror { |
| 38 | |
| 39 | Class* Class::java_lang_Class_ = NULL; |
| 40 | |
| 41 | void Class::SetClassClass(Class* java_lang_Class) { |
| 42 | CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class; |
| 43 | CHECK(java_lang_Class != NULL); |
| 44 | java_lang_Class_ = java_lang_Class; |
| 45 | } |
| 46 | |
| 47 | void Class::ResetClass() { |
| 48 | CHECK(java_lang_Class_ != NULL); |
| 49 | java_lang_Class_ = NULL; |
| 50 | } |
| 51 | |
| 52 | void Class::SetStatus(Status new_status) { |
| 53 | CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted()) |
| 54 | << PrettyClass(this) << " " << GetStatus() << " -> " << new_status; |
| 55 | CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this); |
| 56 | if (new_status > kStatusResolved) { |
| 57 | CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this); |
| 58 | } |
| 59 | if (new_status == kStatusError) { |
| 60 | CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this); |
| 61 | |
| 62 | // stash current exception |
| 63 | Thread* self = Thread::Current(); |
| 64 | SirtRef<Throwable> exception(self, self->GetException()); |
| 65 | CHECK(exception.get() != NULL); |
| 66 | |
| 67 | // clear exception to call FindSystemClass |
| 68 | self->ClearException(); |
| 69 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 70 | Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;"); |
| 71 | CHECK(!self->IsExceptionPending()); |
| 72 | |
| 73 | // only verification errors, not initialization problems, should set a verify error. |
| 74 | // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case. |
| 75 | Class* exception_class = exception->GetClass(); |
| 76 | if (!eiie_class->IsAssignableFrom(exception_class)) { |
| 77 | SetVerifyErrorClass(exception_class); |
| 78 | } |
| 79 | |
| 80 | // restore exception |
| 81 | self->SetException(exception.get()); |
| 82 | } |
| 83 | return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false); |
| 84 | } |
| 85 | |
| 86 | DexCache* Class::GetDexCache() const { |
| 87 | return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false); |
| 88 | } |
| 89 | |
| 90 | void Class::SetDexCache(DexCache* new_dex_cache) { |
| 91 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false); |
| 92 | } |
| 93 | |
| 94 | Object* Class::AllocObject(Thread* self) { |
| 95 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
| 96 | DCHECK(IsInstantiable()) << PrettyClass(this); |
| 97 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 98 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
| 99 | DCHECK_GE(this->object_size_, sizeof(Object)); |
| 100 | return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_); |
| 101 | } |
| 102 | |
| 103 | void Class::SetClassSize(size_t new_class_size) { |
| 104 | DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); |
| 105 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false); |
| 106 | } |
| 107 | |
| 108 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 109 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 110 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 111 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
| 112 | String* Class::ComputeName() { |
| 113 | String* name = GetName(); |
| 114 | if (name != NULL) { |
| 115 | return name; |
| 116 | } |
| 117 | std::string descriptor(ClassHelper(this).GetDescriptor()); |
| 118 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 119 | // The descriptor indicates that this is the class for |
| 120 | // a primitive type; special-case the return value. |
| 121 | const char* c_name = NULL; |
| 122 | switch (descriptor[0]) { |
| 123 | case 'Z': c_name = "boolean"; break; |
| 124 | case 'B': c_name = "byte"; break; |
| 125 | case 'C': c_name = "char"; break; |
| 126 | case 'S': c_name = "short"; break; |
| 127 | case 'I': c_name = "int"; break; |
| 128 | case 'J': c_name = "long"; break; |
| 129 | case 'F': c_name = "float"; break; |
| 130 | case 'D': c_name = "double"; break; |
| 131 | case 'V': c_name = "void"; break; |
| 132 | default: |
| 133 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 134 | } |
| 135 | name = String::AllocFromModifiedUtf8(Thread::Current(), c_name); |
| 136 | } else { |
| 137 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 138 | // components. |
| 139 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 140 | descriptor.erase(0, 1); |
| 141 | descriptor.erase(descriptor.size() - 1); |
| 142 | } |
| 143 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 144 | name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str()); |
| 145 | } |
| 146 | SetName(name); |
| 147 | return name; |
| 148 | } |
| 149 | |
| 150 | void Class::DumpClass(std::ostream& os, int flags) const { |
| 151 | if ((flags & kDumpClassFullDetail) == 0) { |
| 152 | os << PrettyClass(this); |
| 153 | if ((flags & kDumpClassClassLoader) != 0) { |
| 154 | os << ' ' << GetClassLoader(); |
| 155 | } |
| 156 | if ((flags & kDumpClassInitialized) != 0) { |
| 157 | os << ' ' << GetStatus(); |
| 158 | } |
| 159 | os << "\n"; |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | Class* super = GetSuperClass(); |
| 164 | ClassHelper kh(this); |
| 165 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
| 166 | << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", |
| 167 | os << " objectSize=" << SizeOf() << " " |
| 168 | << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n", |
| 169 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 170 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
| 171 | if (super != NULL) { |
| 172 | os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n"; |
| 173 | } |
| 174 | if (IsArrayClass()) { |
| 175 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 176 | } |
| 177 | if (kh.NumDirectInterfaces() > 0) { |
| 178 | os << " interfaces (" << kh.NumDirectInterfaces() << "):\n"; |
| 179 | for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 180 | Class* interface = kh.GetDirectInterface(i); |
| 181 | const ClassLoader* cl = interface->GetClassLoader(); |
| 182 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
| 183 | } |
| 184 | } |
| 185 | os << " vtable (" << NumVirtualMethods() << " entries, " |
| 186 | << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n"; |
| 187 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 188 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str()); |
| 189 | } |
| 190 | os << " direct methods (" << NumDirectMethods() << " entries):\n"; |
| 191 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 192 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str()); |
| 193 | } |
| 194 | if (NumStaticFields() > 0) { |
| 195 | os << " static fields (" << NumStaticFields() << " entries):\n"; |
| 196 | if (IsResolved() || IsErroneous()) { |
| 197 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 198 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str()); |
| 199 | } |
| 200 | } else { |
| 201 | os << " <not yet available>"; |
| 202 | } |
| 203 | } |
| 204 | if (NumInstanceFields() > 0) { |
| 205 | os << " instance fields (" << NumInstanceFields() << " entries):\n"; |
| 206 | if (IsResolved() || IsErroneous()) { |
| 207 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 208 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str()); |
| 209 | } |
| 210 | } else { |
| 211 | os << " <not yet available>"; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 217 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 218 | // Sanity check that the number of bits set in the reference offset bitmap |
| 219 | // agrees with the number of references |
| 220 | size_t count = 0; |
| 221 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 222 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
| 223 | } |
| 224 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count); |
| 225 | } |
| 226 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 227 | new_reference_offsets, false); |
| 228 | } |
| 229 | |
| 230 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 231 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 232 | // Sanity check that the number of bits set in the reference offset bitmap |
| 233 | // agrees with the number of references |
| 234 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 235 | NumReferenceStaticFieldsDuringLinking()); |
| 236 | } |
| 237 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 238 | new_reference_offsets, false); |
| 239 | } |
| 240 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 241 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
| 242 | size_t i = 0; |
| 243 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 244 | ++i; |
| 245 | } |
| 246 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 247 | descriptor2.find('/', i) != StringPiece::npos) { |
| 248 | return false; |
| 249 | } else { |
| 250 | return true; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | bool Class::IsInSamePackage(const Class* that) const { |
| 255 | const Class* klass1 = this; |
| 256 | const Class* klass2 = that; |
| 257 | if (klass1 == klass2) { |
| 258 | return true; |
| 259 | } |
| 260 | // Class loaders must match. |
| 261 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 262 | return false; |
| 263 | } |
| 264 | // Arrays are in the same package when their element classes are. |
| 265 | while (klass1->IsArrayClass()) { |
| 266 | klass1 = klass1->GetComponentType(); |
| 267 | } |
| 268 | while (klass2->IsArrayClass()) { |
| 269 | klass2 = klass2->GetComponentType(); |
| 270 | } |
| 271 | // Compare the package part of the descriptor string. |
| 272 | ClassHelper kh(klass1); |
| 273 | std::string descriptor1(kh.GetDescriptor()); |
| 274 | kh.ChangeClass(klass2); |
| 275 | std::string descriptor2(kh.GetDescriptor()); |
| 276 | return IsInSamePackage(descriptor1, descriptor2); |
| 277 | } |
| 278 | |
| 279 | bool Class::IsClassClass() const { |
| 280 | Class* java_lang_Class = GetClass()->GetClass(); |
| 281 | return this == java_lang_Class; |
| 282 | } |
| 283 | |
| 284 | bool Class::IsStringClass() const { |
| 285 | return this == String::GetJavaLangString(); |
| 286 | } |
| 287 | |
| 288 | bool Class::IsThrowableClass() const { |
| 289 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
| 290 | } |
| 291 | |
| 292 | bool Class::IsFieldClass() const { |
| 293 | Class* java_lang_Class = GetClass(); |
| 294 | Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass(); |
| 295 | return this == java_lang_reflect_Field; |
| 296 | |
| 297 | } |
| 298 | |
| 299 | bool Class::IsMethodClass() const { |
| 300 | return (this == AbstractMethod::GetMethodClass()) || |
| 301 | (this == AbstractMethod::GetConstructorClass()); |
| 302 | |
| 303 | } |
| 304 | |
| 305 | ClassLoader* Class::GetClassLoader() const { |
| 306 | return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false); |
| 307 | } |
| 308 | |
| 309 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
| 310 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false); |
| 311 | } |
| 312 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 313 | AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const { |
| 314 | // Check the current class before checking the interfaces. |
| 315 | AbstractMethod* method = FindDeclaredVirtualMethod(name, signature); |
| 316 | if (method != NULL) { |
| 317 | return method; |
| 318 | } |
| 319 | |
| 320 | int32_t iftable_count = GetIfTableCount(); |
| 321 | IfTable* iftable = GetIfTable(); |
| 322 | for (int32_t i = 0; i < iftable_count; i++) { |
| 323 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); |
| 324 | if (method != NULL) { |
| 325 | return method; |
| 326 | } |
| 327 | } |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 332 | // Check the current class before checking the interfaces. |
| 333 | AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 334 | if (method != NULL) { |
| 335 | return method; |
| 336 | } |
| 337 | |
| 338 | int32_t iftable_count = GetIfTableCount(); |
| 339 | IfTable* iftable = GetIfTable(); |
| 340 | for (int32_t i = 0; i < iftable_count; i++) { |
| 341 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 342 | if (method != NULL) { |
| 343 | return method; |
| 344 | } |
| 345 | } |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
| 351 | MethodHelper mh; |
| 352 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 353 | AbstractMethod* method = GetDirectMethod(i); |
| 354 | mh.ChangeMethod(method); |
| 355 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
| 356 | return method; |
| 357 | } |
| 358 | } |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 363 | if (GetDexCache() == dex_cache) { |
| 364 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 365 | AbstractMethod* method = GetDirectMethod(i); |
| 366 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 367 | return method; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
| 375 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 376 | AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature); |
| 377 | if (method != NULL) { |
| 378 | return method; |
| 379 | } |
| 380 | } |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 385 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 386 | AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); |
| 387 | if (method != NULL) { |
| 388 | return method; |
| 389 | } |
| 390 | } |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
| 395 | const StringPiece& signature) const { |
| 396 | MethodHelper mh; |
| 397 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 398 | AbstractMethod* method = GetVirtualMethod(i); |
| 399 | mh.ChangeMethod(method); |
| 400 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
| 401 | return method; |
| 402 | } |
| 403 | } |
| 404 | return NULL; |
| 405 | } |
| 406 | |
| 407 | AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 408 | if (GetDexCache() == dex_cache) { |
| 409 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 410 | AbstractMethod* method = GetVirtualMethod(i); |
| 411 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 412 | return method; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | return NULL; |
| 417 | } |
| 418 | |
| 419 | AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const { |
| 420 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 421 | AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
| 422 | if (method != NULL) { |
| 423 | return method; |
| 424 | } |
| 425 | } |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
| 430 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 431 | AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 432 | if (method != NULL) { |
| 433 | return method; |
| 434 | } |
| 435 | } |
| 436 | return NULL; |
| 437 | } |
| 438 | |
| 439 | Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
| 440 | // Is the field in this class? |
| 441 | // Interfaces are not relevant because they can't contain instance fields. |
| 442 | FieldHelper fh; |
| 443 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 444 | Field* f = GetInstanceField(i); |
| 445 | fh.ChangeField(f); |
| 446 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
| 447 | return f; |
| 448 | } |
| 449 | } |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 454 | if (GetDexCache() == dex_cache) { |
| 455 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 456 | Field* f = GetInstanceField(i); |
| 457 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 458 | return f; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | return NULL; |
| 463 | } |
| 464 | |
| 465 | Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
| 466 | // Is the field in this class, or any of its superclasses? |
| 467 | // Interfaces are not relevant because they can't contain instance fields. |
| 468 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 469 | Field* f = c->FindDeclaredInstanceField(name, type); |
| 470 | if (f != NULL) { |
| 471 | return f; |
| 472 | } |
| 473 | } |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 478 | // Is the field in this class, or any of its superclasses? |
| 479 | // Interfaces are not relevant because they can't contain instance fields. |
| 480 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 481 | Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
| 482 | if (f != NULL) { |
| 483 | return f; |
| 484 | } |
| 485 | } |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
| 490 | DCHECK(type != NULL); |
| 491 | FieldHelper fh; |
| 492 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 493 | Field* f = GetStaticField(i); |
| 494 | fh.ChangeField(f); |
| 495 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
| 496 | return f; |
| 497 | } |
| 498 | } |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 503 | if (dex_cache == GetDexCache()) { |
| 504 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 505 | Field* f = GetStaticField(i); |
| 506 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 507 | return f; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | return NULL; |
| 512 | } |
| 513 | |
| 514 | Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) { |
| 515 | // Is the field in this class (or its interfaces), or any of its |
| 516 | // superclasses (or their interfaces)? |
| 517 | ClassHelper kh; |
| 518 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 519 | // Is the field in this class? |
| 520 | Field* f = k->FindDeclaredStaticField(name, type); |
| 521 | if (f != NULL) { |
| 522 | return f; |
| 523 | } |
| 524 | // Is this field in any of this class' interfaces? |
| 525 | kh.ChangeClass(k); |
| 526 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 527 | Class* interface = kh.GetDirectInterface(i); |
| 528 | f = interface->FindStaticField(name, type); |
| 529 | if (f != NULL) { |
| 530 | return f; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
| 538 | ClassHelper kh; |
| 539 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 540 | // Is the field in this class? |
| 541 | Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
| 542 | if (f != NULL) { |
| 543 | return f; |
| 544 | } |
| 545 | // Is this field in any of this class' interfaces? |
| 546 | kh.ChangeClass(k); |
| 547 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 548 | Class* interface = kh.GetDirectInterface(i); |
| 549 | f = interface->FindStaticField(dex_cache, dex_field_idx); |
| 550 | if (f != NULL) { |
| 551 | return f; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | return NULL; |
| 556 | } |
| 557 | |
| 558 | Field* Class::FindField(const StringPiece& name, const StringPiece& type) { |
| 559 | // Find a field using the JLS field resolution order |
| 560 | ClassHelper kh; |
| 561 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 562 | // Is the field in this class? |
| 563 | Field* f = k->FindDeclaredInstanceField(name, type); |
| 564 | if (f != NULL) { |
| 565 | return f; |
| 566 | } |
| 567 | f = k->FindDeclaredStaticField(name, type); |
| 568 | if (f != NULL) { |
| 569 | return f; |
| 570 | } |
| 571 | // Is this field in any of this class' interfaces? |
| 572 | kh.ChangeClass(k); |
| 573 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 574 | Class* interface = kh.GetDirectInterface(i); |
| 575 | f = interface->FindStaticField(name, type); |
| 576 | if (f != NULL) { |
| 577 | return f; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | } // namespace mirror |
| 585 | } // namespace art |