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