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