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