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