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