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