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