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