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