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