我读取了在热点OpenJDK 7中抛出异常的代码,并跟踪使用它们的类。它们最终会出现在实现JVM_ENTRY(result_type, header)
的类中,例如jvm.cpp
,java.cpp
等。我的问题是JVM_ENTRY
的功能是什么?它是如何工作的?
以下是jvm.cpp
JVM_ENTRY(jobject, JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass))
JVMWrapper("JVM_AllocateNewObject");
JvmtiVMObjectAllocEventCollector oam;
// Receiver is not used
oop curr_mirror = JNIHandles::resolve_non_null(currClass);
oop init_mirror = JNIHandles::resolve_non_null(initClass);
// Cannot instantiate primitive types
if (java_lang_Class::is_primitive(curr_mirror) || java_lang_Class::is_primitive(init_mirror)) {
ResourceMark rm(THREAD);
THROW_0(vmSymbols::java_lang_InvalidClassException());
}
// Arrays not allowed here, must use JVM_AllocateNewArray
if (Klass::cast(java_lang_Class::as_klassOop(curr_mirror))->oop_is_javaArray() ||
Klass::cast(java_lang_Class::as_klassOop(init_mirror))->oop_is_javaArray()) {
ResourceMark rm(THREAD);
THROW_0(vmSymbols::java_lang_InvalidClassException());
}
instanceKlassHandle curr_klass (THREAD, java_lang_Class::as_klassOop(curr_mirror));
instanceKlassHandle init_klass (THREAD, java_lang_Class::as_klassOop(init_mirror));
assert(curr_klass->is_subclass_of(init_klass()), "just checking");
// Interfaces, abstract classes, and java.lang.Class classes cannot be instantiated directly.
curr_klass->check_valid_for_instantiation(false, CHECK_NULL);
// Make sure klass is initialized, since we are about to instantiate one of them.
curr_klass->initialize(CHECK_NULL);
methodHandle m (THREAD,
init_klass->find_method(vmSymbols::object_initializer_name(),
vmSymbols::void_method_signature()));
if (m.is_null()) {
ResourceMark rm(THREAD);
THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
methodOopDesc::name_and_sig_as_C_string(Klass::cast(init_klass()),
vmSymbols::object_initializer_name(),
vmSymbols::void_method_signature()));
}
if (curr_klass == init_klass && !m->is_public()) {
// Calling the constructor for class 'curr_klass'.
// Only allow calls to a public no-arg constructor.
// This path corresponds to creating an Externalizable object.
THROW_0(vmSymbols::java_lang_IllegalAccessException());
}
if (!force_verify_field_access(curr_klass(), init_klass(), m->access_flags(), false)) {
// subclass 'curr_klass' does not have access to no-arg constructor of 'initcb'
THROW_0(vmSymbols::java_lang_IllegalAccessException());
}
Handle obj = curr_klass->allocate_instance_handle(CHECK_NULL);
// Call constructor m. This might call a constructor higher up in the hierachy
JavaCalls::call_default_constructor(thread, m, obj, CHECK_NULL);
return JNIHandles::make_local(obj());
JVM_END
我在JVM_ENTRY
hotspot/src/share/vm/runtime/interfaceSupport.hpp
的定义
#define JVM_ENTRY(result_type, header) \
extern "C" { \
result_type JNICALL header { \
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
ThreadInVMfromNative __tiv(thread); \
debug_only(VMNativeEntryWrapper __vew;) \
VM_ENTRY_BASE(result_type, header, thread)
答案 0 :(得分:6)
JVM_ENTRY是一个预处理器宏,它添加了一些常用于HotSpot JVM API所有函数的样板代码。此API是JDK类库的本机代码与JVM之间的连接层。
JVM_ENTRY宏的作用:
JavaThread
参数获取指向当前JNIEnv*
的指针。_thread_in_native
到_thread_in_vm
的线程状态转换,必要时在安全点阻止..