1 /*
   2  * Copyright (c) 1997, 2019, 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_OOPS_KLASS_HPP
  26 #define SHARE_OOPS_KLASS_HPP
  27 
  28 #include "classfile/classLoaderData.hpp"
  29 #include "memory/iterator.hpp"
  30 #include "memory/memRegion.hpp"
  31 #include "oops/arrayStorageProperties.hpp"
  32 #include "oops/markOop.hpp"
  33 #include "oops/metadata.hpp"
  34 #include "oops/oop.hpp"
  35 #include "oops/oopHandle.hpp"
  36 #include "utilities/accessFlags.hpp"
  37 #include "utilities/macros.hpp"
  38 #if INCLUDE_JFR
  39 #include "jfr/support/jfrTraceIdExtension.hpp"
  40 #endif
  41 
  42 // Klass IDs for all subclasses of Klass
  43 enum KlassID {
  44   InstanceKlassID,
  45   InstanceRefKlassID,
  46   InstanceMirrorKlassID,
  47   InstanceClassLoaderKlassID,
  48   TypeArrayKlassID,
  49   ValueArrayKlassID,
  50   ObjArrayKlassID
  51 };
  52 
  53 const uint KLASS_ID_COUNT = 7;
  54 
  55 //
  56 // A Klass provides:
  57 //  1: language level class object (method dictionary etc.)
  58 //  2: provide vm dispatch behavior for the object
  59 // Both functions are combined into one C++ class.
  60 
  61 // One reason for the oop/klass dichotomy in the implementation is
  62 // that we don't want a C++ vtbl pointer in every object.  Thus,
  63 // normal oops don't have any virtual functions.  Instead, they
  64 // forward all "virtual" functions to their klass, which does have
  65 // a vtbl and does the C++ dispatch depending on the object's
  66 // actual type.  (See oop.inline.hpp for some of the forwarding code.)
  67 // ALL FUNCTIONS IMPLEMENTING THIS DISPATCH ARE PREFIXED WITH "oop_"!
  68 
  69 // Forward declarations.
  70 template <class T> class Array;
  71 template <class T> class GrowableArray;
  72 class fieldDescriptor;
  73 class KlassSizeStats;
  74 class klassVtable;
  75 class ModuleEntry;
  76 class PackageEntry;
  77 class ParCompactionManager;
  78 class PSPromotionManager;
  79 class vtableEntry;
  80 
  81 class Klass : public Metadata {
  82   friend class VMStructs;
  83   friend class JVMCIVMStructs;
  84  protected:
  85   // If you add a new field that points to any metaspace object, you
  86   // must add this field to Klass::metaspace_pointers_do().
  87 
  88   // note: put frequently-used fields together at start of klass structure
  89   // for better cache behavior (may not make much of a difference but sure won't hurt)
  90   enum { _primary_super_limit = 8 };
  91 
  92   // The "layout helper" is a combined descriptor of object layout.
  93   // For klasses which are neither instance nor array, the value is zero.
  94   //
  95   // For instances, layout helper is a positive number, the instance size.
  96   // This size is already passed through align_object_size and scaled to bytes.
  97   // The low order bit is set if instances of this class cannot be
  98   // allocated using the fastpath.
  99   //
 100   // For arrays, layout helper is a negative number, containing four
 101   // distinct bytes, as follows:
 102   //    MSB:[tag, hsz, ebt, log2(esz)]:LSB
 103   // where:
 104   //    tag is 0x80 if the elements are oops, 0xC0 if non-oops, 0xA0 if value types
 105   //    hsz is array header size in bytes (i.e., offset of first element)
 106   //    ebt is the BasicType of the elements
 107   //    esz is the element size in bytes
 108   // This packed word is arranged so as to be quickly unpacked by the
 109   // various fast paths that use the various subfields.
 110   //
 111   // The esz bits can be used directly by a SLL instruction, without masking.
 112   //
 113   // Note that the array-kind tag looks like 0x00 for instance klasses,
 114   // since their length in bytes is always less than 24Mb.
 115   //
 116   // Final note:  This comes first, immediately after C++ vtable,
 117   // because it is frequently queried.
 118   jint        _layout_helper;
 119 
 120   // Klass identifier used to implement devirtualized oop closure dispatching.
 121   const KlassID _id;
 122 
 123   // The fields _super_check_offset, _secondary_super_cache, _secondary_supers
 124   // and _primary_supers all help make fast subtype checks.  See big discussion
 125   // in doc/server_compiler/checktype.txt
 126   //
 127   // Where to look to observe a supertype (it is &_secondary_super_cache for
 128   // secondary supers, else is &_primary_supers[depth()].
 129   juint       _super_check_offset;
 130 
 131   // Class name.  Instance classes: java/lang/String, etc.  Array classes: [I,
 132   // [Ljava/lang/String;, etc.  Set to zero for all other kinds of classes.
 133   Symbol*     _name;
 134 
 135   // Cache of last observed secondary supertype
 136   Klass*      _secondary_super_cache;
 137   // Array of all secondary supertypes
 138   Array<Klass*>* _secondary_supers;
 139   // Ordered list of all primary supertypes
 140   Klass*      _primary_supers[_primary_super_limit];
 141   // java/lang/Class instance mirroring this class
 142   OopHandle _java_mirror;
 143   // Superclass
 144   Klass*      _super;
 145   // First subclass (NULL if none); _subklass->next_sibling() is next one
 146   Klass* volatile _subklass;
 147   // Sibling link (or NULL); links all subklasses of a klass
 148   Klass* volatile _next_sibling;
 149 
 150   // All klasses loaded by a class loader are chained through these links
 151   Klass*      _next_link;
 152 
 153   // The VM's representation of the ClassLoader used to load this class.
 154   // Provide access the corresponding instance java.lang.ClassLoader.
 155   ClassLoaderData* _class_loader_data;
 156 
 157   jint        _modifier_flags;  // Processed access flags, for use by Class.getModifiers.
 158   AccessFlags _access_flags;    // Access flags. The class/interface distinction is stored here.
 159 
 160   JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
 161 
 162   // Biased locking implementation and statistics
 163   // (the 64-bit chunk goes first, to avoid some fragmentation)
 164   jlong    _last_biased_lock_bulk_revocation_time;
 165   markOop  _prototype_header;   // Used when biased locking is both enabled and disabled for this type
 166   jint     _biased_lock_revocation_count;
 167 
 168   // vtable length
 169   int _vtable_len;
 170 
 171 private:
 172   // This is an index into FileMapHeader::_shared_path_table[], to
 173   // associate this class with the JAR file where it's loaded from during
 174   // dump time. If a class is not loaded from the shared archive, this field is
 175   // -1.
 176   jshort _shared_class_path_index;
 177 
 178 #if INCLUDE_CDS
 179   // Flags of the current shared class.
 180   u2     _shared_class_flags;
 181   enum {
 182     _has_raw_archived_mirror = 1
 183   };
 184 #endif
 185   // The _archived_mirror is set at CDS dump time pointing to the cached mirror
 186   // in the open archive heap region when archiving java object is supported.
 187   CDS_JAVA_HEAP_ONLY(narrowOop _archived_mirror;)
 188 
 189 protected:
 190 
 191   // Constructor
 192   Klass(KlassID id);
 193   Klass() : _id(KlassID(-1)) { assert(DumpSharedSpaces || UseSharedSpaces, "only for cds"); }
 194 
 195   void* operator new(size_t size, ClassLoaderData* loader_data, size_t word_size, TRAPS) throw();
 196 
 197  public:
 198   int id() { return _id; }
 199 
 200   enum DefaultsLookupMode { find_defaults, skip_defaults };
 201   enum OverpassLookupMode { find_overpass, skip_overpass };
 202   enum StaticLookupMode   { find_static,   skip_static };
 203   enum PrivateLookupMode  { find_private,  skip_private };
 204 
 205   bool is_klass() const volatile { return true; }
 206 
 207   // super() cannot be InstanceKlass* -- Java arrays are covariant, and _super is used
 208   // to implement that. NB: the _super of "[Ljava/lang/Integer;" is "[Ljava/lang/Number;"
 209   // If this is not what your code expects, you're probably looking for Klass::java_super().
 210   Klass* super() const               { return _super; }
 211   void set_super(Klass* k)           { _super = k; }
 212 
 213   // initializes _super link, _primary_supers & _secondary_supers arrays
 214   void initialize_supers(Klass* k, Array<InstanceKlass*>* transitive_interfaces, TRAPS);
 215 
 216   // klass-specific helper for initializing _secondary_supers
 217   virtual GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
 218                                                           Array<InstanceKlass*>* transitive_interfaces);
 219 
 220   // java_super is the Java-level super type as specified by Class.getSuperClass.
 221   virtual InstanceKlass* java_super() const  { return NULL; }
 222 
 223   juint    super_check_offset() const  { return _super_check_offset; }
 224   void set_super_check_offset(juint o) { _super_check_offset = o; }
 225 
 226   Klass* secondary_super_cache() const     { return _secondary_super_cache; }
 227   void set_secondary_super_cache(Klass* k) { _secondary_super_cache = k; }
 228 
 229   Array<Klass*>* secondary_supers() const { return _secondary_supers; }
 230   void set_secondary_supers(Array<Klass*>* k) { _secondary_supers = k; }
 231 
 232   // Return the element of the _super chain of the given depth.
 233   // If there is no such element, return either NULL or this.
 234   Klass* primary_super_of_depth(juint i) const {
 235     assert(i < primary_super_limit(), "oob");
 236     Klass* super = _primary_supers[i];
 237     assert(super == NULL || super->super_depth() == i, "correct display");
 238     return super;
 239   }
 240 
 241   // Can this klass be a primary super?  False for interfaces and arrays of
 242   // interfaces.  False also for arrays or classes with long super chains.
 243   bool can_be_primary_super() const {
 244     const juint secondary_offset = in_bytes(secondary_super_cache_offset());
 245     return super_check_offset() != secondary_offset;
 246   }
 247   virtual bool can_be_primary_super_slow() const;
 248 
 249   // Returns number of primary supers; may be a number in the inclusive range [0, primary_super_limit].
 250   juint super_depth() const {
 251     if (!can_be_primary_super()) {
 252       return primary_super_limit();
 253     } else {
 254       juint d = (super_check_offset() - in_bytes(primary_supers_offset())) / sizeof(Klass*);
 255       assert(d < primary_super_limit(), "oob");
 256       assert(_primary_supers[d] == this, "proper init");
 257       return d;
 258     }
 259   }
 260 
 261   // java mirror
 262   oop java_mirror() const;
 263   oop java_mirror_no_keepalive() const;
 264   void set_java_mirror(Handle m);
 265 
 266   oop archived_java_mirror_raw() NOT_CDS_JAVA_HEAP_RETURN_(NULL); // no GC barrier
 267   narrowOop archived_java_mirror_raw_narrow() NOT_CDS_JAVA_HEAP_RETURN_(0); // no GC barrier
 268   void set_archived_java_mirror_raw(oop m) NOT_CDS_JAVA_HEAP_RETURN; // no GC barrier
 269 
 270   // Temporary mirror switch used by RedefineClasses
 271   // Both mirrors are on the ClassLoaderData::_handles list already so no
 272   // barriers are needed.
 273   void set_java_mirror_handle(OopHandle mirror) { _java_mirror = mirror; }
 274   OopHandle java_mirror_handle() const          {
 275     return _java_mirror;
 276   }
 277 
 278   // modifier flags
 279   jint modifier_flags() const          { return _modifier_flags; }
 280   void set_modifier_flags(jint flags)  { _modifier_flags = flags; }
 281 
 282   // size helper
 283   int layout_helper() const            { return _layout_helper; }
 284   void set_layout_helper(int lh)       { _layout_helper = lh; }
 285 
 286   // Note: for instances layout_helper() may include padding.
 287   // Use InstanceKlass::contains_field_offset to classify field offsets.
 288 
 289   // sub/superklass links
 290   Klass* subklass(bool log = false) const;
 291   Klass* next_sibling(bool log = false) const;
 292 
 293   InstanceKlass* superklass() const;
 294   void append_to_sibling_list();           // add newly created receiver to superklass' subklass list
 295 
 296   void set_next_link(Klass* k) { _next_link = k; }
 297   Klass* next_link() const { return _next_link; }   // The next klass defined by the class loader.
 298 
 299   // class loader data
 300   ClassLoaderData* class_loader_data() const               { return _class_loader_data; }
 301   void set_class_loader_data(ClassLoaderData* loader_data) {  _class_loader_data = loader_data; }
 302 
 303   int shared_classpath_index() const   {
 304     return _shared_class_path_index;
 305   };
 306 
 307   void set_shared_classpath_index(int index) {
 308     _shared_class_path_index = index;
 309   };
 310 
 311   void set_has_raw_archived_mirror() {
 312     CDS_ONLY(_shared_class_flags |= _has_raw_archived_mirror;)
 313   }
 314   void clear_has_raw_archived_mirror() {
 315     CDS_ONLY(_shared_class_flags &= ~_has_raw_archived_mirror;)
 316   }
 317   bool has_raw_archived_mirror() const {
 318     CDS_ONLY(return (_shared_class_flags & _has_raw_archived_mirror) != 0;)
 319     NOT_CDS(return false;)
 320   }
 321 
 322   // Obtain the module or package for this class
 323   virtual ModuleEntry* module() const = 0;
 324   virtual PackageEntry* package() const = 0;
 325 
 326  protected:                                // internal accessors
 327   void     set_subklass(Klass* s);
 328   void     set_next_sibling(Klass* s);
 329 
 330  public:
 331 
 332   // Compiler support
 333   static ByteSize super_offset()                 { return in_ByteSize(offset_of(Klass, _super)); }
 334   static ByteSize super_check_offset_offset()    { return in_ByteSize(offset_of(Klass, _super_check_offset)); }
 335   static ByteSize primary_supers_offset()        { return in_ByteSize(offset_of(Klass, _primary_supers)); }
 336   static ByteSize secondary_super_cache_offset() { return in_ByteSize(offset_of(Klass, _secondary_super_cache)); }
 337   static ByteSize secondary_supers_offset()      { return in_ByteSize(offset_of(Klass, _secondary_supers)); }
 338   static ByteSize java_mirror_offset()           { return in_ByteSize(offset_of(Klass, _java_mirror)); }
 339   static ByteSize modifier_flags_offset()        { return in_ByteSize(offset_of(Klass, _modifier_flags)); }
 340   static ByteSize layout_helper_offset()         { return in_ByteSize(offset_of(Klass, _layout_helper)); }
 341   static ByteSize access_flags_offset()          { return in_ByteSize(offset_of(Klass, _access_flags)); }
 342 
 343   // Unpacking layout_helper:
 344   enum {
 345     _lh_neutral_value           = 0,  // neutral non-array non-instance value
 346     _lh_instance_slow_path_bit  = 0x01,
 347     _lh_log2_element_size_shift = BitsPerByte*0,
 348     _lh_log2_element_size_mask  = BitsPerLong-1,
 349     _lh_element_type_shift      = BitsPerByte*1,
 350     _lh_element_type_mask       = right_n_bits(BitsPerByte),  // shifted mask
 351     _lh_header_size_shift       = BitsPerByte*2,
 352     _lh_header_size_mask        = right_n_bits(BitsPerByte),  // shifted mask
 353     _lh_array_tag_bits          = 3,
 354     _lh_array_tag_shift         = BitsPerInt - _lh_array_tag_bits
 355   };
 356 
 357   static const unsigned int _lh_array_tag_type_value = 0Xfffffffc;
 358   static const unsigned int _lh_array_tag_vt_value   = 0Xfffffffd;
 359   static const unsigned int _lh_array_tag_obj_value  = 0Xfffffffe;
 360 
 361   static int layout_helper_size_in_bytes(jint lh) {
 362     assert(lh > (jint)_lh_neutral_value, "must be instance");
 363     return (int) lh & ~_lh_instance_slow_path_bit;
 364   }
 365   static bool layout_helper_needs_slow_path(jint lh) {
 366     assert(lh > (jint)_lh_neutral_value, "must be instance");
 367     return (lh & _lh_instance_slow_path_bit) != 0;
 368   }
 369   static bool layout_helper_is_instance(jint lh) {
 370     return (jint)lh > (jint)_lh_neutral_value;
 371   }
 372   static bool layout_helper_is_array(jint lh) {
 373     return (jint)lh < (jint)_lh_neutral_value;
 374   }
 375   static bool layout_helper_is_typeArray(jint lh) {
 376     return (juint) _lh_array_tag_type_value == (juint)(lh >> _lh_array_tag_shift);
 377   }
 378   static bool layout_helper_is_objArray(jint lh) {
 379     return (juint)_lh_array_tag_obj_value == (juint)(lh >> _lh_array_tag_shift);
 380   }
 381   static bool layout_helper_is_valueArray(jint lh) {
 382     return (juint)_lh_array_tag_vt_value == (juint)(lh >> _lh_array_tag_shift);
 383   }
 384   static int layout_helper_header_size(jint lh) {
 385     assert(lh < (jint)_lh_neutral_value, "must be array");
 386     int hsize = (lh >> _lh_header_size_shift) & _lh_header_size_mask;
 387     assert(hsize > 0 && hsize < (int)sizeof(oopDesc)*3, "sanity");
 388     return hsize;
 389   }
 390   static BasicType layout_helper_element_type(jint lh) {
 391     assert(lh < (jint)_lh_neutral_value, "must be array");
 392     int btvalue = (lh >> _lh_element_type_shift) & _lh_element_type_mask;
 393     assert((btvalue >= T_BOOLEAN && btvalue <= T_OBJECT) || btvalue == T_VALUETYPE, "sanity");
 394     return (BasicType) btvalue;
 395   }
 396 
 397   // Want a pattern to quickly diff against layout header in register
 398   // find something less clever!
 399   static int layout_helper_boolean_diffbit() {
 400     jint zlh = array_layout_helper(T_BOOLEAN);
 401     jint blh = array_layout_helper(T_BYTE);
 402     assert(zlh != blh, "array layout helpers must differ");
 403     int diffbit = 1;
 404     while ((diffbit & (zlh ^ blh)) == 0 && (diffbit & zlh) == 0) {
 405       diffbit <<= 1;
 406       assert(diffbit != 0, "make sure T_BOOLEAN has a different bit than T_BYTE");
 407     }
 408     return diffbit;
 409   }
 410 
 411   static int layout_helper_log2_element_size(jint lh) {
 412     assert(lh < (jint)_lh_neutral_value, "must be array");
 413     int l2esz = (lh >> _lh_log2_element_size_shift) & _lh_log2_element_size_mask;
 414     assert(layout_helper_element_type(lh) == T_VALUETYPE || l2esz <= LogBytesPerLong,
 415            "sanity. l2esz: 0x%x for lh: 0x%x", (uint)l2esz, (uint)lh);
 416     return l2esz;
 417   }
 418   static jint array_layout_helper(jint tag, int hsize, BasicType etype, int log2_esize) {
 419     return (tag        << _lh_array_tag_shift)
 420       |    (hsize      << _lh_header_size_shift)
 421       |    ((int)etype << _lh_element_type_shift)
 422       |    (log2_esize << _lh_log2_element_size_shift);
 423   }
 424   static jint instance_layout_helper(jint size, bool slow_path_flag) {
 425     return (size << LogBytesPerWord)
 426       |    (slow_path_flag ? _lh_instance_slow_path_bit : 0);
 427   }
 428   static int layout_helper_to_size_helper(jint lh) {
 429     assert(lh > (jint)_lh_neutral_value, "must be instance");
 430     // Note that the following expression discards _lh_instance_slow_path_bit.
 431     return lh >> LogBytesPerWord;
 432   }
 433   // Out-of-line version computes everything based on the etype:
 434   static jint array_layout_helper(BasicType etype);
 435 
 436   // What is the maximum number of primary superclasses any klass can have?
 437   static juint primary_super_limit()         { return _primary_super_limit; }
 438 
 439   // vtables
 440   klassVtable vtable() const;
 441   int vtable_length() const { return _vtable_len; }
 442 
 443   // subclass check
 444   bool is_subclass_of(const Klass* k) const;
 445   // subtype check: true if is_subclass_of, or if k is interface and receiver implements it
 446   bool is_subtype_of(Klass* k) const {
 447     juint    off = k->super_check_offset();
 448     Klass* sup = *(Klass**)( (address)this + off );
 449     const juint secondary_offset = in_bytes(secondary_super_cache_offset());
 450     if (sup == k) {
 451       return true;
 452     } else if (off != secondary_offset) {
 453       return false;
 454     } else {
 455       return search_secondary_supers(k);
 456     }
 457   }
 458 
 459   bool search_secondary_supers(Klass* k) const;
 460 
 461   // Find LCA in class hierarchy
 462   Klass *LCA( Klass *k );
 463 
 464   // Check whether reflection/jni/jvm code is allowed to instantiate this class;
 465   // if not, throw either an Error or an Exception.
 466   virtual void check_valid_for_instantiation(bool throwError, TRAPS);
 467 
 468   // array copying
 469   virtual void  copy_array(arrayOop s, int src_pos, arrayOop d, int dst_pos, int length, TRAPS);
 470 
 471   // tells if the class should be initialized
 472   virtual bool should_be_initialized() const    { return false; }
 473   // initializes the klass
 474   virtual void initialize(TRAPS);
 475   // lookup operation for MethodLookupCache
 476   friend class MethodLookupCache;
 477   virtual Klass* find_field(Symbol* name, Symbol* signature, fieldDescriptor* fd) const;
 478   virtual Method* uncached_lookup_method(const Symbol* name, const Symbol* signature,
 479                                          OverpassLookupMode overpass_mode,
 480                                          PrivateLookupMode = find_private) const;
 481  public:
 482   Method* lookup_method(const Symbol* name, const Symbol* signature) const {
 483     return uncached_lookup_method(name, signature, find_overpass);
 484   }
 485 
 486   // array class with specific rank
 487   Klass* array_klass(int rank, TRAPS) {
 488     return array_klass_impl(ArrayStorageProperties::empty, false, rank, THREAD);
 489   }
 490   Klass* array_klass(ArrayStorageProperties storage_props, int rank, TRAPS) {
 491     return array_klass_impl(storage_props, false, rank, THREAD);
 492   }
 493 
 494   // array class with this klass as element type
 495   Klass* array_klass(TRAPS) {
 496     return array_klass_impl(ArrayStorageProperties::empty, false, THREAD);
 497   }
 498 
 499   // These will return NULL instead of allocating on the heap:
 500   // NB: these can block for a mutex, like other functions with TRAPS arg.
 501   Klass* array_klass_or_null(ArrayStorageProperties storage_props, int rank);
 502   Klass* array_klass_or_null(ArrayStorageProperties storage_props);
 503 
 504   virtual oop protection_domain() const = 0;
 505 
 506   oop class_loader() const;
 507 
 508   // This loads the klass's holder as a phantom. This is useful when a weak Klass
 509   // pointer has been "peeked" and then must be kept alive before it may
 510   // be used safely.  All uses of klass_holder need to apply the appropriate barriers,
 511   // except during GC.
 512   oop klass_holder() const { return class_loader_data()->holder_phantom(); }
 513 
 514  protected:
 515   virtual Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, int rank, TRAPS);
 516   virtual Klass* array_klass_impl(ArrayStorageProperties storage_props, bool or_null, TRAPS);
 517 
 518   // Error handling when length > max_length or length < 0
 519   static void check_array_allocation_length(int length, int max_length, TRAPS);
 520 
 521   void set_vtable_length(int len) { _vtable_len= len; }
 522 
 523   vtableEntry* start_of_vtable() const;
 524  public:
 525   Method* method_at_vtable(int index);
 526 
 527   static ByteSize vtable_start_offset();
 528   static ByteSize vtable_length_offset() {
 529     return byte_offset_of(Klass, _vtable_len);
 530   }
 531 
 532   // CDS support - remove and restore oops from metadata. Oops are not shared.
 533   virtual void remove_unshareable_info();
 534   virtual void remove_java_mirror();
 535   virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS);
 536 
 537  public:
 538   // subclass accessor (here for convenience; undefined for non-klass objects)
 539   virtual bool is_leaf_class() const { fatal("not a class"); return false; }
 540  public:
 541   // ALL FUNCTIONS BELOW THIS POINT ARE DISPATCHED FROM AN OOP
 542   // These functions describe behavior for the oop not the KLASS.
 543 
 544   // actual oop size of obj in memory
 545   virtual int oop_size(oop obj) const = 0;
 546 
 547   // Size of klass in word size.
 548   virtual int size() const = 0;
 549 #if INCLUDE_SERVICES
 550   virtual void collect_statistics(KlassSizeStats *sz) const;
 551 #endif
 552 
 553   // Returns the Java name for a class (Resource allocated)
 554   // For arrays, this returns the name of the element with a leading '['.
 555   // For classes, this returns the name with the package separators
 556   //     turned into '.'s.
 557   const char* external_name() const;
 558   // Returns the name for a class (Resource allocated) as the class
 559   // would appear in a signature.
 560   // For arrays, this returns the name of the element with a leading '['.
 561   // For classes, this returns the name with a leading 'L' and a trailing ';'
 562   //     and the package separators as '/'.
 563   // For value classes, this returns the name with a leading 'Q' and a trailing ';'
 564   //     and the package separators as '/'.
 565   virtual const char* signature_name() const;
 566 
 567   const char* joint_in_module_of_loader(const Klass* class2, bool include_parent_loader = false) const;
 568   const char* class_in_module_of_loader(bool use_are = false, bool include_parent_loader = false) const;
 569 
 570   // Returns "interface", "abstract class" or "class".
 571   const char* external_kind() const;
 572 
 573   // type testing operations
 574 #ifdef ASSERT
 575  protected:
 576   virtual bool is_instance_klass_slow()     const { return false; }
 577   virtual bool is_array_klass_slow()        const { return false; }
 578   virtual bool is_objArray_klass_slow()     const { return false; }
 579   virtual bool is_typeArray_klass_slow()    const { return false; }
 580   virtual bool is_valueArray_klass_slow()   const { return false; }
 581 #endif // ASSERT
 582   // current implementation uses this method even in non debug builds
 583   virtual bool is_value_slow()          const { return false; }
 584  public:
 585 
 586   // Fast non-virtual versions
 587   #ifndef ASSERT
 588   #define assert_same_query(xval, xcheck) xval
 589   #else
 590  private:
 591   static bool assert_same_query(bool xval, bool xslow) {
 592     assert(xval == xslow, "slow and fast queries agree");
 593     return xval;
 594   }
 595  public:
 596   #endif
 597   inline  bool is_instance_klass()            const { return assert_same_query(
 598                                                       layout_helper_is_instance(layout_helper()),
 599                                                       is_instance_klass_slow()); }
 600   inline  bool is_array_klass()               const { return assert_same_query(
 601                                                     layout_helper_is_array(layout_helper()),
 602                                                     is_array_klass_slow()); }
 603   inline  bool is_objArray_klass()            const { return assert_same_query(
 604                                                     layout_helper_is_objArray(layout_helper()),
 605                                                     is_objArray_klass_slow()); }
 606   inline  bool is_typeArray_klass()           const { return assert_same_query(
 607                                                     layout_helper_is_typeArray(layout_helper()),
 608                                                     is_typeArray_klass_slow()); }
 609   inline  bool is_value()                     const { return is_value_slow(); } //temporary hack
 610   inline  bool is_valueArray_klass()          const { return assert_same_query(
 611                                                     layout_helper_is_valueArray(layout_helper()),
 612                                                     is_valueArray_klass_slow()); }
 613 
 614   #undef assert_same_query
 615 
 616   // Access flags
 617   AccessFlags access_flags() const         { return _access_flags;  }
 618   void set_access_flags(AccessFlags flags) { _access_flags = flags; }
 619 
 620   bool is_public() const                { return _access_flags.is_public(); }
 621   bool is_final() const                 { return _access_flags.is_final(); }
 622   bool is_interface() const             { return _access_flags.is_interface(); }
 623   bool is_abstract() const              { return _access_flags.is_abstract(); }
 624   bool is_super() const                 { return _access_flags.is_super(); }
 625   bool is_synthetic() const             { return _access_flags.is_synthetic(); }
 626   void set_is_synthetic()               { _access_flags.set_is_synthetic(); }
 627   bool has_finalizer() const            { return _access_flags.has_finalizer(); }
 628   bool has_final_method() const         { return _access_flags.has_final_method(); }
 629   void set_has_finalizer()              { _access_flags.set_has_finalizer(); }
 630   void set_has_final_method()           { _access_flags.set_has_final_method(); }
 631   bool has_vanilla_constructor() const  { return _access_flags.has_vanilla_constructor(); }
 632   void set_has_vanilla_constructor()    { _access_flags.set_has_vanilla_constructor(); }
 633   bool has_miranda_methods () const     { return access_flags().has_miranda_methods(); }
 634   void set_has_miranda_methods()        { _access_flags.set_has_miranda_methods(); }
 635   bool is_shared() const                { return access_flags().is_shared_class(); } // shadows MetaspaceObj::is_shared)()
 636   void set_is_shared()                  { _access_flags.set_is_shared_class(); }
 637 
 638   bool is_cloneable() const;
 639   void set_is_cloneable();
 640 
 641   // Biased locking support
 642   // Note: the prototype header is always set up to be at least the
 643   // prototype markOop. If biased locking is enabled it may further be
 644   // biasable and have an epoch.
 645   markOop prototype_header() const      { return _prototype_header; }
 646   static inline markOop default_prototype_header(Klass* k) {
 647     return (k == NULL) ? markOopDesc::prototype() : k->prototype_header();
 648   }
 649 
 650   // NOTE: once instances of this klass are floating around in the
 651   // system, this header must only be updated at a safepoint.
 652   // NOTE 2: currently we only ever set the prototype header to the
 653   // biasable prototype for instanceKlasses. There is no technical
 654   // reason why it could not be done for arrayKlasses aside from
 655   // wanting to reduce the initial scope of this optimization. There
 656   // are potential problems in setting the bias pattern for
 657   // JVM-internal oops.
 658   inline void set_prototype_header(markOop header);
 659   static ByteSize prototype_header_offset() { return in_ByteSize(offset_of(Klass, _prototype_header)); }
 660 
 661   int  biased_lock_revocation_count() const { return (int) _biased_lock_revocation_count; }
 662   // Atomically increments biased_lock_revocation_count and returns updated value
 663   int atomic_incr_biased_lock_revocation_count();
 664   void set_biased_lock_revocation_count(int val) { _biased_lock_revocation_count = (jint) val; }
 665   jlong last_biased_lock_bulk_revocation_time() { return _last_biased_lock_bulk_revocation_time; }
 666   void  set_last_biased_lock_bulk_revocation_time(jlong cur_time) { _last_biased_lock_bulk_revocation_time = cur_time; }
 667 
 668   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
 669 
 670   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
 671   virtual MetaspaceObj::Type type() const { return ClassType; }
 672 
 673   // Iff the class loader (or mirror for unsafe anonymous classes) is alive the
 674   // Klass is considered alive. This is safe to call before the CLD is marked as
 675   // unloading, and hence during concurrent class unloading.
 676   bool is_loader_alive() const { return class_loader_data()->is_alive(); }
 677 
 678   void clean_subklass();
 679 
 680   static void clean_weak_klass_links(bool unloading_occurred, bool clean_alive_klasses = true);
 681   static void clean_subklass_tree() {
 682     clean_weak_klass_links(/*unloading_occurred*/ true , /* clean_alive_klasses */ false);
 683   }
 684 
 685   virtual void array_klasses_do(void f(Klass* k)) {}
 686 
 687   // Return self, except for abstract classes with exactly 1
 688   // implementor.  Then return the 1 concrete implementation.
 689   Klass *up_cast_abstract();
 690 
 691   // klass name
 692   Symbol* name() const                   { return _name; }
 693   void set_name(Symbol* n);
 694 
 695  public:
 696   // jvm support
 697   virtual jint compute_modifier_flags(TRAPS) const;
 698 
 699   // JVMTI support
 700   virtual jint jvmti_class_status() const;
 701 
 702   // Printing
 703   virtual void print_on(outputStream* st) const;
 704 
 705   virtual void oop_print_value_on(oop obj, outputStream* st);
 706   virtual void oop_print_on      (oop obj, outputStream* st);
 707 
 708   virtual const char* internal_name() const = 0;
 709 
 710   // Verification
 711   virtual void verify_on(outputStream* st);
 712   void verify() { verify_on(tty); }
 713 
 714 #ifndef PRODUCT
 715   bool verify_vtable_index(int index);
 716 #endif
 717 
 718   virtual void oop_verify_on(oop obj, outputStream* st);
 719 
 720   // for error reporting
 721   static Klass* decode_klass_raw(narrowKlass narrow_klass);
 722   static bool is_valid(Klass* k);
 723 
 724   static bool is_null(narrowKlass obj);
 725   static bool is_null(Klass* obj);
 726 
 727   // klass encoding for klass pointer in objects.
 728   static narrowKlass encode_klass_not_null(Klass* v);
 729   static narrowKlass encode_klass(Klass* v);
 730 
 731   static Klass* decode_klass_not_null(narrowKlass v);
 732   static Klass* decode_klass(narrowKlass v);
 733 };
 734 
 735 #endif // SHARE_OOPS_KLASS_HPP