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