class.cc revision 98d1cc8033251c93786e2fa8c59a2e555a9493be
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 "handle_scope-inl.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_ = nullptr; 41 42void Class::SetClassClass(Class* java_lang_Class) { 43 CHECK(java_lang_Class_ == nullptr) 44 << ReadBarrier::BarrierForRoot<mirror::Class, kWithReadBarrier>(&java_lang_Class_) 45 << " " << java_lang_Class; 46 CHECK(java_lang_Class != nullptr); 47 java_lang_Class_ = java_lang_Class; 48} 49 50void Class::ResetClass() { 51 CHECK(java_lang_Class_ != nullptr); 52 java_lang_Class_ = nullptr; 53} 54 55void Class::VisitRoots(RootCallback* callback, void* arg) { 56 if (java_lang_Class_ != nullptr) { 57 callback(reinterpret_cast<mirror::Object**>(&java_lang_Class_), arg, 0, kRootStickyClass); 58 } 59} 60 61void Class::SetStatus(Status new_status, Thread* self) { 62 Status old_status = GetStatus(); 63 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 64 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); 65 if (LIKELY(class_linker_initialized)) { 66 if (UNLIKELY(new_status <= old_status && new_status != kStatusError && 67 new_status != kStatusRetired)) { 68 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " " 69 << old_status << " -> " << new_status; 70 } 71 if (new_status >= kStatusResolved || old_status >= kStatusResolved) { 72 // When classes are being resolved the resolution code should hold the lock. 73 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId()) 74 << "Attempt to change status of class while not holding its lock: " 75 << PrettyClass(this) << " " << old_status << " -> " << new_status; 76 } 77 } 78 if (UNLIKELY(new_status == kStatusError)) { 79 CHECK_NE(GetStatus(), kStatusError) 80 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this); 81 82 // Stash current exception. 83 StackHandleScope<3> hs(self); 84 ThrowLocation old_throw_location; 85 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location))); 86 CHECK(old_exception.Get() != nullptr); 87 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis())); 88 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod())); 89 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc(); 90 bool is_exception_reported = self->IsExceptionReportedToInstrumentation(); 91 // clear exception to call FindSystemClass 92 self->ClearException(); 93 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 94 Class* eiie_class = class_linker->FindSystemClass(self, 95 "Ljava/lang/ExceptionInInitializerError;"); 96 CHECK(!self->IsExceptionPending()); 97 98 // Only verification errors, not initialization problems, should set a verify error. 99 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case. 100 Class* exception_class = old_exception->GetClass(); 101 if (!eiie_class->IsAssignableFrom(exception_class)) { 102 SetVerifyErrorClass(exception_class); 103 } 104 105 // Restore exception. 106 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(), 107 old_throw_dex_pc); 108 self->SetException(gc_safe_throw_location, old_exception.Get()); 109 self->SetExceptionReportedToInstrumentation(is_exception_reported); 110 } 111 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32); 112 if (Runtime::Current()->IsActiveTransaction()) { 113 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); 114 } else { 115 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); 116 } 117 118 if (!class_linker_initialized) { 119 // When the class linker is being initialized its single threaded and by definition there can be 120 // no waiters. During initialization classes may appear temporary but won't be retired as their 121 // size was statically computed. 122 } else { 123 // Classes that are being resolved or initialized need to notify waiters that the class status 124 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. 125 if (IsTemp()) { 126 // Class is a temporary one, ensure that waiters for resolution get notified of retirement 127 // so that they can grab the new version of the class from the class linker's table. 128 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this); 129 if (new_status == kStatusRetired || new_status == kStatusError) { 130 NotifyAll(self); 131 } 132 } else { 133 CHECK_NE(new_status, kStatusRetired); 134 if (old_status >= kStatusResolved || new_status >= kStatusResolved) { 135 NotifyAll(self); 136 } 137 } 138 } 139} 140 141void Class::SetDexCache(DexCache* new_dex_cache) { 142 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache); 143} 144 145void Class::SetClassSize(uint32_t new_class_size) { 146 if (kIsDebugBuild && (new_class_size < GetClassSize())) { 147 DumpClass(LOG(ERROR), kDumpClassFullDetail); 148 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); 149 } 150 // Not called within a transaction. 151 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); 152} 153 154// Return the class' name. The exact format is bizarre, but it's the specified behavior for 155// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" 156// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than 157// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. 158String* Class::ComputeName(Handle<Class> h_this) { 159 String* name = h_this->GetName(); 160 if (name != nullptr) { 161 return name; 162 } 163 std::string descriptor(h_this->GetDescriptor()); 164 Thread* self = Thread::Current(); 165 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { 166 // The descriptor indicates that this is the class for 167 // a primitive type; special-case the return value. 168 const char* c_name = nullptr; 169 switch (descriptor[0]) { 170 case 'Z': c_name = "boolean"; break; 171 case 'B': c_name = "byte"; break; 172 case 'C': c_name = "char"; break; 173 case 'S': c_name = "short"; break; 174 case 'I': c_name = "int"; break; 175 case 'J': c_name = "long"; break; 176 case 'F': c_name = "float"; break; 177 case 'D': c_name = "double"; break; 178 case 'V': c_name = "void"; break; 179 default: 180 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); 181 } 182 name = String::AllocFromModifiedUtf8(self, c_name); 183 } else { 184 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package 185 // components. 186 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { 187 descriptor.erase(0, 1); 188 descriptor.erase(descriptor.size() - 1); 189 } 190 std::replace(descriptor.begin(), descriptor.end(), '/', '.'); 191 name = String::AllocFromModifiedUtf8(self, descriptor.c_str()); 192 } 193 h_this->SetName(name); 194 return name; 195} 196 197void Class::DumpClass(std::ostream& os, int flags) { 198 if ((flags & kDumpClassFullDetail) == 0) { 199 os << PrettyClass(this); 200 if ((flags & kDumpClassClassLoader) != 0) { 201 os << ' ' << GetClassLoader(); 202 } 203 if ((flags & kDumpClassInitialized) != 0) { 204 os << ' ' << GetStatus(); 205 } 206 os << "\n"; 207 return; 208 } 209 210 Thread* self = Thread::Current(); 211 StackHandleScope<2> hs(self); 212 Handle<mirror::Class> h_this(hs.NewHandle(this)); 213 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass())); 214 215 os << "----- " << (IsInterface() ? "interface" : "class") << " " 216 << "'" << GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", 217 os << " objectSize=" << SizeOf() << " " 218 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n", 219 os << StringPrintf(" access=0x%04x.%04x\n", 220 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); 221 if (h_super.Get() != nullptr) { 222 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader() 223 << ")\n"; 224 } 225 if (IsArrayClass()) { 226 os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; 227 } 228 const size_t num_direct_interfaces = NumDirectInterfaces(); 229 if (num_direct_interfaces > 0) { 230 os << " interfaces (" << num_direct_interfaces << "):\n"; 231 for (size_t i = 0; i < num_direct_interfaces; ++i) { 232 Class* interface = GetDirectInterface(self, h_this, i); 233 const ClassLoader* cl = interface->GetClassLoader(); 234 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); 235 } 236 } 237 if (!IsLoaded()) { 238 os << " class not yet loaded"; 239 } else { 240 // After this point, this may have moved due to GetDirectInterface. 241 os << " vtable (" << h_this->NumVirtualMethods() << " entries, " 242 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n"; 243 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 244 os << StringPrintf(" %2zd: %s\n", i, 245 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str()); 246 } 247 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; 248 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { 249 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str()); 250 } 251 if (h_this->NumStaticFields() > 0) { 252 os << " static fields (" << h_this->NumStaticFields() << " entries):\n"; 253 if (h_this->IsResolved() || h_this->IsErroneous()) { 254 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) { 255 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str()); 256 } 257 } else { 258 os << " <not yet available>"; 259 } 260 } 261 if (h_this->NumInstanceFields() > 0) { 262 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n"; 263 if (h_this->IsResolved() || h_this->IsErroneous()) { 264 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) { 265 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str()); 266 } 267 } else { 268 os << " <not yet available>"; 269 } 270 } 271 } 272} 273 274void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { 275 if (new_reference_offsets != CLASS_WALK_SUPER) { 276 // Sanity check that the number of bits set in the reference offset bitmap 277 // agrees with the number of references 278 size_t count = 0; 279 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { 280 count += c->NumReferenceInstanceFieldsDuringLinking(); 281 } 282 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count); 283 } 284 // Not called within a transaction. 285 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), 286 new_reference_offsets); 287} 288 289void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { 290 if (new_reference_offsets != CLASS_WALK_SUPER) { 291 // Sanity check that the number of bits set in the reference offset bitmap 292 // agrees with the number of references 293 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), 294 NumReferenceStaticFieldsDuringLinking()); 295 } 296 // Not called within a transaction. 297 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), 298 new_reference_offsets); 299} 300 301bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { 302 size_t i = 0; 303 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { 304 ++i; 305 } 306 if (descriptor1.find('/', i) != StringPiece::npos || 307 descriptor2.find('/', i) != StringPiece::npos) { 308 return false; 309 } else { 310 return true; 311 } 312} 313 314bool Class::IsInSamePackage(Class* that) { 315 Class* klass1 = this; 316 Class* klass2 = that; 317 if (klass1 == klass2) { 318 return true; 319 } 320 // Class loaders must match. 321 if (klass1->GetClassLoader() != klass2->GetClassLoader()) { 322 return false; 323 } 324 // Arrays are in the same package when their element classes are. 325 while (klass1->IsArrayClass()) { 326 klass1 = klass1->GetComponentType(); 327 } 328 while (klass2->IsArrayClass()) { 329 klass2 = klass2->GetComponentType(); 330 } 331 // trivial check again for array types 332 if (klass1 == klass2) { 333 return true; 334 } 335 // Compare the package part of the descriptor string. 336 return IsInSamePackage(klass1->GetDescriptor().c_str(), klass2->GetDescriptor().c_str()); 337} 338 339bool Class::IsStringClass() const { 340 return this == String::GetJavaLangString(); 341} 342 343bool Class::IsThrowableClass() { 344 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); 345} 346 347void Class::SetClassLoader(ClassLoader* new_class_loader) { 348 if (Runtime::Current()->IsActiveTransaction()) { 349 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); 350 } else { 351 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); 352 } 353} 354 355ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) { 356 // Check the current class before checking the interfaces. 357 ArtMethod* method = FindDeclaredVirtualMethod(name, signature); 358 if (method != nullptr) { 359 return method; 360 } 361 362 int32_t iftable_count = GetIfTableCount(); 363 IfTable* iftable = GetIfTable(); 364 for (int32_t i = 0; i < iftable_count; ++i) { 365 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); 366 if (method != nullptr) { 367 return method; 368 } 369 } 370 return nullptr; 371} 372 373ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) { 374 // Check the current class before checking the interfaces. 375 ArtMethod* method = FindDeclaredVirtualMethod(name, signature); 376 if (method != nullptr) { 377 return method; 378 } 379 380 int32_t iftable_count = GetIfTableCount(); 381 IfTable* iftable = GetIfTable(); 382 for (int32_t i = 0; i < iftable_count; ++i) { 383 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); 384 if (method != nullptr) { 385 return method; 386 } 387 } 388 return nullptr; 389} 390 391ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 392 // Check the current class before checking the interfaces. 393 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 394 if (method != nullptr) { 395 return method; 396 } 397 398 int32_t iftable_count = GetIfTableCount(); 399 IfTable* iftable = GetIfTable(); 400 for (int32_t i = 0; i < iftable_count; ++i) { 401 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 402 if (method != nullptr) { 403 return method; 404 } 405 } 406 return nullptr; 407} 408 409ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) { 410 for (size_t i = 0; i < NumDirectMethods(); ++i) { 411 ArtMethod* method = GetDirectMethod(i); 412 if (name == method->GetName() && method->GetSignature() == signature) { 413 return method; 414 } 415 } 416 return nullptr; 417} 418 419ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) { 420 for (size_t i = 0; i < NumDirectMethods(); ++i) { 421 ArtMethod* method = GetDirectMethod(i); 422 if (name == method->GetName() && signature == method->GetSignature()) { 423 return method; 424 } 425 } 426 return nullptr; 427} 428 429ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 430 if (GetDexCache() == dex_cache) { 431 for (size_t i = 0; i < NumDirectMethods(); ++i) { 432 ArtMethod* method = GetDirectMethod(i); 433 if (method->GetDexMethodIndex() == dex_method_idx) { 434 return method; 435 } 436 } 437 } 438 return nullptr; 439} 440 441ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) { 442 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 443 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); 444 if (method != nullptr) { 445 return method; 446 } 447 } 448 return nullptr; 449} 450 451ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) { 452 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 453 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); 454 if (method != nullptr) { 455 return method; 456 } 457 } 458 return nullptr; 459} 460 461ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 462 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 463 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); 464 if (method != nullptr) { 465 return method; 466 } 467 } 468 return nullptr; 469} 470 471ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) { 472 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 473 ArtMethod* method = GetVirtualMethod(i); 474 if (name == method->GetName() && method->GetSignature() == signature) { 475 return method; 476 } 477 } 478 return nullptr; 479} 480 481ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) { 482 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 483 ArtMethod* method = GetVirtualMethod(i); 484 if (name == method->GetName() && signature == method->GetSignature()) { 485 return method; 486 } 487 } 488 return nullptr; 489} 490 491ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 492 if (GetDexCache() == dex_cache) { 493 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 494 ArtMethod* method = GetVirtualMethod(i); 495 if (method->GetDexMethodIndex() == dex_method_idx) { 496 return method; 497 } 498 } 499 } 500 return nullptr; 501} 502 503ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) { 504 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 505 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); 506 if (method != nullptr) { 507 return method; 508 } 509 } 510 return nullptr; 511} 512 513ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) { 514 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 515 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); 516 if (method != nullptr) { 517 return method; 518 } 519 } 520 return nullptr; 521} 522 523ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 524 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 525 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 526 if (method != nullptr) { 527 return method; 528 } 529 } 530 return nullptr; 531} 532 533ArtMethod* Class::FindClassInitializer() { 534 for (size_t i = 0; i < NumDirectMethods(); ++i) { 535 ArtMethod* method = GetDirectMethod(i); 536 if (method->IsClassInitializer()) { 537 DCHECK_STREQ(method->GetName(), "<clinit>"); 538 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V"); 539 return method; 540 } 541 } 542 return nullptr; 543} 544 545ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { 546 // Is the field in this class? 547 // Interfaces are not relevant because they can't contain instance fields. 548 for (size_t i = 0; i < NumInstanceFields(); ++i) { 549 ArtField* f = GetInstanceField(i); 550 if (name == f->GetName() && type == f->GetTypeDescriptor()) { 551 return f; 552 } 553 } 554 return nullptr; 555} 556 557ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { 558 if (GetDexCache() == dex_cache) { 559 for (size_t i = 0; i < NumInstanceFields(); ++i) { 560 ArtField* f = GetInstanceField(i); 561 if (f->GetDexFieldIndex() == dex_field_idx) { 562 return f; 563 } 564 } 565 } 566 return nullptr; 567} 568 569ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { 570 // Is the field in this class, or any of its superclasses? 571 // Interfaces are not relevant because they can't contain instance fields. 572 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { 573 ArtField* f = c->FindDeclaredInstanceField(name, type); 574 if (f != nullptr) { 575 return f; 576 } 577 } 578 return nullptr; 579} 580 581ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { 582 // Is the field in this class, or any of its superclasses? 583 // Interfaces are not relevant because they can't contain instance fields. 584 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { 585 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); 586 if (f != nullptr) { 587 return f; 588 } 589 } 590 return nullptr; 591} 592 593ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { 594 DCHECK(type != nullptr); 595 for (size_t i = 0; i < NumStaticFields(); ++i) { 596 ArtField* f = GetStaticField(i); 597 if (name == f->GetName() && type == f->GetTypeDescriptor()) { 598 return f; 599 } 600 } 601 return nullptr; 602} 603 604ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { 605 if (dex_cache == GetDexCache()) { 606 for (size_t i = 0; i < NumStaticFields(); ++i) { 607 ArtField* f = GetStaticField(i); 608 if (f->GetDexFieldIndex() == dex_field_idx) { 609 return f; 610 } 611 } 612 } 613 return nullptr; 614} 615 616ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name, 617 const StringPiece& type) { 618 // Is the field in this class (or its interfaces), or any of its 619 // superclasses (or their interfaces)? 620 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { 621 // Is the field in this class? 622 ArtField* f = k->FindDeclaredStaticField(name, type); 623 if (f != nullptr) { 624 return f; 625 } 626 // Wrap k incase it moves during GetDirectInterface. 627 StackHandleScope<1> hs(self); 628 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); 629 // Is this field in any of this class' interfaces? 630 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { 631 StackHandleScope<1> hs(self); 632 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i))); 633 f = FindStaticField(self, interface, name, type); 634 if (f != nullptr) { 635 return f; 636 } 637 } 638 } 639 return nullptr; 640} 641 642ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache, 643 uint32_t dex_field_idx) { 644 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { 645 // Is the field in this class? 646 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); 647 if (f != nullptr) { 648 return f; 649 } 650 // Wrap k incase it moves during GetDirectInterface. 651 StackHandleScope<1> hs(self); 652 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); 653 // Is this field in any of this class' interfaces? 654 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { 655 StackHandleScope<1> hs(self); 656 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i))); 657 f = FindStaticField(self, interface, dex_cache, dex_field_idx); 658 if (f != nullptr) { 659 return f; 660 } 661 } 662 } 663 return nullptr; 664} 665 666ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name, 667 const StringPiece& type) { 668 // Find a field using the JLS field resolution order 669 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { 670 // Is the field in this class? 671 ArtField* f = k->FindDeclaredInstanceField(name, type); 672 if (f != nullptr) { 673 return f; 674 } 675 f = k->FindDeclaredStaticField(name, type); 676 if (f != nullptr) { 677 return f; 678 } 679 // Is this field in any of this class' interfaces? 680 StackHandleScope<1> hs(self); 681 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); 682 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { 683 StackHandleScope<1> hs(self); 684 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i))); 685 f = interface->FindStaticField(self, interface, name, type); 686 if (f != nullptr) { 687 return f; 688 } 689 } 690 } 691 return nullptr; 692} 693 694static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods) 695 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 696 if (methods != nullptr) { 697 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) { 698 mirror::ArtMethod* method = methods->GetWithoutChecks(index); 699 DCHECK(method != nullptr); 700 if (!method->IsNative() && !method->IsAbstract()) { 701 method->SetPreverified(); 702 } 703 } 704 } 705} 706 707void Class::SetPreverifiedFlagOnAllMethods() { 708 DCHECK(IsVerified()); 709 SetPreverifiedFlagOnMethods(GetDirectMethods()); 710 SetPreverifiedFlagOnMethods(GetVirtualMethods()); 711} 712 713std::string Class::GetDescriptor() { 714 if (UNLIKELY(IsArrayClass())) { 715 return GetArrayDescriptor(); 716 } else if (UNLIKELY(IsPrimitive())) { 717 return Primitive::Descriptor(GetPrimitiveType()); 718 } else if (UNLIKELY(IsProxyClass())) { 719 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); 720 } else { 721 const DexFile& dex_file = GetDexFile(); 722 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); 723 return dex_file.GetTypeDescriptor(type_id); 724 } 725} 726 727std::string Class::GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 728 return "[" + GetComponentType()->GetDescriptor(); 729} 730 731const DexFile::ClassDef* Class::GetClassDef() { 732 uint16_t class_def_idx = GetDexClassDefIndex(); 733 if (class_def_idx == DexFile::kDexNoIndex16) { 734 return nullptr; 735 } 736 return &GetDexFile().GetClassDef(class_def_idx); 737} 738 739uint32_t Class::NumDirectInterfaces() { 740 if (IsPrimitive()) { 741 return 0; 742 } else if (IsArrayClass()) { 743 return 2; 744 } else if (IsProxyClass()) { 745 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces(); 746 return interfaces != nullptr ? interfaces->GetLength() : 0; 747 } else { 748 const DexFile::TypeList* interfaces = GetInterfaceTypeList(); 749 if (interfaces == nullptr) { 750 return 0; 751 } else { 752 return interfaces->Size(); 753 } 754 } 755} 756 757uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) { 758 DCHECK(!IsPrimitive()); 759 DCHECK(!IsArrayClass()); 760 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; 761} 762 763mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) { 764 DCHECK(klass.Get() != nullptr); 765 DCHECK(!klass->IsPrimitive()); 766 if (klass->IsArrayClass()) { 767 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 768 if (idx == 0) { 769 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;"); 770 } else { 771 DCHECK_EQ(1U, idx); 772 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;"); 773 } 774 } else if (klass->IsProxyClass()) { 775 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces(); 776 DCHECK(interfaces != nullptr); 777 return interfaces->Get(idx); 778 } else { 779 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx); 780 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx); 781 if (interface == nullptr) { 782 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx, 783 klass.Get()); 784 CHECK(interface != nullptr || self->IsExceptionPending()); 785 } 786 return interface; 787 } 788} 789 790const char* Class::GetSourceFile() { 791 std::string descriptor(GetDescriptor()); 792 const DexFile& dex_file = GetDexFile(); 793 const DexFile::ClassDef* dex_class_def = GetClassDef(); 794 if (dex_class_def == nullptr) { 795 // Generated classes have no class def. 796 return nullptr; 797 } 798 return dex_file.GetSourceFile(*dex_class_def); 799} 800 801std::string Class::GetLocation() { 802 mirror::DexCache* dex_cache = GetDexCache(); 803 if (dex_cache != nullptr && !IsProxyClass()) { 804 return dex_cache->GetLocation()->ToModifiedUtf8(); 805 } 806 // Arrays and proxies are generated and have no corresponding dex file location. 807 return "generated class"; 808} 809 810const DexFile::TypeList* Class::GetInterfaceTypeList() { 811 const DexFile::ClassDef* class_def = GetClassDef(); 812 if (class_def == nullptr) { 813 return nullptr; 814 } 815 return GetDexFile().GetInterfacesList(*class_def); 816} 817 818void Class::PopulateEmbeddedImtAndVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 819 ObjectArray<ArtMethod>* table = GetImTable(); 820 if (table != nullptr) { 821 for (uint32_t i = 0; i < kImtSize; i++) { 822 SetEmbeddedImTableEntry(i, table->Get(i)); 823 } 824 } 825 826 table = GetVTableDuringLinking(); 827 CHECK(table != nullptr); 828 for (int32_t i = 0; i < table->GetLength(); i++) { 829 SetEmbeddedVTableEntry(i, table->Get(i)); 830 } 831} 832 833Class* Class::CopyOf(Thread* self, int32_t new_length) { 834 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); 835 // We may get copied by a compacting GC. 836 StackHandleScope<1> hs(self); 837 Handle<mirror::Class> h_this(hs.NewHandle(this)); 838 gc::Heap* heap = Runtime::Current()->GetHeap(); 839 InitializeClassVisitor visitor(new_length); 840 841 mirror::Object* new_class = 842 kMovingClasses ? heap->AllocObject<true>(self, java_lang_Class_, new_length, visitor) 843 : heap->AllocNonMovableObject<true>(self, java_lang_Class_, new_length, visitor); 844 if (UNLIKELY(new_class == nullptr)) { 845 CHECK(self->IsExceptionPending()); // Expect an OOME. 846 return NULL; 847 } 848 849 mirror::Class* new_class_obj = new_class->AsClass(); 850 memcpy(new_class_obj, h_this.Get(), sizeof(Class)); 851 852 new_class_obj->SetStatus(kStatusResolving, self); 853 new_class_obj->PopulateEmbeddedImtAndVTable(); 854 // Correct some fields. 855 new_class_obj->SetLockWord(LockWord(), false); 856 new_class_obj->SetClassSize(new_length); 857 858 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class_obj); 859 return new_class_obj; 860} 861 862} // namespace mirror 863} // namespace art 864