class.cc revision c528dba35b5faece51ca658fc008b688f8b690ad
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 "art_field-inl.h" 20#include "art_method-inl.h" 21#include "class-inl.h" 22#include "class_linker.h" 23#include "class_loader.h" 24#include "dex_cache.h" 25#include "dex_file-inl.h" 26#include "gc/accounting/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 37namespace art { 38namespace mirror { 39 40Class* Class::java_lang_Class_ = NULL; 41 42void 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 48void Class::ResetClass() { 49 CHECK(java_lang_Class_ != NULL); 50 java_lang_Class_ = NULL; 51} 52 53void Class::VisitRoots(RootVisitor* visitor, void* arg) { 54 if (java_lang_Class_ != nullptr) { 55 java_lang_Class_ = down_cast<Class*>(visitor(java_lang_Class_, arg)); 56 } 57} 58 59void Class::SetStatus(Status new_status, Thread* self) { 60 Status old_status = GetStatus(); 61 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 62 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); 63 if (LIKELY(class_linker_initialized)) { 64 if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) { 65 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " " 66 << old_status << " -> " << new_status; 67 } 68 if (new_status >= kStatusResolved || old_status >= kStatusResolved) { 69 // When classes are being resolved the resolution code should hold the lock. 70 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId()) 71 << "Attempt to change status of class while not holding its lock: " 72 << PrettyClass(this) << " " << old_status << " -> " << new_status; 73 } 74 } 75 if (new_status == kStatusError) { 76 CHECK_NE(GetStatus(), kStatusError) 77 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this); 78 79 // Stash current exception. 80 SirtRef<mirror::Object> old_throw_this_object(self, NULL); 81 SirtRef<mirror::ArtMethod> old_throw_method(self, NULL); 82 SirtRef<mirror::Throwable> old_exception(self, NULL); 83 uint32_t old_throw_dex_pc; 84 { 85 ThrowLocation old_throw_location; 86 mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location); 87 old_throw_this_object.reset(old_throw_location.GetThis()); 88 old_throw_method.reset(old_throw_location.GetMethod()); 89 old_exception.reset(old_exception_obj); 90 old_throw_dex_pc = old_throw_location.GetDexPc(); 91 self->ClearException(); 92 } 93 CHECK(old_exception.get() != NULL); 94 95 // clear exception to call FindSystemClass 96 self->ClearException(); 97 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 98 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;"); 99 CHECK(!self->IsExceptionPending()); 100 101 // Only verification errors, not initialization problems, should set a verify error. 102 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case. 103 Class* exception_class = old_exception->GetClass(); 104 if (!eiie_class->IsAssignableFrom(exception_class)) { 105 SetVerifyErrorClass(exception_class); 106 } 107 108 // Restore exception. 109 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(), 110 old_throw_dex_pc); 111 112 self->SetException(gc_safe_throw_location, old_exception.get()); 113 } 114 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this); 115 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false); 116 // Classes that are being resolved or initialized need to notify waiters that the class status 117 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. 118 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) && 119 class_linker_initialized) { 120 NotifyAll(self); 121 } 122} 123 124void Class::SetDexCache(DexCache* new_dex_cache) { 125 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false); 126} 127 128void Class::SetClassSize(size_t new_class_size) { 129 if (kIsDebugBuild && (new_class_size < GetClassSize())) { 130 DumpClass(LOG(ERROR), kDumpClassFullDetail); 131 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); 132 } 133 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false); 134} 135 136// Return the class' name. The exact format is bizarre, but it's the specified behavior for 137// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" 138// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than 139// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. 140String* Class::ComputeName() { 141 String* name = GetName(); 142 if (name != NULL) { 143 return name; 144 } 145 std::string descriptor(ClassHelper(this).GetDescriptor()); 146 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { 147 // The descriptor indicates that this is the class for 148 // a primitive type; special-case the return value. 149 const char* c_name = NULL; 150 switch (descriptor[0]) { 151 case 'Z': c_name = "boolean"; break; 152 case 'B': c_name = "byte"; break; 153 case 'C': c_name = "char"; break; 154 case 'S': c_name = "short"; break; 155 case 'I': c_name = "int"; break; 156 case 'J': c_name = "long"; break; 157 case 'F': c_name = "float"; break; 158 case 'D': c_name = "double"; break; 159 case 'V': c_name = "void"; break; 160 default: 161 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); 162 } 163 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name); 164 } else { 165 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package 166 // components. 167 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { 168 descriptor.erase(0, 1); 169 descriptor.erase(descriptor.size() - 1); 170 } 171 std::replace(descriptor.begin(), descriptor.end(), '/', '.'); 172 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str()); 173 } 174 SetName(name); 175 return name; 176} 177 178void Class::DumpClass(std::ostream& os, int flags) const { 179 if ((flags & kDumpClassFullDetail) == 0) { 180 os << PrettyClass(this); 181 if ((flags & kDumpClassClassLoader) != 0) { 182 os << ' ' << GetClassLoader(); 183 } 184 if ((flags & kDumpClassInitialized) != 0) { 185 os << ' ' << GetStatus(); 186 } 187 os << "\n"; 188 return; 189 } 190 191 Class* super = GetSuperClass(); 192 ClassHelper kh(this); 193 os << "----- " << (IsInterface() ? "interface" : "class") << " " 194 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", 195 os << " objectSize=" << SizeOf() << " " 196 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n", 197 os << StringPrintf(" access=0x%04x.%04x\n", 198 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); 199 if (super != NULL) { 200 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n"; 201 } 202 if (IsArrayClass()) { 203 os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; 204 } 205 if (kh.NumDirectInterfaces() > 0) { 206 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n"; 207 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) { 208 Class* interface = kh.GetDirectInterface(i); 209 const ClassLoader* cl = interface->GetClassLoader(); 210 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); 211 } 212 } 213 os << " vtable (" << NumVirtualMethods() << " entries, " 214 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n"; 215 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 216 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str()); 217 } 218 os << " direct methods (" << NumDirectMethods() << " entries):\n"; 219 for (size_t i = 0; i < NumDirectMethods(); ++i) { 220 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str()); 221 } 222 if (NumStaticFields() > 0) { 223 os << " static fields (" << NumStaticFields() << " entries):\n"; 224 if (IsResolved() || IsErroneous()) { 225 for (size_t i = 0; i < NumStaticFields(); ++i) { 226 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str()); 227 } 228 } else { 229 os << " <not yet available>"; 230 } 231 } 232 if (NumInstanceFields() > 0) { 233 os << " instance fields (" << NumInstanceFields() << " entries):\n"; 234 if (IsResolved() || IsErroneous()) { 235 for (size_t i = 0; i < NumInstanceFields(); ++i) { 236 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str()); 237 } 238 } else { 239 os << " <not yet available>"; 240 } 241 } 242} 243 244void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { 245 if (new_reference_offsets != CLASS_WALK_SUPER) { 246 // Sanity check that the number of bits set in the reference offset bitmap 247 // agrees with the number of references 248 size_t count = 0; 249 for (Class* c = this; c != NULL; c = c->GetSuperClass()) { 250 count += c->NumReferenceInstanceFieldsDuringLinking(); 251 } 252 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count); 253 } 254 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), 255 new_reference_offsets, false); 256} 257 258void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { 259 if (new_reference_offsets != CLASS_WALK_SUPER) { 260 // Sanity check that the number of bits set in the reference offset bitmap 261 // agrees with the number of references 262 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), 263 NumReferenceStaticFieldsDuringLinking()); 264 } 265 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), 266 new_reference_offsets, false); 267} 268 269bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { 270 size_t i = 0; 271 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { 272 ++i; 273 } 274 if (descriptor1.find('/', i) != StringPiece::npos || 275 descriptor2.find('/', i) != StringPiece::npos) { 276 return false; 277 } else { 278 return true; 279 } 280} 281 282bool Class::IsInSamePackage(const Class* that) const { 283 const Class* klass1 = this; 284 const Class* klass2 = that; 285 if (klass1 == klass2) { 286 return true; 287 } 288 // Class loaders must match. 289 if (klass1->GetClassLoader() != klass2->GetClassLoader()) { 290 return false; 291 } 292 // Arrays are in the same package when their element classes are. 293 while (klass1->IsArrayClass()) { 294 klass1 = klass1->GetComponentType(); 295 } 296 while (klass2->IsArrayClass()) { 297 klass2 = klass2->GetComponentType(); 298 } 299 // trivial check again for array types 300 if (klass1 == klass2) { 301 return true; 302 } 303 // Compare the package part of the descriptor string. 304 return IsInSamePackage(ClassHelper(klass1).GetDescriptor(), 305 ClassHelper(klass2).GetDescriptor()); 306} 307 308bool Class::IsClassClass() const { 309 Class* java_lang_Class = GetClass()->GetClass(); 310 return this == java_lang_Class; 311} 312 313bool Class::IsStringClass() const { 314 return this == String::GetJavaLangString(); 315} 316 317bool Class::IsThrowableClass() const { 318 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); 319} 320 321bool Class::IsArtFieldClass() const { 322 Class* java_lang_Class = GetClass(); 323 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass(); 324 return this == java_lang_reflect_ArtField; 325} 326 327bool Class::IsArtMethodClass() const { 328 return this == ArtMethod::GetJavaLangReflectArtMethod(); 329} 330 331void Class::SetClassLoader(ClassLoader* new_class_loader) { 332 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false); 333} 334 335ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) const { 336 // Check the current class before checking the interfaces. 337 ArtMethod* method = FindDeclaredVirtualMethod(name, signature); 338 if (method != NULL) { 339 return method; 340 } 341 342 int32_t iftable_count = GetIfTableCount(); 343 IfTable* iftable = GetIfTable(); 344 for (int32_t i = 0; i < iftable_count; i++) { 345 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); 346 if (method != NULL) { 347 return method; 348 } 349 } 350 return NULL; 351} 352 353ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { 354 // Check the current class before checking the interfaces. 355 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 356 if (method != NULL) { 357 return method; 358 } 359 360 int32_t iftable_count = GetIfTableCount(); 361 IfTable* iftable = GetIfTable(); 362 for (int32_t i = 0; i < iftable_count; i++) { 363 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 364 if (method != NULL) { 365 return method; 366 } 367 } 368 return NULL; 369} 370 371ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const { 372 MethodHelper mh; 373 for (size_t i = 0; i < NumDirectMethods(); ++i) { 374 ArtMethod* method = GetDirectMethod(i); 375 mh.ChangeMethod(method); 376 if (name == mh.GetName() && mh.GetSignature() == signature) { 377 return method; 378 } 379 } 380 return NULL; 381} 382 383ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const { 384 MethodHelper mh; 385 for (size_t i = 0; i < NumDirectMethods(); ++i) { 386 ArtMethod* method = GetDirectMethod(i); 387 mh.ChangeMethod(method); 388 if (name == mh.GetName() && signature == mh.GetSignature()) { 389 return method; 390 } 391 } 392 return NULL; 393} 394 395ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { 396 if (GetDexCache() == dex_cache) { 397 for (size_t i = 0; i < NumDirectMethods(); ++i) { 398 ArtMethod* method = GetDirectMethod(i); 399 if (method->GetDexMethodIndex() == dex_method_idx) { 400 return method; 401 } 402 } 403 } 404 return NULL; 405} 406 407ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const { 408 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { 409 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); 410 if (method != NULL) { 411 return method; 412 } 413 } 414 return NULL; 415} 416 417ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) const { 418 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { 419 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); 420 if (method != NULL) { 421 return method; 422 } 423 } 424 return NULL; 425} 426 427ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { 428 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { 429 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); 430 if (method != NULL) { 431 return method; 432 } 433 } 434 return NULL; 435} 436 437ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const { 438 MethodHelper mh; 439 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 440 ArtMethod* method = GetVirtualMethod(i); 441 mh.ChangeMethod(method); 442 if (name == mh.GetName() && mh.GetSignature() == signature) { 443 return method; 444 } 445 } 446 return NULL; 447} 448 449ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, 450 const Signature& signature) const { 451 MethodHelper mh; 452 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 453 ArtMethod* method = GetVirtualMethod(i); 454 mh.ChangeMethod(method); 455 if (name == mh.GetName() && signature == mh.GetSignature()) { 456 return method; 457 } 458 } 459 return NULL; 460} 461 462ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { 463 if (GetDexCache() == dex_cache) { 464 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 465 ArtMethod* method = GetVirtualMethod(i); 466 if (method->GetDexMethodIndex() == dex_method_idx) { 467 return method; 468 } 469 } 470 } 471 return NULL; 472} 473 474ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const { 475 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { 476 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); 477 if (method != NULL) { 478 return method; 479 } 480 } 481 return NULL; 482} 483 484ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) const { 485 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { 486 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); 487 if (method != NULL) { 488 return method; 489 } 490 } 491 return NULL; 492} 493 494ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { 495 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { 496 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 497 if (method != NULL) { 498 return method; 499 } 500 } 501 return NULL; 502} 503 504ArtMethod* Class::FindClassInitializer() const { 505 for (size_t i = 0; i < NumDirectMethods(); ++i) { 506 ArtMethod* method = GetDirectMethod(i); 507 if (method->IsConstructor() && method->IsStatic()) { 508 if (kIsDebugBuild) { 509 MethodHelper mh(method); 510 CHECK(mh.IsClassInitializer()); 511 CHECK_STREQ(mh.GetName(), "<clinit>"); 512 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V"); 513 } 514 return method; 515 } 516 } 517 return NULL; 518} 519 520ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { 521 // Is the field in this class? 522 // Interfaces are not relevant because they can't contain instance fields. 523 FieldHelper fh; 524 for (size_t i = 0; i < NumInstanceFields(); ++i) { 525 ArtField* f = GetInstanceField(i); 526 fh.ChangeField(f); 527 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { 528 return f; 529 } 530 } 531 return NULL; 532} 533 534ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { 535 if (GetDexCache() == dex_cache) { 536 for (size_t i = 0; i < NumInstanceFields(); ++i) { 537 ArtField* f = GetInstanceField(i); 538 if (f->GetDexFieldIndex() == dex_field_idx) { 539 return f; 540 } 541 } 542 } 543 return NULL; 544} 545 546ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { 547 // Is the field in this class, or any of its superclasses? 548 // Interfaces are not relevant because they can't contain instance fields. 549 for (Class* c = this; c != NULL; c = c->GetSuperClass()) { 550 ArtField* f = c->FindDeclaredInstanceField(name, type); 551 if (f != NULL) { 552 return f; 553 } 554 } 555 return NULL; 556} 557 558ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { 559 // Is the field in this class, or any of its superclasses? 560 // Interfaces are not relevant because they can't contain instance fields. 561 for (Class* c = this; c != NULL; c = c->GetSuperClass()) { 562 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); 563 if (f != NULL) { 564 return f; 565 } 566 } 567 return NULL; 568} 569 570ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { 571 DCHECK(type != NULL); 572 FieldHelper fh; 573 for (size_t i = 0; i < NumStaticFields(); ++i) { 574 ArtField* f = GetStaticField(i); 575 fh.ChangeField(f); 576 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { 577 return f; 578 } 579 } 580 return NULL; 581} 582 583ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { 584 if (dex_cache == GetDexCache()) { 585 for (size_t i = 0; i < NumStaticFields(); ++i) { 586 ArtField* f = GetStaticField(i); 587 if (f->GetDexFieldIndex() == dex_field_idx) { 588 return f; 589 } 590 } 591 } 592 return NULL; 593} 594 595ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) { 596 // Is the field in this class (or its interfaces), or any of its 597 // superclasses (or their interfaces)? 598 for (Class* k = this; k != NULL; k = k->GetSuperClass()) { 599 // Is the field in this class? 600 ArtField* f = k->FindDeclaredStaticField(name, type); 601 if (f != NULL) { 602 return f; 603 } 604 // Is this field in any of this class' interfaces? 605 ClassHelper kh(k); 606 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { 607 Class* interface = kh.GetDirectInterface(i); 608 f = interface->FindStaticField(name, type); 609 if (f != NULL) { 610 return f; 611 } 612 } 613 } 614 return NULL; 615} 616 617ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { 618 for (Class* k = this; k != NULL; k = k->GetSuperClass()) { 619 // Is the field in this class? 620 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); 621 if (f != NULL) { 622 return f; 623 } 624 // Is this field in any of this class' interfaces? 625 ClassHelper kh(k); 626 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { 627 Class* interface = kh.GetDirectInterface(i); 628 f = interface->FindStaticField(dex_cache, dex_field_idx); 629 if (f != NULL) { 630 return f; 631 } 632 } 633 } 634 return NULL; 635} 636 637ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) { 638 // Find a field using the JLS field resolution order 639 for (Class* k = this; k != NULL; k = k->GetSuperClass()) { 640 // Is the field in this class? 641 ArtField* f = k->FindDeclaredInstanceField(name, type); 642 if (f != NULL) { 643 return f; 644 } 645 f = k->FindDeclaredStaticField(name, type); 646 if (f != NULL) { 647 return f; 648 } 649 // Is this field in any of this class' interfaces? 650 ClassHelper kh(k); 651 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { 652 Class* interface = kh.GetDirectInterface(i); 653 f = interface->FindStaticField(name, type); 654 if (f != NULL) { 655 return f; 656 } 657 } 658 } 659 return NULL; 660} 661 662static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods) 663 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 664 if (methods != NULL) { 665 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) { 666 mirror::ArtMethod* method = methods->GetWithoutChecks(index); 667 DCHECK(method != NULL); 668 if (!method->IsNative() && !method->IsAbstract()) { 669 method->SetPreverified(); 670 } 671 } 672 } 673} 674 675void Class::SetPreverifiedFlagOnAllMethods() { 676 DCHECK(IsVerified()); 677 SetPreverifiedFlagOnMethods(GetDirectMethods()); 678 SetPreverifiedFlagOnMethods(GetVirtualMethods()); 679} 680 681} // namespace mirror 682} // namespace art 683