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