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