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