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