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