class.cc revision 575e78c41ece0dec969d31f46be563d4eb7ae43b
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} 153 154void Class::SetClassSize(uint32_t new_class_size) { 155 if (kIsDebugBuild && (new_class_size < GetClassSize())) { 156 DumpClass(LOG(ERROR), kDumpClassFullDetail); 157 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); 158 } 159 // Not called within a transaction. 160 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); 161} 162 163// Return the class' name. The exact format is bizarre, but it's the specified behavior for 164// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" 165// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than 166// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. 167String* Class::ComputeName(Handle<Class> h_this) { 168 String* name = h_this->GetName(); 169 if (name != nullptr) { 170 return name; 171 } 172 std::string temp; 173 const char* descriptor = h_this->GetDescriptor(&temp); 174 Thread* self = Thread::Current(); 175 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { 176 // The descriptor indicates that this is the class for 177 // a primitive type; special-case the return value. 178 const char* c_name = nullptr; 179 switch (descriptor[0]) { 180 case 'Z': c_name = "boolean"; break; 181 case 'B': c_name = "byte"; break; 182 case 'C': c_name = "char"; break; 183 case 'S': c_name = "short"; break; 184 case 'I': c_name = "int"; break; 185 case 'J': c_name = "long"; break; 186 case 'F': c_name = "float"; break; 187 case 'D': c_name = "double"; break; 188 case 'V': c_name = "void"; break; 189 default: 190 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); 191 } 192 name = String::AllocFromModifiedUtf8(self, c_name); 193 } else { 194 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package 195 // components. 196 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str()); 197 } 198 h_this->SetName(name); 199 return name; 200} 201 202void Class::DumpClass(std::ostream& os, int flags) { 203 if ((flags & kDumpClassFullDetail) == 0) { 204 os << PrettyClass(this); 205 if ((flags & kDumpClassClassLoader) != 0) { 206 os << ' ' << GetClassLoader(); 207 } 208 if ((flags & kDumpClassInitialized) != 0) { 209 os << ' ' << GetStatus(); 210 } 211 os << "\n"; 212 return; 213 } 214 215 Thread* self = Thread::Current(); 216 StackHandleScope<2> hs(self); 217 Handle<mirror::Class> h_this(hs.NewHandle(this)); 218 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass())); 219 220 std::string temp; 221 os << "----- " << (IsInterface() ? "interface" : "class") << " " 222 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n", 223 os << " objectSize=" << SizeOf() << " " 224 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n", 225 os << StringPrintf(" access=0x%04x.%04x\n", 226 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); 227 if (h_super.Get() != nullptr) { 228 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader() 229 << ")\n"; 230 } 231 if (IsArrayClass()) { 232 os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; 233 } 234 const size_t num_direct_interfaces = NumDirectInterfaces(); 235 if (num_direct_interfaces > 0) { 236 os << " interfaces (" << num_direct_interfaces << "):\n"; 237 for (size_t i = 0; i < num_direct_interfaces; ++i) { 238 Class* interface = GetDirectInterface(self, h_this, i); 239 const ClassLoader* cl = interface->GetClassLoader(); 240 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); 241 } 242 } 243 if (!IsLoaded()) { 244 os << " class not yet loaded"; 245 } else { 246 // After this point, this may have moved due to GetDirectInterface. 247 os << " vtable (" << h_this->NumVirtualMethods() << " entries, " 248 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n"; 249 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 250 os << StringPrintf(" %2zd: %s\n", i, 251 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str()); 252 } 253 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; 254 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { 255 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str()); 256 } 257 if (h_this->NumStaticFields() > 0) { 258 os << " static fields (" << h_this->NumStaticFields() << " entries):\n"; 259 if (h_this->IsResolved() || h_this->IsErroneous()) { 260 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) { 261 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str()); 262 } 263 } else { 264 os << " <not yet available>"; 265 } 266 } 267 if (h_this->NumInstanceFields() > 0) { 268 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n"; 269 if (h_this->IsResolved() || h_this->IsErroneous()) { 270 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) { 271 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str()); 272 } 273 } else { 274 os << " <not yet available>"; 275 } 276 } 277 } 278} 279 280void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { 281 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) { 282 // Sanity check that the number of bits set in the reference offset bitmap 283 // agrees with the number of references 284 uint32_t count = 0; 285 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { 286 count += c->NumReferenceInstanceFieldsDuringLinking(); 287 } 288 // +1 for the Class in Object. 289 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count); 290 } 291 // Not called within a transaction. 292 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), 293 new_reference_offsets); 294} 295 296bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { 297 size_t i = 0; 298 size_t min_length = std::min(descriptor1.size(), descriptor2.size()); 299 while (i < min_length && descriptor1[i] == descriptor2[i]) { 300 ++i; 301 } 302 if (descriptor1.find('/', i) != StringPiece::npos || 303 descriptor2.find('/', i) != StringPiece::npos) { 304 return false; 305 } else { 306 return true; 307 } 308} 309 310bool Class::IsInSamePackage(Class* that) { 311 Class* klass1 = this; 312 Class* klass2 = that; 313 if (klass1 == klass2) { 314 return true; 315 } 316 // Class loaders must match. 317 if (klass1->GetClassLoader() != klass2->GetClassLoader()) { 318 return false; 319 } 320 // Arrays are in the same package when their element classes are. 321 while (klass1->IsArrayClass()) { 322 klass1 = klass1->GetComponentType(); 323 } 324 while (klass2->IsArrayClass()) { 325 klass2 = klass2->GetComponentType(); 326 } 327 // trivial check again for array types 328 if (klass1 == klass2) { 329 return true; 330 } 331 // Compare the package part of the descriptor string. 332 std::string temp1, temp2; 333 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2)); 334} 335 336bool Class::IsStringClass() const { 337 return this == String::GetJavaLangString(); 338} 339 340bool Class::IsThrowableClass() { 341 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); 342} 343 344void Class::SetClassLoader(ClassLoader* new_class_loader) { 345 if (Runtime::Current()->IsActiveTransaction()) { 346 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); 347 } else { 348 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); 349 } 350} 351 352ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) { 353 // Check the current class before checking the interfaces. 354 ArtMethod* method = FindDeclaredVirtualMethod(name, signature); 355 if (method != nullptr) { 356 return method; 357 } 358 359 int32_t iftable_count = GetIfTableCount(); 360 IfTable* iftable = GetIfTable(); 361 for (int32_t i = 0; i < iftable_count; ++i) { 362 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); 363 if (method != nullptr) { 364 return method; 365 } 366 } 367 return nullptr; 368} 369 370ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) { 371 // Check the current class before checking the interfaces. 372 ArtMethod* method = FindDeclaredVirtualMethod(name, signature); 373 if (method != nullptr) { 374 return method; 375 } 376 377 int32_t iftable_count = GetIfTableCount(); 378 IfTable* iftable = GetIfTable(); 379 for (int32_t i = 0; i < iftable_count; ++i) { 380 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); 381 if (method != nullptr) { 382 return method; 383 } 384 } 385 return nullptr; 386} 387 388ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 389 // Check the current class before checking the interfaces. 390 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 391 if (method != nullptr) { 392 return method; 393 } 394 395 int32_t iftable_count = GetIfTableCount(); 396 IfTable* iftable = GetIfTable(); 397 for (int32_t i = 0; i < iftable_count; ++i) { 398 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); 399 if (method != nullptr) { 400 return method; 401 } 402 } 403 return nullptr; 404} 405 406ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) { 407 for (size_t i = 0; i < NumDirectMethods(); ++i) { 408 ArtMethod* method = GetDirectMethod(i); 409 if (name == method->GetName() && method->GetSignature() == signature) { 410 return method; 411 } 412 } 413 return nullptr; 414} 415 416ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) { 417 for (size_t i = 0; i < NumDirectMethods(); ++i) { 418 ArtMethod* method = GetDirectMethod(i); 419 if (name == method->GetName() && signature == method->GetSignature()) { 420 return method; 421 } 422 } 423 return nullptr; 424} 425 426ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 427 if (GetDexCache() == dex_cache) { 428 for (size_t i = 0; i < NumDirectMethods(); ++i) { 429 ArtMethod* method = GetDirectMethod(i); 430 if (method->GetDexMethodIndex() == dex_method_idx) { 431 return method; 432 } 433 } 434 } 435 return nullptr; 436} 437 438ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) { 439 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 440 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); 441 if (method != nullptr) { 442 return method; 443 } 444 } 445 return nullptr; 446} 447 448ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) { 449 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 450 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); 451 if (method != nullptr) { 452 return method; 453 } 454 } 455 return nullptr; 456} 457 458ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 459 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { 460 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); 461 if (method != nullptr) { 462 return method; 463 } 464 } 465 return nullptr; 466} 467 468ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) { 469 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 470 ArtMethod* method = GetVirtualMethod(i); 471 if (name == method->GetName() && method->GetSignature() == signature) { 472 return method; 473 } 474 } 475 return nullptr; 476} 477 478ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) { 479 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 480 ArtMethod* method = GetVirtualMethod(i); 481 if (name == method->GetName() && signature == method->GetSignature()) { 482 return method; 483 } 484 } 485 return nullptr; 486} 487 488ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { 489 if (GetDexCache() == dex_cache) { 490 for (size_t i = 0; i < NumVirtualMethods(); ++i) { 491 ArtMethod* method = GetVirtualMethod(i); 492 if (method->GetDexMethodIndex() == dex_method_idx && 493 // A miranda method may have a different DexCache and is always created by linking, 494 // never *declared* in the class. 495 !method->IsMiranda()) { 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 713const char* Class::GetDescriptor(std::string* storage) { 714 if (IsPrimitive()) { 715 return Primitive::Descriptor(GetPrimitiveType()); 716 } else if (IsArrayClass()) { 717 return GetArrayDescriptor(storage); 718 } else if (IsProxyClass()) { 719 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); 720 return storage->c_str(); 721 } else { 722 const DexFile& dex_file = GetDexFile(); 723 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); 724 return dex_file.GetTypeDescriptor(type_id); 725 } 726} 727 728const char* Class::GetArrayDescriptor(std::string* storage) { 729 std::string temp; 730 const char* elem_desc = GetComponentType()->GetDescriptor(&temp); 731 *storage = "["; 732 *storage += elem_desc; 733 return storage->c_str(); 734} 735 736const DexFile::ClassDef* Class::GetClassDef() { 737 uint16_t class_def_idx = GetDexClassDefIndex(); 738 if (class_def_idx == DexFile::kDexNoIndex16) { 739 return nullptr; 740 } 741 return &GetDexFile().GetClassDef(class_def_idx); 742} 743 744uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) { 745 DCHECK(!IsPrimitive()); 746 DCHECK(!IsArrayClass()); 747 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; 748} 749 750mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, 751 uint32_t idx) { 752 DCHECK(klass.Get() != nullptr); 753 DCHECK(!klass->IsPrimitive()); 754 if (klass->IsArrayClass()) { 755 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 756 if (idx == 0) { 757 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;"); 758 } else { 759 DCHECK_EQ(1U, idx); 760 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;"); 761 } 762 } else if (klass->IsProxyClass()) { 763 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces(); 764 DCHECK(interfaces != nullptr); 765 return interfaces->Get(idx); 766 } else { 767 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx); 768 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx); 769 if (interface == nullptr) { 770 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx, 771 klass.Get()); 772 CHECK(interface != nullptr || self->IsExceptionPending()); 773 } 774 return interface; 775 } 776} 777 778const char* Class::GetSourceFile() { 779 const DexFile& dex_file = GetDexFile(); 780 const DexFile::ClassDef* dex_class_def = GetClassDef(); 781 if (dex_class_def == nullptr) { 782 // Generated classes have no class def. 783 return nullptr; 784 } 785 return dex_file.GetSourceFile(*dex_class_def); 786} 787 788std::string Class::GetLocation() { 789 mirror::DexCache* dex_cache = GetDexCache(); 790 if (dex_cache != nullptr && !IsProxyClass()) { 791 return dex_cache->GetLocation()->ToModifiedUtf8(); 792 } 793 // Arrays and proxies are generated and have no corresponding dex file location. 794 return "generated class"; 795} 796 797const DexFile::TypeList* Class::GetInterfaceTypeList() { 798 const DexFile::ClassDef* class_def = GetClassDef(); 799 if (class_def == nullptr) { 800 return nullptr; 801 } 802 return GetDexFile().GetInterfacesList(*class_def); 803} 804 805void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) { 806 for (uint32_t i = 0; i < kImtSize; i++) { 807 // Replace null with conflict. 808 mirror::Object* obj = imt_handle_scope->GetReference(i); 809 DCHECK(obj != nullptr); 810 SetEmbeddedImTableEntry(i, obj->AsArtMethod()); 811 } 812 813 ObjectArray<ArtMethod>* table = GetVTableDuringLinking(); 814 CHECK(table != nullptr) << PrettyClass(this); 815 SetEmbeddedVTableLength(table->GetLength()); 816 for (int32_t i = 0; i < table->GetLength(); i++) { 817 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i)); 818 } 819 820 // Keep java.lang.Object class's vtable around for since it's easier 821 // to be reused by array classes during their linking. 822 if (!IsObjectClass()) { 823 SetVTable(nullptr); 824 } 825} 826 827// The pre-fence visitor for Class::CopyOf(). 828class CopyClassVisitor { 829 public: 830 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, 831 size_t new_length, size_t copy_bytes, 832 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope) 833 : self_(self), orig_(orig), new_length_(new_length), 834 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) { 835 } 836 837 void operator()(Object* obj, size_t usable_size) const 838 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 839 UNUSED(usable_size); 840 mirror::Class* new_class_obj = obj->AsClass(); 841 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_); 842 new_class_obj->SetStatus(Class::kStatusResolving, self_); 843 new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_); 844 new_class_obj->SetClassSize(new_length_); 845 } 846 847 private: 848 Thread* const self_; 849 Handle<mirror::Class>* const orig_; 850 const size_t new_length_; 851 const size_t copy_bytes_; 852 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_; 853 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); 854}; 855 856Class* Class::CopyOf(Thread* self, int32_t new_length, 857 StackHandleScope<kImtSize>* imt_handle_scope) { 858 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); 859 // We may get copied by a compacting GC. 860 StackHandleScope<1> hs(self); 861 Handle<mirror::Class> h_this(hs.NewHandle(this)); 862 gc::Heap* heap = Runtime::Current()->GetHeap(); 863 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() 864 // to skip copying the tail part that we will overwrite here. 865 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope); 866 mirror::Object* new_class = 867 kMovingClasses 868 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) 869 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor); 870 if (UNLIKELY(new_class == nullptr)) { 871 CHECK(self->IsExceptionPending()); // Expect an OOME. 872 return nullptr; 873 } 874 return new_class->AsClass(); 875} 876 877} // namespace mirror 878} // namespace art 879