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