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