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