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