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