概述
文章目录
- oop-klass
- OBJECT hierarchy
- metadata hierarchy
- klass hierarchy
- oop
- classDiagram
- description
- klass
- classDiagram
- description
- InstanceKlass
- _misc_flags
- _constants
- _annotations
oop-klass
当C,C++和Delphi等程序被编译成二进制程序后,原来所定义的高级数据结构都不复存在了,当windows/linux等操作系统(宿主机)加载这些二进制程序时,是不会加载这些语言中所定义的高级数据结构的,宿主机压根就不知道原来定了那些数据结构,哪些类,所有的数据结构都被转换为对特定内存段的偏移地址.例如C中的Struct结构体,被编译后不复存在,汇编和机器语言中没有与之对应的数据结构的概念,CPU更不知道何为结构体.C++和Delphi中的类概念被编译后也不复存在,所谓的类最终变成内存首地址.而JVM虚拟机在加载字节码程序时,会记录字节码中所定义的所有类型的原始信息(元数据),JVM知道程序中包含了哪些类,以及每个类中所关联的字段,方法,父类等信息.这是JVM虚拟机与操作系统最大的区别所在
oop:ordinary object pointer,用来描述对象实例信息,一般保存在HEAP
klass:用来描述java类,是虚拟机内部java类型结构的对等体,一般保存在PERM
根据最新的oopsHierarchy.hpp里的说明,整个继承树分为以下三个
OBJECT hierarchy
继承关系代码如下
// OBJECT hierarchy
// This hierarchy is a representation hierarchy, i.e. if A is a superclass
// of B, A's representation is a prefix of B's representation.
typedef class oopDesc*
oop;
typedef class
instanceOopDesc*
instanceOop;
typedef class
arrayOopDesc*
arrayOop;
typedef class
objArrayOopDesc*
objArrayOop;
typedef class
typeArrayOopDesc*
typeArrayOop;
- oopDesc
这个类定义在oop.hpp中,方法有很对,属性却很少,如下所示
class oopDesc {
friend class VMStructs;
friend class JVMCIVMStructs;
private:
volatile markWord _mark;
union _metadata {
Klass*
_klass;
narrowKlass _compressed_klass;
} _metadata;
......
}
_mark属性主要使用标记对象的各种状态,例如锁,分代,线程状态等等.而_metadata可以看出来指向Klass,至于里面的方法很多.上面的narrowKlass是用于指针压缩,相关定义在oopsHierarchy.hpp如下
typedef juint narrowOop; // Offset instead of address for an oop within a java object
// If compressed klass pointers then use narrowKlass.
typedef juint
narrowKlass;
而juint则在globalDefinitions.hpp定义为u4
metadata hierarchy
// The metadata hierarchy is separate from the oop hierarchy
//
class MetaspaceObj
class
ConstMethod;
class
ConstantPoolCache;
class
MethodData;
//
class Metadata
class
Method;
class
ConstantPool;
//
class CHeapObj
class
CompiledICHolder;
klass hierarchy
// The klass hierarchy is separate from the oop hierarchy.
class Klass;
class
InstanceKlass;
class
InstanceMirrorKlass;
class
InstanceClassLoaderKlass;
class
InstanceRefKlass;
class
ArrayKlass;
class
ObjArrayKlass;
class
TypeArrayKlass;
oop
classDiagram
description
这里面各类的作用如下
class | 源码位置 | description |
---|---|---|
typeArrayOopDesc | typeArrayOop.hpp | 用于基本类型的数组 |
klass
classDiagram
为了方便起见,下面的方法里省略了各种print*
description
这里面各类的作用如下
class | 源码位置 | description |
---|---|---|
MetaspaceObj | allocation.hpp | 从名字上看出任何存放在Metaspace的数据都是一个MetaspaceObj |
Metadata | metadata.hpp | 用于存放和Class相关的元数据 |
Klass | klass.cpp | 对应java中的class |
InstanceKlass | instanceKlass.hpp | jvm用来存放java class信息 |
InstanceRefKlass | instanceRefKlass.hpp | 用于处理Reference相关一些非强引用类型 |
InstanceKlass
// See "The Java Virtual Machine Specification" section 2.16.2-5 for a detailed description
// of the class loading & initialization procedure, and the use of the states.
enum ClassState {
allocated,
// allocated (but not yet linked)
loaded,
// loaded and inserted in class hierarchy (but not linked yet)
linked,
// successfully linked/verified (but not initialized yet)
being_initialized,
// currently running class initializer
fully_initialized,
// initialized (successfull final state)
initialization_error
// error happened during initialization
};
// Describes where oops are located in instances of this klass.
class OopMapBlock {
public:
void increment_count(int diff) { _count += diff; }
int offset_span() const { return _count * heapOopSize; }
int end_offset() const {
return offset() + offset_span();
}
bool is_contiguous(int another_offset) const {
return another_offset == end_offset();
}
// sizeof(OopMapBlock) in words.
static const int size_in_words() {
return align_up((int)sizeof(OopMapBlock), wordSize) >>
LogBytesPerWord;
}
static int compare_offset(const OopMapBlock* a, const OopMapBlock* b) {
return a->offset() - b->offset();
}
private:
int
_offset;//PUBLIC
uint _count;//PUBLIC
};
// An InstanceKlass is the VM level representation of a Java class.
// It contains all information needed for at class at execution runtime.
//
InstanceKlass embedded field layout (after declared fields):
//
[EMBEDDED Java vtable
] size in words = vtable_len
//
[EMBEDDED nonstatic oop-map blocks] size in words = nonstatic_oop_map_size
//
The embedded nonstatic oop-map blocks are short pairs (offset, length)
//
indicating where oops are located in instances of this klass.
//
[EMBEDDED implementor of the interface] only exist for interface
//
[EMBEDDED unsafe_anonymous_host klass] only exist for an unsafe anonymous class (JSR 292 enabled)
//
[EMBEDDED fingerprint
] only if should_store_fingerprint()==true
// forward declaration for class -- see below for definition
class InstanceKlass: public Klass {
friend class VMStructs;
friend class JVMCIVMStructs;
friend class ClassFileParser;
friend class CompileReplay;
public:
static const KlassID ID = InstanceKlassID;
protected:
InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id = ID);
public:
InstanceKlass() { assert(DumpSharedSpaces || UseSharedSpaces, "only for CDS"); }
private:
static InstanceKlass* allocate_instance_klass(const ClassFileParser& parser, TRAPS);
protected:
Annotations*
_annotations;//PUBLIC Annotations for this class
// Package this class is defined in
PackageEntry*
_package_entry;
// Array classes holding elements of this class.
ObjArrayKlass* volatile _array_klasses;//PUBLIC
ConstantPool* _constants;//PUBLIC Constant pool for this class.
Array<jushort>* _inner_classes;//PUBLIC
Array<jushort>* _nest_members;//PUBLIC class info index for the class that is a nest member
// Resolved nest-host klass: either true nest-host or self if we are not
// nested, or an error occurred resolving or validating the nominated
// nest-host. Can also be set directly by JDK API's that establish nest
// relationships.
// By always being set it makes nest-member access checks simpler.
InstanceKlass* _nest_host;
Array<jushort>* _permitted_subclasses;//PUBLIC class info index for the class that is a permitted subclass
Array<RecordComponent*>* _record_components;//PUBLIC The contents of the Record attribute.
// the source debug extension for this klass, NULL if not specified.
// Specified as UTF-8 string without terminating zero byte in the classfile,
// it is stored in the instanceklass as a NULL-terminated UTF-8 string
const char*
_source_debug_extension;
// Number of heapOopSize words used by non-static fields in this klass
// (including inherited fields but after header_size()).
int
_nonstatic_field_size;//PUBLIC
int
_static_field_size;
//PUBLIC number words used by static fields (oop and non-oop) in this klass
int
_nonstatic_oop_map_size;//PUBLIC size in words of nonstatic oop map blocks
int
_itable_len;
// length of Java itable (in words)
// The NestHost attribute. The class info index for the class
// that is the nest-host of this class. This data has not been validated.
u2
_nest_host_index;//PUBLIC
u2
_this_class_index;
//PUBLIC constant pool entry
u2
_static_oop_field_count;//PUBLIC number of static oop fields in this klass
u2
_java_fields_count;
// The number of declared Java fields
volatile u2
_idnum_allocated_count;
// JNI/JVMTI: increments with the addition of methods, old ids don't change
bool
_is_marked_dependent;
//PUBLIC used for marking during flushing and deoptimization
// Class states are defined as ClassState (see above).
// Place the _init_state here to utilize the unused 2-byte after
// _idnum_allocated_count.
u1
_init_state;
// state of class
// This can be used to quickly discriminate among the four kinds of
// InstanceKlass. This should be an enum (?)
static const unsigned _kind_other
= 0; // concrete InstanceKlass
static const unsigned _kind_reference
= 1; // InstanceRefKlass
static const unsigned _kind_class_loader = 2; // InstanceClassLoaderKlass
static const unsigned _kind_mirror
= 3; // InstanceMirrorKlass
u1
_reference_type;
// reference type
u1
_kind;
// kind of InstanceKlass
u2 shared_loader_type_bits() const {
return _misc_is_shared_boot_class|_misc_is_shared_platform_class|_misc_is_shared_app_class;
}
u2
_misc_flags;
// There is more space in access_flags for more flags.
Thread*
_init_thread;
// Pointer to current thread doing initialization (to handle recursive initialization)
OopMapCache*
volatile _oop_map_cache;
//PUBLIC OopMapCache for all methods in the klass (allocated lazily)
JNIid*
_jni_ids;
//PUBLIC First JNI identifier for static fields in this class
jmethodID*
volatile _methods_jmethod_ids;
// jmethodIDs corresponding to method_idnum, or NULL if none
nmethodBucket*
volatile _dep_context;
// packed DependencyContext structure
uint64_t
volatile _dep_context_last_cleaned;
nmethod*
_osr_nmethods_head;
//PUBLIC Head of list of on-stack replacement nmethods for this class
Array<Method*>* _methods;//PUBLIC Method array.
Array<Method*>* _default_methods;//PUBLIC Default Method Array, concrete methods inherited from interfaces
// Interfaces (InstanceKlass*s) this class declares locally to implement.
Array<InstanceKlass*>* _local_interfaces;//只被赋值一次
// Interfaces (InstanceKlass*s) this class implements transitively.
Array<InstanceKlass*>* _transitive_interfaces;//只被赋值一次
Array<int>*
_method_ordering;//PUBLIC Int array containing the original order of method in the class file (for JVMTI).
// Int array containing the vtable_indices for default_methods
// offset matches _default_methods offset
Array<int>*
_default_vtable_indices;//PUBLLIC
// Instance and static variable information, starts with 6-tuples of shorts
// [access, name index, sig index, initval index, low_offset, high_offset]
// for all fields, followed by the generic signature data at the end of
// the array. Only fields with generic signature attributes have the generic
// signature data set in the array. The fields array looks like following:
//
// f1: [access, name index, sig index, initial value index, low_offset, high_offset]
// f2: [access, name index, sig index, initial value index, low_offset, high_offset]
//
...
// fn: [access, name index, sig index, initial value index, low_offset, high_offset]
//
[generic signature index]
//
[generic signature index]
//
...
Array<u2>*
_fields;
// embedded Java vtable follows here
// embedded Java itables follows here
// embedded static fields follows here
// embedded nonstatic oop-map blocks follows here
// embedded implementor of this interface follows here
//
The embedded implementor only exists if the current klass is an
//
iterface. The possible values of the implementor fall into following
//
three cases:
//
NULL: no implementor.
//
A Klass* that's not itself: one implementor.
//
Itself: more than one implementors.
// embedded unsafe_anonymous_host klass follows here
//
The embedded host klass only exists in an unsafe anonymous class for
//
dynamic language support (JSR 292 enabled). The host class grants
//
its access privileges to this class also. The host class is either
//
named, or a previously loaded unsafe anonymous class. A non-anonymous class
//
or an anonymous class loaded through normal classloading does not
//
have this embedded field.
//
friend class SystemDictionary;
static bool _disable_method_binary_search;
public:
// The UNREGISTERED class loader type
bool is_shared_unregistered_class() const {
return (_misc_flags & shared_loader_type_bits()) == 0;
}
void clear_shared_class_loader_type() {
_misc_flags &= ~shared_loader_type_bits();
}
void set_shared_class_loader_type(s2 loader_type);
void assign_class_loader_type();
// methods
Method* method_with_idnum(int idnum);
Method* method_with_orig_idnum(int idnum);
Method* method_with_orig_idnum(int idnum, int version);
// method ordering
void copy_method_ordering(const intArray* m, TRAPS);
// default method vtable_indices
Array<int>* create_new_default_vtable_indices(int len, TRAPS);
private:
friend class fieldDescriptor;
FieldInfo* field(int index) const { return FieldInfo::from_field_array(_fields, index); }
public:
int
field_offset
(int index) const { return field(index)->offset(); }
int
field_access_flags(int index) const { return field(index)->access_flags(); }
Symbol* field_name
(int index) const { return field(index)->name(constants()); }
Symbol* field_signature
(int index) const { return field(index)->signature(constants()); }
// Number of Java declared fields
int java_fields_count() const
{ return (int)_java_fields_count; }
Array<u2>* fields() const
{ return _fields; }
void set_fields(Array<u2>* f, u2 java_fields_count) {
guarantee(_fields == NULL || f == NULL, "Just checking");
_fields = f;
_java_fields_count = java_fields_count;
}
// dynamic nest member support
void set_nest_host(InstanceKlass* host, TRAPS);
bool is_record() const { return _record_components != NULL; }
private:
// Called to verify that k is a member of this nest - does not look at k's nest-host
bool has_nest_member(InstanceKlass* k, TRAPS) const;
public:
// Used to construct informative IllegalAccessError messages at a higher level,
// if there was an issue resolving or validating the nest host.
// Returns NULL if there was no error.
const char* nest_host_error(TRAPS);
// Returns nest-host class, resolving and validating it if needed.
// Returns NULL if resolution is not possible from the calling context.
InstanceKlass* nest_host(TRAPS);
// Check if this klass is a nestmate of k - resolves this nest-host and k's
bool has_nestmate_access_to(InstanceKlass* k, TRAPS);
// Called to verify that k is a permitted subclass of this class
bool has_as_permitted_subclass(const InstanceKlass* k) const;
// method override check
bool is_override(const methodHandle& super_method, Handle targetclassloader, Symbol* targetclassname, TRAPS);
// package
PackageEntry* package() const
{ return _package_entry; }
ModuleEntry* module() const;
bool in_unnamed_package() const
{ return (_package_entry == NULL); }
void set_package(ClassLoaderData* loader_data, PackageEntry* pkg_entry, TRAPS);
// If the package for the InstanceKlass is in the boot loader's package entry
// table then sets the classpath_index field so that
// get_system_package() will know to return a non-null value for the
// package's location.
And, so that the package will be added to the list of
// packages returned by get_system_packages().
// For packages whose classes are loaded from the boot loader class path, the
// classpath_index indicates which entry on the boot loader class path.
void set_classpath_index(s2 path_index, TRAPS);
bool is_same_class_package(const Klass* class2) const;
bool is_same_class_package(oop other_class_loader, const Symbol* other_class_name) const;
// find an enclosing class
InstanceKlass* compute_enclosing_class(bool* inner_is_member, TRAPS) const;
// Find InnerClasses attribute and return outer_class_info_index & inner_name_index.
bool find_inner_classes_attr(int* ooff, int* noff, TRAPS) const;
private:
// Check prohibited package ("java/" only loadable by boot or platform loaders)
static void check_prohibited_package(Symbol* class_name,
ClassLoaderData* loader_data,
TRAPS);
public:
// initialization state
bool is_loaded() const
{ return _init_state >= loaded; }
bool is_linked() const
{ return _init_state >= linked; }
bool is_initialized() const
{ return _init_state == fully_initialized; }
bool is_not_initialized() const
{ return _init_state <
being_initialized; }
bool is_being_initialized() const
{ return _init_state == being_initialized; }
bool is_in_error_state() const
{ return _init_state == initialization_error; }
bool is_reentrant_initialization(Thread *thread)
{ return thread == _init_thread; }
ClassState
init_state()
{ return (ClassState)_init_state; }
// is this a sealed class
bool is_sealed() const;
// initialization (virtuals from Klass)
bool should_be_initialized() const;
// means that initialize should be called
void initialize(TRAPS);
void link_class(TRAPS);
bool link_class_or_fail(TRAPS); // returns false on failure
void rewrite_class(TRAPS);
void link_methods(TRAPS);
Method* class_initializer() const;
// set the class to initialized if no static initializer is present
void eager_initialize(Thread *thread);
// reference type
ReferenceType reference_type() const
{ return (ReferenceType)_reference_type; }
void set_reference_type(ReferenceType t) {
assert(t == (u1)t, "overflow");
_reference_type = (u1)t;
}
static ByteSize reference_type_offset() { return in_ByteSize(offset_of(InstanceKlass, _reference_type)); }
// find local field, returns true if found
bool find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
// find field in direct superinterfaces, returns the interface in which the field is defined
Klass* find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
// find field according to JVM spec 5.4.3.2, returns the klass in which the field is defined
Klass* find_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const;
// find instance or static fields according to JVM spec 5.4.3.2, returns the klass in which the field is defined
Klass* find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const;
// find a non-static or static field given its offset within the class.
bool contains_field_offset(int offset);
bool find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
bool find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const;
private:
inline static int quick_search(const Array<Method*>* methods, const Symbol* name);
public:
static void disable_method_binary_search() {
_disable_method_binary_search = true;
}
// find a local method (returns NULL if not found)
Method* find_method(const Symbol* name, const Symbol* signature) const;
static Method* find_method(const Array<Method*>* methods,
const Symbol* name,
const Symbol* signature);
// find a local method, but skip static methods
Method* find_instance_method(const Symbol* name, const Symbol* signature,
PrivateLookupMode private_mode) const;
static Method* find_instance_method(const Array<Method*>* methods,
const Symbol* name,
const Symbol* signature,
PrivateLookupMode private_mode);
// find a local method (returns NULL if not found)
Method* find_local_method(const Symbol* name,
const Symbol* signature,
OverpassLookupMode overpass_mode,
StaticLookupMode static_mode,
PrivateLookupMode private_mode) const;
// find a local method from given methods array (returns NULL if not found)
static Method* find_local_method(const Array<Method*>* methods,
const Symbol* name,
const Symbol* signature,
OverpassLookupMode overpass_mode,
StaticLookupMode static_mode,
PrivateLookupMode private_mode);
// find a local method index in methods or default_methods (returns -1 if not found)
static int find_method_index(const Array<Method*>* methods,
const Symbol* name,
const Symbol* signature,
OverpassLookupMode overpass_mode,
StaticLookupMode static_mode,
PrivateLookupMode private_mode);
// lookup operation (returns NULL if not found)
Method* uncached_lookup_method(const Symbol* name,
const Symbol* signature,
OverpassLookupMode overpass_mode,
PrivateLookupMode private_mode = find_private) const;
// lookup a method in all the interfaces that this class implements
// (returns NULL if not found)
Method* lookup_method_in_all_interfaces(Symbol* name, Symbol* signature, DefaultsLookupMode defaults_mode) const;
// lookup a method in local defaults then in all interfaces
// (returns NULL if not found)
Method* lookup_method_in_ordered_interfaces(Symbol* name, Symbol* signature) const;
// Find method indices by name.
If a method with the specified name is
// found the index to the first method is returned, and 'end' is filled in
// with the index of first non-name-matching method.
If no method is found
// -1 is returned.
int find_method_by_name(const Symbol* name, int* end) const;
static int find_method_by_name(const Array<Method*>* methods,
const Symbol* name, int* end);
// protection domain
oop protection_domain() const;
// signers
objArrayOop signers() const;
// host class
InstanceKlass* unsafe_anonymous_host() const {
InstanceKlass** hk = adr_unsafe_anonymous_host();
if (hk == NULL) {
assert(!is_unsafe_anonymous(), "Unsafe anonymous classes have host klasses");
return NULL;
} else {
assert(*hk != NULL, "host klass should always be set if the address is not null");
assert(is_unsafe_anonymous(), "Only unsafe anonymous classes have host klasses");
return *hk;
}
}
void set_unsafe_anonymous_host(const InstanceKlass* host) {
assert(is_unsafe_anonymous(), "not unsafe anonymous");
const InstanceKlass** addr = (const InstanceKlass **)adr_unsafe_anonymous_host();
assert(addr != NULL, "no reversed space");
if (addr != NULL) {
*addr = host;
}
}
// source debug extension
const char* source_debug_extension() const { return _source_debug_extension; }
void set_source_debug_extension(const char* array, int length);
// nonstatic oop-map blocks
static int nonstatic_oop_map_size(unsigned int oop_map_count) {
return oop_map_count * OopMapBlock::size_in_words();
}
unsigned int nonstatic_oop_map_count() const {
return _nonstatic_oop_map_size / OopMapBlock::size_in_words();
}
// 对于非JVMTI的情况总是NULL
InstanceKlass* previous_versions() const { return NULL; }
InstanceKlass* get_klass_version(int version) {
return NULL;
}
bool supers_have_passed_fingerprint_checks();
static bool should_store_fingerprint(bool is_hidden_or_anonymous);
bool should_store_fingerprint() const { return should_store_fingerprint(is_hidden() || is_unsafe_anonymous()); }
bool has_stored_fingerprint() const;
uint64_t get_stored_fingerprint() const;
void store_fingerprint(uint64_t fingerprint);
private:
void set_kind(unsigned kind) {
_kind = (u1)kind;
}
bool is_kind(unsigned desired) const {
return _kind == (u1)desired;
}
public:
// Other is anything that is not one of the more specialized kinds of InstanceKlass.
bool is_other_instance_klass() const
{ return is_kind(_kind_other); }
bool is_reference_instance_klass() const
{ return is_kind(_kind_reference); }
bool is_mirror_instance_klass() const
{ return is_kind(_kind_mirror); }
bool is_class_loader_instance_klass() const { return is_kind(_kind_class_loader); }
static void purge_previous_versions(InstanceKlass* ik) { return; };
static bool has_previous_versions_and_reset() { return false; }
void set_cached_class_file(JvmtiCachedClassFileData *data) {
assert(data == NULL, "unexpected call with JVMTI disabled");
}
JvmtiCachedClassFileData * get_cached_class_file() { return (JvmtiCachedClassFileData *)NULL; }
// for adding methods, ConstMethod::UNSET_IDNUM means no more ids available
inline u2 next_method_idnum();
void set_initial_method_idnum(u2 value)
{ _idnum_allocated_count = value; }
u2 enclosing_method_data(int offset) const;
u2 enclosing_method_class_index() const {
return enclosing_method_data(enclosing_method_class_index_offset);
}
u2 enclosing_method_method_index() {
return enclosing_method_data(enclosing_method_method_index_offset);
}
void set_enclosing_method_indices(u2 class_index,
u2 method_index);
// jmethodID support
jmethodID get_jmethod_id(const methodHandle& method_h);
jmethodID get_jmethod_id_fetch_or_update(size_t idnum,
jmethodID new_id, jmethodID* new_jmeths,
jmethodID* to_dealloc_id_p,
jmethodID** to_dealloc_jmeths_p);
static void get_jmethod_id_length_value(jmethodID* cache, size_t idnum,
size_t *length_p, jmethodID* id_p);
void ensure_space_for_methodids(int start_offset = 0);
jmethodID jmethod_id_or_null(Method* method);
instanceOop allocate_instance(TRAPS);// allocation
static instanceOop allocate_instance(oop cls, TRAPS);
// additional member function to return a handle
instanceHandle allocate_instance_handle(TRAPS);
objArrayOop allocate_objArray(int n, int length, TRAPS);
// Helper function
static instanceOop register_finalizer(instanceOop i, TRAPS);
// Check whether reflection/jni/jvm code is allowed to instantiate this class;
// if not, throw either an Error or an Exception.
virtual void check_valid_for_instantiation(bool throwError, TRAPS);
void call_class_initializer(TRAPS);// initialization
void set_initialization_state_and_notify(ClassState state, TRAPS);
void mask_for(const methodHandle& method, int bci, InterpreterOopMap* entry);
JNIid* jni_id_for(int offset);
// maintenance of deoptimization dependencies
inline DependencyContext dependencies();
int
mark_dependent_nmethods(KlassDepChange& changes);
void add_dependent_nmethod(nmethod* nm);
void remove_dependent_nmethod(nmethod* nm);
void clean_dependency_context();
// On-stack replacement support
void add_osr_nmethod(nmethod* n);
bool remove_osr_nmethod(nmethod* n);
int mark_osr_nmethods(const Method* m);
nmethod* lookup_osr_nmethod(const Method* m, int bci, int level, bool match_level) const;
// support for stub routines
static ByteSize init_state_offset()
{ return in_ByteSize(offset_of(InstanceKlass, _init_state)); }
JFR_ONLY(DEFINE_KLASS_TRACE_ID_OFFSET;)
static ByteSize init_thread_offset() { return in_ByteSize(offset_of(InstanceKlass, _init_thread)); }
// subclass/subinterface checks
bool implements_interface(Klass* k) const;
bool is_same_or_direct_interface(Klass* k) const;
// Access to the implementor of an interface.
Klass* implementor() const;
void set_implementor(Klass* k);
int
nof_implementors() const;
void add_implementor(Klass* k);
// k is a new class that implements this interface
void init_implementor();
// initialize
// link this class into the implementors list of every interface it implements
void process_interfaces(Thread *thread);
// virtual operations from Klass
GrowableArray<Klass*>* compute_secondary_supers(int num_extra_slots,
Array<InstanceKlass*>* transitive_interfaces);
bool can_be_primary_super_slow() const;
int oop_size(oop obj)
const
{ return size_helper(); }
// slow because it's a virtual call and used for verifying the layout_helper.
// Using the layout_helper bits, we can call is_instance_klass without a virtual call.
DEBUG_ONLY(bool is_instance_klass_slow() const
{ return true; })
// Iterators
void do_local_static_fields(FieldClosure* cl);
void do_nonstatic_fields(FieldClosure* cl); // including inherited fields
void do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle, TRAPS);
void methods_do(void f(Method* method));
void array_klasses_do(void f(Klass* k));
void array_klasses_do(void f(Klass* k, TRAPS), TRAPS);
static InstanceKlass* cast(Klass* k) {
return const_cast<InstanceKlass*>(cast(const_cast<const Klass*>(k)));
}
static const InstanceKlass* cast(const Klass* k) {
assert(k != NULL, "k should not be null");
assert(k->is_instance_klass(), "cast to InstanceKlass");
return static_cast<const InstanceKlass*>(k);
}
virtual InstanceKlass* java_super() const {
return (super() == NULL) ? NULL : cast(super());
}
// Sizing (in words)
static int header_size()
{ return sizeof(InstanceKlass)/wordSize; }
static int size(int vtable_length, int itable_length,
int nonstatic_oop_map_size,
bool is_interface, bool is_unsafe_anonymous, bool has_stored_fingerprint) {
return align_metadata_size(header_size() +
vtable_length +
itable_length +
nonstatic_oop_map_size +
(is_interface ? (int)sizeof(Klass*)/wordSize : 0) +
(is_unsafe_anonymous ? (int)sizeof(Klass*)/wordSize : 0) +
(has_stored_fingerprint ? (int)sizeof(uint64_t*)/wordSize : 0));
}
int size() const{
return size(vtable_length(),itable_length(),nonstatic_oop_map_size(),
is_interface(),is_unsafe_anonymous(), has_stored_fingerprint());
}
intptr_t* start_of_itable()
const { return (intptr_t*)start_of_vtable() + vtable_length(); }
intptr_t* end_of_itable()
const { return start_of_itable() + itable_length(); }
int
itable_offset_in_words() const { return start_of_itable() - (intptr_t*)this; }
oop static_field_base_raw() { return java_mirror(); }
OopMapBlock* start_of_nonstatic_oop_maps() const {
return (OopMapBlock*)(start_of_itable() + itable_length());
}
Klass** end_of_nonstatic_oop_maps() const {
return (Klass**)(start_of_nonstatic_oop_maps() +
nonstatic_oop_map_count());
}
Klass* volatile* adr_implementor() const {
if (is_interface()) {
return (Klass* volatile*)end_of_nonstatic_oop_maps();
} else {
return NULL;
}
};
InstanceKlass** adr_unsafe_anonymous_host() const {
if (is_unsafe_anonymous()) {
InstanceKlass** adr_impl = (InstanceKlass**)adr_implementor();
if (adr_impl != NULL) {
return adr_impl + 1;
} else {
return (InstanceKlass **)end_of_nonstatic_oop_maps();
}
} else {
return NULL;
}
}
address adr_fingerprint() const {
if (has_stored_fingerprint()) {
InstanceKlass** adr_host = adr_unsafe_anonymous_host();
if (adr_host != NULL) {
return (address)(adr_host + 1);
}
Klass* volatile* adr_impl = adr_implementor();
if (adr_impl != NULL) {
return (address)(adr_impl + 1);
}
return (address)end_of_nonstatic_oop_maps();
} else {
return NULL;
}
}
// Use this to return the size of an instance in heap words:
int size_helper() const {
return layout_helper_to_size_helper(layout_helper());
}
// This bit is initialized in classFileParser.cpp.
// It is false under any of the following conditions:
//
- the class is abstract (including any interface)
//
- the class has a finalizer (if !RegisterFinalizersAtInit)
//
- the class size is larger than FastAllocateSizeLimit
//
- the class is java/lang/Class, which cannot be allocated directly
bool can_be_fastpath_allocated() const {
return !layout_helper_needs_slow_path(layout_helper());
}
// Java itable
klassItable itable() const;
// return klassItable wrapper
Method* method_at_itable(Klass* holder, int index, TRAPS);
void clean_weak_instanceklass_links();
private:
void clean_implementors_list();
void clean_method_data();
public:
// Explicit metaspace deallocation of fields
// For RedefineClasses and class file parsing errors, we need to deallocate
// instanceKlasses and the metadata they point to.
void deallocate_contents(ClassLoaderData* loader_data);
static void deallocate_methods(ClassLoaderData* loader_data,
Array<Method*>* methods);
void static deallocate_interfaces(ClassLoaderData* loader_data,
const Klass* super_klass,
Array<InstanceKlass*>* local_interfaces,
Array<InstanceKlass*>* transitive_interfaces);
void static deallocate_record_components(ClassLoaderData* loader_data,
Array<RecordComponent*>* record_component);
// callbacks for actions during class unloading
static void unload_class(InstanceKlass* ik);
virtual void release_C_heap_structures();
// Naming
const char* signature_name() const;
// Oop fields (and metadata) iterators
//
// The InstanceKlass iterators also visits the Object's klass.
// Forward iteration
public:
// Iterate over all oop fields in the oop maps.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_oop_maps(oop obj, OopClosureType* closure);
// Iterate over all oop fields and metadata.
template <typename T, class OopClosureType>
inline void oop_oop_iterate(oop obj, OopClosureType* closure);
// Iterate over all oop fields in one oop map.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_oop_map(OopMapBlock* map, oop obj, OopClosureType* closure);
// Reverse iteration
// Iterate over all oop fields and metadata.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_reverse(oop obj, OopClosureType* closure);
private:
// Iterate over all oop fields in the oop maps.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_oop_maps_reverse(oop obj, OopClosureType* closure);
// Iterate over all oop fields in one oop map.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_oop_map_reverse(OopMapBlock* map, oop obj, OopClosureType* closure);
// Bounded range iteration
public:
// Iterate over all oop fields in the oop maps.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_oop_maps_bounded(oop obj, OopClosureType* closure, MemRegion mr);
// Iterate over all oop fields and metadata.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_bounded(oop obj, OopClosureType* closure, MemRegion mr);
private:
// Iterate over all oop fields in one oop map.
template <typename T, class OopClosureType>
inline void oop_oop_iterate_oop_map_bounded(OopMapBlock* map, oop obj, OopClosureType* closure, MemRegion mr);
public:
u2 idnum_allocated_count() const
{ return _idnum_allocated_count; }
private:
// initialization state
void set_init_state(ClassState state);
void set_init_thread(Thread *thread)
{ _init_thread = thread; }
// The RedefineClasses() API can cause new method idnums to be needed
// which will cause the caches to grow. Safety requires different
// cache management logic if the caches can grow instead of just
// going from NULL to non-NULL.
bool idnum_can_increment() const
{ return has_been_redefined(); }
inline jmethodID* methods_jmethod_ids_acquire() const;
inline void release_set_methods_jmethod_ids(jmethodID* jmeths);
// Lock during initialization
public:
// Lock for (1) initialization; (2) access to the ConstantPool of this class.
// Must be one per class and it has to be a VM internal object so java code
// cannot lock it (like the mirror).
// It has to be an object not a Mutex because it's held through java calls.
oop init_lock() const;
private:
void fence_and_clear_init_lock();
bool link_class_impl
(TRAPS);
bool verify_code
(TRAPS);
void initialize_impl
(TRAPS);
void initialize_super_interfaces
(TRAPS);
void eager_initialize_impl
();
/* jni_id_for_impl for jfieldID only */
JNIid* jni_id_for_impl
(int offset);
// Returns the array class for the n'th dimension
Klass* array_klass_impl(bool or_null, int n, TRAPS);
// Returns the array class with this class as element type
Klass* array_klass_impl(bool or_null, TRAPS);
// find a local method (returns NULL if not found)
Method* find_method_impl(const Symbol* name,
const Symbol* signature,
OverpassLookupMode overpass_mode,
StaticLookupMode static_mode,
PrivateLookupMode private_mode) const;
static Method* find_method_impl(const Array<Method*>* methods,
const Symbol* name,
const Symbol* signature,
OverpassLookupMode overpass_mode,
StaticLookupMode static_mode,
PrivateLookupMode private_mode);
// Free CHeap allocated fields.
void release_C_heap_structures_internal();
public:
// CDS support - remove and restore oops from metadata. Oops are not shared.
virtual void remove_unshareable_info();
virtual void remove_java_mirror();
void restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, PackageEntry* pkg_entry, TRAPS);
// jvm support
jint compute_modifier_flags(TRAPS) const;
public:
// JVMTI support
jint jvmti_class_status() const;
virtual void metaspace_pointers_do(MetaspaceClosure* iter);
public:
// Printing
#ifndef PRODUCT
void print_on(outputStream* st) const;
#endif
void print_value_on(outputStream* st) const;
void oop_print_value_on(oop obj, outputStream* st);
#ifndef PRODUCT
void oop_print_on
(oop obj, outputStream* st);
void print_dependent_nmethods(bool verbose = false);
bool is_dependent_nmethod(nmethod* nm);
bool verify_itable_index(int index);
#endif
const char* internal_name() const;
// Verification
void verify_on(outputStream* st);
void oop_verify_on(oop obj, outputStream* st);
// Logging
void print_class_load_logging(ClassLoaderData* loader_data,
const char* module_name,
const ClassFileStream* cfs) const;
};
// for adding methods
// UNSET_IDNUM return means no more ids available
inline u2 InstanceKlass::next_method_idnum() {
if (_idnum_allocated_count == ConstMethod::MAX_IDNUM) {
return ConstMethod::UNSET_IDNUM; // no more ids available
} else {
return _idnum_allocated_count++;
}
}
_misc_flags
InstanceKlass里有个位图字段,一个顶十六个,那就是 _misc_flags,它的16位的含义可以参见下面的枚举
enum {
_misc_rewritten
= 1 << 0,
// methods rewritten.
_misc_has_nonstatic_fields
= 1 << 1,
// for sizing with UseCompressedOops
_misc_should_verify_class
= 1 << 2,
// allow caching of preverification
_misc_is_unsafe_anonymous
= 1 << 3,
// has embedded _unsafe_anonymous_host field
_misc_is_contended
= 1 << 4,
// marked with contended annotation
_misc_has_nonstatic_concrete_methods
= 1 << 5,
// class/superclass/implemented interfaces has non-static, concrete methods
_misc_declares_nonstatic_concrete_methods = 1 << 6,
// directly declares non-static, concrete methods
_misc_has_been_redefined
= 1 << 7,
// class has been redefined
_misc_has_passed_fingerprint_check
= 1 << 8,
// when this class was loaded, the fingerprint computed from its
// code source was found to be matching the value recorded by AOT.
_misc_is_scratch_class
= 1 << 9,
// class is the redefined scratch class
_misc_is_shared_boot_class
= 1 << 10, // defining class loader is boot class loader
_misc_is_shared_platform_class
= 1 << 11, // defining class loader is platform class loader
_misc_is_shared_app_class
= 1 << 12, // defining class loader is app class loader
_misc_has_resolved_methods
= 1 << 13, // resolved methods table entries added for this class
_misc_is_being_redefined
= 1 << 14, // used for locking redefinition
_misc_has_contended_annotations
= 1 << 15
// has @Contended annotation
};
所以下面众多方法就是通过_misc_flags来读写
mask | 读方法 | 写方法 | 说明 |
---|---|---|---|
_misc_rewritten | is_rewritten() | set_rewritten() | |
_misc_has_nonstatic_fields | has_nonstatic_fields() | set_has_nonstatic_fields(bool b) | |
_misc_should_verify_class | should_verify_class | set_should_verify_class | |
_misc_is_unsafe_anonymous | is_unsafe_anonymous() | set_is_unsafe_anonymous(bool value) | |
_misc_is_contended | is_contended() | set_is_contended(bool value) | |
_misc_has_nonstatic_concrete_methods | has_nonstatic_concrete_methods() | set_has_nonstatic_concrete_methods(bool b) | |
_misc_declares_nonstatic_concrete_methods | declares_nonstatic_concrete_methods() | set_declares_nonstatic_concrete_methods(bool b) | |
_misc_has_been_redefined | has_been_redefined() | set_has_been_redefined() | |
_misc_has_passed_fingerprint_check | has_passed_fingerprint_check() | set_has_passed_fingerprint_check(bool b) | |
_misc_is_scratch_class | is_scratch_class() | set_is_scratch_class() | |
_misc_is_shared_boot_class | is_shared_boot_class() | - | |
_misc_is_shared_platform_class | is_shared_platform_class() | - | |
_misc_is_shared_app_clas | is_shared_app_clas() | - | |
_misc_has_resolved_methods | has_resolved_methods() | set_has_resolved_methods() | |
_misc_is_being_redefined | is_being_redefined() | set_is_being_redefined(bool value) | JVMTI |
_misc_has_contended_annotations | has_contended_annotations() | set_has_contended_annotations(bool value) |
_constants
_constants是一个重要属性,很多操作都是委托给_constants的同名方法,包括以下这些
- bool on_stack() const
The constant pool is on stack if any of the methods are executing or referenced by handles. - void set_generic_signature_index(u2 sig_index)
- u2 generic_signature_index() const
- Symbol* generic_signature() const
- void set_major_version(u2 major_version)
- u2 major_version() const
- void set_minor_version(u2 minor_version)
- u2 minor_version() const
- Symbol* source_file_name() const
- u2 source_file_name_index() const
- void set_source_file_name_index(u2 sourcefile_index)
_annotations
类似的,有些方法委托给了_annotations同名方法,需要注意的是由于_annotations可能为NULL,所以需要先检查了再调用
- AnnotationArray* class_annotations() const
- Array<AnnotationArray*>* fields_annotations() const
- AnnotationArray* class_type_annotations() const
- Array<AnnotationArray*>* fields_type_annotations() const
最后
以上就是坚定小笼包为你收集整理的OpenJDK源码阅读-Oop&Klassoop-klassoopklass的全部内容,希望文章能够帮你解决OpenJDK源码阅读-Oop&Klassoop-klassoopklass所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复