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