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