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