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