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