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