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