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   // TODO
 294   bool _set_finals;
 295 
 296   friend class SystemDictionary;
 297 
 298  public:
 299   bool set_finals()       { return _set_finals; }
 300   void set_finals(bool b) { _set_finals = b; }
 301 
 302   bool has_nonstatic_fields() const        {
 303     return (_misc_flags & _misc_has_nonstatic_fields) != 0;
 304   }
 305   void set_has_nonstatic_fields(bool b)    {
 306     if (b) {
 307       _misc_flags |= _misc_has_nonstatic_fields;
 308     } else {
 309       _misc_flags &= ~_misc_has_nonstatic_fields;
 310     }
 311   }
 312 
 313   // field sizes
 314   int nonstatic_field_size() const         { return _nonstatic_field_size; }
 315   void set_nonstatic_field_size(int size)  { _nonstatic_field_size = size; }
 316 
 317   int static_field_size() const            { return _static_field_size; }
 318   void set_static_field_size(int size)     { _static_field_size = size; }
 319 
 320   int static_oop_field_count() const       { return (int)_static_oop_field_count; }
 321   void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; }
 322 
 323   // Java vtable
 324   int  vtable_length() const               { return _vtable_len; }
 325   void set_vtable_length(int len)          { _vtable_len = len; }
 326 
 327   // Java itable
 328   int  itable_length() const               { return _itable_len; }
 329   void set_itable_length(int len)          { _itable_len = len; }
 330 
 331   // array klasses
 332   Klass* array_klasses() const             { return _array_klasses; }
 333   void set_array_klasses(Klass* k)         { _array_klasses = k; }
 334 
 335   // methods
 336   Array<Method*>* methods() const          { return _methods; }
 337   void set_methods(Array<Method*>* a)      { _methods = a; }
 338   Method* method_with_idnum(int idnum);
 339   Method* method_with_orig_idnum(int idnum);
 340   Method* method_with_orig_idnum(int idnum, int version);
 341 
 342   // method ordering
 343   Array<int>* method_ordering() const     { return _method_ordering; }
 344   void set_method_ordering(Array<int>* m) { _method_ordering = m; }
 345   void copy_method_ordering(intArray* m, TRAPS);
 346 
 347   // default_methods
 348   Array<Method*>* default_methods() const  { return _default_methods; }
 349   void set_default_methods(Array<Method*>* a) { _default_methods = a; }
 350 
 351   // default method vtable_indices
 352   Array<int>* default_vtable_indices() const { return _default_vtable_indices; }
 353   void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; }
 354   Array<int>* create_new_default_vtable_indices(int len, TRAPS);
 355 
 356   // interfaces
 357   Array<Klass*>* local_interfaces() const          { return _local_interfaces; }
 358   void set_local_interfaces(Array<Klass*>* a)      {
 359     guarantee(_local_interfaces == NULL || a == NULL, "Just checking");
 360     _local_interfaces = a; }
 361 
 362   Array<Klass*>* transitive_interfaces() const     { return _transitive_interfaces; }
 363   void set_transitive_interfaces(Array<Klass*>* a) {
 364     guarantee(_transitive_interfaces == NULL || a == NULL, "Just checking");
 365     _transitive_interfaces = a;
 366   }
 367 
 368  private:
 369   friend class fieldDescriptor;
 370   FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
 371 
 372  public:
 373   int     field_offset      (int index) const { return field(index)->offset(); }
 374   int     field_access_flags(int index) const { return field(index)->access_flags(); }
 375   Symbol* field_name        (int index) const { return field(index)->name(constants()); }
 376   Symbol* field_signature   (int index) const { return field(index)->signature(constants()); }
 377 
 378   // Number of Java declared fields
 379   int java_fields_count() const           { return (int)_java_fields_count; }
 380 
 381   Array<u2>* fields() const            { return _fields; }
 382   void set_fields(Array<u2>* f, u2 java_fields_count) {
 383     guarantee(_fields == NULL || f == NULL, "Just checking");
 384     _fields = f;
 385     _java_fields_count = java_fields_count;
 386   }
 387 
 388   // inner classes
 389   Array<u2>* inner_classes() const       { return _inner_classes; }
 390   void set_inner_classes(Array<u2>* f)   { _inner_classes = f; }
 391 
 392   enum InnerClassAttributeOffset {
 393     // From http://mirror.eng/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc10.html#18814
 394     inner_class_inner_class_info_offset = 0,
 395     inner_class_outer_class_info_offset = 1,
 396     inner_class_inner_name_offset = 2,
 397     inner_class_access_flags_offset = 3,
 398     inner_class_next_offset = 4
 399   };
 400 
 401   enum EnclosingMethodAttributeOffset {
 402     enclosing_method_class_index_offset = 0,
 403     enclosing_method_method_index_offset = 1,
 404     enclosing_method_attribute_size = 2
 405   };
 406 
 407   // method override check
 408   bool is_override(methodHandle super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS);
 409 
 410   // package
 411   bool is_same_class_package(Klass* class2);
 412   bool is_same_class_package(oop classloader2, Symbol* classname2);
 413   static bool is_same_class_package(oop class_loader1, Symbol* class_name1, oop class_loader2, Symbol* class_name2);
 414 
 415   // find an enclosing class
 416   Klass* compute_enclosing_class(bool* inner_is_member, TRAPS) {
 417     instanceKlassHandle self(THREAD, this);
 418     return compute_enclosing_class_impl(self, inner_is_member, THREAD);
 419   }
 420   static Klass* compute_enclosing_class_impl(instanceKlassHandle self,
 421                                              bool* inner_is_member, TRAPS);
 422 
 423   // Find InnerClasses attribute for k and return outer_class_info_index & inner_name_index.
 424   static bool find_inner_classes_attr(instanceKlassHandle k,
 425                                       int* ooff, int* noff, TRAPS);
 426 
 427   // tell if two classes have the same enclosing class (at package level)
 428   bool is_same_package_member(Klass* class2, TRAPS) {
 429     instanceKlassHandle self(THREAD, this);
 430     return is_same_package_member_impl(self, class2, THREAD);
 431   }
 432   static bool is_same_package_member_impl(instanceKlassHandle self,
 433                                           Klass* class2, TRAPS);
 434 
 435   // initialization state
 436   bool is_loaded() const                   { return _init_state >= loaded; }
 437   bool is_linked() const                   { return _init_state >= linked; }
 438   bool is_initialized() const              { return _init_state == fully_initialized; }
 439   bool is_not_initialized() const          { return _init_state <  being_initialized; }
 440   bool is_being_initialized() const        { return _init_state == being_initialized; }
 441   bool is_in_error_state() const           { return _init_state == initialization_error; }
 442   bool is_reentrant_initialization(Thread *thread)  { return thread == _init_thread; }
 443   ClassState  init_state()                 { return (ClassState)_init_state; }
 444   bool is_rewritten() const                { return (_misc_flags & _misc_rewritten) != 0; }
 445 
 446   // defineClass specified verification
 447   bool should_verify_class() const         {
 448     return (_misc_flags & _misc_should_verify_class) != 0;
 449   }
 450   void set_should_verify_class(bool value) {
 451     if (value) {
 452       _misc_flags |= _misc_should_verify_class;
 453     } else {
 454       _misc_flags &= ~_misc_should_verify_class;
 455     }
 456   }
 457 
 458   // marking
 459   bool is_marked_dependent() const         { return _is_marked_dependent; }
 460   void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
 461 
 462   bool has_unloaded_dependent() const         { return _has_unloaded_dependent; }
 463   void set_has_unloaded_dependent(bool value) { _has_unloaded_dependent = value; }
 464 
 465   // initialization (virtuals from Klass)
 466   bool should_be_initialized() const;  // means that initialize should be called
 467   void initialize(TRAPS);
 468   void link_class(TRAPS);
 469   bool link_class_or_fail(TRAPS); // returns false on failure
 470   void unlink_class();
 471   void rewrite_class(TRAPS);
 472   void link_methods(TRAPS);
 473   Method* class_initializer();
 474 
 475   // set the class to initialized if no static initializer is present
 476   void eager_initialize(Thread *thread);
 477 
 478   // reference type
 479   ReferenceType reference_type() const     { return (ReferenceType)_reference_type; }
 480   void set_reference_type(ReferenceType t) {
 481     assert(t == (u1)t, "overflow");
 482     _reference_type = (u1)t;
 483   }
 484 
 485   static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
 486 
 487   // find local field, returns true if found
 488   bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
 489   // find field in direct superinterfaces, returns the interface in which the field is defined
 490   Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
 491   // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 492   Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
 493   // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined
 494   Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const;
 495 
 496   // find a non-static or static field given its offset within the class.
 497   bool contains_field_offset(int offset) {
 498     return instanceOopDesc::contains_field_offset(offset, nonstatic_field_size());
 499   }
 500 
 501   bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
 502   bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
 503 
 504   // find a local method (returns NULL if not found)
 505   Method* find_method(Symbol* name, Symbol* signature) const;
 506   static Method* find_method(Array<Method*>* methods, Symbol* name, Symbol* signature);
 507 
 508   // find a local method, but skip static methods
 509   Method* find_instance_method(Symbol* name, Symbol* signature);
 510   static Method* find_instance_method(Array<Method*>* methods, Symbol* name, Symbol* signature);
 511 
 512   // true if method matches signature and conforms to skipping_X conditions.
 513   static bool method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static);
 514 
 515   // find a local method index in default_methods (returns -1 if not found)
 516   static int find_method_index(Array<Method*>* methods, Symbol* name, Symbol* signature,
 517                                OverpassLookupMode overpass_mode, StaticLookupMode static_mode);
 518 
 519   // lookup operation (returns NULL if not found)
 520   Method* uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const;
 521 
 522   // lookup a method in all the interfaces that this class implements
 523   // (returns NULL if not found)
 524   Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature, DefaultsLookupMode defaults_mode) const;
 525 
 526   // lookup a method in local defaults then in all interfaces
 527   // (returns NULL if not found)
 528   Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const;
 529 
 530   // Find method indices by name.  If a method with the specified name is
 531   // found the index to the first method is returned, and 'end' is filled in
 532   // with the index of first non-name-matching method.  If no method is found
 533   // -1 is returned.
 534   int find_method_by_name(Symbol* name, int* end);
 535   static int find_method_by_name(Array<Method*>* methods, Symbol* name, int* end);
 536 
 537   // constant pool
 538   ConstantPool* constants() const        { return _constants; }
 539   void set_constants(ConstantPool* c)    { _constants = c; }
 540 
 541   // protection domain
 542   oop protection_domain() const;
 543 
 544   // signers
 545   objArrayOop signers() const;
 546 
 547   // host class
 548   Klass* host_klass() const              {
 549     Klass** hk = (Klass**)adr_host_klass();
 550     if (hk == NULL) {
 551       return NULL;
 552     } else {
 553       assert(*hk != NULL, "host klass should always be set if the address is not null");
 554       return *hk;
 555     }
 556   }
 557   void set_host_klass(Klass* host)            {
 558     assert(is_anonymous(), "not anonymous");
 559     Klass** addr = (Klass**)adr_host_klass();
 560     assert(addr != NULL, "no reversed space");
 561     if (addr != NULL) {
 562       *addr = host;
 563     }
 564   }
 565   bool is_anonymous() const                {
 566     return (_misc_flags & _misc_is_anonymous) != 0;
 567   }
 568   void set_is_anonymous(bool value)        {
 569     if (value) {
 570       _misc_flags |= _misc_is_anonymous;
 571     } else {
 572       _misc_flags &= ~_misc_is_anonymous;
 573     }
 574   }
 575 
 576   // Oop that keeps the metadata for this class from being unloaded
 577   // in places where the metadata is stored in other places, like nmethods
 578   oop klass_holder() const {
 579     return is_anonymous() ? java_mirror() : class_loader();
 580   }
 581 
 582   bool is_contended() const                {
 583     return (_misc_flags & _misc_is_contended) != 0;
 584   }
 585   void set_is_contended(bool value)        {
 586     if (value) {
 587       _misc_flags |= _misc_is_contended;
 588     } else {
 589       _misc_flags &= ~_misc_is_contended;
 590     }
 591   }
 592 
 593   // source file name
 594   Symbol* source_file_name() const               {
 595     return (_source_file_name_index == 0) ?
 596       (Symbol*)NULL : _constants->symbol_at(_source_file_name_index);
 597   }
 598   u2 source_file_name_index() const              {
 599     return _source_file_name_index;
 600   }
 601   void set_source_file_name_index(u2 sourcefile_index) {
 602     _source_file_name_index = sourcefile_index;
 603   }
 604 
 605   // minor and major version numbers of class file
 606   u2 minor_version() const                 { return _minor_version; }
 607   void set_minor_version(u2 minor_version) { _minor_version = minor_version; }
 608   u2 major_version() const                 { return _major_version; }
 609   void set_major_version(u2 major_version) { _major_version = major_version; }
 610 
 611   // source debug extension
 612   char* source_debug_extension() const     { return _source_debug_extension; }
 613   void set_source_debug_extension(char* array, int length);
 614 
 615   // symbol unloading support (refcount already added)
 616   Symbol* array_name()                     { return _array_name; }
 617   void set_array_name(Symbol* name)        { assert(_array_name == NULL  || name == NULL, "name already created"); _array_name = name; }
 618 
 619   // nonstatic oop-map blocks
 620   static int nonstatic_oop_map_size(unsigned int oop_map_count) {
 621     return oop_map_count * OopMapBlock::size_in_words();
 622   }
 623   unsigned int nonstatic_oop_map_count() const {
 624     return _nonstatic_oop_map_size / OopMapBlock::size_in_words();
 625   }
 626   int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; }
 627   void set_nonstatic_oop_map_size(int words) {
 628     _nonstatic_oop_map_size = words;
 629   }
 630 
 631   // RedefineClasses() support for previous versions:
 632   void add_previous_version(instanceKlassHandle ikh, int emcp_method_count);
 633 
 634   InstanceKlass* previous_versions() const { return _previous_versions; }
 635 
 636   InstanceKlass* get_klass_version(int version) {
 637     for (InstanceKlass* ik = this; ik != NULL; ik = ik->previous_versions()) {
 638       if (ik->constants()->version() == version) {
 639         return ik;
 640       }
 641     }
 642     return NULL;
 643   }
 644 
 645   bool has_been_redefined() const {
 646     return (_misc_flags & _misc_has_been_redefined) != 0;
 647   }
 648   void set_has_been_redefined() {
 649     _misc_flags |= _misc_has_been_redefined;
 650   }
 651 
 652   bool is_scratch_class() const {
 653     return (_misc_flags & _misc_is_scratch_class) != 0;
 654   }
 655 
 656   void set_is_scratch_class() {
 657     _misc_flags |= _misc_is_scratch_class;
 658   }
 659 
 660   void init_previous_versions() {
 661     _previous_versions = NULL;
 662   }
 663 
 664  private:
 665   static int  _previous_version_count;
 666  public:
 667   static void purge_previous_versions(InstanceKlass* ik);
 668   static bool has_previous_versions() { return _previous_version_count > 0; }
 669 
 670   // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation
 671   void set_cached_class_file(JvmtiCachedClassFileData *data) {
 672     _cached_class_file = data;
 673   }
 674   JvmtiCachedClassFileData * get_cached_class_file() { return _cached_class_file; }
 675   jint get_cached_class_file_len();
 676   unsigned char * get_cached_class_file_bytes();
 677 
 678   // JVMTI: Support for caching of field indices, types, and offsets
 679   void set_jvmti_cached_class_field_map(JvmtiCachedClassFieldMap* descriptor) {
 680     _jvmti_cached_class_field_map = descriptor;
 681   }
 682   JvmtiCachedClassFieldMap* jvmti_cached_class_field_map() const {
 683     return _jvmti_cached_class_field_map;
 684   }
 685 
 686   bool has_default_methods() const {
 687     return (_misc_flags & _misc_has_default_methods) != 0;
 688   }
 689   void set_has_default_methods(bool b) {
 690     if (b) {
 691       _misc_flags |= _misc_has_default_methods;
 692     } else {
 693       _misc_flags &= ~_misc_has_default_methods;
 694     }
 695   }
 696 
 697   bool declares_default_methods() const {
 698     return (_misc_flags & _misc_declares_default_methods) != 0;
 699   }
 700   void set_declares_default_methods(bool b) {
 701     if (b) {
 702       _misc_flags |= _misc_declares_default_methods;
 703     } else {
 704       _misc_flags &= ~_misc_declares_default_methods;
 705     }
 706   }
 707 
 708   // for adding methods, ConstMethod::UNSET_IDNUM means no more ids available
 709   inline u2 next_method_idnum();
 710   void set_initial_method_idnum(u2 value)             { _idnum_allocated_count = value; }
 711 
 712   // generics support
 713   Symbol* generic_signature() const                   {
 714     return (_generic_signature_index == 0) ?
 715       (Symbol*)NULL : _constants->symbol_at(_generic_signature_index);
 716   }
 717   u2 generic_signature_index() const                  {
 718     return _generic_signature_index;
 719   }
 720   void set_generic_signature_index(u2 sig_index)      {
 721     _generic_signature_index = sig_index;
 722   }
 723 
 724   u2 enclosing_method_data(int offset);
 725   u2 enclosing_method_class_index() {
 726     return enclosing_method_data(enclosing_method_class_index_offset);
 727   }
 728   u2 enclosing_method_method_index() {
 729     return enclosing_method_data(enclosing_method_method_index_offset);
 730   }
 731   void set_enclosing_method_indices(u2 class_index,
 732                                     u2 method_index);
 733 
 734   // jmethodID support
 735   static jmethodID get_jmethod_id(instanceKlassHandle ik_h,
 736                      methodHandle method_h);
 737   static jmethodID get_jmethod_id_fetch_or_update(instanceKlassHandle ik_h,
 738                      size_t idnum, jmethodID new_id, jmethodID* new_jmeths,
 739                      jmethodID* to_dealloc_id_p,
 740                      jmethodID** to_dealloc_jmeths_p);
 741   static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum,
 742                 size_t *length_p, jmethodID* id_p);
 743   void ensure_space_for_methodids(int start_offset = 0);
 744   jmethodID jmethod_id_or_null(Method* method);
 745 
 746   // annotations support
 747   Annotations* annotations() const          { return _annotations; }
 748   void set_annotations(Annotations* anno)   { _annotations = anno; }
 749 
 750   AnnotationArray* class_annotations() const {
 751     return (_annotations != NULL) ? _annotations->class_annotations() : NULL;
 752   }
 753   Array<AnnotationArray*>* fields_annotations() const {
 754     return (_annotations != NULL) ? _annotations->fields_annotations() : NULL;
 755   }
 756   AnnotationArray* class_type_annotations() const {
 757     return (_annotations != NULL) ? _annotations->class_type_annotations() : NULL;
 758   }
 759   Array<AnnotationArray*>* fields_type_annotations() const {
 760     return (_annotations != NULL) ? _annotations->fields_type_annotations() : NULL;
 761   }
 762   // allocation
 763   instanceOop allocate_instance(TRAPS);
 764 
 765   // additional member function to return a handle
 766   instanceHandle allocate_instance_handle(TRAPS)      { return instanceHandle(THREAD, allocate_instance(THREAD)); }
 767 
 768   objArrayOop allocate_objArray(int n, int length, TRAPS);
 769   // Helper function
 770   static instanceOop register_finalizer(instanceOop i, TRAPS);
 771 
 772   // Check whether reflection/jni/jvm code is allowed to instantiate this class;
 773   // if not, throw either an Error or an Exception.
 774   virtual void check_valid_for_instantiation(bool throwError, TRAPS);
 775 
 776   // initialization
 777   void call_class_initializer(TRAPS);
 778   void set_initialization_state_and_notify(ClassState state, TRAPS);
 779 
 780   // OopMapCache support
 781   OopMapCache* oop_map_cache()               { return _oop_map_cache; }
 782   void set_oop_map_cache(OopMapCache *cache) { _oop_map_cache = cache; }
 783   void mask_for(methodHandle method, int bci, InterpreterOopMap* entry);
 784 
 785   // JNI identifier support (for static fields - for jni performance)
 786   JNIid* jni_ids()                               { return _jni_ids; }
 787   void set_jni_ids(JNIid* ids)                   { _jni_ids = ids; }
 788   JNIid* jni_id_for(int offset);
 789 
 790   // maintenance of deoptimization dependencies
 791   int mark_dependent_nmethods(DepChange& changes);
 792   void add_dependent_nmethod(nmethod* nm);
 793   void remove_dependent_nmethod(nmethod* nm);
 794 
 795   // On-stack replacement support
 796   nmethod* osr_nmethods_head() const         { return _osr_nmethods_head; };
 797   void set_osr_nmethods_head(nmethod* h)     { _osr_nmethods_head = h; };
 798   void add_osr_nmethod(nmethod* n);
 799   void remove_osr_nmethod(nmethod* n);
 800   int mark_osr_nmethods(const Method* m);
 801   nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
 802 
 803   // Breakpoint support (see methods on Method* for details)
 804   BreakpointInfo* breakpoints() const       { return _breakpoints; };
 805   void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; };
 806 
 807   // support for stub routines
 808   static ByteSize init_state_offset()  { return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
 809   TRACE_DEFINE_OFFSET;
 810   static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
 811 
 812   // subclass/subinterface checks
 813   bool implements_interface(Klass* k) const;
 814   bool is_same_or_direct_interface(Klass* k) const;
 815 
 816 #ifdef ASSERT
 817   // check whether this class or one of its superclasses was redefined
 818   bool has_redefined_this_or_super() const;
 819 #endif
 820 
 821   // Access to the implementor of an interface.
 822   Klass* implementor() const
 823   {
 824     Klass** k = adr_implementor();
 825     if (k == NULL) {
 826       return NULL;
 827     } else {
 828       return *k;
 829     }
 830   }
 831 
 832   void set_implementor(Klass* k) {
 833     assert(is_interface(), "not interface");
 834     Klass** addr = adr_implementor();
 835     assert(addr != NULL, "null addr");
 836     if (addr != NULL) {
 837       *addr = k;
 838     }
 839   }
 840 
 841   int  nof_implementors() const       {
 842     Klass* k = implementor();
 843     if (k == NULL) {
 844       return 0;
 845     } else if (k != this) {
 846       return 1;
 847     } else {
 848       return 2;
 849     }
 850   }
 851 
 852   void add_implementor(Klass* k);  // k is a new class that implements this interface
 853   void init_implementor();           // initialize
 854 
 855   // link this class into the implementors list of every interface it implements
 856   void process_interfaces(Thread *thread);
 857 
 858   // virtual operations from Klass
 859   bool is_leaf_class() const               { return _subklass == NULL; }
 860   GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots);
 861   bool compute_is_subtype_of(Klass* k);
 862   bool can_be_primary_super_slow() const;
 863   int oop_size(oop obj)  const             { return size_helper(); }
 864   bool oop_is_instance_slow() const        { return true; }
 865 
 866   // Iterators
 867   void do_local_static_fields(FieldClosure* cl);
 868   void do_nonstatic_fields(FieldClosure* cl); // including inherited fields
 869   void do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle, TRAPS);
 870 
 871   void methods_do(void f(Method* method));
 872   void array_klasses_do(void f(Klass* k));
 873   void array_klasses_do(void f(Klass* k, TRAPS), TRAPS);
 874   bool super_types_do(SuperTypeClosure* blk);
 875 
 876   // Casting from Klass*
 877   static InstanceKlass* cast(Klass* k) {
 878     assert(k == NULL || k->is_klass(), "must be");
 879     assert(k == NULL || k->oop_is_instance(), "cast to InstanceKlass");
 880     return (InstanceKlass*) k;
 881   }
 882 
 883   InstanceKlass* java_super() const {
 884     return (super() == NULL) ? NULL : cast(super());
 885   }
 886 
 887   // Sizing (in words)
 888   static int header_size()            { return align_object_offset(sizeof(InstanceKlass)/HeapWordSize); }
 889 
 890   static int size(int vtable_length, int itable_length,
 891                   int nonstatic_oop_map_size,
 892                   bool is_interface, bool is_anonymous) {
 893     return align_object_size(header_size() +
 894            align_object_offset(vtable_length) +
 895            align_object_offset(itable_length) +
 896            ((is_interface || is_anonymous) ?
 897              align_object_offset(nonstatic_oop_map_size) :
 898              nonstatic_oop_map_size) +
 899            (is_interface ? (int)sizeof(Klass*)/HeapWordSize : 0) +
 900            (is_anonymous ? (int)sizeof(Klass*)/HeapWordSize : 0));
 901   }
 902   int size() const                    { return size(vtable_length(),
 903                                                itable_length(),
 904                                                nonstatic_oop_map_size(),
 905                                                is_interface(),
 906                                                is_anonymous());
 907   }
 908 #if INCLUDE_SERVICES
 909   virtual void collect_statistics(KlassSizeStats *sz) const;
 910 #endif
 911 
 912   static int vtable_start_offset()    { return header_size(); }
 913   static int vtable_length_offset()   { return offset_of(InstanceKlass, _vtable_len) / HeapWordSize; }
 914 
 915   intptr_t* start_of_vtable() const        { return ((intptr_t*)this) + vtable_start_offset(); }
 916   intptr_t* start_of_itable() const        { return start_of_vtable() + align_object_offset(vtable_length()); }
 917   int  itable_offset_in_words() const { return start_of_itable() - (intptr_t*)this; }
 918 
 919   intptr_t* end_of_itable() const          { return start_of_itable() + itable_length(); }
 920 
 921   address static_field_addr(int offset);
 922 
 923   OopMapBlock* start_of_nonstatic_oop_maps() const {
 924     return (OopMapBlock*)(start_of_itable() + align_object_offset(itable_length()));
 925   }
 926 
 927   Klass** end_of_nonstatic_oop_maps() const {
 928     return (Klass**)(start_of_nonstatic_oop_maps() +
 929                      nonstatic_oop_map_count());
 930   }
 931 
 932   Klass** adr_implementor() const {
 933     if (is_interface()) {
 934       return (Klass**)end_of_nonstatic_oop_maps();
 935     } else {
 936       return NULL;
 937     }
 938   };
 939 
 940   Klass** adr_host_klass() const {
 941     if (is_anonymous()) {
 942       Klass** adr_impl = adr_implementor();
 943       if (adr_impl != NULL) {
 944         return adr_impl + 1;
 945       } else {
 946         return end_of_nonstatic_oop_maps();
 947       }
 948     } else {
 949       return NULL;
 950     }
 951   }
 952 
 953   // Use this to return the size of an instance in heap words:
 954   int size_helper() const {
 955     return layout_helper_to_size_helper(layout_helper());
 956   }
 957 
 958   // This bit is initialized in classFileParser.cpp.
 959   // It is false under any of the following conditions:
 960   //  - the class is abstract (including any interface)
 961   //  - the class has a finalizer (if !RegisterFinalizersAtInit)
 962   //  - the class size is larger than FastAllocateSizeLimit
 963   //  - the class is java/lang/Class, which cannot be allocated directly
 964   bool can_be_fastpath_allocated() const {
 965     return !layout_helper_needs_slow_path(layout_helper());
 966   }
 967 
 968   // Java vtable/itable
 969   klassVtable* vtable() const;        // return new klassVtable wrapper
 970   inline Method* method_at_vtable(int index);
 971   klassItable* itable() const;        // return new klassItable wrapper
 972   Method* method_at_itable(Klass* holder, int index, TRAPS);
 973 
 974 #if INCLUDE_JVMTI
 975   void adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed);
 976 #endif // INCLUDE_JVMTI
 977 
 978   void clean_implementors_list(BoolObjectClosure* is_alive);
 979   void clean_method_data(BoolObjectClosure* is_alive);
 980   void clean_dependent_nmethods();
 981 
 982   // Explicit metaspace deallocation of fields
 983   // For RedefineClasses and class file parsing errors, we need to deallocate
 984   // instanceKlasses and the metadata they point to.
 985   void deallocate_contents(ClassLoaderData* loader_data);
 986   static void deallocate_methods(ClassLoaderData* loader_data,
 987                                  Array<Method*>* methods);
 988   void static deallocate_interfaces(ClassLoaderData* loader_data,
 989                                     Klass* super_klass,
 990                                     Array<Klass*>* local_interfaces,
 991                                     Array<Klass*>* transitive_interfaces);
 992 
 993   // The constant pool is on stack if any of the methods are executing or
 994   // referenced by handles.
 995   bool on_stack() const { return _constants->on_stack(); }
 996 
 997   // callbacks for actions during class unloading
 998   static void notify_unload_class(InstanceKlass* ik);
 999   static void release_C_heap_structures(InstanceKlass* ik);
1000 
1001   // Naming
1002   const char* signature_name() const;
1003 
1004   // GC specific object visitors
1005   //
1006   // Mark Sweep
1007   void oop_ms_follow_contents(oop obj);
1008   int  oop_ms_adjust_pointers(oop obj);
1009 #if INCLUDE_ALL_GCS
1010   // Parallel Scavenge
1011   void oop_ps_push_contents(  oop obj, PSPromotionManager* pm);
1012   // Parallel Compact
1013   void oop_pc_follow_contents(oop obj, ParCompactionManager* cm);
1014   void oop_pc_update_pointers(oop obj);
1015 #endif
1016 
1017   // Oop fields (and metadata) iterators
1018   //  [nv = true]  Use non-virtual calls to do_oop_nv.
1019   //  [nv = false] Use virtual calls to do_oop.
1020   //
1021   // The InstanceKlass iterators also visits the Object's klass.
1022 
1023   // Forward iteration
1024  public:
1025   // Iterate over all oop fields in the oop maps.
1026   template <bool nv, class OopClosureType>
1027   inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
1028 
1029  protected:
1030   // Iterate over all oop fields and metadata.
1031   template <bool nv, class OopClosureType>
1032   inline int oop_oop_iterate(oop obj, OopClosureType* closure);
1033 
1034  private:
1035   // Iterate over all oop fields in the oop maps.
1036   // Specialized for [T = oop] or [T = narrowOop].
1037   template <bool nv, typename T, class OopClosureType>
1038   inline void oop_oop_iterate_oop_maps_specialized(oop obj, OopClosureType* closure);
1039 
1040   // Iterate over all oop fields in one oop map.
1041   template <bool nv, typename T, class OopClosureType>
1042   inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
1043 
1044 
1045   // Reverse iteration
1046 #if INCLUDE_ALL_GCS
1047  public:
1048   // Iterate over all oop fields in the oop maps.
1049   template <bool nv, class OopClosureType>
1050   inline void oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure);
1051 
1052  protected:
1053   // Iterate over all oop fields and metadata.
1054   template <bool nv, class OopClosureType>
1055   inline int oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
1056 
1057  private:
1058   // Iterate over all oop fields in the oop maps.
1059   // Specialized for [T = oop] or [T = narrowOop].
1060   template <bool nv, typename T, class OopClosureType>
1061   inline void oop_oop_iterate_oop_maps_specialized_reverse(oop obj, OopClosureType* closure);
1062 
1063   // Iterate over all oop fields in one oop map.
1064   template <bool nv, typename T, class OopClosureType>
1065   inline void oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure);
1066 #endif
1067 
1068 
1069   // Bounded range iteration
1070  public:
1071   // Iterate over all oop fields in the oop maps.
1072   template <bool nv, class OopClosureType>
1073   inline void oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1074 
1075  protected:
1076   // Iterate over all oop fields and metadata.
1077   template <bool nv, class OopClosureType>
1078   inline int oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1079 
1080  private:
1081   // Iterate over all oop fields in the oop maps.
1082   // Specialized for [T = oop] or [T = narrowOop].
1083   template <bool nv, typename T, class OopClosureType>
1084   inline void oop_oop_iterate_oop_maps_specialized_bounded(oop obj, OopClosureType* closure, MemRegion mr);
1085 
1086   // Iterate over all oop fields in one oop map.
1087   template <bool nv, typename T, class OopClosureType>
1088   inline void oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr);
1089 
1090 
1091  public:
1092 
1093   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL)
1094   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL)
1095 
1096 #if INCLUDE_ALL_GCS
1097   ALL_OOP_OOP_ITERATE_CLOSURES_1(OOP_OOP_ITERATE_DECL_BACKWARDS)
1098   ALL_OOP_OOP_ITERATE_CLOSURES_2(OOP_OOP_ITERATE_DECL_BACKWARDS)
1099 #endif // INCLUDE_ALL_GCS
1100 
1101   u2 idnum_allocated_count() const      { return _idnum_allocated_count; }
1102 
1103 public:
1104   void set_in_error_state() {
1105     assert(DumpSharedSpaces, "only call this when dumping archive");
1106     _init_state = initialization_error;
1107   }
1108   bool check_sharing_error_state();
1109 
1110 private:
1111   // initialization state
1112 #ifdef ASSERT
1113   void set_init_state(ClassState state);
1114 #else
1115   void set_init_state(ClassState state) { _init_state = (u1)state; }
1116 #endif
1117   void set_rewritten()                  { _misc_flags |= _misc_rewritten; }
1118   void set_init_thread(Thread *thread)  { _init_thread = thread; }
1119 
1120   // The RedefineClasses() API can cause new method idnums to be needed
1121   // which will cause the caches to grow. Safety requires different
1122   // cache management logic if the caches can grow instead of just
1123   // going from NULL to non-NULL.
1124   bool idnum_can_increment() const      { return has_been_redefined(); }
1125   jmethodID* methods_jmethod_ids_acquire() const
1126          { return (jmethodID*)OrderAccess::load_ptr_acquire(&_methods_jmethod_ids); }
1127   void release_set_methods_jmethod_ids(jmethodID* jmeths)
1128          { OrderAccess::release_store_ptr(&_methods_jmethod_ids, jmeths); }
1129 
1130   // Lock during initialization
1131 public:
1132   // Lock for (1) initialization; (2) access to the ConstantPool of this class.
1133   // Must be one per class and it has to be a VM internal object so java code
1134   // cannot lock it (like the mirror).
1135   // It has to be an object not a Mutex because it's held through java calls.
1136   oop init_lock() const;
1137 private:
1138   void fence_and_clear_init_lock();
1139 
1140   // Static methods that are used to implement member methods where an exposed this pointer
1141   // is needed due to possible GCs
1142   static bool link_class_impl                           (instanceKlassHandle this_k, bool throw_verifyerror, TRAPS);
1143   static bool verify_code                               (instanceKlassHandle this_k, bool throw_verifyerror, TRAPS);
1144   static void initialize_impl                           (instanceKlassHandle this_k, TRAPS);
1145   static void initialize_super_interfaces               (instanceKlassHandle this_k, TRAPS);
1146   static void eager_initialize_impl                     (instanceKlassHandle this_k);
1147   static void set_initialization_state_and_notify_impl  (instanceKlassHandle this_k, ClassState state, TRAPS);
1148   static void call_class_initializer_impl               (instanceKlassHandle this_k, TRAPS);
1149   static Klass* array_klass_impl                        (instanceKlassHandle this_k, bool or_null, int n, TRAPS);
1150   static void do_local_static_fields_impl               (instanceKlassHandle this_k, void f(fieldDescriptor* fd, Handle, TRAPS), Handle, TRAPS);
1151   /* jni_id_for_impl for jfieldID only */
1152   static JNIid* jni_id_for_impl                         (instanceKlassHandle this_k, int offset);
1153 
1154   // Returns the array class for the n'th dimension
1155   Klass* array_klass_impl(bool or_null, int n, TRAPS);
1156 
1157   // Returns the array class with this class as element type
1158   Klass* array_klass_impl(bool or_null, TRAPS);
1159 
1160   // find a local method (returns NULL if not found)
1161   Method* find_method_impl(Symbol* name, Symbol* signature,
1162                            OverpassLookupMode overpass_mode, StaticLookupMode static_mode) const;
1163   static Method* find_method_impl(Array<Method*>* methods, Symbol* name, Symbol* signature,
1164                                   OverpassLookupMode overpass_mode, StaticLookupMode static_mode);
1165 
1166   // Free CHeap allocated fields.
1167   void release_C_heap_structures();
1168 
1169   // RedefineClasses support
1170   void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; }
1171   void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count);
1172 public:
1173   // CDS support - remove and restore oops from metadata. Oops are not shared.
1174   virtual void remove_unshareable_info();
1175   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
1176 
1177   // jvm support
1178   jint compute_modifier_flags(TRAPS) const;
1179 
1180   // JSR-292 support
1181   MemberNameTable* member_names() { return _member_names; }
1182   void set_member_names(MemberNameTable* member_names) { _member_names = member_names; }
1183   bool add_member_name(Handle member_name);
1184 
1185 public:
1186   // JVMTI support
1187   jint jvmti_class_status() const;
1188 
1189  public:
1190   // Printing
1191 #ifndef PRODUCT
1192   void print_on(outputStream* st) const;
1193 #endif
1194   void print_value_on(outputStream* st) const;
1195 
1196   void oop_print_value_on(oop obj, outputStream* st);
1197 
1198 #ifndef PRODUCT
1199   void oop_print_on      (oop obj, outputStream* st);
1200 
1201   void print_dependent_nmethods(bool verbose = false);
1202   bool is_dependent_nmethod(nmethod* nm);
1203 #endif
1204 
1205   const char* internal_name() const;
1206 
1207   // Verification
1208   void verify_on(outputStream* st);
1209 
1210   void oop_verify_on(oop obj, outputStream* st);
1211 };
1212 
1213 inline Method* InstanceKlass::method_at_vtable(int index)  {
1214 #ifndef PRODUCT
1215   assert(index >= 0, "valid vtable index");
1216   if (DebugVtables) {
1217     verify_vtable_index(index);
1218   }
1219 #endif
1220   vtableEntry* ve = (vtableEntry*)start_of_vtable();
1221   return ve[index].method();
1222 }
1223 
1224 // for adding methods
1225 // UNSET_IDNUM return means no more ids available
1226 inline u2 InstanceKlass::next_method_idnum() {
1227   if (_idnum_allocated_count == ConstMethod::MAX_IDNUM) {
1228     return ConstMethod::UNSET_IDNUM; // no more ids available
1229   } else {
1230     return _idnum_allocated_count++;
1231   }
1232 }
1233 
1234 
1235 /* JNIid class for jfieldIDs only */
1236 class JNIid: public CHeapObj<mtClass> {
1237   friend class VMStructs;
1238  private:
1239   Klass*             _holder;
1240   JNIid*             _next;
1241   int                _offset;
1242 #ifdef ASSERT
1243   bool               _is_static_field_id;
1244 #endif
1245 
1246  public:
1247   // Accessors
1248   Klass* holder() const           { return _holder; }
1249   int offset() const              { return _offset; }
1250   JNIid* next()                   { return _next; }
1251   // Constructor
1252   JNIid(Klass* holder, int offset, JNIid* next);
1253   // Identifier lookup
1254   JNIid* find(int offset);
1255 
1256   bool find_local_field(fieldDescriptor* fd) {
1257     return InstanceKlass::cast(holder())->find_local_field_from_offset(offset(), true, fd);
1258   }
1259 
1260   static void deallocate(JNIid* id);
1261   // Debugging
1262 #ifdef ASSERT
1263   bool is_static_field_id() const { return _is_static_field_id; }
1264   void set_is_static_field_id()   { _is_static_field_id = true; }
1265 #endif
1266   void verify(Klass* holder);
1267 };
1268 
1269 class nmethodBucketEntry : public CHeapObj<mtClass> {
1270   friend class VMStructs;
1271 private:
1272   nmethod*            _nmethod;
1273   int                 _count;
1274   nmethodBucketEntry* _next;
1275 public:
1276     nmethodBucketEntry(nmethod* nmethod, nmethodBucketEntry* next) {
1277     _nmethod = nmethod;
1278     _next = next;
1279     _count = 1;
1280   }
1281   int count()                             { return _count; }
1282   int increment()                         { _count += 1; return _count; }
1283   int decrement();
1284   nmethodBucketEntry* next()              { return _next; }
1285   void set_next(nmethodBucketEntry* b)    { _next = b; }
1286   nmethod* get_nmethod()                  { return _nmethod; }
1287 
1288   static int mark_dependent_nmethods(nmethodBucketEntry* deps, DepChange& changes);
1289   static nmethodBucketEntry* add_dependent_nmethod(nmethodBucketEntry* deps, nmethod* nm);
1290   static bool remove_dependent_nmethod(nmethodBucketEntry* deps, nmethod* nm, bool& found);
1291   static nmethodBucketEntry* clean_dependent_nmethods(nmethodBucketEntry* deps);
1292 
1293 #ifndef PRODUCT
1294   static void verify(nmethodBucketEntry* deps);
1295   static void print_dependent_nmethods(nmethodBucketEntry* deps, bool verbose);
1296   static bool is_dependent_nmethod(nmethodBucketEntry* deps, nmethod* nm);
1297 #endif //PRODUCT
1298 };
1299 
1300 //
1301 // nmethodBucket is used to record dependent nmethods for
1302 // deoptimization.  nmethod dependencies are actually <klass, method>
1303 // pairs but we really only care about the klass part for purposes of
1304 // finding nmethods which might need to be deoptimized.  Instead of
1305 // recording the method, a count of how many times a particular nmethod
1306 // was recorded is kept.  This ensures that any recording errors are
1307 // noticed since an nmethod should be removed as many times are it's
1308 // added.
1309 //
1310 class nmethodBucket: public CHeapObj<mtClass> {
1311   friend class VMStructs;
1312  private:
1313   enum {
1314     FIRST_Bucket = 0,
1315     KlassBucket = FIRST_Bucket,
1316     CallSiteBucket,
1317     ConstantFieldBucket,
1318     Bucket_LIMIT
1319   };
1320 
1321   nmethodBucketEntry* _buckets[Bucket_LIMIT];
1322 
1323   nmethodBucket() {
1324     for (int i = FIRST_Bucket; i < Bucket_LIMIT; i++) {
1325       _buckets[i] = NULL;
1326     }
1327   }
1328   static int bucket_index(DepChange& changes);
1329 
1330  public:


1331 
1332   static int mark_dependent_nmethods(nmethodBucket* deps, DepChange& changes);
1333   static nmethodBucket* add_dependent_nmethod(nmethodBucket* deps, nmethod* nm);
1334   static bool remove_dependent_nmethod(nmethodBucket* deps, nmethod* nm);
1335   static nmethodBucket* clean_dependent_nmethods(nmethodBucket* deps);
1336   static int release(nmethodBucket* deps);
1337 #ifndef PRODUCT
1338   static void verify(nmethodBucket* deps);
1339   static void print_dependent_nmethods(nmethodBucket* deps, bool verbose);
1340   static bool is_dependent_nmethod(nmethodBucket* deps, nmethod* nm);
1341 #endif //PRODUCT
1342 };
1343 
1344 // An iterator that's used to access the inner classes indices in the
1345 // InstanceKlass::_inner_classes array.
1346 class InnerClassesIterator : public StackObj {
1347  private:
1348   Array<jushort>* _inner_classes;
1349   int _length;
1350   int _idx;
1351  public:
1352 
1353   InnerClassesIterator(instanceKlassHandle k) {
1354     _inner_classes = k->inner_classes();
1355     if (k->inner_classes() != NULL) {
1356       _length = _inner_classes->length();
1357       // The inner class array's length should be the multiple of
1358       // inner_class_next_offset if it only contains the InnerClasses
1359       // attribute data, or it should be
1360       // n*inner_class_next_offset+enclosing_method_attribute_size
1361       // if it also contains the EnclosingMethod data.
1362       assert((_length % InstanceKlass::inner_class_next_offset == 0 ||
1363               _length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size),
1364              "just checking");
1365       // Remove the enclosing_method portion if exists.
1366       if (_length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size) {
1367         _length -= InstanceKlass::enclosing_method_attribute_size;
1368       }
1369     } else {
1370       _length = 0;
1371     }
1372     _idx = 0;
1373   }
1374 
1375   int length() const {
1376     return _length;
1377   }
1378 
1379   void next() {
1380     _idx += InstanceKlass::inner_class_next_offset;
1381   }
1382 
1383   bool done() const {
1384     return (_idx >= _length);
1385   }
1386 
1387   u2 inner_class_info_index() const {
1388     return _inner_classes->at(
1389                _idx + InstanceKlass::inner_class_inner_class_info_offset);
1390   }
1391 
1392   void set_inner_class_info_index(u2 index) {
1393     _inner_classes->at_put(
1394                _idx + InstanceKlass::inner_class_inner_class_info_offset, index);
1395   }
1396 
1397   u2 outer_class_info_index() const {
1398     return _inner_classes->at(
1399                _idx + InstanceKlass::inner_class_outer_class_info_offset);
1400   }
1401 
1402   void set_outer_class_info_index(u2 index) {
1403     _inner_classes->at_put(
1404                _idx + InstanceKlass::inner_class_outer_class_info_offset, index);
1405   }
1406 
1407   u2 inner_name_index() const {
1408     return _inner_classes->at(
1409                _idx + InstanceKlass::inner_class_inner_name_offset);
1410   }
1411 
1412   void set_inner_name_index(u2 index) {
1413     _inner_classes->at_put(
1414                _idx + InstanceKlass::inner_class_inner_name_offset, index);
1415   }
1416 
1417   u2 inner_access_flags() const {
1418     return _inner_classes->at(
1419                _idx + InstanceKlass::inner_class_access_flags_offset);
1420   }
1421 };
1422 
1423 #endif // SHARE_VM_OOPS_INSTANCEKLASS_HPP
--- EOF ---