RFR: 8367142: Simplify java mirror handling in JNI methods [v2]

Ioi Lam iklam at openjdk.org
Tue Sep 9 22:09:06 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()`.

Ioi Lam has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains four commits:

 - Merge branch 'master' into 8367142-simplify-java-mirror-handling-in-jni-methods
 - @dholmes-ora comments - remove class_to_verify_considering_redefinition() changes, to be done in separate PR
 - 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=01
  Stats: 150 lines in 17 files changed: 20 ins; 32 del; 98 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 serviceability-dev mailing list