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