RFR: 8367142: Simplify java mirror handling in JNI methods

Ioi Lam iklam at openjdk.org
Tue Sep 9 05:27:22 UTC 2025


The purpose of this PR is to simplify JNI code and also to avoid unnecessary `InstanceKlass::cast()` calls.

This PR is intended to be a strict clean-up that preserves existing behaviors.

The following helper functions are added to simplify boilerplate code in JNI methods.


static Klass* java_lang_Class::as_Klass(jobject java_class);
static InstanceKlass* java_lang_Class::as_InstanceKlass(oop java_class);
static InstanceKlass* java_lang_Class::as_InstanceKlass(jobject java_class);

Klass* get_klass_considering_redefinition(jclass cls, JavaThread *thread);
InstanceKlass* get_instance_klass_considering_redefinition(jclass cls, JavaThread *thread);


Notes:

[1] Before this PR, we have both patterns:


java_lang_Class::as_Klass(JNIHandles::resolve(cls));
java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));


If `cls` is null, we would get an assert in both cases (`as_Klass()` requires a non-null input). Therefore, I am using `resolve_non_null()` in the `jobject` versions of `as_Klass()`.

[2] I refactored `JvmtiThreadState::class_to_verify_considering_redefinition()` so that the caller of this funcation can avoid using `InstanceKlass::cast()`. This is possible because we ONLY store `InstanceKlass*` in `JvmtiThreadState::set_class_being_redefined()`

I also removed a few cases of unnecessary `InstanceKlass::cast()`.

-------------

Commit messages:
 - more fixes
 - tmp: Clean up java mirror handling in JNI methods

Changes: https://git.openjdk.org/jdk/pull/27158/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=27158&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8367142
  Stats: 278 lines in 19 files changed: 46 ins; 65 del; 167 mod
  Patch: https://git.openjdk.org/jdk/pull/27158.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/27158/head:pull/27158

PR: https://git.openjdk.org/jdk/pull/27158


More information about the hotspot-dev mailing list