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