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