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