1 /* 2 * Copyright (c) 1997, 2023, 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_INSTANCEKLASS_HPP 26 #define SHARE_OOPS_INSTANCEKLASS_HPP 27 28 #include "code/vmreg.hpp" 29 #include "memory/referenceType.hpp" 30 #include "oops/annotations.hpp" 31 #include "oops/constMethod.hpp" 32 #include "oops/constantPool.hpp" 33 #include "oops/fieldInfo.hpp" 34 #include "oops/instanceKlassFlags.hpp" 35 #include "oops/instanceOop.hpp" 36 #include "runtime/handles.hpp" 37 #include "utilities/accessFlags.hpp" 38 #include "utilities/align.hpp" 39 #include "utilities/macros.hpp" 40 #if INCLUDE_JFR 41 #include "jfr/support/jfrKlassExtension.hpp" 42 #endif 43 44 class DeoptimizationScope; 45 class klassItable; 46 class RecordComponent; 47 48 // An InstanceKlass is the VM level representation of a Java class. 49 // It contains all information needed for at class at execution runtime. 50 51 // InstanceKlass embedded field layout (after declared fields): 52 // [EMBEDDED Java vtable ] size in words = vtable_len 53 // [EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size 54 // The embedded nonstatic oop-map blocks are short pairs (offset, length) 55 // indicating where oops are located in instances of this klass. 56 // [EMBEDDED implementor of the interface] only exist for interface 57 // [EMBEDDED inline_type_field_klasses] only if has_inline_fields() == true 58 // [EMBEDDED InlineKlassFixedBlock] only if is an InlineKlass instance 59 60 61 // forward declaration for class -- see below for definition 62 #if INCLUDE_JVMTI 63 class BreakpointInfo; 64 #endif 65 class ClassFileParser; 66 class ClassFileStream; 67 class KlassDepChange; 68 class DependencyContext; 69 class fieldDescriptor; 70 class jniIdMapBase; 71 class JNIid; 72 class JvmtiCachedClassFieldMap; 73 class nmethodBucket; 74 class OopMapCache; 75 class BufferedInlineTypeBlob; 76 class InterpreterOopMap; 77 class PackageEntry; 78 class ModuleEntry; 79 80 // This is used in iterators below. 81 class FieldClosure: public StackObj { 82 public: 83 virtual void do_field(fieldDescriptor* fd) = 0; 84 }; 85 86 // Print fields. 87 // If "obj" argument to constructor is null, prints static fields, otherwise prints non-static fields. 88 class FieldPrinter: public FieldClosure { 89 oop _obj; 90 outputStream* _st; 91 public: 92 FieldPrinter(outputStream* st, oop obj = nullptr) : _obj(obj), _st(st) {} 93 void do_field(fieldDescriptor* fd); 94 }; 95 96 // Describes where oops are located in instances of this klass. 97 class OopMapBlock { 98 public: 99 // Byte offset of the first oop mapped by this block. 100 int offset() const { return _offset; } 101 void set_offset(int offset) { _offset = offset; } 102 103 // Number of oops in this block. 104 uint count() const { return _count; } 105 void set_count(uint count) { _count = count; } 106 107 void increment_count(int diff) { _count += diff; } 108 109 int offset_span() const { return _count * heapOopSize; } 110 111 int end_offset() const { 112 return offset() + offset_span(); 113 } 114 115 bool is_contiguous(int another_offset) const { 116 return another_offset == end_offset(); 117 } 118 119 // sizeof(OopMapBlock) in words. 120 static const int size_in_words() { 121 return align_up((int)sizeof(OopMapBlock), wordSize) >> 122 LogBytesPerWord; 123 } 124 125 static int compare_offset(const OopMapBlock* a, const OopMapBlock* b) { 126 return a->offset() - b->offset(); 127 } 128 129 private: 130 int _offset; 131 uint _count; 132 }; 133 134 struct JvmtiCachedClassFileData; 135 136 class SigEntry; 137 138 class InlineKlassFixedBlock { 139 Array<SigEntry>** _extended_sig; 140 Array<VMRegPair>** _return_regs; 141 address* _pack_handler; 142 address* _pack_handler_jobject; 143 address* _unpack_handler; 144 int* _default_value_offset; 145 ArrayKlass** _null_free_inline_array_klasses; 146 int _alignment; 147 int _first_field_offset; 148 int _exact_size_in_bytes; 149 150 friend class InlineKlass; 151 }; 152 153 class InstanceKlass: public Klass { 154 friend class VMStructs; 155 friend class JVMCIVMStructs; 156 friend class ClassFileParser; 157 friend class CompileReplay; 158 friend class TemplateTable; 159 160 public: 161 static const KlassKind Kind = InstanceKlassKind; 162 163 protected: 164 InstanceKlass(const ClassFileParser& parser, KlassKind kind = Kind, ReferenceType reference_type = REF_NONE); 165 166 public: 167 InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); } 168 169 // See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description 170 // of the class loading & initialization procedure, and the use of the states. 171 enum ClassState : u1 { 172 allocated, // allocated (but not yet linked) 173 loaded, // loaded and inserted in class hierarchy (but not linked yet) 174 being_linked, // currently running verifier and rewriter 175 linked, // successfully linked/verified (but not initialized yet) 176 being_initialized, // currently running class initializer 177 fully_initialized, // initialized (successful final state) 178 initialization_error // error happened during initialization 179 }; 180 181 private: 182 static InstanceKlass* allocate_instance_klass(const ClassFileParser& parser, TRAPS); 183 184 protected: 185 // If you add a new field that points to any metaspace object, you 186 // must add this field to InstanceKlass::metaspace_pointers_do(). 187 188 // Annotations for this class 189 Annotations* _annotations; 190 // Package this class is defined in 191 PackageEntry* _package_entry; 192 // Array classes holding elements of this class. 193 ArrayKlass* volatile _array_klasses; 194 // Constant pool for this class. 195 ConstantPool* _constants; 196 // The InnerClasses attribute and EnclosingMethod attribute. The 197 // _inner_classes is an array of shorts. If the class has InnerClasses 198 // attribute, then the _inner_classes array begins with 4-tuples of shorts 199 // [inner_class_info_index, outer_class_info_index, 200 // inner_name_index, inner_class_access_flags] for the InnerClasses 201 // attribute. If the EnclosingMethod attribute exists, it occupies the 202 // last two shorts [class_index, method_index] of the array. If only 203 // the InnerClasses attribute exists, the _inner_classes array length is 204 // number_of_inner_classes * 4. If the class has both InnerClasses 205 // and EnclosingMethod attributes the _inner_classes array length is 206 // number_of_inner_classes * 4 + enclosing_method_attribute_size. 207 Array<jushort>* _inner_classes; 208 209 // The NestMembers attribute. An array of shorts, where each is a 210 // class info index for the class that is a nest member. This data 211 // has not been validated. 212 Array<jushort>* _nest_members; 213 214 // Resolved nest-host klass: either true nest-host or self if we are not 215 // nested, or an error occurred resolving or validating the nominated 216 // nest-host. Can also be set directly by JDK API's that establish nest 217 // relationships. 218 // By always being set it makes nest-member access checks simpler. 219 InstanceKlass* _nest_host; 220 221 // The PermittedSubclasses attribute. An array of shorts, where each is a 222 // class info index for the class that is a permitted subclass. 223 Array<jushort>* _permitted_subclasses; 224 225 // The contents of the Record attribute. 226 Array<RecordComponent*>* _record_components; 227 228 // the source debug extension for this klass, null if not specified. 229 // Specified as UTF-8 string without terminating zero byte in the classfile, 230 // it is stored in the instanceklass as a null-terminated UTF-8 string 231 const char* _source_debug_extension; 232 233 // Number of heapOopSize words used by non-static fields in this klass 234 // (including inherited fields but after header_size()). 235 int _nonstatic_field_size; 236 int _static_field_size; // number words used by static fields (oop and non-oop) in this klass 237 int _nonstatic_oop_map_size; // size in words of nonstatic oop map blocks 238 int _itable_len; // length of Java itable (in words) 239 240 // The NestHost attribute. The class info index for the class 241 // that is the nest-host of this class. This data has not been validated. 242 u2 _nest_host_index; 243 u2 _this_class_index; // constant pool entry 244 u2 _static_oop_field_count; // number of static oop fields in this klass 245 246 volatile u2 _idnum_allocated_count; // JNI/JVMTI: increments with the addition of methods, old ids don't change 247 248 volatile ClassState _init_state; // state of class 249 250 u1 _reference_type; // reference type 251 252 // State is set either at parse time or while executing, atomically to not disturb other state 253 InstanceKlassFlags _misc_flags; 254 255 Monitor* _init_monitor; // mutual exclusion to _init_state and _init_thread. 256 JavaThread* volatile _init_thread; // Pointer to current thread doing initialization (to handle recursive initialization) 257 258 OopMapCache* volatile _oop_map_cache; // OopMapCache for all methods in the klass (allocated lazily) 259 JNIid* _jni_ids; // First JNI identifier for static fields in this class 260 jmethodID* volatile _methods_jmethod_ids; // jmethodIDs corresponding to method_idnum, or null if none 261 nmethodBucket* volatile _dep_context; // packed DependencyContext structure 262 uint64_t volatile _dep_context_last_cleaned; 263 nmethod* _osr_nmethods_head; // Head of list of on-stack replacement nmethods for this class 264 #if INCLUDE_JVMTI 265 BreakpointInfo* _breakpoints; // bpt lists, managed by Method* 266 // Linked instanceKlasses of previous versions 267 InstanceKlass* _previous_versions; 268 // JVMTI fields can be moved to their own structure - see 6315920 269 // JVMTI: cached class file, before retransformable agent modified it in CFLH 270 JvmtiCachedClassFileData* _cached_class_file; 271 #endif 272 273 #if INCLUDE_JVMTI 274 JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map; // JVMTI: used during heap iteration 275 #endif 276 277 NOT_PRODUCT(int _verify_count;) // to avoid redundant verifies 278 NOT_PRODUCT(volatile int _shared_class_load_count;) // ensure a shared class is loaded only once 279 280 // Method array. 281 Array<Method*>* _methods; 282 // Default Method Array, concrete methods inherited from interfaces 283 Array<Method*>* _default_methods; 284 // Interfaces (InstanceKlass*s) this class declares locally to implement. 285 Array<InstanceKlass*>* _local_interfaces; 286 // Interfaces (InstanceKlass*s) this class implements transitively. 287 Array<InstanceKlass*>* _transitive_interfaces; 288 // Int array containing the original order of method in the class file (for JVMTI). 289 Array<int>* _method_ordering; 290 // Int array containing the vtable_indices for default_methods 291 // offset matches _default_methods offset 292 Array<int>* _default_vtable_indices; 293 294 // Fields information is stored in an UNSIGNED5 encoded stream (see fieldInfo.hpp) 295 Array<u1>* _fieldinfo_stream; 296 Array<FieldStatus>* _fields_status; 297 298 const Klass** _inline_type_field_klasses; // For "inline class" fields, NULL if none present 299 Array<u2>* _preload_classes; 300 const InlineKlassFixedBlock* _adr_inlineklass_fixed_block; 301 302 // embedded Java vtable follows here 303 // embedded Java itables follows here 304 // embedded static fields follows here 305 // embedded nonstatic oop-map blocks follows here 306 // embedded implementor of this interface follows here 307 // The embedded implementor only exists if the current klass is an 308 // interface. The possible values of the implementor fall into following 309 // three cases: 310 // null: no implementor. 311 // A Klass* that's not itself: one implementor. 312 // Itself: more than one implementors. 313 // 314 315 friend class SystemDictionary; 316 317 static bool _disable_method_binary_search; 318 319 // Controls finalizer registration 320 static bool _finalization_enabled; 321 322 public: 323 324 // Queries finalization state 325 static bool is_finalization_enabled() { return _finalization_enabled; } 326 327 // Sets finalization state 328 static void set_finalization_enabled(bool val) { _finalization_enabled = val; } 329 330 // The three BUILTIN class loader types 331 bool is_shared_boot_class() const { return _misc_flags.is_shared_boot_class(); } 332 bool is_shared_platform_class() const { return _misc_flags.is_shared_platform_class(); } 333 bool is_shared_app_class() const { return _misc_flags.is_shared_app_class(); } 334 // The UNREGISTERED class loader type 335 bool is_shared_unregistered_class() const { return _misc_flags.is_shared_unregistered_class(); } 336 337 // Check if the class can be shared in CDS 338 bool is_shareable() const; 339 340 bool shared_loading_failed() const { return _misc_flags.shared_loading_failed(); } 341 342 void set_shared_loading_failed() { _misc_flags.set_shared_loading_failed(true); } 343 344 #if INCLUDE_CDS 345 void set_shared_class_loader_type(s2 loader_type) { _misc_flags.set_shared_class_loader_type(loader_type); } 346 void assign_class_loader_type() { _misc_flags.assign_class_loader_type(_class_loader_data); } 347 #endif 348 349 bool has_nonstatic_fields() const { return _misc_flags.has_nonstatic_fields(); } 350 void set_has_nonstatic_fields(bool b) { _misc_flags.set_has_nonstatic_fields(b); } 351 352 bool has_localvariable_table() const { return _misc_flags.has_localvariable_table(); } 353 void set_has_localvariable_table(bool b) { _misc_flags.set_has_localvariable_table(b); } 354 355 bool has_inline_type_fields() const { return _misc_flags.has_inline_type_fields(); } 356 void set_has_inline_type_fields() { _misc_flags.set_has_inline_type_fields(true); } 357 358 bool is_empty_inline_type() const { return _misc_flags.is_empty_inline_type(); } 359 void set_is_empty_inline_type() { _misc_flags.set_is_empty_inline_type(true); } 360 361 // Note: The naturally_atomic property only applies to 362 // inline classes; it is never true on identity classes. 363 // The bit is placed on instanceKlass for convenience. 364 365 // Query if h/w provides atomic load/store for instances. 366 bool is_naturally_atomic() const { return _misc_flags.is_naturally_atomic(); } 367 void set_is_naturally_atomic() { _misc_flags.set_is_naturally_atomic(true); } 368 369 // Query if this class implements jl.NonTearable or was 370 // mentioned in the JVM option ForceNonTearable. 371 // This bit can occur anywhere, but is only significant 372 // for inline classes *and* their super types. 373 // It inherits from supers along with NonTearable. 374 bool is_declared_atomic() const { return _misc_flags.is_declared_atomic(); } 375 void set_is_declared_atomic() { _misc_flags.set_is_declared_atomic(true); } 376 377 bool carries_value_modifier() const { return _misc_flags.carries_value_modifier(); } 378 void set_carries_value_modifier() { _misc_flags.set_carries_value_modifier(true); } 379 380 bool carries_identity_modifier() const { return _misc_flags.carries_identity_modifier(); } 381 void set_carries_identity_modifier() { _misc_flags.set_carries_identity_modifier(true); } 382 383 // field sizes 384 int nonstatic_field_size() const { return _nonstatic_field_size; } 385 void set_nonstatic_field_size(int size) { _nonstatic_field_size = size; } 386 387 int static_field_size() const { return _static_field_size; } 388 void set_static_field_size(int size) { _static_field_size = size; } 389 390 int static_oop_field_count() const { return (int)_static_oop_field_count; } 391 void set_static_oop_field_count(u2 size) { _static_oop_field_count = size; } 392 393 // Java itable 394 int itable_length() const { return _itable_len; } 395 void set_itable_length(int len) { _itable_len = len; } 396 397 // array klasses 398 ArrayKlass* array_klasses() const { return _array_klasses; } 399 inline ArrayKlass* array_klasses_acquire() const; // load with acquire semantics 400 inline void release_set_array_klasses(ArrayKlass* k); // store with release semantics 401 402 // methods 403 Array<Method*>* methods() const { return _methods; } 404 void set_methods(Array<Method*>* a) { _methods = a; } 405 Method* method_with_idnum(int idnum); 406 Method* method_with_orig_idnum(int idnum); 407 Method* method_with_orig_idnum(int idnum, int version); 408 409 // method ordering 410 Array<int>* method_ordering() const { return _method_ordering; } 411 void set_method_ordering(Array<int>* m) { _method_ordering = m; } 412 void copy_method_ordering(const intArray* m, TRAPS); 413 414 // default_methods 415 Array<Method*>* default_methods() const { return _default_methods; } 416 void set_default_methods(Array<Method*>* a) { _default_methods = a; } 417 418 // default method vtable_indices 419 Array<int>* default_vtable_indices() const { return _default_vtable_indices; } 420 void set_default_vtable_indices(Array<int>* v) { _default_vtable_indices = v; } 421 Array<int>* create_new_default_vtable_indices(int len, TRAPS); 422 423 // interfaces 424 Array<InstanceKlass*>* local_interfaces() const { return _local_interfaces; } 425 void set_local_interfaces(Array<InstanceKlass*>* a) { 426 guarantee(_local_interfaces == nullptr || a == nullptr, "Just checking"); 427 _local_interfaces = a; } 428 429 Array<InstanceKlass*>* transitive_interfaces() const { return _transitive_interfaces; } 430 void set_transitive_interfaces(Array<InstanceKlass*>* a) { 431 guarantee(_transitive_interfaces == nullptr || a == nullptr, "Just checking"); 432 _transitive_interfaces = a; 433 } 434 435 private: 436 friend class fieldDescriptor; 437 FieldInfo field(int index) const; 438 439 public: 440 int field_offset (int index) const { return field(index).offset(); } 441 int field_access_flags(int index) const { return field(index).access_flags().as_int(); } 442 FieldInfo::FieldFlags field_flags(int index) const { return field(index).field_flags(); } 443 FieldStatus field_status(int index) const { return fields_status()->at(index); } 444 inline Symbol* field_name (int index) const; 445 inline Symbol* field_signature (int index) const; 446 bool field_is_inlined(int index) const { return field_flags(index).is_inlined(); } 447 bool field_is_null_free_inline_type(int index) const; 448 449 // Number of Java declared fields 450 int java_fields_count() const; 451 int total_fields_count() const; 452 453 Array<u1>* fieldinfo_stream() const { return _fieldinfo_stream; } 454 void set_fieldinfo_stream(Array<u1>* fis) { _fieldinfo_stream = fis; } 455 456 Array<FieldStatus>* fields_status() const {return _fields_status; } 457 void set_fields_status(Array<FieldStatus>* array) { _fields_status = array; } 458 459 Array<u2>* preload_classes() const { return _preload_classes; } 460 void set_preload_classes(Array<u2>* c) { _preload_classes = c; } 461 462 // inner classes 463 Array<u2>* inner_classes() const { return _inner_classes; } 464 void set_inner_classes(Array<u2>* f) { _inner_classes = f; } 465 466 // nest members 467 Array<u2>* nest_members() const { return _nest_members; } 468 void set_nest_members(Array<u2>* m) { _nest_members = m; } 469 470 // nest-host index 471 jushort nest_host_index() const { return _nest_host_index; } 472 void set_nest_host_index(u2 i) { _nest_host_index = i; } 473 // dynamic nest member support 474 void set_nest_host(InstanceKlass* host); 475 476 // record components 477 Array<RecordComponent*>* record_components() const { return _record_components; } 478 void set_record_components(Array<RecordComponent*>* record_components) { 479 _record_components = record_components; 480 } 481 bool is_record() const; 482 483 // permitted subclasses 484 Array<u2>* permitted_subclasses() const { return _permitted_subclasses; } 485 void set_permitted_subclasses(Array<u2>* s) { _permitted_subclasses = s; } 486 487 private: 488 // Called to verify that k is a member of this nest - does not look at k's nest-host, 489 // nor does it resolve any CP entries or load any classes. 490 bool has_nest_member(JavaThread* current, InstanceKlass* k) const; 491 492 public: 493 // Call this only if you know that the nest host has been initialized. 494 InstanceKlass* nest_host_not_null() { 495 assert(_nest_host != nullptr, "must be"); 496 return _nest_host; 497 } 498 // Used to construct informative IllegalAccessError messages at a higher level, 499 // if there was an issue resolving or validating the nest host. 500 // Returns null if there was no error. 501 const char* nest_host_error(); 502 // Returns nest-host class, resolving and validating it if needed. 503 // Returns null if resolution is not possible from the calling context. 504 InstanceKlass* nest_host(TRAPS); 505 // Check if this klass is a nestmate of k - resolves this nest-host and k's 506 bool has_nestmate_access_to(InstanceKlass* k, TRAPS); 507 508 // Called to verify that k is a permitted subclass of this class 509 bool has_as_permitted_subclass(const InstanceKlass* k) const; 510 511 enum InnerClassAttributeOffset { 512 // From http://mirror.eng/products/jdk/1.1/docs/guide/innerclasses/spec/innerclasses.doc10.html#18814 513 inner_class_inner_class_info_offset = 0, 514 inner_class_outer_class_info_offset = 1, 515 inner_class_inner_name_offset = 2, 516 inner_class_access_flags_offset = 3, 517 inner_class_next_offset = 4 518 }; 519 520 enum EnclosingMethodAttributeOffset { 521 enclosing_method_class_index_offset = 0, 522 enclosing_method_method_index_offset = 1, 523 enclosing_method_attribute_size = 2 524 }; 525 526 // package 527 PackageEntry* package() const { return _package_entry; } 528 ModuleEntry* module() const; 529 bool in_unnamed_package() const { return (_package_entry == nullptr); } 530 void set_package(ClassLoaderData* loader_data, PackageEntry* pkg_entry, TRAPS); 531 // If the package for the InstanceKlass is in the boot loader's package entry 532 // table then sets the classpath_index field so that 533 // get_system_package() will know to return a non-null value for the 534 // package's location. And, so that the package will be added to the list of 535 // packages returned by get_system_packages(). 536 // For packages whose classes are loaded from the boot loader class path, the 537 // classpath_index indicates which entry on the boot loader class path. 538 void set_classpath_index(s2 path_index); 539 bool is_same_class_package(const Klass* class2) const; 540 bool is_same_class_package(oop other_class_loader, const Symbol* other_class_name) const; 541 542 // find an enclosing class 543 InstanceKlass* compute_enclosing_class(bool* inner_is_member, TRAPS) const; 544 545 // Find InnerClasses attribute and return outer_class_info_index & inner_name_index. 546 bool find_inner_classes_attr(int* ooff, int* noff, TRAPS) const; 547 548 private: 549 // Check prohibited package ("java/" only loadable by boot or platform loaders) 550 static void check_prohibited_package(Symbol* class_name, 551 ClassLoaderData* loader_data, 552 TRAPS); 553 public: 554 // initialization state 555 bool is_loaded() const { return init_state() >= loaded; } 556 bool is_linked() const { return init_state() >= linked; } 557 bool is_being_linked() const { return init_state() == being_linked; } 558 bool is_initialized() const { return init_state() == fully_initialized; } 559 bool is_not_initialized() const { return init_state() < being_initialized; } 560 bool is_being_initialized() const { return init_state() == being_initialized; } 561 bool is_in_error_state() const { return init_state() == initialization_error; } 562 bool is_init_thread(JavaThread *thread) { return thread == Atomic::load(&_init_thread); } 563 ClassState init_state() const { return Atomic::load(&_init_state); } 564 const char* init_state_name() const; 565 bool is_rewritten() const { return _misc_flags.rewritten(); } 566 567 class LockLinkState : public StackObj { 568 InstanceKlass* _ik; 569 JavaThread* _current; 570 public: 571 LockLinkState(InstanceKlass* ik, JavaThread* current) : _ik(ik), _current(current) { 572 ik->check_link_state_and_wait(current); 573 } 574 ~LockLinkState() { 575 if (!_ik->is_linked()) { 576 // Reset to loaded if linking failed. 577 _ik->set_initialization_state_and_notify(loaded, _current); 578 } 579 } 580 }; 581 582 // is this a sealed class 583 bool is_sealed() const; 584 585 // defineClass specified verification 586 bool should_verify_class() const { return _misc_flags.should_verify_class(); } 587 void set_should_verify_class(bool value) { _misc_flags.set_should_verify_class(value); } 588 589 // marking 590 bool is_marked_dependent() const { return _misc_flags.is_marked_dependent(); } 591 void set_is_marked_dependent(bool value) { _misc_flags.set_is_marked_dependent(value); } 592 593 static ByteSize kind_offset() { return in_ByteSize(offset_of(InstanceKlass, _kind)); } 594 static ByteSize misc_flags_offset() { return in_ByteSize(offset_of(InstanceKlass, _misc_flags)); } 595 596 // initialization (virtuals from Klass) 597 bool should_be_initialized() const; // means that initialize should be called 598 void initialize(TRAPS); 599 void link_class(TRAPS); 600 bool link_class_or_fail(TRAPS); // returns false on failure 601 void rewrite_class(TRAPS); 602 void link_methods(TRAPS); 603 Method* class_initializer() const; 604 605 // reference type 606 ReferenceType reference_type() const { return (ReferenceType)_reference_type; } 607 608 // this class cp index 609 u2 this_class_index() const { return _this_class_index; } 610 void set_this_class_index(u2 index) { _this_class_index = index; } 611 612 static ByteSize reference_type_offset() { return byte_offset_of(InstanceKlass, _reference_type); } 613 614 // find local field, returns true if found 615 bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const; 616 // find field in direct superinterfaces, returns the interface in which the field is defined 617 Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const; 618 // find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined 619 Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const; 620 // find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined 621 Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const; 622 623 // find a non-static or static field given its offset within the class. 624 bool contains_field_offset(int offset); 625 626 bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const; 627 bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const; 628 629 private: 630 inline static int quick_search(const Array<Method*>* methods, const Symbol* name); 631 632 public: 633 static void disable_method_binary_search() { 634 _disable_method_binary_search = true; 635 } 636 637 // find a local method (returns null if not found) 638 Method* find_method(const Symbol* name, const Symbol* signature) const; 639 static Method* find_method(const Array<Method*>* methods, 640 const Symbol* name, 641 const Symbol* signature); 642 643 // find a local method, but skip static methods 644 Method* find_instance_method(const Symbol* name, const Symbol* signature, 645 PrivateLookupMode private_mode) const; 646 static Method* find_instance_method(const Array<Method*>* methods, 647 const Symbol* name, 648 const Symbol* signature, 649 PrivateLookupMode private_mode); 650 651 // find a local method (returns null if not found) 652 Method* find_local_method(const Symbol* name, 653 const Symbol* signature, 654 OverpassLookupMode overpass_mode, 655 StaticLookupMode static_mode, 656 PrivateLookupMode private_mode) const; 657 658 // find a local method from given methods array (returns null if not found) 659 static Method* find_local_method(const Array<Method*>* methods, 660 const Symbol* name, 661 const Symbol* signature, 662 OverpassLookupMode overpass_mode, 663 StaticLookupMode static_mode, 664 PrivateLookupMode private_mode); 665 666 // find a local method index in methods or default_methods (returns -1 if not found) 667 static int find_method_index(const Array<Method*>* methods, 668 const Symbol* name, 669 const Symbol* signature, 670 OverpassLookupMode overpass_mode, 671 StaticLookupMode static_mode, 672 PrivateLookupMode private_mode); 673 674 // lookup operation (returns null if not found) 675 Method* uncached_lookup_method(const Symbol* name, 676 const Symbol* signature, 677 OverpassLookupMode overpass_mode, 678 PrivateLookupMode private_mode = PrivateLookupMode::find) const; 679 680 // lookup a method in all the interfaces that this class implements 681 // (returns null if not found) 682 Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature, DefaultsLookupMode defaults_mode) const; 683 684 // lookup a method in local defaults then in all interfaces 685 // (returns null if not found) 686 Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const; 687 688 // Find method indices by name. If a method with the specified name is 689 // found the index to the first method is returned, and 'end' is filled in 690 // with the index of first non-name-matching method. If no method is found 691 // -1 is returned. 692 int find_method_by_name(const Symbol* name, int* end) const; 693 static int find_method_by_name(const Array<Method*>* methods, 694 const Symbol* name, int* end); 695 696 // constant pool 697 ConstantPool* constants() const { return _constants; } 698 void set_constants(ConstantPool* c) { _constants = c; } 699 700 // protection domain 701 oop protection_domain() const; 702 703 // signers 704 objArrayOop signers() const; 705 706 bool is_contended() const { return _misc_flags.is_contended(); } 707 void set_is_contended(bool value) { _misc_flags.set_is_contended(value); } 708 709 // source file name 710 Symbol* source_file_name() const { return _constants->source_file_name(); } 711 u2 source_file_name_index() const { return _constants->source_file_name_index(); } 712 void set_source_file_name_index(u2 sourcefile_index) { _constants->set_source_file_name_index(sourcefile_index); } 713 714 // minor and major version numbers of class file 715 u2 minor_version() const { return _constants->minor_version(); } 716 void set_minor_version(u2 minor_version) { _constants->set_minor_version(minor_version); } 717 u2 major_version() const { return _constants->major_version(); } 718 void set_major_version(u2 major_version) { _constants->set_major_version(major_version); } 719 720 // source debug extension 721 const char* source_debug_extension() const { return _source_debug_extension; } 722 void set_source_debug_extension(const char* array, int length); 723 724 // nonstatic oop-map blocks 725 static int nonstatic_oop_map_size(unsigned int oop_map_count) { 726 return oop_map_count * OopMapBlock::size_in_words(); 727 } 728 unsigned int nonstatic_oop_map_count() const { 729 return _nonstatic_oop_map_size / OopMapBlock::size_in_words(); 730 } 731 int nonstatic_oop_map_size() const { return _nonstatic_oop_map_size; } 732 void set_nonstatic_oop_map_size(int words) { 733 _nonstatic_oop_map_size = words; 734 } 735 736 bool has_contended_annotations() const { return _misc_flags.has_contended_annotations(); } 737 void set_has_contended_annotations(bool value) { _misc_flags.set_has_contended_annotations(value); } 738 739 #if INCLUDE_JVMTI 740 // Redefinition locking. Class can only be redefined by one thread at a time. 741 // The flag is in access_flags so that it can be set and reset using atomic 742 // operations, and not be reset by other misc_flag settings. 743 bool is_being_redefined() const { return _misc_flags.is_being_redefined(); } 744 void set_is_being_redefined(bool value) { _misc_flags.set_is_being_redefined(value); } 745 746 // RedefineClasses() support for previous versions: 747 void add_previous_version(InstanceKlass* ik, int emcp_method_count); 748 void purge_previous_version_list(); 749 750 InstanceKlass* previous_versions() const { return _previous_versions; } 751 #else 752 InstanceKlass* previous_versions() const { return nullptr; } 753 #endif 754 755 InstanceKlass* get_klass_version(int version) { 756 for (InstanceKlass* ik = this; ik != nullptr; ik = ik->previous_versions()) { 757 if (ik->constants()->version() == version) { 758 return ik; 759 } 760 } 761 return nullptr; 762 } 763 764 bool has_been_redefined() const { return _misc_flags.has_been_redefined(); } 765 void set_has_been_redefined() { _misc_flags.set_has_been_redefined(true); } 766 767 bool is_scratch_class() const { return _misc_flags.is_scratch_class(); } 768 void set_is_scratch_class() { _misc_flags.set_is_scratch_class(true); } 769 770 bool has_resolved_methods() const { return _misc_flags.has_resolved_methods(); } 771 void set_has_resolved_methods() { _misc_flags.set_has_resolved_methods(true); } 772 void set_has_resolved_methods(bool value) { _misc_flags.set_has_resolved_methods(value); } 773 774 public: 775 #if INCLUDE_JVMTI 776 777 void init_previous_versions() { 778 _previous_versions = nullptr; 779 } 780 781 private: 782 static bool _should_clean_previous_versions; 783 public: 784 static void purge_previous_versions(InstanceKlass* ik) { 785 if (ik->has_been_redefined()) { 786 ik->purge_previous_version_list(); 787 } 788 } 789 790 static bool should_clean_previous_versions_and_reset(); 791 static bool should_clean_previous_versions() { return _should_clean_previous_versions; } 792 793 // JVMTI: Support for caching a class file before it is modified by an agent that can do retransformation 794 void set_cached_class_file(JvmtiCachedClassFileData *data) { 795 _cached_class_file = data; 796 } 797 JvmtiCachedClassFileData * get_cached_class_file(); 798 jint get_cached_class_file_len(); 799 unsigned char * get_cached_class_file_bytes(); 800 801 // JVMTI: Support for caching of field indices, types, and offsets 802 void set_jvmti_cached_class_field_map(JvmtiCachedClassFieldMap* descriptor) { 803 _jvmti_cached_class_field_map = descriptor; 804 } 805 JvmtiCachedClassFieldMap* jvmti_cached_class_field_map() const { 806 return _jvmti_cached_class_field_map; 807 } 808 #else // INCLUDE_JVMTI 809 810 static void purge_previous_versions(InstanceKlass* ik) { return; }; 811 static bool should_clean_previous_versions_and_reset() { return false; } 812 813 void set_cached_class_file(JvmtiCachedClassFileData *data) { 814 assert(data == nullptr, "unexpected call with JVMTI disabled"); 815 } 816 JvmtiCachedClassFileData * get_cached_class_file() { return (JvmtiCachedClassFileData *)nullptr; } 817 818 #endif // INCLUDE_JVMTI 819 820 bool has_nonstatic_concrete_methods() const { return _misc_flags.has_nonstatic_concrete_methods(); } 821 void set_has_nonstatic_concrete_methods(bool b) { _misc_flags.set_has_nonstatic_concrete_methods(b); } 822 823 bool declares_nonstatic_concrete_methods() const { return _misc_flags.declares_nonstatic_concrete_methods(); } 824 void set_declares_nonstatic_concrete_methods(bool b) { _misc_flags.set_declares_nonstatic_concrete_methods(b); } 825 826 bool has_vanilla_constructor() const { return _misc_flags.has_vanilla_constructor(); } 827 void set_has_vanilla_constructor() { _misc_flags.set_has_vanilla_constructor(true); } 828 bool has_miranda_methods () const { return _misc_flags.has_miranda_methods(); } 829 void set_has_miranda_methods() { _misc_flags.set_has_miranda_methods(true); } 830 bool has_final_method() const { return _misc_flags.has_final_method(); } 831 void set_has_final_method() { _misc_flags.set_has_final_method(true); } 832 833 // for adding methods, ConstMethod::UNSET_IDNUM means no more ids available 834 inline u2 next_method_idnum(); 835 void set_initial_method_idnum(u2 value) { _idnum_allocated_count = value; } 836 837 // generics support 838 Symbol* generic_signature() const { return _constants->generic_signature(); } 839 u2 generic_signature_index() const { return _constants->generic_signature_index(); } 840 void set_generic_signature_index(u2 sig_index) { _constants->set_generic_signature_index(sig_index); } 841 842 u2 enclosing_method_data(int offset) const; 843 u2 enclosing_method_class_index() const { 844 return enclosing_method_data(enclosing_method_class_index_offset); 845 } 846 u2 enclosing_method_method_index() { 847 return enclosing_method_data(enclosing_method_method_index_offset); 848 } 849 void set_enclosing_method_indices(u2 class_index, 850 u2 method_index); 851 852 // jmethodID support 853 jmethodID get_jmethod_id(const methodHandle& method_h); 854 jmethodID get_jmethod_id_fetch_or_update(size_t idnum, 855 jmethodID new_id, jmethodID* new_jmeths, 856 jmethodID* to_dealloc_id_p, 857 jmethodID** to_dealloc_jmeths_p); 858 static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum, 859 size_t *length_p, jmethodID* id_p); 860 void ensure_space_for_methodids(int start_offset = 0); 861 jmethodID jmethod_id_or_null(Method* method); 862 863 // annotations support 864 Annotations* annotations() const { return _annotations; } 865 void set_annotations(Annotations* anno) { _annotations = anno; } 866 867 AnnotationArray* class_annotations() const { 868 return (_annotations != nullptr) ? _annotations->class_annotations() : nullptr; 869 } 870 Array<AnnotationArray*>* fields_annotations() const { 871 return (_annotations != nullptr) ? _annotations->fields_annotations() : nullptr; 872 } 873 AnnotationArray* class_type_annotations() const { 874 return (_annotations != nullptr) ? _annotations->class_type_annotations() : nullptr; 875 } 876 Array<AnnotationArray*>* fields_type_annotations() const { 877 return (_annotations != nullptr) ? _annotations->fields_type_annotations() : nullptr; 878 } 879 // allocation 880 instanceOop allocate_instance(TRAPS); 881 static instanceOop allocate_instance(oop cls, TRAPS); 882 883 // additional member function to return a handle 884 instanceHandle allocate_instance_handle(TRAPS); 885 886 objArrayOop allocate_objArray(int n, int length, TRAPS); 887 // Helper function 888 static instanceOop register_finalizer(instanceOop i, TRAPS); 889 890 // Check whether reflection/jni/jvm code is allowed to instantiate this class; 891 // if not, throw either an Error or an Exception. 892 virtual void check_valid_for_instantiation(bool throwError, TRAPS); 893 894 // initialization 895 void call_class_initializer(TRAPS); 896 void set_initialization_state_and_notify(ClassState state, JavaThread* current); 897 898 // OopMapCache support 899 OopMapCache* oop_map_cache() { return _oop_map_cache; } 900 void set_oop_map_cache(OopMapCache *cache) { _oop_map_cache = cache; } 901 void mask_for(const methodHandle& method, int bci, InterpreterOopMap* entry); 902 903 // JNI identifier support (for static fields - for jni performance) 904 JNIid* jni_ids() { return _jni_ids; } 905 void set_jni_ids(JNIid* ids) { _jni_ids = ids; } 906 JNIid* jni_id_for(int offset); 907 908 // maintenance of deoptimization dependencies 909 inline DependencyContext dependencies(); 910 void mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes); 911 void add_dependent_nmethod(nmethod* nm); 912 void clean_dependency_context(); 913 // Setup link to hierarchy and deoptimize 914 void add_to_hierarchy(JavaThread* current); 915 916 // On-stack replacement support 917 nmethod* osr_nmethods_head() const { return _osr_nmethods_head; }; 918 void set_osr_nmethods_head(nmethod* h) { _osr_nmethods_head = h; }; 919 void add_osr_nmethod(nmethod* n); 920 bool remove_osr_nmethod(nmethod* n); 921 int mark_osr_nmethods(DeoptimizationScope* deopt_scope, const Method* m); 922 nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const; 923 924 #if INCLUDE_JVMTI 925 // Breakpoint support (see methods on Method* for details) 926 BreakpointInfo* breakpoints() const { return _breakpoints; }; 927 void set_breakpoints(BreakpointInfo* bps) { _breakpoints = bps; }; 928 #endif 929 930 // support for stub routines 931 static ByteSize init_state_offset() { return byte_offset_of(InstanceKlass, _init_state); } 932 JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;) 933 static ByteSize init_thread_offset() { return byte_offset_of(InstanceKlass, _init_thread); } 934 935 static ByteSize inline_type_field_klasses_offset() { return in_ByteSize(offset_of(InstanceKlass, _inline_type_field_klasses)); } 936 static ByteSize adr_inlineklass_fixed_block_offset() { return in_ByteSize(offset_of(InstanceKlass, _adr_inlineklass_fixed_block)); } 937 938 // subclass/subinterface checks 939 bool implements_interface(Klass* k) const; 940 bool is_same_or_direct_interface(Klass* k) const; 941 942 #ifdef ASSERT 943 // check whether this class or one of its superclasses was redefined 944 bool has_redefined_this_or_super() const; 945 #endif 946 947 // Access to the implementor of an interface. 948 InstanceKlass* implementor() const; 949 void set_implementor(InstanceKlass* ik); 950 int nof_implementors() const; 951 void add_implementor(InstanceKlass* ik); // ik is a new class that implements this interface 952 void init_implementor(); // initialize 953 954 private: 955 // link this class into the implementors list of every interface it implements 956 void process_interfaces(); 957 958 public: 959 // virtual operations from Klass 960 GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots, 961 Array<InstanceKlass*>* transitive_interfaces); 962 bool can_be_primary_super_slow() const; 963 size_t oop_size(oop obj) const { return size_helper(); } 964 // slow because it's a virtual call and used for verifying the layout_helper. 965 // Using the layout_helper bits, we can call is_instance_klass without a virtual call. 966 DEBUG_ONLY(bool is_instance_klass_slow() const { return true; }) 967 968 // Iterators 969 void do_local_static_fields(FieldClosure* cl); 970 void do_nonstatic_fields(FieldClosure* cl); // including inherited fields 971 void do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle, TRAPS); 972 void print_nonstatic_fields(FieldClosure* cl); // including inherited and injected fields 973 974 void methods_do(void f(Method* method)); 975 976 static InstanceKlass* cast(Klass* k) { 977 return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k))); 978 } 979 980 static const InstanceKlass* cast(const Klass* k) { 981 assert(k != nullptr, "k should not be null"); 982 assert(k->is_instance_klass(), "cast to InstanceKlass"); 983 return static_cast<const InstanceKlass*>(k); 984 } 985 986 virtual InstanceKlass* java_super() const { 987 return (super() == nullptr) ? nullptr : cast(super()); 988 } 989 990 // Sizing (in words) 991 static int header_size() { return sizeof(InstanceKlass)/wordSize; } 992 993 static int size(int vtable_length, int itable_length, 994 int nonstatic_oop_map_size, 995 bool is_interface, 996 int java_fields, bool is_inline_type) { 997 return align_metadata_size(header_size() + 998 vtable_length + 999 itable_length + 1000 nonstatic_oop_map_size + 1001 (is_interface ? (int)sizeof(Klass*)/wordSize : 0) + 1002 (java_fields * (int)sizeof(Klass*)/wordSize) + 1003 (is_inline_type ? (int)sizeof(InlineKlassFixedBlock) : 0)); 1004 } 1005 1006 int size() const { return size(vtable_length(), 1007 itable_length(), 1008 nonstatic_oop_map_size(), 1009 is_interface(), 1010 has_inline_type_fields() ? java_fields_count() : 0, 1011 is_inline_klass()); 1012 } 1013 1014 1015 inline intptr_t* start_of_itable() const; 1016 inline intptr_t* end_of_itable() const; 1017 inline int itable_offset_in_words() const; 1018 inline oop static_field_base_raw(); 1019 bool bounds_check(address addr, bool edge_ok = false, intptr_t size_in_bytes = -1) const PRODUCT_RETURN0; 1020 1021 inline OopMapBlock* start_of_nonstatic_oop_maps() const; 1022 inline Klass** end_of_nonstatic_oop_maps() const; 1023 1024 inline InstanceKlass* volatile* adr_implementor() const; 1025 1026 inline address adr_inline_type_field_klasses() const; 1027 inline Klass* get_inline_type_field_klass(int idx) const; 1028 inline Klass* get_inline_type_field_klass_or_null(int idx) const; 1029 inline void set_inline_type_field_klass(int idx, Klass* k); 1030 inline void reset_inline_type_field_klass(int idx); 1031 1032 // Use this to return the size of an instance in heap words: 1033 virtual int size_helper() const { 1034 return layout_helper_to_size_helper(layout_helper()); 1035 } 1036 1037 // This bit is initialized in classFileParser.cpp. 1038 // It is false under any of the following conditions: 1039 // - the class is abstract (including any interface) 1040 // - the class has a finalizer (if !RegisterFinalizersAtInit) 1041 // - the class size is larger than FastAllocateSizeLimit 1042 // - the class is java/lang/Class, which cannot be allocated directly 1043 bool can_be_fastpath_allocated() const { 1044 return !layout_helper_needs_slow_path(layout_helper()); 1045 } 1046 1047 // Java itable 1048 klassItable itable() const; // return klassItable wrapper 1049 Method* method_at_itable(InstanceKlass* holder, int index, TRAPS); 1050 Method* method_at_itable_or_null(InstanceKlass* holder, int index, bool& itable_entry_found); 1051 int vtable_index_of_interface_method(Method* method); 1052 1053 #if INCLUDE_JVMTI 1054 void adjust_default_methods(bool* trace_name_printed); 1055 #endif // INCLUDE_JVMTI 1056 1057 void clean_weak_instanceklass_links(); 1058 private: 1059 void clean_implementors_list(); 1060 void clean_method_data(); 1061 1062 public: 1063 // Explicit metaspace deallocation of fields 1064 // For RedefineClasses and class file parsing errors, we need to deallocate 1065 // instanceKlasses and the metadata they point to. 1066 void deallocate_contents(ClassLoaderData* loader_data); 1067 static void deallocate_methods(ClassLoaderData* loader_data, 1068 Array<Method*>* methods); 1069 void static deallocate_interfaces(ClassLoaderData* loader_data, 1070 const Klass* super_klass, 1071 Array<InstanceKlass*>* local_interfaces, 1072 Array<InstanceKlass*>* transitive_interfaces); 1073 void static deallocate_record_components(ClassLoaderData* loader_data, 1074 Array<RecordComponent*>* record_component); 1075 1076 // The constant pool is on stack if any of the methods are executing or 1077 // referenced by handles. 1078 bool on_stack() const { return _constants->on_stack(); } 1079 1080 // callbacks for actions during class unloading 1081 static void unload_class(InstanceKlass* ik); 1082 1083 virtual void release_C_heap_structures(bool release_sub_metadata = true); 1084 1085 // Naming 1086 const char* signature_name() const; 1087 const char* signature_name_of_carrier(char c) const; 1088 1089 // Oop fields (and metadata) iterators 1090 // 1091 // The InstanceKlass iterators also visits the Object's klass. 1092 1093 // Forward iteration 1094 public: 1095 // Iterate over all oop fields in the oop maps. 1096 template <typename T, class OopClosureType> 1097 inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure); 1098 1099 // Iterate over all oop fields and metadata. 1100 template <typename T, class OopClosureType> 1101 inline void oop_oop_iterate(oop obj, OopClosureType* closure); 1102 1103 // Iterate over all oop fields in one oop map. 1104 template <typename T, class OopClosureType> 1105 inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure); 1106 1107 1108 // Reverse iteration 1109 // Iterate over all oop fields and metadata. 1110 template <typename T, class OopClosureType> 1111 inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure); 1112 1113 private: 1114 // Iterate over all oop fields in the oop maps. 1115 template <typename T, class OopClosureType> 1116 inline void oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure); 1117 1118 // Iterate over all oop fields in one oop map. 1119 template <typename T, class OopClosureType> 1120 inline void oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure); 1121 1122 1123 // Bounded range iteration 1124 public: 1125 // Iterate over all oop fields in the oop maps. 1126 template <typename T, class OopClosureType> 1127 inline void oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr); 1128 1129 // Iterate over all oop fields and metadata. 1130 template <typename T, class OopClosureType> 1131 inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr); 1132 1133 private: 1134 // Iterate over all oop fields in one oop map. 1135 template <typename T, class OopClosureType> 1136 inline void oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr); 1137 1138 1139 public: 1140 u2 idnum_allocated_count() const { return _idnum_allocated_count; } 1141 1142 private: 1143 // initialization state 1144 void set_init_state(ClassState state); 1145 void set_rewritten() { _misc_flags.set_rewritten(true); } 1146 void set_init_thread(JavaThread *thread) { 1147 assert((thread == JavaThread::current() && _init_thread == nullptr) || 1148 (thread == nullptr && _init_thread == JavaThread::current()), "Only one thread is allowed to own initialization"); 1149 Atomic::store(&_init_thread, thread); 1150 } 1151 1152 // The RedefineClasses() API can cause new method idnums to be needed 1153 // which will cause the caches to grow. Safety requires different 1154 // cache management logic if the caches can grow instead of just 1155 // going from null to non-null. 1156 bool idnum_can_increment() const { return has_been_redefined(); } 1157 inline jmethodID* methods_jmethod_ids_acquire() const; 1158 inline void release_set_methods_jmethod_ids(jmethodID* jmeths); 1159 1160 // Lock during initialization 1161 public: 1162 // Returns the array class for the n'th dimension 1163 virtual Klass* array_klass(int n, TRAPS); 1164 virtual Klass* array_klass_or_null(int n); 1165 1166 // Returns the array class with this class as element type 1167 virtual Klass* array_klass(TRAPS); 1168 virtual Klass* array_klass_or_null(); 1169 1170 static void clean_initialization_error_table(); 1171 1172 Monitor* init_monitor() const { return _init_monitor; } 1173 private: 1174 void check_link_state_and_wait(JavaThread* current); 1175 bool link_class_impl (TRAPS); 1176 bool verify_code (TRAPS); 1177 void initialize_impl (TRAPS); 1178 void initialize_super_interfaces (TRAPS); 1179 1180 void add_initialization_error(JavaThread* current, Handle exception); 1181 oop get_initialization_error(JavaThread* current); 1182 1183 // find a local method (returns null if not found) 1184 Method* find_method_impl(const Symbol* name, 1185 const Symbol* signature, 1186 OverpassLookupMode overpass_mode, 1187 StaticLookupMode static_mode, 1188 PrivateLookupMode private_mode) const; 1189 1190 static Method* find_method_impl(const Array<Method*>* methods, 1191 const Symbol* name, 1192 const Symbol* signature, 1193 OverpassLookupMode overpass_mode, 1194 StaticLookupMode static_mode, 1195 PrivateLookupMode private_mode); 1196 1197 #if INCLUDE_JVMTI 1198 // RedefineClasses support 1199 void link_previous_versions(InstanceKlass* pv) { _previous_versions = pv; } 1200 void mark_newly_obsolete_methods(Array<Method*>* old_methods, int emcp_method_count); 1201 #endif 1202 // log class name to classlist 1203 void log_to_classlist() const; 1204 public: 1205 1206 #if INCLUDE_CDS 1207 // CDS support - remove and restore oops from metadata. Oops are not shared. 1208 virtual void remove_unshareable_info(); 1209 void remove_unshareable_flags(); 1210 virtual void remove_java_mirror(); 1211 virtual void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS); 1212 void init_shared_package_entry(); 1213 bool can_be_verified_at_dumptime() const; 1214 bool methods_contain_jsr_bytecode() const; 1215 void compute_has_loops_flag_for_methods(); 1216 #endif 1217 1218 jint compute_modifier_flags() const; 1219 1220 public: 1221 // JVMTI support 1222 jint jvmti_class_status() const; 1223 1224 virtual void metaspace_pointers_do(MetaspaceClosure* iter); 1225 1226 public: 1227 // Printing 1228 void print_on(outputStream* st) const; 1229 void print_value_on(outputStream* st) const; 1230 1231 void oop_print_value_on(oop obj, outputStream* st); 1232 1233 void oop_print_on (oop obj, outputStream* st); 1234 1235 #ifndef PRODUCT 1236 void print_dependent_nmethods(bool verbose = false); 1237 bool is_dependent_nmethod(nmethod* nm); 1238 bool verify_itable_index(int index); 1239 #endif 1240 1241 const char* internal_name() const; 1242 1243 // Verification 1244 void verify_on(outputStream* st); 1245 1246 void oop_verify_on(oop obj, outputStream* st); 1247 1248 // Logging 1249 void print_class_load_logging(ClassLoaderData* loader_data, 1250 const ModuleEntry* module_entry, 1251 const ClassFileStream* cfs) const; 1252 }; 1253 1254 // for adding methods 1255 // UNSET_IDNUM return means no more ids available 1256 inline u2 InstanceKlass::next_method_idnum() { 1257 if (_idnum_allocated_count == ConstMethod::MAX_IDNUM) { 1258 return ConstMethod::UNSET_IDNUM; // no more ids available 1259 } else { 1260 return _idnum_allocated_count++; 1261 } 1262 } 1263 1264 class PrintClassClosure : public KlassClosure { 1265 private: 1266 outputStream* _st; 1267 bool _verbose; 1268 public: 1269 PrintClassClosure(outputStream* st, bool verbose); 1270 1271 void do_klass(Klass* k); 1272 }; 1273 1274 /* JNIid class for jfieldIDs only */ 1275 class JNIid: public CHeapObj<mtClass> { 1276 friend class VMStructs; 1277 private: 1278 Klass* _holder; 1279 JNIid* _next; 1280 int _offset; 1281 #ifdef ASSERT 1282 bool _is_static_field_id; 1283 #endif 1284 1285 public: 1286 // Accessors 1287 Klass* holder() const { return _holder; } 1288 int offset() const { return _offset; } 1289 JNIid* next() { return _next; } 1290 // Constructor 1291 JNIid(Klass* holder, int offset, JNIid* next); 1292 // Identifier lookup 1293 JNIid* find(int offset); 1294 1295 bool find_local_field(fieldDescriptor* fd) { 1296 return InstanceKlass::cast(holder())->find_local_field_from_offset(offset(), true, fd); 1297 } 1298 1299 static void deallocate(JNIid* id); 1300 // Debugging 1301 #ifdef ASSERT 1302 bool is_static_field_id() const { return _is_static_field_id; } 1303 void set_is_static_field_id() { _is_static_field_id = true; } 1304 #endif 1305 void verify(Klass* holder); 1306 }; 1307 1308 // An iterator that's used to access the inner classes indices in the 1309 // InstanceKlass::_inner_classes array. 1310 class InnerClassesIterator : public StackObj { 1311 private: 1312 Array<jushort>* _inner_classes; 1313 int _length; 1314 int _idx; 1315 public: 1316 1317 InnerClassesIterator(const InstanceKlass* k) { 1318 _inner_classes = k->inner_classes(); 1319 if (k->inner_classes() != nullptr) { 1320 _length = _inner_classes->length(); 1321 // The inner class array's length should be the multiple of 1322 // inner_class_next_offset if it only contains the InnerClasses 1323 // attribute data, or it should be 1324 // n*inner_class_next_offset+enclosing_method_attribute_size 1325 // if it also contains the EnclosingMethod data. 1326 assert((_length % InstanceKlass::inner_class_next_offset == 0 || 1327 _length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size), 1328 "just checking"); 1329 // Remove the enclosing_method portion if exists. 1330 if (_length % InstanceKlass::inner_class_next_offset == InstanceKlass::enclosing_method_attribute_size) { 1331 _length -= InstanceKlass::enclosing_method_attribute_size; 1332 } 1333 } else { 1334 _length = 0; 1335 } 1336 _idx = 0; 1337 } 1338 1339 int length() const { 1340 return _length; 1341 } 1342 1343 void next() { 1344 _idx += InstanceKlass::inner_class_next_offset; 1345 } 1346 1347 bool done() const { 1348 return (_idx >= _length); 1349 } 1350 1351 u2 inner_class_info_index() const { 1352 return _inner_classes->at( 1353 _idx + InstanceKlass::inner_class_inner_class_info_offset); 1354 } 1355 1356 void set_inner_class_info_index(u2 index) { 1357 _inner_classes->at_put( 1358 _idx + InstanceKlass::inner_class_inner_class_info_offset, index); 1359 } 1360 1361 u2 outer_class_info_index() const { 1362 return _inner_classes->at( 1363 _idx + InstanceKlass::inner_class_outer_class_info_offset); 1364 } 1365 1366 void set_outer_class_info_index(u2 index) { 1367 _inner_classes->at_put( 1368 _idx + InstanceKlass::inner_class_outer_class_info_offset, index); 1369 } 1370 1371 u2 inner_name_index() const { 1372 return _inner_classes->at( 1373 _idx + InstanceKlass::inner_class_inner_name_offset); 1374 } 1375 1376 void set_inner_name_index(u2 index) { 1377 _inner_classes->at_put( 1378 _idx + InstanceKlass::inner_class_inner_name_offset, index); 1379 } 1380 1381 u2 inner_access_flags() const { 1382 return _inner_classes->at( 1383 _idx + InstanceKlass::inner_class_access_flags_offset); 1384 } 1385 }; 1386 1387 // Iterator over class hierarchy under a particular class. Implements depth-first pre-order traversal. 1388 // Usage: 1389 // for (ClassHierarchyIterator iter(root_klass); !iter.done(); iter.next()) { 1390 // Klass* k = iter.klass(); 1391 // ... 1392 // } 1393 class ClassHierarchyIterator : public StackObj { 1394 private: 1395 InstanceKlass* _root; 1396 Klass* _current; 1397 bool _visit_subclasses; 1398 1399 public: 1400 ClassHierarchyIterator(InstanceKlass* root) : _root(root), _current(root), _visit_subclasses(true) { 1401 assert(_root == _current, "required"); // initial state 1402 } 1403 1404 bool done() { 1405 return (_current == nullptr); 1406 } 1407 1408 // Make a step iterating over the class hierarchy under the root class. 1409 // Skips subclasses if requested. 1410 void next(); 1411 1412 Klass* klass() { 1413 assert(!done(), "sanity"); 1414 return _current; 1415 } 1416 1417 // Skip subclasses of the current class. 1418 void skip_subclasses() { 1419 _visit_subclasses = false; 1420 } 1421 }; 1422 1423 #endif // SHARE_OOPS_INSTANCEKLASS_HPP