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

Coleen Phillimore coleenp at openjdk.org
Wed Sep 10 11:48:37 UTC 2025


On Tue, 9 Sep 2025 22:09:06 GMT, Ioi Lam <iklam at openjdk.org> wrote:

>> 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);
>> 
>> 
>> Note:
>> 
>> 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()`.
>
> 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

jfr also is a boundary between Java -> Native code. That can have jobjects too, but resolve them before calling javaClasses.

src/hotspot/share/classfile/javaClasses.hpp line 1905:

> 1903: 
> 1904: 
> 1905:   InstanceKlass* klass() const { return vmClasses::klass_at(klass_id); }

Can you fix the indentation?

src/hotspot/share/classfile/javaClasses.inline.hpp line 297:

> 295: inline Klass* java_lang_Class::as_Klass(jobject java_class) {
> 296:   return as_Klass(JNIHandles::resolve_non_null(java_class));
> 297: }

The JNIHandles shouldn't be imported to this file.  The resolve should happen in the caller.

src/hotspot/share/classfile/javaClasses.inline.hpp line 305:

> 303: }
> 304: 
> 305: inline InstanceKlass* java_lang_Class::as_InstanceKlass(jobject java_class) {

Same here.

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

Changes requested by coleenp (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/27158#pullrequestreview-3205765862
PR Review Comment: https://git.openjdk.org/jdk/pull/27158#discussion_r2336482267
PR Review Comment: https://git.openjdk.org/jdk/pull/27158#discussion_r2336483940
PR Review Comment: https://git.openjdk.org/jdk/pull/27158#discussion_r2336484858


More information about the serviceability-dev mailing list