1 /*
   2  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_OOPS_INSTANCEKLASS_HPP
  26 #define SHARE_OOPS_INSTANCEKLASS_HPP
  27 
  28 #include "code/vmreg.hpp"
  29 #include "memory/referenceType.hpp"
  30 #include "oops/annotations.hpp"
  31 #include "oops/constMethod.hpp"
  32 #include "oops/fieldInfo.hpp"
  33 #include "oops/instanceKlassFlags.hpp"
  34 #include "oops/instanceOop.hpp"
  35 #include "runtime/handles.hpp"
  36 #include "runtime/javaThread.hpp"
  37 #include "utilities/accessFlags.hpp"
  38 #include "utilities/align.hpp"
  39 #include "utilities/growableArray.hpp"
  40 #include "utilities/macros.hpp"
  41 #if INCLUDE_JFR
  42 #include "jfr/support/jfrKlassExtension.hpp"
  43 #endif
  44 
  45 class ConstantPool;
  46 class DeoptimizationScope;
  47 class klassItable;
  48 class Monitor;
  49 class RecordComponent;
  50 
  51 // An InstanceKlass is the VM level representation of a Java class.
  52 // It contains all information needed for at class at execution runtime.
  53 
  54 //  InstanceKlass embedded field layout (after declared fields):
  55 //    [EMBEDDED Java vtable             ] size in words = vtable_len
  56 //    [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size
  57 //      The embedded nonstatic oop-map blocks are short pairs (offset, length)
  58 //      indicating where oops are located in instances of this klass.
  59 //    [EMBEDDED implementor of the interface] only exist for interface
  60 //    [EMBEDDED InlineKlassFixedBlock] only if is an InlineKlass instance
  61 
  62 
  63 // forward declaration for class -- see below for definition
  64 #if INCLUDE_JVMTI
  65 class BreakpointInfo;
  66 #endif
  67 class ClassFileParser;
  68 class ClassFileStream;
  69 class KlassDepChange;
  70 class DependencyContext;
  71 class fieldDescriptor;
  72 class jniIdMapBase;
  73 class JNIid;
  74 class JvmtiCachedClassFieldMap;
  75 class nmethodBucket;
  76 class OopMapCache;
  77 class BufferedInlineTypeBlob;
  78 class InterpreterOopMap;
  79 class PackageEntry;
  80 class ModuleEntry;
  81 
  82 // This is used in iterators below.
  83 class FieldClosure: public StackObj {
  84 public:
  85   virtual void do_field(fieldDescriptor* fd) = 0;
  86 };
  87 
  88 // Print fields.
  89 // If "obj" argument to constructor is null, prints static fields, otherwise prints non-static fields.
  90 class FieldPrinter: public FieldClosure {
  91    oop _obj;
  92    outputStream* _st;
  93  public:
  94    FieldPrinter(outputStream* st, oop obj = nullptr) : _obj(obj), _st(st) {}
  95    void do_field(fieldDescriptor* fd);
  96 };
  97 
  98 // Describes where oops are located in instances of this klass.
  99 class OopMapBlock {
 100  public:
 101   // Byte offset of the first oop mapped by this block.
 102   int offset() const          { return _offset; }
 103   void set_offset(int offset) { _offset = offset; }
 104 
 105   // Number of oops in this block.
 106   uint count() const         { return _count; }
 107   void set_count(uint count) { _count = count; }
 108 
 109   void increment_count(int diff) { _count += diff; }
 110 
 111   int offset_span() const { return _count * heapOopSize; }
 112 
 113   int end_offset() const {
 114     return offset() + offset_span();
 115   }
 116 
 117   bool is_contiguous(int another_offset) const {
 118     return another_offset == end_offset();
 119   }
 120 
 121   // sizeof(OopMapBlock) in words.
 122   static int size_in_words() {
 123     return align_up((int)sizeof(OopMapBlock), wordSize) >>
 124       LogBytesPerWord;
 125   }
 126 
 127   static int compare_offset(const OopMapBlock* a, const OopMapBlock* b) {
 128     return a->offset() - b->offset();
 129   }
 130 
 131  private:
 132   int  _offset;
 133   uint _count;
 134 };
 135 
 136 struct JvmtiCachedClassFileData;
 137 
 138 class SigEntry;
 139 
 140 class InlineKlassFixedBlock {
 141   Array<SigEntry>** _extended_sig;
 142   Array<VMRegPair>** _return_regs;
 143   address* _pack_handler;
 144   address* _pack_handler_jobject;
 145   address* _unpack_handler;
 146   int* _default_value_offset;
 147   ArrayKlass** _null_free_inline_array_klasses;
 148   int _alignment;
 149   int _first_field_offset;
 150   int _exact_size_in_bytes;
 151 
 152   friend class InlineKlass;
 153 };
 154 
 155 class InstanceKlass: public Klass {
 156   friend class VMStructs;
 157   friend class JVMCIVMStructs;
 158   friend class ClassFileParser;
 159   friend class CompileReplay;
 160   friend class TemplateTable;
 161 
 162  public:
 163   static const KlassKind Kind = InstanceKlassKind;
 164 
 165  protected:
 166   InstanceKlass(const ClassFileParser& parser, KlassKind kind = Kind, ReferenceType reference_type = REF_NONE);
 167 
 168  public:
 169   InstanceKlass();
 170 
 171   // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description
 172   // of the class loading & initialization procedure, and the use of the states.
 173   enum ClassState : u1 {
 174     allocated,                          // allocated (but not yet linked)
 175     loaded,                             // loaded and inserted in class hierarchy (but not linked yet)
 176     being_linked,                       // currently running verifier and rewriter
 177     linked,                             // successfully linked/verified (but not initialized yet)
 178     being_initialized,                  // currently running class initializer
 179     fully_initialized,                  // initialized (successful final state)
 180     initialization_error                // error happened during initialization
 181   };
 182 
 183  private:
 184   static InstanceKlass* allocate_instance_klass(const ClassFileParser& parser, TRAPS);
 185 
 186  protected:
 187   // If you add a new field that points to any metaspace object, you
 188   // must add this field to InstanceKlass::metaspace_pointers_do().
 189 
 190   // Annotations for this class
 191   Annotations*    _annotations;
 192   // Package this class is defined in
 193   PackageEntry*   _package_entry;
 194   // Array classes holding elements of this class.
 195   ArrayKlass* volatile _array_klasses;
 196   // Constant pool for this class.
 197   ConstantPool* _constants;
 198   // The InnerClasses attribute and EnclosingMethod attribute. The
 199   // _inner_classes is an array of shorts. If the class has InnerClasses
 200   // attribute, then the _inner_classes array begins with 4-tuples of shorts
 201   // [inner_class_info_index, outer_class_info_index,
 202   // inner_name_index, inner_class_access_flags] for the InnerClasses
 203   // attribute. If the EnclosingMethod attribute exists, it occupies the
 204   // last two shorts [class_index, method_index] of the array. If only
 205   // the InnerClasses attribute exists, the _inner_classes array length is
 206   // number_of_inner_classes * 4. If the class has both InnerClasses
 207   // and EnclosingMethod attributes the _inner_classes array length is
 208   // number_of_inner_classes * 4 + enclosing_method_attribute_size.
 209   Array<jushort>* _inner_classes;
 210 
 211   // The NestMembers attribute. An array of shorts, where each is a
 212   // class info index for the class that is a nest member. This data
 213   // has not been validated.
 214   Array<jushort>* _nest_members;
 215 
 216   // Resolved nest-host klass: either true nest-host or self if we are not
 217   // nested, or an error occurred resolving or validating the nominated
 218   // nest-host. Can also be set directly by JDK API's that establish nest
 219   // relationships.
 220   // By always being set it makes nest-member access checks simpler.
 221   InstanceKlass* _nest_host;
 222 
 223   // The PermittedSubclasses attribute. An array of shorts, where each is a
 224   // class info index for the class that is a permitted subclass.
 225   Array<jushort>* _permitted_subclasses;
 226 
 227   // The contents of the Record attribute.
 228   Array<RecordComponent*>* _record_components;
 229 
 230   // the source debug extension for this klass, null if not specified.
 231   // Specified as UTF-8 string without terminating zero byte in the classfile,
 232   // it is stored in the instanceklass as a null-terminated UTF-8 string
 233   const char*     _source_debug_extension;
 234 
 235   // Number of heapOopSize words used by non-static fields in this klass
 236   // (including inherited fields but after header_size()).
 237   int             _nonstatic_field_size;
 238   int             _static_field_size;       // number words used by static fields (oop and non-oop) in this klass
 239   int             _nonstatic_oop_map_size;  // size in words of nonstatic oop map blocks
 240   int             _itable_len;              // length of Java itable (in words)
 241 
 242   // The NestHost attribute. The class info index for the class
 243   // that is the nest-host of this class. This data has not been validated.
 244   u2              _nest_host_index;
 245   u2              _this_class_index;        // constant pool entry
 246   u2              _static_oop_field_count;  // number of static oop fields in this klass
 247 
 248   volatile u2     _idnum_allocated_count;   // JNI/JVMTI: increments with the addition of methods, old ids don't change
 249 
 250   volatile ClassState _init_state;          // state of class
 251 
 252   u1              _reference_type;          // reference type
 253 
 254   // State is set either at parse time or while executing, atomically to not disturb other state
 255   InstanceKlassFlags _misc_flags;
 256 
 257   Monitor*             _init_monitor;       // mutual exclusion to _init_state and _init_thread.
 258   JavaThread* volatile _init_thread;        // Pointer to current thread doing initialization (to handle recursive initialization)
 259 
 260   OopMapCache*    volatile _oop_map_cache;   // OopMapCache for all methods in the klass (allocated lazily)
 261   JNIid*          _jni_ids;              // First JNI identifier for static fields in this class
 262   jmethodID*      volatile _methods_jmethod_ids;  // jmethodIDs corresponding to method_idnum, or null if none
 263   nmethodBucket*  volatile _dep_context;          // packed DependencyContext structure
 264   uint64_t        volatile _dep_context_last_cleaned;
 265   nmethod*        _osr_nmethods_head;    // Head of list of on-stack replacement nmethods for this class
 266 #if INCLUDE_JVMTI
 267   BreakpointInfo* _breakpoints;          // bpt lists, managed by Method*
 268   // Linked instanceKlasses of previous versions
 269   InstanceKlass* _previous_versions;
 270   // JVMTI fields can be moved to their own structure - see 6315920
 271   // JVMTI: cached class file, before retransformable agent modified it in CFLH
 272   JvmtiCachedClassFileData* _cached_class_file;
 273 #endif
 274 
 275 #if INCLUDE_JVMTI
 276   JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map;  // JVMTI: used during heap iteration
 277 #endif
 278 
 279   NOT_PRODUCT(int _verify_count;)  // to avoid redundant verifies
 280   NOT_PRODUCT(volatile int _shared_class_load_count;) // ensure a shared class is loaded only once
 281 
 282   // Method array.
 283   Array<Method*>* _methods;
 284   // Default Method Array, concrete methods inherited from interfaces
 285   Array<Method*>* _default_methods;
 286   // Interfaces (InstanceKlass*s) this class declares locally to implement.
 287   Array<InstanceKlass*>* _local_interfaces;
 288   // Interfaces (InstanceKlass*s) this class implements transitively.
 289   Array<InstanceKlass*>* _transitive_interfaces;
 290   // Int array containing the original order of method in the class file (for JVMTI).
 291   Array<int>*     _method_ordering;
 292   // Int array containing the vtable_indices for default_methods
 293   // offset matches _default_methods offset
 294   Array<int>*     _default_vtable_indices;
 295 
 296   // Fields information is stored in an UNSIGNED5 encoded stream (see fieldInfo.hpp)
 297   Array<u1>*          _fieldinfo_stream;
 298   Array<FieldStatus>* _fields_status;
 299 
 300   Array<InlineKlass*>* _inline_type_field_klasses; // For "inline class" fields, null if none present
 301   Array<u2>* _preload_classes;
 302   const InlineKlassFixedBlock* _adr_inlineklass_fixed_block;
 303 
 304   // embedded Java vtable follows here
 305   // embedded Java itables follows here
 306   // embedded static fields follows here
 307   // embedded nonstatic oop-map blocks follows here
 308   // embedded implementor of this interface follows here
 309   //   The embedded implementor only exists if the current klass is an
 310   //   interface. The possible values of the implementor fall into following
 311   //   three cases:
 312   //     null: no implementor.
 313   //     A Klass* that's not itself: one implementor.
 314   //     Itself: more than one implementors.
 315   //
 316 
 317   friend class SystemDictionary;
 318 
 319   static bool _disable_method_binary_search;
 320 
 321   // Controls finalizer registration
 322   static bool _finalization_enabled;
 323 
 324  public:
 325 
 326   // Queries finalization state
 327   static bool is_finalization_enabled() { return _finalization_enabled; }
 328 
 329   // Sets finalization state
 330   static void set_finalization_enabled(bool val) { _finalization_enabled = val; }
 331 
 332   // The three BUILTIN class loader types
 333   bool is_shared_boot_class() const { return _misc_flags.is_shared_boot_class(); }
 334   bool is_shared_platform_class() const { return _misc_flags.is_shared_platform_class(); }
 335   bool is_shared_app_class() const {  return _misc_flags.is_shared_app_class(); }
 336   // The UNREGISTERED class loader type
 337   bool is_shared_unregistered_class() const { return _misc_flags.is_shared_unregistered_class(); }
 338 
 339   // Check if the class can be shared in CDS
 340   bool is_shareable() const;
 341 
 342   bool shared_loading_failed() const { return _misc_flags.shared_loading_failed(); }
 343 
 344   void set_shared_loading_failed() { _misc_flags.set_shared_loading_failed(true); }
 345 
 346 #if INCLUDE_CDS
 347   void set_shared_class_loader_type(s2 loader_type) { _misc_flags.set_shared_class_loader_type(loader_type); }
 348   void assign_class_loader_type() { _misc_flags.assign_class_loader_type(_class_loader_data); }
 349 #endif
 350 
 351   bool has_nonstatic_fields() const        { return _misc_flags.has_nonstatic_fields(); }
 352   void set_has_nonstatic_fields(bool b)    { _misc_flags.set_has_nonstatic_fields(b); }
 353 
 354   bool has_localvariable_table() const     { return _misc_flags.has_localvariable_table(); }
 355   void set_has_localvariable_table(bool b) { _misc_flags.set_has_localvariable_table(b); }
 356 
 357   bool has_inline_type_fields() const { return _misc_flags.has_inline_type_fields(); }
 358   void set_has_inline_type_fields()   { _misc_flags.set_has_inline_type_fields(true); }
 359 
 360   bool is_empty_inline_type() const   { return _misc_flags.is_empty_inline_type(); }
 361   void set_is_empty_inline_type()     { _misc_flags.set_is_empty_inline_type(true); }
 362 
 363   // Note:  The naturally_atomic property only applies to
 364   // inline classes; it is never true on identity classes.
 365   // The bit is placed on instanceKlass for convenience.
 366 
 367   // Query if h/w provides atomic load/store for instances.
 368   bool is_naturally_atomic() const  { return _misc_flags.is_naturally_atomic(); }
 369   void set_is_naturally_atomic()    { _misc_flags.set_is_naturally_atomic(true); }
 370 
 371   // Query if this class implements jl.NonTearable or was
 372   // mentioned in the JVM option ForceNonTearable.
 373   // This bit can occur anywhere, but is only significant
 374   // for inline classes *and* their super types.
 375   // It inherits from supers along with NonTearable.
 376   bool must_be_atomic() const { return _misc_flags.must_be_atomic(); }
 377   void set_must_be_atomic()   { _misc_flags.set_must_be_atomic(true); }
 378 
 379   bool is_implicitly_constructible() const { return _misc_flags.is_implicitly_constructible(); }
 380   void set_is_implicitly_constructible()   { _misc_flags.set_is_implicitly_constructible(true); }
 381 
 382   // field sizes
 383   int nonstatic_field_size() const         { return _nonstatic_field_size; }
 384   void set_nonstatic_field_size(int size)  { _nonstatic_field_size = size; }
 385 
 386   int static_field_size() const            { return _static_field_size; }
 387   void set_static_field_size(int size)     { _static_field_size = size; }
 388 
 389   int static_oop_field_count() const       { return (int)_static_oop_field_count; }
 390   void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
 391 
 392   // Java itable
 393   int  itable_length() const               { return _itable_len; }
 394   void set_itable_length(int len)          { _itable_len = len; }
 395 
 396   // array klasses
 397   ArrayKlass* array_klasses() const     { return _array_klasses; }
 398   inline ArrayKlass* array_klasses_acquire() const; // load with acquire semantics
 399   inline void release_set_array_klasses(ArrayKlass* k); // store with release semantics
 400   void set_array_klasses(ArrayKlass* k) { _array_klasses = k; }
 401 
 402   // methods
 403   Array<Method*>* methods() const          { return _methods; }
 404   void set_methods(Array<Method*>* a)      { _methods = a; }
 405   Method* method_with_idnum(int idnum);
 406   Method* method_with_orig_idnum(int idnum);
 407   Method* method_with_orig_idnum(int idnum, int version);
 408 
 409   // method ordering
 410   Array<int>* method_ordering() const     { return _method_ordering; }
 411   void set_method_ordering(Array<int>* m) { _method_ordering = m; }
 412   void copy_method_ordering(const intArray* m, TRAPS);
 413 
 414   // default_methods
 415   Array<Method*>* default_methods() const  { return _default_methods; }
 416   void set_default_methods(Array<Method*>* a) { _default_methods = a; }
 417 
 418   // default method vtable_indices
 419   Array<int>* default_vtable_indices() const { return _default_vtable_indices; }
 420   void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; }
 421   Array<int>* create_new_default_vtable_indices(int len, TRAPS);
 422 
 423   // interfaces
 424   Array<InstanceKlass*>* local_interfaces() const          { return _local_interfaces; }
 425   void set_local_interfaces(Array<InstanceKlass*>* a)      {
 426     guarantee(_local_interfaces == nullptr || a == nullptr, "Just checking");
 427     _local_interfaces = a; }
 428 
 429   Array<InstanceKlass*>* transitive_interfaces() const     { return _transitive_interfaces; }
 430   void set_transitive_interfaces(Array<InstanceKlass*>* a) {
 431     guarantee(_transitive_interfaces == nullptr || a == nullptr, "Just checking");
 432     _transitive_interfaces = a;
 433   }
 434 
 435  private:
 436   friend class fieldDescriptor;
 437   FieldInfo field(int index) const;
 438 
 439  public:
 440   int field_offset      (int index) const { return field(index).offset(); }
 441   int field_access_flags(int index) const { return field(index).access_flags().as_int(); }
 442   FieldInfo::FieldFlags field_flags(int index) const { return field(index).field_flags(); }
 443   FieldStatus field_status(int index)   const { return fields_status()->at(index); }
 444   inline Symbol* field_name        (int index) const;
 445   inline Symbol* field_signature   (int index) const;
 446   bool field_is_flat(int index) const { return field_flags(index).is_flat(); }
 447   bool field_is_null_free_inline_type(int index) const;
 448   bool is_class_in_preload_attribute(Symbol* name) const;
 449 
 450   // Number of Java declared fields
 451   int java_fields_count() const;
 452   int total_fields_count() const;
 453 
 454   Array<u1>* fieldinfo_stream() const { return _fieldinfo_stream; }
 455   void set_fieldinfo_stream(Array<u1>* fis) { _fieldinfo_stream = fis; }
 456 
 457   Array<FieldStatus>* fields_status() const {return _fields_status; }
 458   void set_fields_status(Array<FieldStatus>* array) { _fields_status = array; }
 459 
 460   Array<u2>* preload_classes() const { return _preload_classes; }
 461   void set_preload_classes(Array<u2>* c) { _preload_classes = c; }
 462 
 463   // inner classes
 464   Array<u2>* inner_classes() const       { return _inner_classes; }
 465   void set_inner_classes(Array<u2>* f)   { _inner_classes = f; }
 466 
 467   // nest members
 468   Array<u2>* nest_members() const     { return _nest_members; }
 469   void set_nest_members(Array<u2>* m) { _nest_members = m; }
 470 
 471   // nest-host index
 472   jushort nest_host_index() const { return _nest_host_index; }
 473   void set_nest_host_index(u2 i)  { _nest_host_index = i; }
 474   // dynamic nest member support
 475   void set_nest_host(InstanceKlass* host);
 476 
 477   // record components
 478   Array<RecordComponent*>* record_components() const { return _record_components; }
 479   void set_record_components(Array<RecordComponent*>* record_components) {
 480     _record_components = record_components;
 481   }
 482   bool is_record() const;
 483 
 484   // permitted subclasses
 485   Array<u2>* permitted_subclasses() const     { return _permitted_subclasses; }
 486   void set_permitted_subclasses(Array<u2>* s) { _permitted_subclasses = s; }
 487 
 488 private:
 489   // Called to verify that k is a member of this nest - does not look at k's nest-host,
 490   // nor does it resolve any CP entries or load any classes.
 491   bool has_nest_member(JavaThread* current, InstanceKlass* k) const;
 492 
 493 public:
 494   // Call this only if you know that the nest host has been initialized.
 495   InstanceKlass* nest_host_not_null() {
 496     assert(_nest_host != nullptr, "must be");
 497     return _nest_host;
 498   }
 499   // Used to construct informative IllegalAccessError messages at a higher level,
 500   // if there was an issue resolving or validating the nest host.
 501   // Returns null if there was no error.
 502   const char* nest_host_error();
 503   // Returns nest-host class, resolving and validating it if needed.
 504   // Returns null if resolution is not possible from the calling context.
 505   InstanceKlass* nest_host(TRAPS);
 506   // Check if this klass is a nestmate of k - resolves this nest-host and k's
 507   bool has_nestmate_access_to(InstanceKlass* k, TRAPS);
 508 
 509   // Called to verify that k is a permitted subclass of this class
 510   bool has_as_permitted_subclass(const InstanceKlass* k) const;
 511 
 512   enum InnerClassAttributeOffset {
 513     // From http://mirror.eng/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc10.html#18814
 514     inner_class_inner_class_info_offset = 0,
 515     inner_class_outer_class_info_offset = 1,
 516     inner_class_inner_name_offset = 2,
 517     inner_class_access_flags_offset = 3,
 518     inner_class_next_offset = 4
 519   };
 520 
 521   enum EnclosingMethodAttributeOffset {
 522     enclosing_method_class_index_offset = 0,
 523     enclosing_method_method_index_offset = 1,
 524     enclosing_method_attribute_size = 2
 525   };
 526 
 527   // package
 528   PackageEntry* package() const     { return _package_entry; }
 529   ModuleEntry* module() const;
 530   bool in_unnamed_package() const   { return (_package_entry == nullptr); }
 531   void set_package(ClassLoaderData* loader_data, PackageEntry* pkg_entry, TRAPS);
 532   // If the package for the InstanceKlass is in the boot loader's package entry
 533   // table then sets the classpath_index field so that
 534   // get_system_package() will know to return a non-null value for the
 535   // package's location.  And, so that the package will be added to the list of
 536   // packages returned by get_system_packages().
 537   // For packages whose classes are loaded from the boot loader class path, the
 538   // classpath_index indicates which entry on the boot loader class path.
 539   void set_classpath_index(s2 path_index);
 540   bool is_same_class_package(const Klass* class2) const;
 541   bool is_same_class_package(oop other_class_loader, const Symbol* other_class_name) const;
 542 
 543   // find an enclosing class
 544   InstanceKlass* compute_enclosing_class(bool* inner_is_member, TRAPS) const;
 545 
 546   // Find InnerClasses attribute and return outer_class_info_index & inner_name_index.
 547   bool find_inner_classes_attr(int* ooff, int* noff, TRAPS) const;
 548 
 549  private:
 550   // Check prohibited package ("java/" only loadable by boot or platform loaders)
 551   static void check_prohibited_package(Symbol* class_name,
 552                                        ClassLoaderData* loader_data,
 553                                        TRAPS);
 554 
 555   JavaThread* init_thread()  { return Atomic::load(&_init_thread); }
 556   // We can safely access the name as long as we hold the _init_monitor.
 557   const char* init_thread_name() {
 558     assert(_init_monitor->owned_by_self(), "Must hold _init_monitor here");
 559     return init_thread()->name_raw();
 560   }
 561 
 562  public:
 563   // initialization state
 564   bool is_loaded() const                   { return init_state() >= loaded; }
 565   bool is_linked() const                   { return init_state() >= linked; }
 566   bool is_being_linked() const             { return init_state() == being_linked; }
 567   bool is_initialized() const              { return init_state() == fully_initialized; }
 568   bool is_not_initialized() const          { return init_state() <  being_initialized; }
 569   bool is_being_initialized() const        { return init_state() == being_initialized; }
 570   bool is_in_error_state() const           { return init_state() == initialization_error; }
 571   bool is_init_thread(JavaThread *thread)  { return thread == init_thread(); }
 572   ClassState  init_state() const           { return Atomic::load(&_init_state); }
 573   const char* init_state_name() const;
 574   bool is_rewritten() const                { return _misc_flags.rewritten(); }
 575 
 576   class LockLinkState : public StackObj {
 577     InstanceKlass* _ik;
 578     JavaThread*    _current;
 579    public:
 580     LockLinkState(InstanceKlass* ik, JavaThread* current) : _ik(ik), _current(current) {
 581       ik->check_link_state_and_wait(current);
 582     }
 583     ~LockLinkState() {
 584       if (!_ik->is_linked()) {
 585         // Reset to loaded if linking failed.
 586         _ik->set_initialization_state_and_notify(loaded, _current);
 587       }
 588     }
 589   };
 590 
 591   // is this a sealed class
 592   bool is_sealed() const;
 593 
 594   // defineClass specified verification
 595   bool should_verify_class() const         { return _misc_flags.should_verify_class(); }
 596   void set_should_verify_class(bool value) { _misc_flags.set_should_verify_class(value); }
 597 
 598   // marking
 599   bool is_marked_dependent() const         { return _misc_flags.is_marked_dependent(); }
 600   void set_is_marked_dependent(bool value) { _misc_flags.set_is_marked_dependent(value); }
 601 
 602   static ByteSize kind_offset() { return in_ByteSize(offset_of(InstanceKlass, _kind)); }
 603   static ByteSize misc_flags_offset() { return in_ByteSize(offset_of(InstanceKlass, _misc_flags)); }
 604 
 605   // initialization (virtuals from Klass)
 606   bool should_be_initialized() const;  // means that initialize should be called
 607   void initialize(TRAPS);
 608   void link_class(TRAPS);
 609   bool link_class_or_fail(TRAPS); // returns false on failure
 610   void rewrite_class(TRAPS);
 611   void link_methods(TRAPS);
 612   Method* class_initializer() const;
 613 
 614   // reference type
 615   ReferenceType reference_type() const     { return (ReferenceType)_reference_type; }
 616 
 617   // this class cp index
 618   u2 this_class_index() const             { return _this_class_index; }
 619   void set_this_class_index(u2 index)     { _this_class_index = index; }
 620 
 621   static ByteSize reference_type_offset() { return byte_offset_of(InstanceKlass, _reference_type); }
 622 
 623   // find local field, returns true if found
 624   bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
 625   // find field in direct superinterfaces, returns the interface in which the field is defined
 626   Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
 627   // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 628   Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
 629   // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 630   Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const;
 631 
 632   // find a non-static or static field given its offset within the class.
 633   bool contains_field_offset(int offset);
 634 
 635   bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
 636   bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
 637 
 638  private:
 639   inline static int quick_search(const Array<Method*>* methods, const Symbol* name);
 640 
 641  public:
 642   static void disable_method_binary_search() {
 643     _disable_method_binary_search = true;
 644   }
 645 
 646   // find a local method (returns null if not found)
 647   Method* find_method(const Symbol* name, const Symbol* signature) const;
 648   static Method* find_method(const Array<Method*>* methods,
 649                              const Symbol* name,
 650                              const Symbol* signature);
 651 
 652   // find a local method, but skip static methods
 653   Method* find_instance_method(const Symbol* name, const Symbol* signature,
 654                                PrivateLookupMode private_mode) const;
 655   static Method* find_instance_method(const Array<Method*>* methods,
 656                                       const Symbol* name,
 657                                       const Symbol* signature,
 658                                       PrivateLookupMode private_mode);
 659 
 660   // find a local method (returns null if not found)
 661   Method* find_local_method(const Symbol* name,
 662                             const Symbol* signature,
 663                             OverpassLookupMode overpass_mode,
 664                             StaticLookupMode static_mode,
 665                             PrivateLookupMode private_mode) const;
 666 
 667   // find a local method from given methods array (returns null if not found)
 668   static Method* find_local_method(const Array<Method*>* methods,
 669                                    const Symbol* name,
 670                                    const Symbol* signature,
 671                                    OverpassLookupMode overpass_mode,
 672                                    StaticLookupMode static_mode,
 673                                    PrivateLookupMode private_mode);
 674 
 675   // find a local method index in methods or default_methods (returns -1 if not found)
 676   static int find_method_index(const Array<Method*>* methods,
 677                                const Symbol* name,
 678                                const Symbol* signature,
 679                                OverpassLookupMode overpass_mode,
 680                                StaticLookupMode static_mode,
 681                                PrivateLookupMode private_mode);
 682 
 683   // lookup operation (returns null if not found)
 684   Method* uncached_lookup_method(const Symbol* name,
 685                                  const Symbol* signature,
 686                                  OverpassLookupMode overpass_mode,
 687                                  PrivateLookupMode private_mode = PrivateLookupMode::find) const;
 688 
 689   // lookup a method in all the interfaces that this class implements
 690   // (returns null if not found)
 691   Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature, DefaultsLookupMode defaults_mode) const;
 692 
 693   // lookup a method in local defaults then in all interfaces
 694   // (returns null if not found)
 695   Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const;
 696 
 697   // Find method indices by name.  If a method with the specified name is
 698   // found the index to the first method is returned, and 'end' is filled in
 699   // with the index of first non-name-matching method.  If no method is found
 700   // -1 is returned.
 701   int find_method_by_name(const Symbol* name, int* end) const;
 702   static int find_method_by_name(const Array<Method*>* methods,
 703                                  const Symbol* name, int* end);
 704 
 705   // constant pool
 706   ConstantPool* constants() const        { return _constants; }
 707   void set_constants(ConstantPool* c)    { _constants = c; }
 708 
 709   // protection domain
 710   oop protection_domain() const;
 711 
 712   // signers
 713   objArrayOop signers() const;
 714 
 715   bool is_contended() const                { return _misc_flags.is_contended(); }
 716   void set_is_contended(bool value)        { _misc_flags.set_is_contended(value); }
 717 
 718   // source file name
 719   Symbol* source_file_name() const;
 720   u2 source_file_name_index() const;
 721   void set_source_file_name_index(u2 sourcefile_index);
 722 
 723   // minor and major version numbers of class file
 724   u2 minor_version() const;
 725   void set_minor_version(u2 minor_version);
 726   u2 major_version() const;
 727   void set_major_version(u2 major_version);
 728 
 729   // source debug extension
 730   const char* source_debug_extension() const { return _source_debug_extension; }
 731   void set_source_debug_extension(const char* array, int length);
 732 
 733   // nonstatic oop-map blocks
 734   static int nonstatic_oop_map_size(unsigned int oop_map_count) {
 735     return oop_map_count * OopMapBlock::size_in_words();
 736   }
 737   unsigned int nonstatic_oop_map_count() const {
 738     return _nonstatic_oop_map_size / OopMapBlock::size_in_words();
 739   }
 740   int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; }
 741   void set_nonstatic_oop_map_size(int words) {
 742     _nonstatic_oop_map_size = words;
 743   }
 744 
 745   bool has_contended_annotations() const { return _misc_flags.has_contended_annotations(); }
 746   void set_has_contended_annotations(bool value)  { _misc_flags.set_has_contended_annotations(value); }
 747 
 748 #if INCLUDE_JVMTI
 749   // Redefinition locking.  Class can only be redefined by one thread at a time.
 750   // The flag is in access_flags so that it can be set and reset using atomic
 751   // operations, and not be reset by other misc_flag settings.
 752   bool is_being_redefined() const          { return _misc_flags.is_being_redefined(); }
 753   void set_is_being_redefined(bool value)  { _misc_flags.set_is_being_redefined(value); }
 754 
 755   // RedefineClasses() support for previous versions:
 756   void add_previous_version(InstanceKlass* ik, int emcp_method_count);
 757   void purge_previous_version_list();
 758 
 759   InstanceKlass* previous_versions() const { return _previous_versions; }
 760 #else
 761   InstanceKlass* previous_versions() const { return nullptr; }
 762 #endif
 763 
 764   InstanceKlass* get_klass_version(int version);
 765 
 766   bool has_been_redefined() const { return _misc_flags.has_been_redefined(); }
 767   void set_has_been_redefined() { _misc_flags.set_has_been_redefined(true); }
 768 
 769   bool is_scratch_class() const { return _misc_flags.is_scratch_class(); }
 770   void set_is_scratch_class() { _misc_flags.set_is_scratch_class(true); }
 771 
 772   bool has_resolved_methods() const { return _misc_flags.has_resolved_methods(); }
 773   void set_has_resolved_methods()   { _misc_flags.set_has_resolved_methods(true); }
 774   void set_has_resolved_methods(bool value)   { _misc_flags.set_has_resolved_methods(value); }
 775 
 776 public:
 777 #if INCLUDE_JVMTI
 778 
 779   void init_previous_versions() {
 780     _previous_versions = nullptr;
 781   }
 782 
 783  private:
 784   static bool  _should_clean_previous_versions;
 785  public:
 786   static void purge_previous_versions(InstanceKlass* ik) {
 787     if (ik->has_been_redefined()) {
 788       ik->purge_previous_version_list();
 789     }
 790   }
 791 
 792   static bool should_clean_previous_versions_and_reset();
 793   static bool should_clean_previous_versions() { return _should_clean_previous_versions; }
 794 
 795   // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation
 796   void set_cached_class_file(JvmtiCachedClassFileData *data) {
 797     _cached_class_file = data;
 798   }
 799   JvmtiCachedClassFileData * get_cached_class_file();
 800   jint get_cached_class_file_len();
 801   unsigned char * get_cached_class_file_bytes();
 802 
 803   // JVMTI: Support for caching of field indices, types, and offsets
 804   void set_jvmti_cached_class_field_map(JvmtiCachedClassFieldMap* descriptor) {
 805     _jvmti_cached_class_field_map = descriptor;
 806   }
 807   JvmtiCachedClassFieldMap* jvmti_cached_class_field_map() const {
 808     return _jvmti_cached_class_field_map;
 809   }
 810 #else // INCLUDE_JVMTI
 811 
 812   static void purge_previous_versions(InstanceKlass* ik) { return; };
 813   static bool should_clean_previous_versions_and_reset() { return false; }
 814 
 815   void set_cached_class_file(JvmtiCachedClassFileData *data) {
 816     assert(data == nullptr, "unexpected call with JVMTI disabled");
 817   }
 818   JvmtiCachedClassFileData * get_cached_class_file() { return (JvmtiCachedClassFileData *)nullptr; }
 819 
 820 #endif // INCLUDE_JVMTI
 821 
 822   bool has_nonstatic_concrete_methods() const { return _misc_flags.has_nonstatic_concrete_methods(); }
 823   void set_has_nonstatic_concrete_methods(bool b) { _misc_flags.set_has_nonstatic_concrete_methods(b); }
 824 
 825   bool declares_nonstatic_concrete_methods() const { return _misc_flags.declares_nonstatic_concrete_methods(); }
 826   void set_declares_nonstatic_concrete_methods(bool b) { _misc_flags.set_declares_nonstatic_concrete_methods(b); }
 827 
 828   bool has_vanilla_constructor() const  { return _misc_flags.has_vanilla_constructor(); }
 829   void set_has_vanilla_constructor()    { _misc_flags.set_has_vanilla_constructor(true); }
 830   bool has_miranda_methods () const     { return _misc_flags.has_miranda_methods(); }
 831   void set_has_miranda_methods()        { _misc_flags.set_has_miranda_methods(true); }
 832   bool has_final_method() const         { return _misc_flags.has_final_method(); }
 833   void set_has_final_method()           { _misc_flags.set_has_final_method(true); }
 834 
 835   // for adding methods, ConstMethod::UNSET_IDNUM means no more ids available
 836   inline u2 next_method_idnum();
 837   void set_initial_method_idnum(u2 value)             { _idnum_allocated_count = value; }
 838 
 839   // generics support
 840   Symbol* generic_signature() const;
 841   u2 generic_signature_index() const;
 842   void set_generic_signature_index(u2 sig_index);
 843 
 844   u2 enclosing_method_data(int offset) const;
 845   u2 enclosing_method_class_index() const {
 846     return enclosing_method_data(enclosing_method_class_index_offset);
 847   }
 848   u2 enclosing_method_method_index() {
 849     return enclosing_method_data(enclosing_method_method_index_offset);
 850   }
 851   void set_enclosing_method_indices(u2 class_index,
 852                                     u2 method_index);
 853 
 854   // jmethodID support
 855   jmethodID get_jmethod_id(const methodHandle& method_h);
 856   jmethodID get_jmethod_id_fetch_or_update(size_t idnum,
 857                      jmethodID new_id, jmethodID* new_jmeths,
 858                      jmethodID* to_dealloc_id_p,
 859                      jmethodID** to_dealloc_jmeths_p);
 860   static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum,
 861                 size_t *length_p, jmethodID* id_p);
 862   void ensure_space_for_methodids(int start_offset = 0);
 863   jmethodID jmethod_id_or_null(Method* method);
 864 
 865   // annotations support
 866   Annotations* annotations() const          { return _annotations; }
 867   void set_annotations(Annotations* anno)   { _annotations = anno; }
 868 
 869   AnnotationArray* class_annotations() const {
 870     return (_annotations != nullptr) ? _annotations->class_annotations() : nullptr;
 871   }
 872   Array<AnnotationArray*>* fields_annotations() const {
 873     return (_annotations != nullptr) ? _annotations->fields_annotations() : nullptr;
 874   }
 875   AnnotationArray* class_type_annotations() const {
 876     return (_annotations != nullptr) ? _annotations->class_type_annotations() : nullptr;
 877   }
 878   Array<AnnotationArray*>* fields_type_annotations() const {
 879     return (_annotations != nullptr) ? _annotations->fields_type_annotations() : nullptr;
 880   }
 881   // allocation
 882   instanceOop allocate_instance(TRAPS);
 883   static instanceOop allocate_instance(oop cls, TRAPS);
 884 
 885   // additional member function to return a handle
 886   instanceHandle allocate_instance_handle(TRAPS);
 887 
 888   objArrayOop allocate_objArray(int n, int length, TRAPS);
 889   // Helper function
 890   static instanceOop register_finalizer(instanceOop i, TRAPS);
 891 
 892   // Check whether reflection/jni/jvm code is allowed to instantiate this class;
 893   // if not, throw either an Error or an Exception.
 894   virtual void check_valid_for_instantiation(bool throwError, TRAPS);
 895 
 896   // initialization
 897   void call_class_initializer(TRAPS);
 898   void set_initialization_state_and_notify(ClassState state, JavaThread* current);
 899 
 900   // OopMapCache support
 901   OopMapCache* oop_map_cache()               { return _oop_map_cache; }
 902   void set_oop_map_cache(OopMapCache *cache) { _oop_map_cache = cache; }
 903   void mask_for(const methodHandle& method, int bci, InterpreterOopMap* entry);
 904 
 905   // JNI identifier support (for static fields - for jni performance)
 906   JNIid* jni_ids()                               { return _jni_ids; }
 907   void set_jni_ids(JNIid* ids)                   { _jni_ids = ids; }
 908   JNIid* jni_id_for(int offset);
 909 
 910   // maintenance of deoptimization dependencies
 911   inline DependencyContext dependencies();
 912   void mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes);
 913   void add_dependent_nmethod(nmethod* nm);
 914   void clean_dependency_context();
 915   // Setup link to hierarchy and deoptimize
 916   void add_to_hierarchy(JavaThread* current);
 917 
 918   // On-stack replacement support
 919   nmethod* osr_nmethods_head() const         { return _osr_nmethods_head; };
 920   void set_osr_nmethods_head(nmethod* h)     { _osr_nmethods_head = h; };
 921   void add_osr_nmethod(nmethod* n);
 922   bool remove_osr_nmethod(nmethod* n);
 923   int mark_osr_nmethods(DeoptimizationScope* deopt_scope, const Method* m);
 924   nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
 925 
 926 #if INCLUDE_JVMTI
 927   // Breakpoint support (see methods on Method* for details)
 928   BreakpointInfo* breakpoints() const       { return _breakpoints; };
 929   void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
 930 #endif
 931 
 932   // support for stub routines
 933   static ByteSize init_state_offset()  { return byte_offset_of(InstanceKlass, _init_state); }
 934   JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)
 935   static ByteSize init_thread_offset() { return byte_offset_of(InstanceKlass, _init_thread); }
 936 
 937   static ByteSize inline_type_field_klasses_offset() { return in_ByteSize(offset_of(InstanceKlass, _inline_type_field_klasses)); }
 938   static ByteSize adr_inlineklass_fixed_block_offset() { return in_ByteSize(offset_of(InstanceKlass, _adr_inlineklass_fixed_block)); }
 939 
 940   // subclass/subinterface checks
 941   bool implements_interface(Klass* k) const;
 942   bool is_same_or_direct_interface(Klass* k) const;
 943 
 944 #ifdef ASSERT
 945   // check whether this class or one of its superclasses was redefined
 946   bool has_redefined_this_or_super() const;
 947 #endif
 948 
 949   // Access to the implementor of an interface.
 950   InstanceKlass* implementor() const;
 951   void set_implementor(InstanceKlass* ik);
 952   int  nof_implementors() const;
 953   void add_implementor(InstanceKlass* ik);  // ik is a new class that implements this interface
 954   void init_implementor();           // initialize
 955 
 956  private:
 957   // link this class into the implementors list of every interface it implements
 958   void process_interfaces();
 959 
 960  public:
 961   // virtual operations from Klass
 962   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
 963                                                   Array<InstanceKlass*>* transitive_interfaces);
 964   bool can_be_primary_super_slow() const;
 965   size_t oop_size(oop obj)  const             { return size_helper(); }
 966   // slow because it's a virtual call and used for verifying the layout_helper.
 967   // Using the layout_helper bits, we can call is_instance_klass without a virtual call.
 968   DEBUG_ONLY(bool is_instance_klass_slow() const      { return true; })
 969 
 970   // Iterators
 971   void do_local_static_fields(FieldClosure* cl);
 972   void do_nonstatic_fields(FieldClosure* cl); // including inherited fields
 973   void do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle, TRAPS);
 974   void print_nonstatic_fields(FieldClosure* cl); // including inherited and injected fields
 975 
 976   void methods_do(void f(Method* method));
 977 
 978   static InstanceKlass* cast(Klass* k) {
 979     return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k)));
 980   }
 981 
 982   static const InstanceKlass* cast(const Klass* k) {
 983     assert(k != nullptr, "k should not be null");
 984     assert(k->is_instance_klass(), "cast to InstanceKlass");
 985     return static_cast<const InstanceKlass*>(k);
 986   }
 987 
 988   virtual InstanceKlass* java_super() const {
 989     return (super() == nullptr) ? nullptr : cast(super());
 990   }
 991 
 992   // Sizing (in words)
 993   static int header_size()            { return sizeof(InstanceKlass)/wordSize; }
 994 
 995   static int size(int vtable_length, int itable_length,
 996                   int nonstatic_oop_map_size,
 997                   bool is_interface,
 998                   bool is_inline_type) {
 999     return align_metadata_size(header_size() +
1000            vtable_length +
1001            itable_length +
1002            nonstatic_oop_map_size +
1003            (is_interface ? (int)sizeof(Klass*)/wordSize : 0) +
1004            (is_inline_type ? (int)sizeof(InlineKlassFixedBlock) : 0));
1005   }
1006 
1007   int size() const                    { return size(vtable_length(),
1008                                                itable_length(),
1009                                                nonstatic_oop_map_size(),
1010                                                is_interface(),
1011                                                is_inline_klass());
1012   }
1013 
1014 
1015   inline intptr_t* start_of_itable() const;
1016   inline intptr_t* end_of_itable() const;
1017   inline oop static_field_base_raw();
1018   bool bounds_check(address addr, bool edge_ok = false, intptr_t size_in_bytes = -1) const PRODUCT_RETURN0;
1019 
1020   inline OopMapBlock* start_of_nonstatic_oop_maps() const;
1021   inline Klass** end_of_nonstatic_oop_maps() const;
1022 
1023   inline InstanceKlass* volatile* adr_implementor() const;
1024 
1025   Array<InlineKlass*>* inline_type_field_klasses_array() const { return _inline_type_field_klasses; }
1026   void set_inline_type_field_klasses_array(Array<InlineKlass*>* array) { _inline_type_field_klasses = array; }
1027 
1028   inline InlineKlass* get_inline_type_field_klass(int idx) const;
1029   inline InlineKlass* get_inline_type_field_klass_or_null(int idx) const;
1030   inline void set_inline_type_field_klass(int idx, InlineKlass* k);
1031   inline void reset_inline_type_field_klass(int idx);
1032 
1033   // Use this to return the size of an instance in heap words:
1034   virtual int size_helper() const {
1035     return layout_helper_to_size_helper(layout_helper());
1036   }
1037 
1038   // This bit is initialized in classFileParser.cpp.
1039   // It is false under any of the following conditions:
1040   //  - the class is abstract (including any interface)
1041   //  - the class has a finalizer (if !RegisterFinalizersAtInit)
1042   //  - the class size is larger than FastAllocateSizeLimit
1043   //  - the class is java/lang/Class, which cannot be allocated directly
1044   bool can_be_fastpath_allocated() const {
1045     return !layout_helper_needs_slow_path(layout_helper());
1046   }
1047 
1048   // Java itable
1049   klassItable itable() const;        // return klassItable wrapper
1050   Method* method_at_itable(InstanceKlass* holder, int index, TRAPS);
1051   Method* method_at_itable_or_null(InstanceKlass* holder, int index, bool& itable_entry_found);
1052   int vtable_index_of_interface_method(Method* method);
1053 
1054 #if INCLUDE_JVMTI
1055   void adjust_default_methods(bool* trace_name_printed);
1056 #endif // INCLUDE_JVMTI
1057 
1058   void clean_weak_instanceklass_links();
1059  private:
1060   void clean_implementors_list();
1061   void clean_method_data();
1062 
1063  public:
1064   // Explicit metaspace deallocation of fields
1065   // For RedefineClasses and class file parsing errors, we need to deallocate
1066   // instanceKlasses and the metadata they point to.
1067   void deallocate_contents(ClassLoaderData* loader_data);
1068   static void deallocate_methods(ClassLoaderData* loader_data,
1069                                  Array<Method*>* methods);
1070   void static deallocate_interfaces(ClassLoaderData* loader_data,
1071                                     const Klass* super_klass,
1072                                     Array<InstanceKlass*>* local_interfaces,
1073                                     Array<InstanceKlass*>* transitive_interfaces);
1074   void static deallocate_record_components(ClassLoaderData* loader_data,
1075                                            Array<RecordComponent*>* record_component);
1076 
1077   virtual bool on_stack() const;
1078 
1079   // callbacks for actions during class unloading
1080   static void unload_class(InstanceKlass* ik);
1081 
1082   virtual void release_C_heap_structures(bool release_sub_metadata = true);
1083 
1084   // Naming
1085   const char* signature_name() const;
1086   const char* signature_name_of_carrier(char c) const;
1087 
1088   // Oop fields (and metadata) iterators
1089   //
1090   // The InstanceKlass iterators also visits the Object's klass.
1091 
1092   // Forward iteration
1093  public:
1094   // Iterate over all oop fields in the oop maps.
1095   template <typename T, class OopClosureType>
1096   inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1097 
1098   // Iterate over all oop fields and metadata.
1099   template <typename T, class OopClosureType>
1100   inline void oop_oop_iterate(oop obj, OopClosureType* closure);
1101 
1102   // Iterate over all oop fields in one oop map.
1103   template <typename T, class OopClosureType>
1104   inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1105 
1106 
1107   // Reverse iteration
1108   // Iterate over all oop fields and metadata.
1109   template <typename T, class OopClosureType>
1110   inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
1111 
1112  private:
1113   // Iterate over all oop fields in the oop maps.
1114   template <typename T, class OopClosureType>
1115   inline void oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure);
1116 
1117   // Iterate over all oop fields in one oop map.
1118   template <typename T, class OopClosureType>
1119   inline void oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure);
1120 
1121 
1122   // Bounded range iteration
1123  public:
1124   // Iterate over all oop fields in the oop maps.
1125   template <typename T, class OopClosureType>
1126   inline void oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1127 
1128   // Iterate over all oop fields and metadata.
1129   template <typename T, class OopClosureType>
1130   inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1131 
1132  private:
1133   // Iterate over all oop fields in one oop map.
1134   template <typename T, class OopClosureType>
1135   inline void oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr);
1136 
1137 
1138  public:
1139   u2 idnum_allocated_count() const      { return _idnum_allocated_count; }
1140 
1141  private:
1142   // initialization state
1143   void set_init_state(ClassState state);
1144   void set_rewritten()                  { _misc_flags.set_rewritten(true); }
1145   void set_init_thread(JavaThread *thread)  {
1146     assert((thread == JavaThread::current() && _init_thread == nullptr) ||
1147            (thread == nullptr && _init_thread == JavaThread::current()), "Only one thread is allowed to own initialization");
1148     Atomic::store(&_init_thread, thread);
1149   }
1150 
1151   // The RedefineClasses() API can cause new method idnums to be needed
1152   // which will cause the caches to grow. Safety requires different
1153   // cache management logic if the caches can grow instead of just
1154   // going from null to non-null.
1155   bool idnum_can_increment() const      { return has_been_redefined(); }
1156   inline jmethodID* methods_jmethod_ids_acquire() const;
1157   inline void release_set_methods_jmethod_ids(jmethodID* jmeths);
1158   // This nulls out jmethodIDs for all methods in 'klass'
1159   static void clear_jmethod_ids(InstanceKlass* klass);
1160 
1161   // Lock during initialization
1162 public:
1163   // Returns the array class for the n'th dimension
1164   virtual ArrayKlass* array_klass(int n, TRAPS);
1165   virtual ArrayKlass* array_klass_or_null(int n);
1166 
1167   // Returns the array class with this class as element type
1168   virtual ArrayKlass* array_klass(TRAPS);
1169   virtual ArrayKlass* array_klass_or_null();
1170 
1171   static void clean_initialization_error_table();
1172 
1173   Monitor* init_monitor() const { return _init_monitor; }
1174 private:
1175   void check_link_state_and_wait(JavaThread* current);
1176   bool link_class_impl                           (TRAPS);
1177   bool verify_code                               (TRAPS);
1178   void initialize_impl                           (TRAPS);
1179   void initialize_super_interfaces               (TRAPS);
1180 
1181   void add_initialization_error(JavaThread* current, Handle exception);
1182   oop get_initialization_error(JavaThread* current);
1183 
1184   // find a local method (returns null if not found)
1185   Method* find_method_impl(const Symbol* name,
1186                            const Symbol* signature,
1187                            OverpassLookupMode overpass_mode,
1188                            StaticLookupMode static_mode,
1189                            PrivateLookupMode private_mode) const;
1190 
1191   static Method* find_method_impl(const Array<Method*>* methods,
1192                                   const Symbol* name,
1193                                   const Symbol* signature,
1194                                   OverpassLookupMode overpass_mode,
1195                                   StaticLookupMode static_mode,
1196                                   PrivateLookupMode private_mode);
1197 
1198 #if INCLUDE_JVMTI
1199   // RedefineClasses support
1200   void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; }
1201   void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count);
1202 #endif
1203   // log class name to classlist
1204   void log_to_classlist() const;
1205 public:
1206 
1207 #if INCLUDE_CDS
1208   // CDS support - remove and restore oops from metadata. Oops are not shared.
1209   virtual void remove_unshareable_info();
1210   void remove_unshareable_flags();
1211   virtual void remove_java_mirror();
1212   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
1213   void init_shared_package_entry();
1214   bool can_be_verified_at_dumptime() const;
1215   bool methods_contain_jsr_bytecode() const;
1216   void compute_has_loops_flag_for_methods();
1217 #endif
1218 
1219   jint compute_modifier_flags() const;
1220 
1221 public:
1222   // JVMTI support
1223   jint jvmti_class_status() const;
1224 
1225   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
1226 
1227  public:
1228   // Printing
1229   void print_on(outputStream* st) const;
1230   void print_value_on(outputStream* st) const;
1231 
1232   void oop_print_value_on(oop obj, outputStream* st);
1233 
1234   void oop_print_on      (oop obj, outputStream* st);
1235 
1236 #ifndef PRODUCT
1237   void print_dependent_nmethods(bool verbose = false);
1238   bool is_dependent_nmethod(nmethod* nm);
1239   bool verify_itable_index(int index);
1240 #endif
1241 
1242   const char* internal_name() const;
1243 
1244   // Verification
1245   void verify_on(outputStream* st);
1246 
1247   void oop_verify_on(oop obj, outputStream* st);
1248 
1249   // Logging
1250   void print_class_load_logging(ClassLoaderData* loader_data,
1251                                 const ModuleEntry* module_entry,
1252                                 const ClassFileStream* cfs) const;
1253  private:
1254   void print_class_load_cause_logging() const;
1255   void print_class_load_helper(ClassLoaderData* loader_data,
1256                                const ModuleEntry* module_entry,
1257                                const ClassFileStream* cfs) const;
1258 };
1259 
1260 // for adding methods
1261 // UNSET_IDNUM return means no more ids available
1262 inline u2 InstanceKlass::next_method_idnum() {
1263   if (_idnum_allocated_count == ConstMethod::MAX_IDNUM) {
1264     return ConstMethod::UNSET_IDNUM; // no more ids available
1265   } else {
1266     return _idnum_allocated_count++;
1267   }
1268 }
1269 
1270 class PrintClassClosure : public KlassClosure {
1271 private:
1272   outputStream* _st;
1273   bool _verbose;
1274 public:
1275   PrintClassClosure(outputStream* st, bool verbose);
1276 
1277   void do_klass(Klass* k);
1278 };
1279 
1280 /* JNIid class for jfieldIDs only */
1281 class JNIid: public CHeapObj<mtClass> {
1282   friend class VMStructs;
1283  private:
1284   Klass*             _holder;
1285   JNIid*             _next;
1286   int                _offset;
1287 #ifdef ASSERT
1288   bool               _is_static_field_id;
1289 #endif
1290 
1291  public:
1292   // Accessors
1293   Klass* holder() const           { return _holder; }
1294   int offset() const              { return _offset; }
1295   JNIid* next()                   { return _next; }
1296   // Constructor
1297   JNIid(Klass* holder, int offset, JNIid* next);
1298   // Identifier lookup
1299   JNIid* find(int offset);
1300 
1301   bool find_local_field(fieldDescriptor* fd) {
1302     return InstanceKlass::cast(holder())->find_local_field_from_offset(offset(), true, fd);
1303   }
1304 
1305   static void deallocate(JNIid* id);
1306   // Debugging
1307 #ifdef ASSERT
1308   bool is_static_field_id() const { return _is_static_field_id; }
1309   void set_is_static_field_id()   { _is_static_field_id = true; }
1310 #endif
1311   void verify(Klass* holder);
1312 };
1313 
1314 // An iterator that's used to access the inner classes indices in the
1315 // InstanceKlass::_inner_classes array.
1316 class InnerClassesIterator : public StackObj {
1317  private:
1318   Array<jushort>* _inner_classes;
1319   int _length;
1320   int _idx;
1321  public:
1322 
1323   InnerClassesIterator(const InstanceKlass* k) {
1324     _inner_classes = k->inner_classes();
1325     if (k->inner_classes() != nullptr) {
1326       _length = _inner_classes->length();
1327       // The inner class array's length should be the multiple of
1328       // inner_class_next_offset if it only contains the InnerClasses
1329       // attribute data, or it should be
1330       // n*inner_class_next_offset+enclosing_method_attribute_size
1331       // if it also contains the EnclosingMethod data.
1332       assert((_length % InstanceKlass::inner_class_next_offset == 0 ||
1333               _length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size),
1334              "just checking");
1335       // Remove the enclosing_method portion if exists.
1336       if (_length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size) {
1337         _length -= InstanceKlass::enclosing_method_attribute_size;
1338       }
1339     } else {
1340       _length = 0;
1341     }
1342     _idx = 0;
1343   }
1344 
1345   int length() const {
1346     return _length;
1347   }
1348 
1349   void next() {
1350     _idx += InstanceKlass::inner_class_next_offset;
1351   }
1352 
1353   bool done() const {
1354     return (_idx >= _length);
1355   }
1356 
1357   u2 inner_class_info_index() const {
1358     return _inner_classes->at(
1359                _idx + InstanceKlass::inner_class_inner_class_info_offset);
1360   }
1361 
1362   void set_inner_class_info_index(u2 index) {
1363     _inner_classes->at_put(
1364                _idx + InstanceKlass::inner_class_inner_class_info_offset, index);
1365   }
1366 
1367   u2 outer_class_info_index() const {
1368     return _inner_classes->at(
1369                _idx + InstanceKlass::inner_class_outer_class_info_offset);
1370   }
1371 
1372   void set_outer_class_info_index(u2 index) {
1373     _inner_classes->at_put(
1374                _idx + InstanceKlass::inner_class_outer_class_info_offset, index);
1375   }
1376 
1377   u2 inner_name_index() const {
1378     return _inner_classes->at(
1379                _idx + InstanceKlass::inner_class_inner_name_offset);
1380   }
1381 
1382   void set_inner_name_index(u2 index) {
1383     _inner_classes->at_put(
1384                _idx + InstanceKlass::inner_class_inner_name_offset, index);
1385   }
1386 
1387   u2 inner_access_flags() const {
1388     return _inner_classes->at(
1389                _idx + InstanceKlass::inner_class_access_flags_offset);
1390   }
1391 };
1392 
1393 // Iterator over class hierarchy under a particular class. Implements depth-first pre-order traversal.
1394 // Usage:
1395 //  for (ClassHierarchyIterator iter(root_klass); !iter.done(); iter.next()) {
1396 //    Klass* k = iter.klass();
1397 //    ...
1398 //  }
1399 class ClassHierarchyIterator : public StackObj {
1400  private:
1401   InstanceKlass* _root;
1402   Klass*         _current;
1403   bool           _visit_subclasses;
1404 
1405  public:
1406   ClassHierarchyIterator(InstanceKlass* root) : _root(root), _current(root), _visit_subclasses(true) {
1407     assert(_root == _current, "required"); // initial state
1408   }
1409 
1410   bool done() {
1411     return (_current == nullptr);
1412   }
1413 
1414   // Make a step iterating over the class hierarchy under the root class.
1415   // Skips subclasses if requested.
1416   void next();
1417 
1418   Klass* klass() {
1419     assert(!done(), "sanity");
1420     return _current;
1421   }
1422 
1423   // Skip subclasses of the current class.
1424   void skip_subclasses() {
1425     _visit_subclasses = false;
1426   }
1427 };
1428 
1429 #endif // SHARE_OOPS_INSTANCEKLASS_HPP