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