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