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