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