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