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