< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page

  52 #include "jvmtifiles/jvmti.h"
  53 #include "logging/log.hpp"
  54 #include "klass.inline.hpp"
  55 #include "logging/logMessage.hpp"
  56 #include "logging/logStream.hpp"
  57 #include "memory/allocation.inline.hpp"
  58 #include "memory/iterator.inline.hpp"
  59 #include "memory/metadataFactory.hpp"
  60 #include "memory/metaspaceClosure.hpp"
  61 #include "memory/oopFactory.hpp"
  62 #include "memory/resourceArea.hpp"
  63 #include "memory/universe.hpp"
  64 #include "oops/fieldStreams.inline.hpp"
  65 #include "oops/constantPool.hpp"
  66 #include "oops/instanceClassLoaderKlass.hpp"
  67 #include "oops/instanceKlass.inline.hpp"
  68 #include "oops/instanceMirrorKlass.hpp"
  69 #include "oops/instanceOop.hpp"
  70 #include "oops/instanceStackChunkKlass.hpp"
  71 #include "oops/klass.inline.hpp"

  72 #include "oops/method.hpp"
  73 #include "oops/oop.inline.hpp"
  74 #include "oops/recordComponent.hpp"
  75 #include "oops/symbol.hpp"

  76 #include "prims/jvmtiExport.hpp"
  77 #include "prims/jvmtiRedefineClasses.hpp"
  78 #include "prims/jvmtiThreadState.hpp"
  79 #include "prims/methodComparator.hpp"
  80 #include "runtime/arguments.hpp"
  81 #include "runtime/deoptimization.hpp"
  82 #include "runtime/atomic.hpp"
  83 #include "runtime/fieldDescriptor.inline.hpp"
  84 #include "runtime/handles.inline.hpp"
  85 #include "runtime/javaCalls.hpp"
  86 #include "runtime/javaThread.inline.hpp"
  87 #include "runtime/mutexLocker.hpp"
  88 #include "runtime/orderAccess.hpp"
  89 #include "runtime/os.inline.hpp"
  90 #include "runtime/reflection.hpp"
  91 #include "runtime/synchronizer.hpp"
  92 #include "runtime/threads.hpp"
  93 #include "services/classLoadingService.hpp"
  94 #include "services/finalizerService.hpp"
  95 #include "services/threadService.hpp"

 133 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)     \
 134   {                                                              \
 135     char* data = nullptr;                                        \
 136     int len = 0;                                                 \
 137     Symbol* clss_name = name();                                  \
 138     if (clss_name != nullptr) {                                  \
 139       data = (char*)clss_name->bytes();                          \
 140       len = clss_name->utf8_length();                            \
 141     }                                                            \
 142     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
 143       data, len, (void*)class_loader(), thread_type, wait);      \
 144   }
 145 
 146 #else //  ndef DTRACE_ENABLED
 147 
 148 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
 149 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
 150 
 151 #endif //  ndef DTRACE_ENABLED
 152 





 153 bool InstanceKlass::_finalization_enabled = true;
 154 
 155 static inline bool is_class_loader(const Symbol* class_name,
 156                                    const ClassFileParser& parser) {
 157   assert(class_name != nullptr, "invariant");
 158 
 159   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 160     return true;
 161   }
 162 
 163   if (vmClasses::ClassLoader_klass_loaded()) {
 164     const Klass* const super_klass = parser.super_klass();
 165     if (super_klass != nullptr) {
 166       if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
 167         return true;
 168       }
 169     }
 170   }
 171   return false;
 172 }
 173 













 174 static inline bool is_stack_chunk_class(const Symbol* class_name,
 175                                         const ClassLoaderData* loader_data) {
 176   return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
 177           loader_data->is_the_null_class_loader_data());
 178 }
 179 
 180 // private: called to verify that k is a static member of this nest.
 181 // We know that k is an instance class in the same package and hence the
 182 // same classloader.
 183 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
 184   assert(!is_hidden(), "unexpected hidden class");
 185   if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
 186     if (log_is_enabled(Trace, class, nestmates)) {
 187       ResourceMark rm(current);
 188       log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
 189                                   k->external_name(), this->external_name());
 190     }
 191     return false;
 192   }
 193 

 448 }
 449 
 450 const char* InstanceKlass::nest_host_error() {
 451   if (_nest_host_index == 0) {
 452     return nullptr;
 453   } else {
 454     constantPoolHandle cph(Thread::current(), constants());
 455     return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
 456   }
 457 }
 458 
 459 void* InstanceKlass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size,
 460                                   bool use_class_space, TRAPS) throw() {
 461   return Metaspace::allocate(loader_data, word_size, ClassType, use_class_space, THREAD);
 462 }
 463 
 464 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 465   const int size = InstanceKlass::size(parser.vtable_size(),
 466                                        parser.itable_size(),
 467                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 468                                        parser.is_interface());

 469 
 470   const Symbol* const class_name = parser.class_name();
 471   assert(class_name != nullptr, "invariant");
 472   ClassLoaderData* loader_data = parser.loader_data();
 473   assert(loader_data != nullptr, "invariant");
 474 
 475   InstanceKlass* ik;
 476   const bool use_class_space = parser.klass_needs_narrow_id();
 477 
 478   // Allocation
 479   if (parser.is_instance_ref_klass()) {
 480     // java.lang.ref.Reference
 481     ik = new (loader_data, size, use_class_space, THREAD) InstanceRefKlass(parser);
 482   } else if (class_name == vmSymbols::java_lang_Class()) {
 483     // mirror - java.lang.Class
 484     ik = new (loader_data, size, use_class_space, THREAD) InstanceMirrorKlass(parser);
 485   } else if (is_stack_chunk_class(class_name, loader_data)) {
 486     // stack chunk
 487     ik = new (loader_data, size, use_class_space, THREAD) InstanceStackChunkKlass(parser);
 488   } else if (is_class_loader(class_name, parser)) {
 489     // class loader - java.lang.ClassLoader
 490     ik = new (loader_data, size, use_class_space, THREAD) InstanceClassLoaderKlass(parser);



 491   } else {
 492     // normal
 493     ik = new (loader_data, size, use_class_space, THREAD) InstanceKlass(parser);
 494   }
 495 
 496   if (ik != nullptr && UseCompressedClassPointers && use_class_space) {
 497     assert(CompressedKlassPointers::is_encodable(ik),
 498            "Klass " PTR_FORMAT "needs a narrow Klass ID, but is not encodable", p2i(ik));
 499   }
 500 
 501   // Check for pending exception before adding to the loader data and incrementing
 502   // class count.  Can get OOM here.
 503   if (HAS_PENDING_EXCEPTION) {
 504     return nullptr;
 505   }
 506 






 507   return ik;
 508 }
 509 























 510 
 511 // copy method ordering from resource area to Metaspace
 512 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 513   if (m != nullptr) {
 514     // allocate a new array and copy contents (memcpy?)
 515     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 516     for (int i = 0; i < m->length(); i++) {
 517       _method_ordering->at_put(i, m->at(i));
 518     }
 519   } else {
 520     _method_ordering = Universe::the_empty_int_array();
 521   }
 522 }
 523 
 524 // create a new array of vtable_indices for default methods
 525 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 526   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 527   assert(default_vtable_indices() == nullptr, "only create once");
 528   set_default_vtable_indices(vtable_indices);
 529   return vtable_indices;
 530 }
 531 
 532 
 533 InstanceKlass::InstanceKlass() {
 534   assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
 535 }
 536 
 537 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
 538   Klass(kind),
 539   _nest_members(nullptr),
 540   _nest_host(nullptr),
 541   _permitted_subclasses(nullptr),
 542   _record_components(nullptr),
 543   _static_field_size(parser.static_field_size()),
 544   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 545   _itable_len(parser.itable_size()),
 546   _nest_host_index(0),
 547   _init_state(allocated),
 548   _reference_type(reference_type),
 549   _init_thread(nullptr)



 550 {
 551   set_vtable_length(parser.vtable_size());
 552   set_access_flags(parser.access_flags());
 553   if (parser.is_hidden()) set_is_hidden();
 554   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 555                                                     false));



 556 
 557   assert(nullptr == _methods, "underlying memory not zeroed?");
 558   assert(is_instance_klass(), "is layout incorrect?");
 559   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 560 }
 561 
 562 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 563                                        Array<Method*>* methods) {
 564   if (methods != nullptr && methods != Universe::the_empty_method_array() &&
 565       !methods->is_shared()) {
 566     for (int i = 0; i < methods->length(); i++) {
 567       Method* method = methods->at(i);
 568       if (method == nullptr) continue;  // maybe null if error processing
 569       // Only want to delete methods that are not executing for RedefineClasses.
 570       // The previous version will point to them so they're not totally dangling
 571       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 572       MetadataFactory::free_metadata(loader_data, method);
 573     }
 574     MetadataFactory::free_array<Method*>(loader_data, methods);
 575   }

 675       (address)(secondary_supers()) != (address)(transitive_interfaces()) &&
 676       !secondary_supers()->is_shared()) {
 677     MetadataFactory::free_array<Klass*>(loader_data, secondary_supers());
 678   }
 679   set_secondary_supers(nullptr, SECONDARY_SUPERS_BITMAP_EMPTY);
 680 
 681   deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
 682   set_transitive_interfaces(nullptr);
 683   set_local_interfaces(nullptr);
 684 
 685   if (fieldinfo_stream() != nullptr && !fieldinfo_stream()->is_shared()) {
 686     MetadataFactory::free_array<u1>(loader_data, fieldinfo_stream());
 687   }
 688   set_fieldinfo_stream(nullptr);
 689 
 690   if (fields_status() != nullptr && !fields_status()->is_shared()) {
 691     MetadataFactory::free_array<FieldStatus>(loader_data, fields_status());
 692   }
 693   set_fields_status(nullptr);
 694 





 695   // If a method from a redefined class is using this constant pool, don't
 696   // delete it, yet.  The new class's previous version will point to this.
 697   if (constants() != nullptr) {
 698     assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
 699     if (!constants()->is_shared()) {
 700       MetadataFactory::free_metadata(loader_data, constants());
 701     }
 702     // Delete any cached resolution errors for the constant pool
 703     SystemDictionary::delete_resolution_error(constants());
 704 
 705     set_constants(nullptr);
 706   }
 707 
 708   if (inner_classes() != nullptr &&
 709       inner_classes() != Universe::the_empty_short_array() &&
 710       !inner_classes()->is_shared()) {
 711     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 712   }
 713   set_inner_classes(nullptr);
 714 
 715   if (nest_members() != nullptr &&
 716       nest_members() != Universe::the_empty_short_array() &&
 717       !nest_members()->is_shared()) {
 718     MetadataFactory::free_array<jushort>(loader_data, nest_members());
 719   }
 720   set_nest_members(nullptr);
 721 
 722   if (permitted_subclasses() != nullptr &&
 723       permitted_subclasses() != Universe::the_empty_short_array() &&
 724       !permitted_subclasses()->is_shared()) {
 725     MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
 726   }
 727   set_permitted_subclasses(nullptr);
 728 







 729   // We should deallocate the Annotations instance if it's not in shared spaces.
 730   if (annotations() != nullptr && !annotations()->is_shared()) {
 731     MetadataFactory::free_metadata(loader_data, annotations());
 732   }
 733   set_annotations(nullptr);
 734 
 735   SystemDictionaryShared::handle_class_unloading(this);
 736 
 737 #if INCLUDE_CDS_JAVA_HEAP
 738   if (CDSConfig::is_dumping_heap()) {
 739     HeapShared::remove_scratch_objects(this);
 740   }
 741 #endif
 742 }
 743 
 744 bool InstanceKlass::is_record() const {
 745   return _record_components != nullptr &&
 746          is_final() &&
 747          java_super() == vmClasses::Record_klass();
 748 }

 943         vmSymbols::java_lang_IncompatibleClassChangeError(),
 944         "class %s has interface %s as super class",
 945         external_name(),
 946         super_klass->external_name()
 947       );
 948       return false;
 949     }
 950 
 951     InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
 952     ik_super->link_class_impl(CHECK_false);
 953   }
 954 
 955   // link all interfaces implemented by this class before linking this class
 956   Array<InstanceKlass*>* interfaces = local_interfaces();
 957   int num_interfaces = interfaces->length();
 958   for (int index = 0; index < num_interfaces; index++) {
 959     InstanceKlass* interk = interfaces->at(index);
 960     interk->link_class_impl(CHECK_false);
 961   }
 962 










































































































 963   // in case the class is linked in the process of linking its superclasses
 964   if (is_linked()) {
 965     return true;
 966   }
 967 
 968   // trace only the link time for this klass that includes
 969   // the verification time
 970   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 971                              ClassLoader::perf_class_link_selftime(),
 972                              ClassLoader::perf_classes_linked(),
 973                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 974                              jt->get_thread_stat()->perf_timers_addr(),
 975                              PerfClassTraceTime::CLASS_LINK);
 976 
 977   // verification & rewriting
 978   {
 979     HandleMark hm(THREAD);
 980     Handle h_init_lock(THREAD, init_lock());
 981     ObjectLocker ol(h_init_lock, jt);
 982     // rewritten will have been set if loader constraint error found

1247       ss.print("Could not initialize class %s", external_name());
1248       if (cause.is_null()) {
1249         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1250       } else {
1251         THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1252                         ss.as_string(), cause);
1253       }
1254     } else {
1255 
1256       // Step 6
1257       set_init_state(being_initialized);
1258       set_init_thread(jt);
1259       if (debug_logging_enabled) {
1260         ResourceMark rm(jt);
1261         log_debug(class, init)("Thread \"%s\" is initializing %s",
1262                                jt->name(), external_name());
1263       }
1264     }
1265   }
1266 





















1267   // Step 7
1268   // Next, if C is a class rather than an interface, initialize it's super class and super
1269   // interfaces.
1270   if (!is_interface()) {
1271     Klass* super_klass = super();
1272     if (super_klass != nullptr && super_klass->should_be_initialized()) {
1273       super_klass->initialize(THREAD);
1274     }
1275     // If C implements any interface that declares a non-static, concrete method,
1276     // the initialization of C triggers initialization of its super interfaces.
1277     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1278     // having a superinterface that declares, non-static, concrete methods
1279     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1280       initialize_super_interfaces(THREAD);
1281     }
1282 
1283     // If any exceptions, complete abruptly, throwing the same exception as above.
1284     if (HAS_PENDING_EXCEPTION) {
1285       Handle e(THREAD, PENDING_EXCEPTION);
1286       CLEAR_PENDING_EXCEPTION;
1287       {
1288         EXCEPTION_MARK;
1289         add_initialization_error(THREAD, e);
1290         // Locks object, set state, and notify all waiting threads
1291         set_initialization_state_and_notify(initialization_error, THREAD);
1292         CLEAR_PENDING_EXCEPTION;
1293       }
1294       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1295       THROW_OOP(e());
1296     }
1297   }
1298 
1299 
1300   // Step 8
1301   {
1302     DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1303     if (class_initializer() != nullptr) {
1304       // Timer includes any side effects of class initialization (resolution,
1305       // etc), but not recursive entry into call_class_initializer().
1306       PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1307                                ClassLoader::perf_class_init_selftime(),
1308                                ClassLoader::perf_classes_inited(),
1309                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1310                                jt->get_thread_stat()->perf_timers_addr(),
1311                                PerfClassTraceTime::CLASS_CLINIT);
1312       call_class_initializer(THREAD);
1313     } else {
1314       // The elapsed time is so small it's not worth counting.
1315       if (UsePerfData) {
1316         ClassLoader::perf_classes_inited()->inc();
1317       }
1318       call_class_initializer(THREAD);
1319     }

1604     ResourceMark rm(THREAD);
1605     THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1606               : vmSymbols::java_lang_InstantiationException(), external_name());
1607   }
1608   if (this == vmClasses::Class_klass()) {
1609     ResourceMark rm(THREAD);
1610     THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1611               : vmSymbols::java_lang_IllegalAccessException(), external_name());
1612   }
1613 }
1614 
1615 ArrayKlass* InstanceKlass::array_klass(int n, TRAPS) {
1616   // Need load-acquire for lock-free read
1617   if (array_klasses_acquire() == nullptr) {
1618 
1619     // Recursively lock array allocation
1620     RecursiveLocker rl(MultiArray_lock, THREAD);
1621 
1622     // Check if another thread created the array klass while we were waiting for the lock.
1623     if (array_klasses() == nullptr) {
1624       ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
1625       // use 'release' to pair with lock-free load
1626       release_set_array_klasses(k);
1627     }
1628   }
1629 
1630   // array_klasses() will always be set at this point
1631   ObjArrayKlass* ak = array_klasses();
1632   assert(ak != nullptr, "should be set");
1633   return ak->array_klass(n, THREAD);
1634 }
1635 
1636 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1637   // Need load-acquire for lock-free read
1638   ObjArrayKlass* oak = array_klasses_acquire();
1639   if (oak == nullptr) {
1640     return nullptr;
1641   } else {
1642     return oak->array_klass_or_null(n);
1643   }
1644 }
1645 
1646 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1647   return array_klass(1, THREAD);
1648 }
1649 
1650 ArrayKlass* InstanceKlass::array_klass_or_null() {
1651   return array_klass_or_null(1);
1652 }
1653 
1654 static int call_class_initializer_counter = 0;   // for debugging
1655 
1656 Method* InstanceKlass::class_initializer() const {
1657   Method* clinit = find_method(
1658       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1659   if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1660     return clinit;
1661   }
1662   return nullptr;
1663 }
1664 
1665 void InstanceKlass::call_class_initializer(TRAPS) {
1666   if (ReplayCompiles &&
1667       (ReplaySuppressInitializers == 1 ||
1668        (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1669     // Hide the existence of the initializer for the purpose of replaying the compile
1670     return;
1671   }
1672 
1673 #if INCLUDE_CDS
1674   // This is needed to ensure the consistency of the archived heap objects.
1675   if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1676     AOTClassInitializer::call_runtime_setup(THREAD, this);
1677     return;
1678   } else if (has_archived_enum_objs()) {
1679     assert(is_shared(), "must be");

1748 
1749 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1750   InterpreterOopMap* entry_for) {
1751   // Lazily create the _oop_map_cache at first request.
1752   // Load_acquire is needed to safely get instance published with CAS by another thread.
1753   OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1754   if (oop_map_cache == nullptr) {
1755     // Try to install new instance atomically.
1756     oop_map_cache = new OopMapCache();
1757     OopMapCache* other = Atomic::cmpxchg(&_oop_map_cache, (OopMapCache*)nullptr, oop_map_cache);
1758     if (other != nullptr) {
1759       // Someone else managed to install before us, ditch local copy and use the existing one.
1760       delete oop_map_cache;
1761       oop_map_cache = other;
1762     }
1763   }
1764   // _oop_map_cache is constant after init; lookup below does its own locking.
1765   oop_map_cache->lookup(method, bci, entry_for);
1766 }
1767 
1768 bool InstanceKlass::contains_field_offset(int offset) {
1769   fieldDescriptor fd;
1770   return find_field_from_offset(offset, false, &fd);
1771 }
1772 
1773 FieldInfo InstanceKlass::field(int index) const {
1774   for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1775     if (fs.index() == index) {
1776       return fs.to_FieldInfo();
1777     }
1778   }
1779   fatal("Field not found");
1780   return FieldInfo();
1781 }
1782 
1783 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1784   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1785     Symbol* f_name = fs.name();
1786     Symbol* f_sig  = fs.signature();
1787     if (f_name == name && f_sig == sig) {
1788       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1789       return true;
1790     }
1791   }

1833 
1834 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1835   // search order according to newest JVM spec (5.4.3.2, p.167).
1836   // 1) search for field in current klass
1837   if (find_local_field(name, sig, fd)) {
1838     if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1839   }
1840   // 2) search for field recursively in direct superinterfaces
1841   if (is_static) {
1842     Klass* intf = find_interface_field(name, sig, fd);
1843     if (intf != nullptr) return intf;
1844   }
1845   // 3) apply field lookup recursively if superclass exists
1846   { Klass* supr = super();
1847     if (supr != nullptr) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1848   }
1849   // 4) otherwise field lookup fails
1850   return nullptr;
1851 }
1852 









1853 
1854 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1855   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1856     if (fs.offset() == offset) {
1857       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1858       if (fd->is_static() == is_static) return true;
1859     }
1860   }
1861   return false;
1862 }
1863 
1864 
1865 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1866   Klass* klass = const_cast<InstanceKlass*>(this);
1867   while (klass != nullptr) {
1868     if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1869       return true;
1870     }
1871     klass = klass->super();
1872   }

2224 }
2225 
2226 // uncached_lookup_method searches both the local class methods array and all
2227 // superclasses methods arrays, skipping any overpass methods in superclasses,
2228 // and possibly skipping private methods.
2229 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2230                                               const Symbol* signature,
2231                                               OverpassLookupMode overpass_mode,
2232                                               PrivateLookupMode private_mode) const {
2233   OverpassLookupMode overpass_local_mode = overpass_mode;
2234   const Klass* klass = this;
2235   while (klass != nullptr) {
2236     Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2237                                                                         signature,
2238                                                                         overpass_local_mode,
2239                                                                         StaticLookupMode::find,
2240                                                                         private_mode);
2241     if (method != nullptr) {
2242       return method;
2243     }



2244     klass = klass->super();
2245     overpass_local_mode = OverpassLookupMode::skip;   // Always ignore overpass methods in superclasses
2246   }
2247   return nullptr;
2248 }
2249 
2250 #ifdef ASSERT
2251 // search through class hierarchy and return true if this class or
2252 // one of the superclasses was redefined
2253 bool InstanceKlass::has_redefined_this_or_super() const {
2254   const Klass* klass = this;
2255   while (klass != nullptr) {
2256     if (InstanceKlass::cast(klass)->has_been_redefined()) {
2257       return true;
2258     }
2259     klass = klass->super();
2260   }
2261   return false;
2262 }
2263 #endif

2621     int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
2622 
2623     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
2624                          / itableOffsetEntry::size();
2625 
2626     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2627       if (ioe->interface_klass() != nullptr) {
2628         it->push(ioe->interface_klass_addr());
2629         itableMethodEntry* ime = ioe->first_method_entry(this);
2630         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2631         for (int index = 0; index < n; index ++) {
2632           it->push(ime[index].method_addr());
2633         }
2634       }
2635     }
2636   }
2637 
2638   it->push(&_nest_host);
2639   it->push(&_nest_members);
2640   it->push(&_permitted_subclasses);

2641   it->push(&_record_components);

2642 }
2643 
2644 #if INCLUDE_CDS
2645 void InstanceKlass::remove_unshareable_info() {
2646 
2647   if (is_linked()) {
2648     assert(can_be_verified_at_dumptime(), "must be");
2649     // Remember this so we can avoid walking the hierarchy at runtime.
2650     set_verified_at_dump_time();
2651   }
2652 
2653   Klass::remove_unshareable_info();
2654 
2655   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2656     // Classes are attempted to link during dumping and may fail,
2657     // but these classes are still in the dictionary and class list in CLD.
2658     // If the class has failed verification, there is nothing else to remove.
2659     return;
2660   }
2661 

2667 
2668   { // Otherwise this needs to take out the Compile_lock.
2669     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2670     init_implementor();
2671   }
2672 
2673   // Call remove_unshareable_info() on other objects that belong to this class, except
2674   // for constants()->remove_unshareable_info(), which is called in a separate pass in
2675   // ArchiveBuilder::make_klasses_shareable(),
2676 
2677   for (int i = 0; i < methods()->length(); i++) {
2678     Method* m = methods()->at(i);
2679     m->remove_unshareable_info();
2680   }
2681 
2682   // do array classes also.
2683   if (array_klasses() != nullptr) {
2684     array_klasses()->remove_unshareable_info();
2685   }
2686 
2687   // These are not allocated from metaspace. They are safe to set to null.
2688   _source_debug_extension = nullptr;
2689   _dep_context = nullptr;
2690   _osr_nmethods_head = nullptr;
2691 #if INCLUDE_JVMTI
2692   _breakpoints = nullptr;
2693   _previous_versions = nullptr;
2694   _cached_class_file = nullptr;
2695   _jvmti_cached_class_field_map = nullptr;
2696 #endif
2697 
2698   _init_thread = nullptr;
2699   _methods_jmethod_ids = nullptr;
2700   _jni_ids = nullptr;
2701   _oop_map_cache = nullptr;
2702   if (CDSConfig::is_dumping_method_handles() && HeapShared::is_lambda_proxy_klass(this)) {
2703     // keep _nest_host
2704   } else {
2705     // clear _nest_host to ensure re-load at runtime
2706     _nest_host = nullptr;
2707   }

2756 void InstanceKlass::compute_has_loops_flag_for_methods() {
2757   Array<Method*>* methods = this->methods();
2758   for (int index = 0; index < methods->length(); ++index) {
2759     Method* m = methods->at(index);
2760     if (!m->is_overpass()) { // work around JDK-8305771
2761       m->compute_has_loops_flag();
2762     }
2763   }
2764 }
2765 
2766 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2767                                              PackageEntry* pkg_entry, TRAPS) {
2768   // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
2769   // before the InstanceKlass is added to the SystemDictionary. Make
2770   // sure the current state is <loaded.
2771   assert(!is_loaded(), "invalid init state");
2772   assert(!shared_loading_failed(), "Must not try to load failed class again");
2773   set_package(loader_data, pkg_entry, CHECK);
2774   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2775 




2776   Array<Method*>* methods = this->methods();
2777   int num_methods = methods->length();
2778   for (int index = 0; index < num_methods; ++index) {
2779     methods->at(index)->restore_unshareable_info(CHECK);
2780   }
2781 #if INCLUDE_JVMTI
2782   if (JvmtiExport::has_redefined_a_class()) {
2783     // Reinitialize vtable because RedefineClasses may have changed some
2784     // entries in this vtable for super classes so the CDS vtable might
2785     // point to old or obsolete entries.  RedefineClasses doesn't fix up
2786     // vtables in the shared system dictionary, only the main one.
2787     // It also redefines the itable too so fix that too.
2788     // First fix any default methods that point to a super class that may
2789     // have been redefined.
2790     bool trace_name_printed = false;
2791     adjust_default_methods(&trace_name_printed);
2792     if (verified_at_dump_time()) {
2793       // Initialize vtable and itable for classes which can be verified at dump time.
2794       // Unlinked classes such as old classes with major version < 50 cannot be verified
2795       // at dump time.
2796       vtable().initialize_vtable();
2797       itable().initialize_itable();
2798     }
2799   }
2800 #endif // INCLUDE_JVMTI
2801 
2802   // restore constant pool resolved references
2803   constants()->restore_unshareable_info(CHECK);
2804 
2805   if (array_klasses() != nullptr) {
2806     // To get a consistent list of classes we need MultiArray_lock to ensure
2807     // array classes aren't observed while they are being restored.
2808     RecursiveLocker rl(MultiArray_lock, THREAD);
2809     assert(this == array_klasses()->bottom_klass(), "sanity");
2810     // Array classes have null protection domain.
2811     // --> see ArrayKlass::complete_create_array_klass()
2812     array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2813   }
2814 
2815   // Initialize @ValueBased class annotation if not already set in the archived klass.
2816   if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2817     set_is_value_based();
2818   }
2819 }
2820 
2821 // Check if a class or any of its supertypes has a version older than 50.
2822 // CDS will not perform verification of old classes during dump time because
2823 // without changing the old verifier, the verification constraint cannot be
2824 // retrieved during dump time.
2825 // Verification of archived old classes will be performed during run time.
2826 bool InstanceKlass::can_be_verified_at_dumptime() const {
2827   if (MetaspaceShared::is_in_shared_metaspace(this)) {
2828     // This is a class that was dumped into the base archive, so we know
2829     // it was verified at dump time.

2986   } else {
2987     // Adding one to the attribute length in order to store a null terminator
2988     // character could cause an overflow because the attribute length is
2989     // already coded with an u4 in the classfile, but in practice, it's
2990     // unlikely to happen.
2991     assert((length+1) > length, "Overflow checking");
2992     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2993     for (int i = 0; i < length; i++) {
2994       sde[i] = array[i];
2995     }
2996     sde[length] = '\0';
2997     _source_debug_extension = sde;
2998   }
2999 }
3000 
3001 Symbol* InstanceKlass::generic_signature() const                   { return _constants->generic_signature(); }
3002 u2 InstanceKlass::generic_signature_index() const                  { return _constants->generic_signature_index(); }
3003 void InstanceKlass::set_generic_signature_index(u2 sig_index)      { _constants->set_generic_signature_index(sig_index); }
3004 
3005 const char* InstanceKlass::signature_name() const {


3006 

3007   // Get the internal name as a c string
3008   const char* src = (const char*) (name()->as_C_string());
3009   const int src_length = (int)strlen(src);
3010 
3011   char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
3012 
3013   // Add L as type indicator
3014   int dest_index = 0;
3015   dest[dest_index++] = JVM_SIGNATURE_CLASS;
3016 
3017   // Add the actual class name
3018   for (int src_index = 0; src_index < src_length; ) {
3019     dest[dest_index++] = src[src_index++];
3020   }
3021 
3022   if (is_hidden()) { // Replace the last '+' with a '.'.
3023     for (int index = (int)src_length; index > 0; index--) {
3024       if (dest[index] == '+') {
3025         dest[index] = JVM_SIGNATURE_DOT;
3026         break;
3027       }
3028     }
3029   }
3030 
3031   // Add the semicolon and the null
3032   dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
3033   dest[dest_index] = '\0';
3034   return dest;
3035 }

3342 u2 InstanceKlass::compute_modifier_flags() const {
3343   u2 access = access_flags().as_unsigned_short();
3344 
3345   // But check if it happens to be member class.
3346   InnerClassesIterator iter(this);
3347   for (; !iter.done(); iter.next()) {
3348     int ioff = iter.inner_class_info_index();
3349     // Inner class attribute can be zero, skip it.
3350     // Strange but true:  JVM spec. allows null inner class refs.
3351     if (ioff == 0) continue;
3352 
3353     // only look at classes that are already loaded
3354     // since we are looking for the flags for our self.
3355     Symbol* inner_name = constants()->klass_name_at(ioff);
3356     if (name() == inner_name) {
3357       // This is really a member class.
3358       access = iter.inner_access_flags();
3359       break;
3360     }
3361   }
3362   // Remember to strip ACC_SUPER bit
3363   return (access & (~JVM_ACC_SUPER));
3364 }
3365 
3366 jint InstanceKlass::jvmti_class_status() const {
3367   jint result = 0;
3368 
3369   if (is_linked()) {
3370     result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3371   }
3372 
3373   if (is_initialized()) {
3374     assert(is_linked(), "Class status is not consistent");
3375     result |= JVMTI_CLASS_STATUS_INITIALIZED;
3376   }
3377   if (is_in_error_state()) {
3378     result |= JVMTI_CLASS_STATUS_ERROR;
3379   }
3380   return result;
3381 }
3382 
3383 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {

3597     }
3598     osr = osr->osr_link();
3599   }
3600 
3601   assert(match_level == false || best == nullptr, "shouldn't pick up anything if match_level is set");
3602   if (best != nullptr && best->comp_level() >= comp_level) {
3603     return best;
3604   }
3605   return nullptr;
3606 }
3607 
3608 // -----------------------------------------------------------------------------------------------------
3609 // Printing
3610 
3611 #define BULLET  " - "
3612 
3613 static const char* state_names[] = {
3614   "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3615 };
3616 
3617 static void print_vtable(intptr_t* start, int len, outputStream* st) {



3618   for (int i = 0; i < len; i++) {
3619     intptr_t e = start[i];
3620     st->print("%d : " INTPTR_FORMAT, i, e);





3621     if (MetaspaceObj::is_valid((Metadata*)e)) {
3622       st->print(" ");
3623       ((Metadata*)e)->print_value_on(st);






3624     }
3625     st->cr();
3626   }
3627 }
3628 
3629 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3630   return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);





















3631 }
3632 
3633 const char* InstanceKlass::init_state_name() const {
3634   return state_names[init_state()];
3635 }
3636 
3637 void InstanceKlass::print_on(outputStream* st) const {
3638   assert(is_klass(), "must be klass");
3639   Klass::print_on(st);
3640 
3641   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3642   st->print(BULLET"klass size:        %d", size());                               st->cr();
3643   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3644   st->print(BULLET"flags:             "); _misc_flags.print_on(st);               st->cr();
3645   st->print(BULLET"state:             "); st->print_cr("%s", init_state_name());
3646   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3647   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3648   st->print(BULLET"sub:               ");
3649   Klass* sub = subklass();
3650   int n;
3651   for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3652     if (n < MaxSubklassPrintSize) {
3653       sub->print_value_on(st);
3654       st->print("   ");
3655     }
3656   }
3657   if (n >= MaxSubklassPrintSize) st->print("(%zd more klasses...)", n - MaxSubklassPrintSize);
3658   st->cr();
3659 
3660   if (is_interface()) {
3661     st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
3662     if (nof_implementors() == 1) {
3663       st->print_cr(BULLET"implementor:    ");
3664       st->print("   ");
3665       implementor()->print_value_on(st);
3666       st->cr();
3667     }
3668   }
3669 
3670   st->print(BULLET"arrays:            "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3671   st->print(BULLET"methods:           "); methods()->print_value_on(st);               st->cr();
3672   if (Verbose || WizardMode) {
3673     Array<Method*>* method_array = methods();
3674     for (int i = 0; i < method_array->length(); i++) {
3675       st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3676     }
3677   }
3678   st->print(BULLET"method ordering:   "); method_ordering()->print_value_on(st);      st->cr();
3679   if (default_methods() != nullptr) {
3680     st->print(BULLET"default_methods:   "); default_methods()->print_value_on(st);    st->cr();
3681     if (Verbose) {
3682       Array<Method*>* method_array = default_methods();
3683       for (int i = 0; i < method_array->length(); i++) {
3684         st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3685       }
3686     }
3687   }
3688   print_on_maybe_null(st, BULLET"default vtable indices:   ", default_vtable_indices());
3689   st->print(BULLET"local interfaces:  "); local_interfaces()->print_value_on(st);      st->cr();
3690   st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3691 
3692   st->print(BULLET"secondary supers: "); secondary_supers()->print_value_on(st); st->cr();
3693 
3694   st->print(BULLET"hash_slot:         %d", hash_slot()); st->cr();
3695   st->print(BULLET"secondary bitmap: " UINTX_FORMAT_X_0, _secondary_supers_bitmap); st->cr();
3696 
3697   if (secondary_supers() != nullptr) {
3698     if (Verbose) {
3699       bool is_hashed = (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL);
3700       st->print_cr(BULLET"---- secondary supers (%d words):", _secondary_supers->length());
3701       for (int i = 0; i < _secondary_supers->length(); i++) {
3702         ResourceMark rm; // for external_name()
3703         Klass* secondary_super = _secondary_supers->at(i);
3704         st->print(BULLET"%2d:", i);
3705         if (is_hashed) {
3706           int home_slot = compute_home_slot(secondary_super, _secondary_supers_bitmap);

3726   print_on_maybe_null(st, BULLET"field type annotations:  ", fields_type_annotations());
3727   {
3728     bool have_pv = false;
3729     // previous versions are linked together through the InstanceKlass
3730     for (InstanceKlass* pv_node = previous_versions();
3731          pv_node != nullptr;
3732          pv_node = pv_node->previous_versions()) {
3733       if (!have_pv)
3734         st->print(BULLET"previous version:  ");
3735       have_pv = true;
3736       pv_node->constants()->print_value_on(st);
3737     }
3738     if (have_pv) st->cr();
3739   }
3740 
3741   print_on_maybe_null(st, BULLET"generic signature: ", generic_signature());
3742   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();
3743   st->print(BULLET"nest members:     "); nest_members()->print_value_on(st);     st->cr();
3744   print_on_maybe_null(st, BULLET"record components:     ", record_components());
3745   st->print(BULLET"permitted subclasses:     "); permitted_subclasses()->print_value_on(st);     st->cr();

3746   if (java_mirror() != nullptr) {
3747     st->print(BULLET"java mirror:       ");
3748     java_mirror()->print_value_on(st);
3749     st->cr();
3750   } else {
3751     st->print_cr(BULLET"java mirror:       null");
3752   }
3753   st->print(BULLET"vtable length      %d  (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3754   if (vtable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_vtable(), vtable_length(), st);
3755   st->print(BULLET"itable length      %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3756   if (itable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_itable(), itable_length(), st);
3757   st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3758 
3759   FieldPrinter print_static_field(st);
3760   ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3761   st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3762   FieldPrinter print_nonstatic_field(st);
3763   InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3764   ik->print_nonstatic_fields(&print_nonstatic_field);
3765 
3766   st->print(BULLET"non-static oop maps: ");
3767   OopMapBlock* map     = start_of_nonstatic_oop_maps();
3768   OopMapBlock* end_map = map + nonstatic_oop_map_count();
3769   while (map < end_map) {
3770     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3771     map++;
3772   }
3773   st->cr();
3774 }
3775 
3776 void InstanceKlass::print_value_on(outputStream* st) const {
3777   assert(is_klass(), "must be klass");
3778   if (Verbose || WizardMode)  access_flags().print_on(st);
3779   name()->print_value_on(st);
3780 }
3781 
3782 void FieldPrinter::do_field(fieldDescriptor* fd) {

3783   _st->print(BULLET);
3784    if (_obj == nullptr) {
3785      fd->print_on(_st);
3786      _st->cr();
3787    } else {
3788      fd->print_on_for(_st, _obj);
3789      _st->cr();
3790    }
3791 }
3792 
3793 
3794 void InstanceKlass::oop_print_on(oop obj, outputStream* st) {
3795   Klass::oop_print_on(obj, st);
3796 
3797   if (this == vmClasses::String_klass()) {
3798     typeArrayOop value  = java_lang_String::value(obj);
3799     juint        length = java_lang_String::length(obj);
3800     if (value != nullptr &&
3801         value->is_typeArray() &&
3802         length <= (juint) value->length()) {
3803       st->print(BULLET"string: ");
3804       java_lang_String::print(obj, st);
3805       st->cr();
3806     }
3807   }
3808 
3809   st->print_cr(BULLET"---- fields (total size %zu words):", oop_size(obj));
3810   FieldPrinter print_field(st, obj);
3811   print_nonstatic_fields(&print_field);
3812 
3813   if (this == vmClasses::Class_klass()) {
3814     st->print(BULLET"signature: ");
3815     java_lang_Class::print_signature(obj, st);
3816     st->cr();
3817     Klass* real_klass = java_lang_Class::as_Klass(obj);
3818     if (real_klass != nullptr && real_klass->is_instance_klass()) {
3819       st->print_cr(BULLET"---- static fields (%d):", java_lang_Class::static_oop_field_count(obj));
3820       InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
3821     }
3822   } else if (this == vmClasses::MethodType_klass()) {
3823     st->print(BULLET"signature: ");
3824     java_lang_invoke_MethodType::print_signature(obj, st);
3825     st->cr();
3826   }
3827 }
3828 
3829 #ifndef PRODUCT
3830 

  52 #include "jvmtifiles/jvmti.h"
  53 #include "logging/log.hpp"
  54 #include "klass.inline.hpp"
  55 #include "logging/logMessage.hpp"
  56 #include "logging/logStream.hpp"
  57 #include "memory/allocation.inline.hpp"
  58 #include "memory/iterator.inline.hpp"
  59 #include "memory/metadataFactory.hpp"
  60 #include "memory/metaspaceClosure.hpp"
  61 #include "memory/oopFactory.hpp"
  62 #include "memory/resourceArea.hpp"
  63 #include "memory/universe.hpp"
  64 #include "oops/fieldStreams.inline.hpp"
  65 #include "oops/constantPool.hpp"
  66 #include "oops/instanceClassLoaderKlass.hpp"
  67 #include "oops/instanceKlass.inline.hpp"
  68 #include "oops/instanceMirrorKlass.hpp"
  69 #include "oops/instanceOop.hpp"
  70 #include "oops/instanceStackChunkKlass.hpp"
  71 #include "oops/klass.inline.hpp"
  72 #include "oops/markWord.hpp"
  73 #include "oops/method.hpp"
  74 #include "oops/oop.inline.hpp"
  75 #include "oops/recordComponent.hpp"
  76 #include "oops/symbol.hpp"
  77 #include "oops/inlineKlass.hpp"
  78 #include "prims/jvmtiExport.hpp"
  79 #include "prims/jvmtiRedefineClasses.hpp"
  80 #include "prims/jvmtiThreadState.hpp"
  81 #include "prims/methodComparator.hpp"
  82 #include "runtime/arguments.hpp"
  83 #include "runtime/deoptimization.hpp"
  84 #include "runtime/atomic.hpp"
  85 #include "runtime/fieldDescriptor.inline.hpp"
  86 #include "runtime/handles.inline.hpp"
  87 #include "runtime/javaCalls.hpp"
  88 #include "runtime/javaThread.inline.hpp"
  89 #include "runtime/mutexLocker.hpp"
  90 #include "runtime/orderAccess.hpp"
  91 #include "runtime/os.inline.hpp"
  92 #include "runtime/reflection.hpp"
  93 #include "runtime/synchronizer.hpp"
  94 #include "runtime/threads.hpp"
  95 #include "services/classLoadingService.hpp"
  96 #include "services/finalizerService.hpp"
  97 #include "services/threadService.hpp"

 135 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)     \
 136   {                                                              \
 137     char* data = nullptr;                                        \
 138     int len = 0;                                                 \
 139     Symbol* clss_name = name();                                  \
 140     if (clss_name != nullptr) {                                  \
 141       data = (char*)clss_name->bytes();                          \
 142       len = clss_name->utf8_length();                            \
 143     }                                                            \
 144     HOTSPOT_CLASS_INITIALIZATION_##type(                         \
 145       data, len, (void*)class_loader(), thread_type, wait);      \
 146   }
 147 
 148 #else //  ndef DTRACE_ENABLED
 149 
 150 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
 151 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
 152 
 153 #endif //  ndef DTRACE_ENABLED
 154 
 155 void InlineLayoutInfo::metaspace_pointers_do(MetaspaceClosure* it) {
 156   log_trace(cds)("Iter(InlineFieldInfo): %p", this);
 157   it->push(&_klass);
 158 }
 159 
 160 bool InstanceKlass::_finalization_enabled = true;
 161 
 162 static inline bool is_class_loader(const Symbol* class_name,
 163                                    const ClassFileParser& parser) {
 164   assert(class_name != nullptr, "invariant");
 165 
 166   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 167     return true;
 168   }
 169 
 170   if (vmClasses::ClassLoader_klass_loaded()) {
 171     const Klass* const super_klass = parser.super_klass();
 172     if (super_klass != nullptr) {
 173       if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
 174         return true;
 175       }
 176     }
 177   }
 178   return false;
 179 }
 180 
 181 bool InstanceKlass::field_is_null_free_inline_type(int index) const {
 182   return field(index).field_flags().is_null_free_inline_type();
 183 }
 184 
 185 bool InstanceKlass::is_class_in_loadable_descriptors_attribute(Symbol* name) const {
 186   if (_loadable_descriptors == nullptr) return false;
 187   for (int i = 0; i < _loadable_descriptors->length(); i++) {
 188         Symbol* class_name = _constants->symbol_at(_loadable_descriptors->at(i));
 189         if (class_name == name) return true;
 190   }
 191   return false;
 192 }
 193 
 194 static inline bool is_stack_chunk_class(const Symbol* class_name,
 195                                         const ClassLoaderData* loader_data) {
 196   return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
 197           loader_data->is_the_null_class_loader_data());
 198 }
 199 
 200 // private: called to verify that k is a static member of this nest.
 201 // We know that k is an instance class in the same package and hence the
 202 // same classloader.
 203 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
 204   assert(!is_hidden(), "unexpected hidden class");
 205   if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
 206     if (log_is_enabled(Trace, class, nestmates)) {
 207       ResourceMark rm(current);
 208       log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
 209                                   k->external_name(), this->external_name());
 210     }
 211     return false;
 212   }
 213 

 468 }
 469 
 470 const char* InstanceKlass::nest_host_error() {
 471   if (_nest_host_index == 0) {
 472     return nullptr;
 473   } else {
 474     constantPoolHandle cph(Thread::current(), constants());
 475     return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
 476   }
 477 }
 478 
 479 void* InstanceKlass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size,
 480                                   bool use_class_space, TRAPS) throw() {
 481   return Metaspace::allocate(loader_data, word_size, ClassType, use_class_space, THREAD);
 482 }
 483 
 484 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 485   const int size = InstanceKlass::size(parser.vtable_size(),
 486                                        parser.itable_size(),
 487                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 488                                        parser.is_interface(),
 489                                        parser.is_inline_type());
 490 
 491   const Symbol* const class_name = parser.class_name();
 492   assert(class_name != nullptr, "invariant");
 493   ClassLoaderData* loader_data = parser.loader_data();
 494   assert(loader_data != nullptr, "invariant");
 495 
 496   InstanceKlass* ik;
 497   const bool use_class_space = parser.klass_needs_narrow_id();
 498 
 499   // Allocation
 500   if (parser.is_instance_ref_klass()) {
 501     // java.lang.ref.Reference
 502     ik = new (loader_data, size, use_class_space, THREAD) InstanceRefKlass(parser);
 503   } else if (class_name == vmSymbols::java_lang_Class()) {
 504     // mirror - java.lang.Class
 505     ik = new (loader_data, size, use_class_space, THREAD) InstanceMirrorKlass(parser);
 506   } else if (is_stack_chunk_class(class_name, loader_data)) {
 507     // stack chunk
 508     ik = new (loader_data, size, use_class_space, THREAD) InstanceStackChunkKlass(parser);
 509   } else if (is_class_loader(class_name, parser)) {
 510     // class loader - java.lang.ClassLoader
 511     ik = new (loader_data, size, use_class_space, THREAD) InstanceClassLoaderKlass(parser);
 512   } else if (parser.is_inline_type()) {
 513     // inline type
 514     ik = new (loader_data, size, use_class_space, THREAD) InlineKlass(parser);
 515   } else {
 516     // normal
 517     ik = new (loader_data, size, use_class_space, THREAD) InstanceKlass(parser);
 518   }
 519 
 520   if (ik != nullptr && UseCompressedClassPointers && use_class_space) {
 521     assert(CompressedKlassPointers::is_encodable(ik),
 522            "Klass " PTR_FORMAT "needs a narrow Klass ID, but is not encodable", p2i(ik));
 523   }
 524 
 525   // Check for pending exception before adding to the loader data and incrementing
 526   // class count.  Can get OOM here.
 527   if (HAS_PENDING_EXCEPTION) {
 528     return nullptr;
 529   }
 530 
 531 #ifdef ASSERT
 532   ik->bounds_check((address) ik->start_of_vtable(), false, size);
 533   ik->bounds_check((address) ik->start_of_itable(), false, size);
 534   ik->bounds_check((address) ik->end_of_itable(), true, size);
 535   ik->bounds_check((address) ik->end_of_nonstatic_oop_maps(), true, size);
 536 #endif //ASSERT
 537   return ik;
 538 }
 539 
 540 #ifndef PRODUCT
 541 bool InstanceKlass::bounds_check(address addr, bool edge_ok, intptr_t size_in_bytes) const {
 542   const char* bad = nullptr;
 543   address end = nullptr;
 544   if (addr < (address)this) {
 545     bad = "before";
 546   } else if (addr == (address)this) {
 547     if (edge_ok)  return true;
 548     bad = "just before";
 549   } else if (addr == (end = (address)this + sizeof(intptr_t) * (size_in_bytes < 0 ? size() : size_in_bytes))) {
 550     if (edge_ok)  return true;
 551     bad = "just after";
 552   } else if (addr > end) {
 553     bad = "after";
 554   } else {
 555     return true;
 556   }
 557   tty->print_cr("%s object bounds: " INTPTR_FORMAT " [" INTPTR_FORMAT ".." INTPTR_FORMAT "]",
 558       bad, (intptr_t)addr, (intptr_t)this, (intptr_t)end);
 559   Verbose = WizardMode = true; this->print(); //@@
 560   return false;
 561 }
 562 #endif //PRODUCT
 563 
 564 // copy method ordering from resource area to Metaspace
 565 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 566   if (m != nullptr) {
 567     // allocate a new array and copy contents (memcpy?)
 568     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 569     for (int i = 0; i < m->length(); i++) {
 570       _method_ordering->at_put(i, m->at(i));
 571     }
 572   } else {
 573     _method_ordering = Universe::the_empty_int_array();
 574   }
 575 }
 576 
 577 // create a new array of vtable_indices for default methods
 578 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 579   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 580   assert(default_vtable_indices() == nullptr, "only create once");
 581   set_default_vtable_indices(vtable_indices);
 582   return vtable_indices;
 583 }
 584 
 585 
 586 InstanceKlass::InstanceKlass() {
 587   assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
 588 }
 589 
 590 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, markWord prototype_header, ReferenceType reference_type) :
 591   Klass(kind, prototype_header),
 592   _nest_members(nullptr),
 593   _nest_host(nullptr),
 594   _permitted_subclasses(nullptr),
 595   _record_components(nullptr),
 596   _static_field_size(parser.static_field_size()),
 597   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 598   _itable_len(parser.itable_size()),
 599   _nest_host_index(0),
 600   _init_state(allocated),
 601   _reference_type(reference_type),
 602   _init_thread(nullptr),
 603   _inline_layout_info_array(nullptr),
 604   _loadable_descriptors(nullptr),
 605   _adr_inlineklass_fixed_block(nullptr)
 606 {
 607   set_vtable_length(parser.vtable_size());
 608   set_access_flags(parser.access_flags());
 609   if (parser.is_hidden()) set_is_hidden();
 610   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 611                                                     false));
 612   if (parser.has_inline_fields()) {
 613     set_has_inline_type_fields();
 614   }
 615 
 616   assert(nullptr == _methods, "underlying memory not zeroed?");
 617   assert(is_instance_klass(), "is layout incorrect?");
 618   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 619 }
 620 
 621 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 622                                        Array<Method*>* methods) {
 623   if (methods != nullptr && methods != Universe::the_empty_method_array() &&
 624       !methods->is_shared()) {
 625     for (int i = 0; i < methods->length(); i++) {
 626       Method* method = methods->at(i);
 627       if (method == nullptr) continue;  // maybe null if error processing
 628       // Only want to delete methods that are not executing for RedefineClasses.
 629       // The previous version will point to them so they're not totally dangling
 630       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 631       MetadataFactory::free_metadata(loader_data, method);
 632     }
 633     MetadataFactory::free_array<Method*>(loader_data, methods);
 634   }

 734       (address)(secondary_supers()) != (address)(transitive_interfaces()) &&
 735       !secondary_supers()->is_shared()) {
 736     MetadataFactory::free_array<Klass*>(loader_data, secondary_supers());
 737   }
 738   set_secondary_supers(nullptr, SECONDARY_SUPERS_BITMAP_EMPTY);
 739 
 740   deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
 741   set_transitive_interfaces(nullptr);
 742   set_local_interfaces(nullptr);
 743 
 744   if (fieldinfo_stream() != nullptr && !fieldinfo_stream()->is_shared()) {
 745     MetadataFactory::free_array<u1>(loader_data, fieldinfo_stream());
 746   }
 747   set_fieldinfo_stream(nullptr);
 748 
 749   if (fields_status() != nullptr && !fields_status()->is_shared()) {
 750     MetadataFactory::free_array<FieldStatus>(loader_data, fields_status());
 751   }
 752   set_fields_status(nullptr);
 753 
 754   if (inline_layout_info_array() != nullptr) {
 755     MetadataFactory::free_array<InlineLayoutInfo>(loader_data, inline_layout_info_array());
 756   }
 757   set_inline_layout_info_array(nullptr);
 758 
 759   // If a method from a redefined class is using this constant pool, don't
 760   // delete it, yet.  The new class's previous version will point to this.
 761   if (constants() != nullptr) {
 762     assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
 763     if (!constants()->is_shared()) {
 764       MetadataFactory::free_metadata(loader_data, constants());
 765     }
 766     // Delete any cached resolution errors for the constant pool
 767     SystemDictionary::delete_resolution_error(constants());
 768 
 769     set_constants(nullptr);
 770   }
 771 
 772   if (inner_classes() != nullptr &&
 773       inner_classes() != Universe::the_empty_short_array() &&
 774       !inner_classes()->is_shared()) {
 775     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 776   }
 777   set_inner_classes(nullptr);
 778 
 779   if (nest_members() != nullptr &&
 780       nest_members() != Universe::the_empty_short_array() &&
 781       !nest_members()->is_shared()) {
 782     MetadataFactory::free_array<jushort>(loader_data, nest_members());
 783   }
 784   set_nest_members(nullptr);
 785 
 786   if (permitted_subclasses() != nullptr &&
 787       permitted_subclasses() != Universe::the_empty_short_array() &&
 788       !permitted_subclasses()->is_shared()) {
 789     MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
 790   }
 791   set_permitted_subclasses(nullptr);
 792 
 793   if (loadable_descriptors() != nullptr &&
 794       loadable_descriptors() != Universe::the_empty_short_array() &&
 795       !loadable_descriptors()->is_shared()) {
 796     MetadataFactory::free_array<jushort>(loader_data, loadable_descriptors());
 797   }
 798   set_loadable_descriptors(nullptr);
 799 
 800   // We should deallocate the Annotations instance if it's not in shared spaces.
 801   if (annotations() != nullptr && !annotations()->is_shared()) {
 802     MetadataFactory::free_metadata(loader_data, annotations());
 803   }
 804   set_annotations(nullptr);
 805 
 806   SystemDictionaryShared::handle_class_unloading(this);
 807 
 808 #if INCLUDE_CDS_JAVA_HEAP
 809   if (CDSConfig::is_dumping_heap()) {
 810     HeapShared::remove_scratch_objects(this);
 811   }
 812 #endif
 813 }
 814 
 815 bool InstanceKlass::is_record() const {
 816   return _record_components != nullptr &&
 817          is_final() &&
 818          java_super() == vmClasses::Record_klass();
 819 }

1014         vmSymbols::java_lang_IncompatibleClassChangeError(),
1015         "class %s has interface %s as super class",
1016         external_name(),
1017         super_klass->external_name()
1018       );
1019       return false;
1020     }
1021 
1022     InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
1023     ik_super->link_class_impl(CHECK_false);
1024   }
1025 
1026   // link all interfaces implemented by this class before linking this class
1027   Array<InstanceKlass*>* interfaces = local_interfaces();
1028   int num_interfaces = interfaces->length();
1029   for (int index = 0; index < num_interfaces; index++) {
1030     InstanceKlass* interk = interfaces->at(index);
1031     interk->link_class_impl(CHECK_false);
1032   }
1033 
1034 
1035   // If a class declares a method that uses an inline class as an argument
1036   // type or return inline type, this inline class must be loaded during the
1037   // linking of this class because size and properties of the inline class
1038   // must be known in order to be able to perform inline type optimizations.
1039   // The implementation below is an approximation of this rule, the code
1040   // iterates over all methods of the current class (including overridden
1041   // methods), not only the methods declared by this class. This
1042   // approximation makes the code simpler, and doesn't change the semantic
1043   // because classes declaring methods overridden by the current class are
1044   // linked (and have performed their own pre-loading) before the linking
1045   // of the current class.
1046 
1047 
1048   // Note:
1049   // Inline class types are loaded during
1050   // the loading phase (see ClassFileParser::post_process_parsed_stream()).
1051   // Inline class types used as element types for array creation
1052   // are not pre-loaded. Their loading is triggered by either anewarray
1053   // or multianewarray bytecodes.
1054 
1055   // Could it be possible to do the following processing only if the
1056   // class uses inline types?
1057   if (EnableValhalla) {
1058     ResourceMark rm(THREAD);
1059     for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1060       if (fs.is_null_free_inline_type() && fs.access_flags().is_static()) {
1061         assert(fs.access_flags().is_strict(), "null-free fields must be strict");
1062         Symbol* sig = fs.signature();
1063         TempNewSymbol s = Signature::strip_envelope(sig);
1064         if (s != name()) {
1065           log_info(class, preload)("Preloading class %s during linking of class %s. Cause: a null-free static field is declared with this type", s->as_C_string(), name()->as_C_string());
1066           Klass* klass = SystemDictionary::resolve_or_fail(s,
1067                                                           Handle(THREAD, class_loader()), true,
1068                                                           CHECK_false);
1069           if (HAS_PENDING_EXCEPTION) {
1070             log_warning(class, preload)("Preloading of class %s during linking of class %s (cause: null-free static field) failed: %s",
1071                                       s->as_C_string(), name()->as_C_string(), PENDING_EXCEPTION->klass()->name()->as_C_string());
1072             return false; // Exception is still pending
1073           }
1074           log_info(class, preload)("Preloading of class %s during linking of class %s (cause: null-free static field) succeeded",
1075                                    s->as_C_string(), name()->as_C_string());
1076           assert(klass != nullptr, "Sanity check");
1077           if (klass->is_abstract()) {
1078             THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(),
1079                       err_msg("Class %s expects class %s to be concrete value class, but it is an abstract class",
1080                       name()->as_C_string(),
1081                       InstanceKlass::cast(klass)->external_name()), false);
1082           }
1083           if (!klass->is_inline_klass()) {
1084             THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(),
1085                        err_msg("class %s expects class %s to be a value class but it is an identity class",
1086                        name()->as_C_string(), klass->external_name()), false);
1087           }
1088           InlineKlass* vk = InlineKlass::cast(klass);
1089           // if (!vk->is_implicitly_constructible()) {
1090           //    THROW_MSG_(vmSymbols::java_lang_IncompatibleClassChangeError(),
1091           //               err_msg("class %s is not implicitly constructible and it is used in a null restricted static field (not supported)",
1092           //               klass->external_name()), false);
1093           // }
1094           // the inline_type_field_klasses_array might have been loaded with CDS, so update only if not already set and check consistency
1095           InlineLayoutInfo* li = inline_layout_info_adr(fs.index());
1096           if (li->klass() == nullptr) {
1097             li->set_klass(InlineKlass::cast(vk));
1098             li->set_kind(LayoutKind::REFERENCE);
1099           }
1100           assert(get_inline_type_field_klass(fs.index()) == vk, "Must match");
1101         } else {
1102           InlineLayoutInfo* li = inline_layout_info_adr(fs.index());
1103           if (li->klass() == nullptr) {
1104             li->set_klass(InlineKlass::cast(this));
1105             li->set_kind(LayoutKind::REFERENCE);
1106           }
1107           assert(get_inline_type_field_klass(fs.index()) == this, "Must match");
1108         }
1109       }
1110     }
1111 
1112     // Aggressively preloading all classes from the LoadableDescriptors attribute
1113     if (loadable_descriptors() != nullptr) {
1114       HandleMark hm(THREAD);
1115       for (int i = 0; i < loadable_descriptors()->length(); i++) {
1116         Symbol* sig = constants()->symbol_at(loadable_descriptors()->at(i));
1117         if (!Signature::has_envelope(sig)) continue;
1118         TempNewSymbol class_name = Signature::strip_envelope(sig);
1119         if (class_name == name()) continue;
1120         log_info(class, preload)("Preloading class %s during linking of class %s because of the class is listed in the LoadableDescriptors attribute", sig->as_C_string(), name()->as_C_string());
1121         oop loader = class_loader();
1122         Klass* klass = SystemDictionary::resolve_or_null(class_name,
1123                                                          Handle(THREAD, loader), THREAD);
1124         if (HAS_PENDING_EXCEPTION) {
1125           CLEAR_PENDING_EXCEPTION;
1126         }
1127         if (klass != nullptr) {
1128           log_info(class, preload)("Preloading of class %s during linking of class %s (cause: LoadableDescriptors attribute) succeeded", class_name->as_C_string(), name()->as_C_string());
1129           if (!klass->is_inline_klass()) {
1130             // Non value class are allowed by the current spec, but it could be an indication of an issue so let's log a warning
1131               log_warning(class, preload)("Preloading class %s during linking of class %s (cause: LoadableDescriptors attribute) but loaded class is not a value class", class_name->as_C_string(), name()->as_C_string());
1132           }
1133         } else {
1134           log_warning(class, preload)("Preloading of class %s during linking of class %s (cause: LoadableDescriptors attribute) failed", class_name->as_C_string(), name()->as_C_string());
1135         }
1136       }
1137     }
1138   }
1139 
1140   // in case the class is linked in the process of linking its superclasses
1141   if (is_linked()) {
1142     return true;
1143   }
1144 
1145   // trace only the link time for this klass that includes
1146   // the verification time
1147   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
1148                              ClassLoader::perf_class_link_selftime(),
1149                              ClassLoader::perf_classes_linked(),
1150                              jt->get_thread_stat()->perf_recursion_counts_addr(),
1151                              jt->get_thread_stat()->perf_timers_addr(),
1152                              PerfClassTraceTime::CLASS_LINK);
1153 
1154   // verification & rewriting
1155   {
1156     HandleMark hm(THREAD);
1157     Handle h_init_lock(THREAD, init_lock());
1158     ObjectLocker ol(h_init_lock, jt);
1159     // rewritten will have been set if loader constraint error found

1424       ss.print("Could not initialize class %s", external_name());
1425       if (cause.is_null()) {
1426         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1427       } else {
1428         THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1429                         ss.as_string(), cause);
1430       }
1431     } else {
1432 
1433       // Step 6
1434       set_init_state(being_initialized);
1435       set_init_thread(jt);
1436       if (debug_logging_enabled) {
1437         ResourceMark rm(jt);
1438         log_debug(class, init)("Thread \"%s\" is initializing %s",
1439                                jt->name(), external_name());
1440       }
1441     }
1442   }
1443 
1444   // Pre-allocating an all-zero value to be used to reset nullable flat storages
1445   if (is_inline_klass()) {
1446       InlineKlass* vk = InlineKlass::cast(this);
1447       if (vk->has_nullable_atomic_layout()) {
1448         oop val = vk->allocate_instance(THREAD);
1449         if (HAS_PENDING_EXCEPTION) {
1450             Handle e(THREAD, PENDING_EXCEPTION);
1451             CLEAR_PENDING_EXCEPTION;
1452             {
1453                 EXCEPTION_MARK;
1454                 add_initialization_error(THREAD, e);
1455                 // Locks object, set state, and notify all waiting threads
1456                 set_initialization_state_and_notify(initialization_error, THREAD);
1457                 CLEAR_PENDING_EXCEPTION;
1458             }
1459             THROW_OOP(e());
1460         }
1461         vk->set_null_reset_value(val);
1462       }
1463   }
1464 
1465   // Step 7
1466   // Next, if C is a class rather than an interface, initialize it's super class and super
1467   // interfaces.
1468   if (!is_interface()) {
1469     Klass* super_klass = super();
1470     if (super_klass != nullptr && super_klass->should_be_initialized()) {
1471       super_klass->initialize(THREAD);
1472     }
1473     // If C implements any interface that declares a non-static, concrete method,
1474     // the initialization of C triggers initialization of its super interfaces.
1475     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1476     // having a superinterface that declares, non-static, concrete methods
1477     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1478       initialize_super_interfaces(THREAD);
1479     }
1480 
1481     // If any exceptions, complete abruptly, throwing the same exception as above.
1482     if (HAS_PENDING_EXCEPTION) {
1483       Handle e(THREAD, PENDING_EXCEPTION);
1484       CLEAR_PENDING_EXCEPTION;
1485       {
1486         EXCEPTION_MARK;
1487         add_initialization_error(THREAD, e);
1488         // Locks object, set state, and notify all waiting threads
1489         set_initialization_state_and_notify(initialization_error, THREAD);
1490         CLEAR_PENDING_EXCEPTION;
1491       }
1492       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1493       THROW_OOP(e());
1494     }
1495   }
1496 

1497   // Step 8
1498   {
1499     DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1500     if (class_initializer() != nullptr) {
1501       // Timer includes any side effects of class initialization (resolution,
1502       // etc), but not recursive entry into call_class_initializer().
1503       PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1504                                ClassLoader::perf_class_init_selftime(),
1505                                ClassLoader::perf_classes_inited(),
1506                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1507                                jt->get_thread_stat()->perf_timers_addr(),
1508                                PerfClassTraceTime::CLASS_CLINIT);
1509       call_class_initializer(THREAD);
1510     } else {
1511       // The elapsed time is so small it's not worth counting.
1512       if (UsePerfData) {
1513         ClassLoader::perf_classes_inited()->inc();
1514       }
1515       call_class_initializer(THREAD);
1516     }

1801     ResourceMark rm(THREAD);
1802     THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1803               : vmSymbols::java_lang_InstantiationException(), external_name());
1804   }
1805   if (this == vmClasses::Class_klass()) {
1806     ResourceMark rm(THREAD);
1807     THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1808               : vmSymbols::java_lang_IllegalAccessException(), external_name());
1809   }
1810 }
1811 
1812 ArrayKlass* InstanceKlass::array_klass(int n, TRAPS) {
1813   // Need load-acquire for lock-free read
1814   if (array_klasses_acquire() == nullptr) {
1815 
1816     // Recursively lock array allocation
1817     RecursiveLocker rl(MultiArray_lock, THREAD);
1818 
1819     // Check if another thread created the array klass while we were waiting for the lock.
1820     if (array_klasses() == nullptr) {
1821       ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, false, CHECK_NULL);
1822       // use 'release' to pair with lock-free load
1823       release_set_array_klasses(k);
1824     }
1825   }
1826 
1827   // array_klasses() will always be set at this point
1828   ArrayKlass* ak = array_klasses();
1829   assert(ak != nullptr, "should be set");
1830   return ak->array_klass(n, THREAD);
1831 }
1832 
1833 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1834   // Need load-acquire for lock-free read
1835   ArrayKlass* ak = array_klasses_acquire();
1836   if (ak == nullptr) {
1837     return nullptr;
1838   } else {
1839     return ak->array_klass_or_null(n);
1840   }
1841 }
1842 
1843 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1844   return array_klass(1, THREAD);
1845 }
1846 
1847 ArrayKlass* InstanceKlass::array_klass_or_null() {
1848   return array_klass_or_null(1);
1849 }
1850 
1851 static int call_class_initializer_counter = 0;   // for debugging
1852 
1853 Method* InstanceKlass::class_initializer() const {
1854   Method* clinit = find_method(
1855       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1856   if (clinit != nullptr && clinit->is_class_initializer()) {
1857     return clinit;
1858   }
1859   return nullptr;
1860 }
1861 
1862 void InstanceKlass::call_class_initializer(TRAPS) {
1863   if (ReplayCompiles &&
1864       (ReplaySuppressInitializers == 1 ||
1865        (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1866     // Hide the existence of the initializer for the purpose of replaying the compile
1867     return;
1868   }
1869 
1870 #if INCLUDE_CDS
1871   // This is needed to ensure the consistency of the archived heap objects.
1872   if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1873     AOTClassInitializer::call_runtime_setup(THREAD, this);
1874     return;
1875   } else if (has_archived_enum_objs()) {
1876     assert(is_shared(), "must be");

1945 
1946 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1947   InterpreterOopMap* entry_for) {
1948   // Lazily create the _oop_map_cache at first request.
1949   // Load_acquire is needed to safely get instance published with CAS by another thread.
1950   OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1951   if (oop_map_cache == nullptr) {
1952     // Try to install new instance atomically.
1953     oop_map_cache = new OopMapCache();
1954     OopMapCache* other = Atomic::cmpxchg(&_oop_map_cache, (OopMapCache*)nullptr, oop_map_cache);
1955     if (other != nullptr) {
1956       // Someone else managed to install before us, ditch local copy and use the existing one.
1957       delete oop_map_cache;
1958       oop_map_cache = other;
1959     }
1960   }
1961   // _oop_map_cache is constant after init; lookup below does its own locking.
1962   oop_map_cache->lookup(method, bci, entry_for);
1963 }
1964 




1965 
1966 FieldInfo InstanceKlass::field(int index) const {
1967   for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1968     if (fs.index() == index) {
1969       return fs.to_FieldInfo();
1970     }
1971   }
1972   fatal("Field not found");
1973   return FieldInfo();
1974 }
1975 
1976 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1977   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1978     Symbol* f_name = fs.name();
1979     Symbol* f_sig  = fs.signature();
1980     if (f_name == name && f_sig == sig) {
1981       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1982       return true;
1983     }
1984   }

2026 
2027 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
2028   // search order according to newest JVM spec (5.4.3.2, p.167).
2029   // 1) search for field in current klass
2030   if (find_local_field(name, sig, fd)) {
2031     if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
2032   }
2033   // 2) search for field recursively in direct superinterfaces
2034   if (is_static) {
2035     Klass* intf = find_interface_field(name, sig, fd);
2036     if (intf != nullptr) return intf;
2037   }
2038   // 3) apply field lookup recursively if superclass exists
2039   { Klass* supr = super();
2040     if (supr != nullptr) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
2041   }
2042   // 4) otherwise field lookup fails
2043   return nullptr;
2044 }
2045 
2046 bool InstanceKlass::contains_field_offset(int offset) {
2047   if (this->is_inline_klass()) {
2048     InlineKlass* vk = InlineKlass::cast(this);
2049     return offset >= vk->payload_offset() && offset < (vk->payload_offset() + vk->payload_size_in_bytes());
2050   } else {
2051     fieldDescriptor fd;
2052     return find_field_from_offset(offset, false, &fd);
2053   }
2054 }
2055 
2056 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
2057   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
2058     if (fs.offset() == offset) {
2059       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
2060       if (fd->is_static() == is_static) return true;
2061     }
2062   }
2063   return false;
2064 }
2065 
2066 
2067 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
2068   Klass* klass = const_cast<InstanceKlass*>(this);
2069   while (klass != nullptr) {
2070     if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
2071       return true;
2072     }
2073     klass = klass->super();
2074   }

2426 }
2427 
2428 // uncached_lookup_method searches both the local class methods array and all
2429 // superclasses methods arrays, skipping any overpass methods in superclasses,
2430 // and possibly skipping private methods.
2431 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2432                                               const Symbol* signature,
2433                                               OverpassLookupMode overpass_mode,
2434                                               PrivateLookupMode private_mode) const {
2435   OverpassLookupMode overpass_local_mode = overpass_mode;
2436   const Klass* klass = this;
2437   while (klass != nullptr) {
2438     Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2439                                                                         signature,
2440                                                                         overpass_local_mode,
2441                                                                         StaticLookupMode::find,
2442                                                                         private_mode);
2443     if (method != nullptr) {
2444       return method;
2445     }
2446     if (name == vmSymbols::object_initializer_name()) {
2447       break;  // <init> is never inherited
2448     }
2449     klass = klass->super();
2450     overpass_local_mode = OverpassLookupMode::skip;   // Always ignore overpass methods in superclasses
2451   }
2452   return nullptr;
2453 }
2454 
2455 #ifdef ASSERT
2456 // search through class hierarchy and return true if this class or
2457 // one of the superclasses was redefined
2458 bool InstanceKlass::has_redefined_this_or_super() const {
2459   const Klass* klass = this;
2460   while (klass != nullptr) {
2461     if (InstanceKlass::cast(klass)->has_been_redefined()) {
2462       return true;
2463     }
2464     klass = klass->super();
2465   }
2466   return false;
2467 }
2468 #endif

2826     int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
2827 
2828     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
2829                          / itableOffsetEntry::size();
2830 
2831     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2832       if (ioe->interface_klass() != nullptr) {
2833         it->push(ioe->interface_klass_addr());
2834         itableMethodEntry* ime = ioe->first_method_entry(this);
2835         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2836         for (int index = 0; index < n; index ++) {
2837           it->push(ime[index].method_addr());
2838         }
2839       }
2840     }
2841   }
2842 
2843   it->push(&_nest_host);
2844   it->push(&_nest_members);
2845   it->push(&_permitted_subclasses);
2846   it->push(&_loadable_descriptors);
2847   it->push(&_record_components);
2848   it->push(&_inline_layout_info_array, MetaspaceClosure::_writable);
2849 }
2850 
2851 #if INCLUDE_CDS
2852 void InstanceKlass::remove_unshareable_info() {
2853 
2854   if (is_linked()) {
2855     assert(can_be_verified_at_dumptime(), "must be");
2856     // Remember this so we can avoid walking the hierarchy at runtime.
2857     set_verified_at_dump_time();
2858   }
2859 
2860   Klass::remove_unshareable_info();
2861 
2862   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2863     // Classes are attempted to link during dumping and may fail,
2864     // but these classes are still in the dictionary and class list in CLD.
2865     // If the class has failed verification, there is nothing else to remove.
2866     return;
2867   }
2868 

2874 
2875   { // Otherwise this needs to take out the Compile_lock.
2876     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2877     init_implementor();
2878   }
2879 
2880   // Call remove_unshareable_info() on other objects that belong to this class, except
2881   // for constants()->remove_unshareable_info(), which is called in a separate pass in
2882   // ArchiveBuilder::make_klasses_shareable(),
2883 
2884   for (int i = 0; i < methods()->length(); i++) {
2885     Method* m = methods()->at(i);
2886     m->remove_unshareable_info();
2887   }
2888 
2889   // do array classes also.
2890   if (array_klasses() != nullptr) {
2891     array_klasses()->remove_unshareable_info();
2892   }
2893 
2894   // These are not allocated from metaspace. They are safe to set to nullptr.
2895   _source_debug_extension = nullptr;
2896   _dep_context = nullptr;
2897   _osr_nmethods_head = nullptr;
2898 #if INCLUDE_JVMTI
2899   _breakpoints = nullptr;
2900   _previous_versions = nullptr;
2901   _cached_class_file = nullptr;
2902   _jvmti_cached_class_field_map = nullptr;
2903 #endif
2904 
2905   _init_thread = nullptr;
2906   _methods_jmethod_ids = nullptr;
2907   _jni_ids = nullptr;
2908   _oop_map_cache = nullptr;
2909   if (CDSConfig::is_dumping_method_handles() && HeapShared::is_lambda_proxy_klass(this)) {
2910     // keep _nest_host
2911   } else {
2912     // clear _nest_host to ensure re-load at runtime
2913     _nest_host = nullptr;
2914   }

2963 void InstanceKlass::compute_has_loops_flag_for_methods() {
2964   Array<Method*>* methods = this->methods();
2965   for (int index = 0; index < methods->length(); ++index) {
2966     Method* m = methods->at(index);
2967     if (!m->is_overpass()) { // work around JDK-8305771
2968       m->compute_has_loops_flag();
2969     }
2970   }
2971 }
2972 
2973 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2974                                              PackageEntry* pkg_entry, TRAPS) {
2975   // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
2976   // before the InstanceKlass is added to the SystemDictionary. Make
2977   // sure the current state is <loaded.
2978   assert(!is_loaded(), "invalid init state");
2979   assert(!shared_loading_failed(), "Must not try to load failed class again");
2980   set_package(loader_data, pkg_entry, CHECK);
2981   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2982 
2983   if (is_inline_klass()) {
2984     InlineKlass::cast(this)->initialize_calling_convention(CHECK);
2985   }
2986 
2987   Array<Method*>* methods = this->methods();
2988   int num_methods = methods->length();
2989   for (int index = 0; index < num_methods; ++index) {
2990     methods->at(index)->restore_unshareable_info(CHECK);
2991   }
2992 #if INCLUDE_JVMTI
2993   if (JvmtiExport::has_redefined_a_class()) {
2994     // Reinitialize vtable because RedefineClasses may have changed some
2995     // entries in this vtable for super classes so the CDS vtable might
2996     // point to old or obsolete entries.  RedefineClasses doesn't fix up
2997     // vtables in the shared system dictionary, only the main one.
2998     // It also redefines the itable too so fix that too.
2999     // First fix any default methods that point to a super class that may
3000     // have been redefined.
3001     bool trace_name_printed = false;
3002     adjust_default_methods(&trace_name_printed);
3003     if (verified_at_dump_time()) {
3004       // Initialize vtable and itable for classes which can be verified at dump time.
3005       // Unlinked classes such as old classes with major version < 50 cannot be verified
3006       // at dump time.
3007       vtable().initialize_vtable();
3008       itable().initialize_itable();
3009     }
3010   }
3011 #endif // INCLUDE_JVMTI
3012 
3013   // restore constant pool resolved references
3014   constants()->restore_unshareable_info(CHECK);
3015 
3016   if (array_klasses() != nullptr) {
3017     // To get a consistent list of classes we need MultiArray_lock to ensure
3018     // array classes aren't observed while they are being restored.
3019     RecursiveLocker rl(MultiArray_lock, THREAD);
3020     assert(this == ObjArrayKlass::cast(array_klasses())->bottom_klass(), "sanity");
3021     // Array classes have null protection domain.
3022     // --> see ArrayKlass::complete_create_array_klass()
3023     array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
3024   }
3025 
3026   // Initialize @ValueBased class annotation if not already set in the archived klass.
3027   if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
3028     set_is_value_based();
3029   }
3030 }
3031 
3032 // Check if a class or any of its supertypes has a version older than 50.
3033 // CDS will not perform verification of old classes during dump time because
3034 // without changing the old verifier, the verification constraint cannot be
3035 // retrieved during dump time.
3036 // Verification of archived old classes will be performed during run time.
3037 bool InstanceKlass::can_be_verified_at_dumptime() const {
3038   if (MetaspaceShared::is_in_shared_metaspace(this)) {
3039     // This is a class that was dumped into the base archive, so we know
3040     // it was verified at dump time.

3197   } else {
3198     // Adding one to the attribute length in order to store a null terminator
3199     // character could cause an overflow because the attribute length is
3200     // already coded with an u4 in the classfile, but in practice, it's
3201     // unlikely to happen.
3202     assert((length+1) > length, "Overflow checking");
3203     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
3204     for (int i = 0; i < length; i++) {
3205       sde[i] = array[i];
3206     }
3207     sde[length] = '\0';
3208     _source_debug_extension = sde;
3209   }
3210 }
3211 
3212 Symbol* InstanceKlass::generic_signature() const                   { return _constants->generic_signature(); }
3213 u2 InstanceKlass::generic_signature_index() const                  { return _constants->generic_signature_index(); }
3214 void InstanceKlass::set_generic_signature_index(u2 sig_index)      { _constants->set_generic_signature_index(sig_index); }
3215 
3216 const char* InstanceKlass::signature_name() const {
3217   return signature_name_of_carrier(JVM_SIGNATURE_CLASS);
3218 }
3219 
3220 const char* InstanceKlass::signature_name_of_carrier(char c) const {
3221   // Get the internal name as a c string
3222   const char* src = (const char*) (name()->as_C_string());
3223   const int src_length = (int)strlen(src);
3224 
3225   char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
3226 
3227   // Add L or Q as type indicator
3228   int dest_index = 0;
3229   dest[dest_index++] = c;
3230 
3231   // Add the actual class name
3232   for (int src_index = 0; src_index < src_length; ) {
3233     dest[dest_index++] = src[src_index++];
3234   }
3235 
3236   if (is_hidden()) { // Replace the last '+' with a '.'.
3237     for (int index = (int)src_length; index > 0; index--) {
3238       if (dest[index] == '+') {
3239         dest[index] = JVM_SIGNATURE_DOT;
3240         break;
3241       }
3242     }
3243   }
3244 
3245   // Add the semicolon and the null
3246   dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
3247   dest[dest_index] = '\0';
3248   return dest;
3249 }

3556 u2 InstanceKlass::compute_modifier_flags() const {
3557   u2 access = access_flags().as_unsigned_short();
3558 
3559   // But check if it happens to be member class.
3560   InnerClassesIterator iter(this);
3561   for (; !iter.done(); iter.next()) {
3562     int ioff = iter.inner_class_info_index();
3563     // Inner class attribute can be zero, skip it.
3564     // Strange but true:  JVM spec. allows null inner class refs.
3565     if (ioff == 0) continue;
3566 
3567     // only look at classes that are already loaded
3568     // since we are looking for the flags for our self.
3569     Symbol* inner_name = constants()->klass_name_at(ioff);
3570     if (name() == inner_name) {
3571       // This is really a member class.
3572       access = iter.inner_access_flags();
3573       break;
3574     }
3575   }
3576   return access;

3577 }
3578 
3579 jint InstanceKlass::jvmti_class_status() const {
3580   jint result = 0;
3581 
3582   if (is_linked()) {
3583     result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3584   }
3585 
3586   if (is_initialized()) {
3587     assert(is_linked(), "Class status is not consistent");
3588     result |= JVMTI_CLASS_STATUS_INITIALIZED;
3589   }
3590   if (is_in_error_state()) {
3591     result |= JVMTI_CLASS_STATUS_ERROR;
3592   }
3593   return result;
3594 }
3595 
3596 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {

3810     }
3811     osr = osr->osr_link();
3812   }
3813 
3814   assert(match_level == false || best == nullptr, "shouldn't pick up anything if match_level is set");
3815   if (best != nullptr && best->comp_level() >= comp_level) {
3816     return best;
3817   }
3818   return nullptr;
3819 }
3820 
3821 // -----------------------------------------------------------------------------------------------------
3822 // Printing
3823 
3824 #define BULLET  " - "
3825 
3826 static const char* state_names[] = {
3827   "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3828 };
3829 
3830 static void print_vtable(address self, intptr_t* start, int len, outputStream* st) {
3831   ResourceMark rm;
3832   int* forward_refs = NEW_RESOURCE_ARRAY(int, len);
3833   for (int i = 0; i < len; i++)  forward_refs[i] = 0;
3834   for (int i = 0; i < len; i++) {
3835     intptr_t e = start[i];
3836     st->print("%d : " INTPTR_FORMAT, i, e);
3837     if (forward_refs[i] != 0) {
3838       int from = forward_refs[i];
3839       int off = (int) start[from];
3840       st->print(" (offset %d <= [%d])", off, from);
3841     }
3842     if (MetaspaceObj::is_valid((Metadata*)e)) {
3843       st->print(" ");
3844       ((Metadata*)e)->print_value_on(st);
3845     } else if (self != nullptr && e > 0 && e < 0x10000) {
3846       address location = self + e;
3847       int index = (int)((intptr_t*)location - start);
3848       st->print(" (offset %d => [%d])", (int)e, index);
3849       if (index >= 0 && index < len)
3850         forward_refs[index] = i;
3851     }
3852     st->cr();
3853   }
3854 }
3855 
3856 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3857   return print_vtable(nullptr, reinterpret_cast<intptr_t*>(start), len, st);
3858 }
3859 
3860 template<typename T>
3861  static void print_array_on(outputStream* st, Array<T>* array) {
3862    if (array == nullptr) { st->print_cr("nullptr"); return; }
3863    array->print_value_on(st); st->cr();
3864    if (Verbose || WizardMode) {
3865      for (int i = 0; i < array->length(); i++) {
3866        st->print("%d : ", i); array->at(i)->print_value_on(st); st->cr();
3867      }
3868    }
3869  }
3870 
3871 static void print_array_on(outputStream* st, Array<int>* array) {
3872   if (array == nullptr) { st->print_cr("nullptr"); return; }
3873   array->print_value_on(st); st->cr();
3874   if (Verbose || WizardMode) {
3875     for (int i = 0; i < array->length(); i++) {
3876       st->print("%d : %d", i, array->at(i)); st->cr();
3877     }
3878   }
3879 }
3880 
3881 const char* InstanceKlass::init_state_name() const {
3882   return state_names[init_state()];
3883 }
3884 
3885 void InstanceKlass::print_on(outputStream* st) const {
3886   assert(is_klass(), "must be klass");
3887   Klass::print_on(st);
3888 
3889   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3890   st->print(BULLET"klass size:        %d", size());                               st->cr();
3891   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3892   st->print(BULLET"flags:             "); _misc_flags.print_on(st);               st->cr();
3893   st->print(BULLET"state:             "); st->print_cr("%s", init_state_name());
3894   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3895   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3896   st->print(BULLET"sub:               ");
3897   Klass* sub = subklass();
3898   int n;
3899   for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3900     if (n < MaxSubklassPrintSize) {
3901       sub->print_value_on(st);
3902       st->print("   ");
3903     }
3904   }
3905   if (n >= MaxSubklassPrintSize) st->print("(%zd more klasses...)", n - MaxSubklassPrintSize);
3906   st->cr();
3907 
3908   if (is_interface()) {
3909     st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
3910     if (nof_implementors() == 1) {
3911       st->print_cr(BULLET"implementor:    ");
3912       st->print("   ");
3913       implementor()->print_value_on(st);
3914       st->cr();
3915     }
3916   }
3917 
3918   st->print(BULLET"arrays:            "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3919   st->print(BULLET"methods:           "); print_array_on(st, methods());
3920   st->print(BULLET"method ordering:   "); print_array_on(st, method_ordering());






3921   if (default_methods() != nullptr) {
3922     st->print(BULLET"default_methods:   "); print_array_on(st, default_methods());






3923   }
3924   print_on_maybe_null(st, BULLET"default vtable indices:   ", default_vtable_indices());
3925   st->print(BULLET"local interfaces:  "); local_interfaces()->print_value_on(st);      st->cr();
3926   st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3927 
3928   st->print(BULLET"secondary supers: "); secondary_supers()->print_value_on(st); st->cr();
3929 
3930   st->print(BULLET"hash_slot:         %d", hash_slot()); st->cr();
3931   st->print(BULLET"secondary bitmap: " UINTX_FORMAT_X_0, _secondary_supers_bitmap); st->cr();
3932 
3933   if (secondary_supers() != nullptr) {
3934     if (Verbose) {
3935       bool is_hashed = (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL);
3936       st->print_cr(BULLET"---- secondary supers (%d words):", _secondary_supers->length());
3937       for (int i = 0; i < _secondary_supers->length(); i++) {
3938         ResourceMark rm; // for external_name()
3939         Klass* secondary_super = _secondary_supers->at(i);
3940         st->print(BULLET"%2d:", i);
3941         if (is_hashed) {
3942           int home_slot = compute_home_slot(secondary_super, _secondary_supers_bitmap);

3962   print_on_maybe_null(st, BULLET"field type annotations:  ", fields_type_annotations());
3963   {
3964     bool have_pv = false;
3965     // previous versions are linked together through the InstanceKlass
3966     for (InstanceKlass* pv_node = previous_versions();
3967          pv_node != nullptr;
3968          pv_node = pv_node->previous_versions()) {
3969       if (!have_pv)
3970         st->print(BULLET"previous version:  ");
3971       have_pv = true;
3972       pv_node->constants()->print_value_on(st);
3973     }
3974     if (have_pv) st->cr();
3975   }
3976 
3977   print_on_maybe_null(st, BULLET"generic signature: ", generic_signature());
3978   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();
3979   st->print(BULLET"nest members:     "); nest_members()->print_value_on(st);     st->cr();
3980   print_on_maybe_null(st, BULLET"record components:     ", record_components());
3981   st->print(BULLET"permitted subclasses:     "); permitted_subclasses()->print_value_on(st);     st->cr();
3982   st->print(BULLET"loadable descriptors:     "); loadable_descriptors()->print_value_on(st); st->cr();
3983   if (java_mirror() != nullptr) {
3984     st->print(BULLET"java mirror:       ");
3985     java_mirror()->print_value_on(st);
3986     st->cr();
3987   } else {
3988     st->print_cr(BULLET"java mirror:       null");
3989   }
3990   st->print(BULLET"vtable length      %d  (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3991   if (vtable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_vtable(), vtable_length(), st);
3992   st->print(BULLET"itable length      %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3993   if (itable_length() > 0 && (Verbose || WizardMode))  print_vtable(nullptr, start_of_itable(), itable_length(), st);
3994   st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3995 
3996   FieldPrinter print_static_field(st);
3997   ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3998   st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3999   FieldPrinter print_nonstatic_field(st);
4000   InstanceKlass* ik = const_cast<InstanceKlass*>(this);
4001   ik->print_nonstatic_fields(&print_nonstatic_field);
4002 
4003   st->print(BULLET"non-static oop maps: ");
4004   OopMapBlock* map     = start_of_nonstatic_oop_maps();
4005   OopMapBlock* end_map = map + nonstatic_oop_map_count();
4006   while (map < end_map) {
4007     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
4008     map++;
4009   }
4010   st->cr();
4011 }
4012 
4013 void InstanceKlass::print_value_on(outputStream* st) const {
4014   assert(is_klass(), "must be klass");
4015   if (Verbose || WizardMode)  access_flags().print_on(st);
4016   name()->print_value_on(st);
4017 }
4018 
4019 void FieldPrinter::do_field(fieldDescriptor* fd) {
4020   for (int i = 0; i < _indent; i++) _st->print("  ");
4021   _st->print(BULLET);
4022    if (_obj == nullptr) {
4023      fd->print_on(_st, _base_offset);
4024      _st->cr();
4025    } else {
4026      fd->print_on_for(_st, _obj, _indent, _base_offset);
4027      if (!fd->field_flags().is_flat()) _st->cr();
4028    }
4029 }
4030 
4031 
4032 void InstanceKlass::oop_print_on(oop obj, outputStream* st, int indent, int base_offset) {
4033   Klass::oop_print_on(obj, st);
4034 
4035   if (this == vmClasses::String_klass()) {
4036     typeArrayOop value  = java_lang_String::value(obj);
4037     juint        length = java_lang_String::length(obj);
4038     if (value != nullptr &&
4039         value->is_typeArray() &&
4040         length <= (juint) value->length()) {
4041       st->print(BULLET"string: ");
4042       java_lang_String::print(obj, st);
4043       st->cr();
4044     }
4045   }
4046 
4047   st->print_cr(BULLET"---- fields (total size %zu words):", oop_size(obj));
4048   FieldPrinter print_field(st, obj, indent, base_offset);
4049   print_nonstatic_fields(&print_field);
4050 
4051   if (this == vmClasses::Class_klass()) {
4052     st->print(BULLET"signature: ");
4053     java_lang_Class::print_signature(obj, st);
4054     st->cr();
4055     Klass* real_klass = java_lang_Class::as_Klass(obj);
4056     if (real_klass != nullptr && real_klass->is_instance_klass()) {
4057       st->print_cr(BULLET"---- static fields (%d):", java_lang_Class::static_oop_field_count(obj));
4058       InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
4059     }
4060   } else if (this == vmClasses::MethodType_klass()) {
4061     st->print(BULLET"signature: ");
4062     java_lang_invoke_MethodType::print_signature(obj, st);
4063     st->cr();
4064   }
4065 }
4066 
4067 #ifndef PRODUCT
4068 
< prev index next >