From 5afe379b0978a3bc40a31cf51c8a5ebf3ed780d5 Mon Sep 17 00:00:00 2001 From: Igor Rudenko Date: Mon, 2 Dec 2024 19:07:15 +0200 Subject: [PATCH] Add support of Class.isInterface() --- tests/should_check_if_class_is_interface.rs | 10 ++++++++++ tests/test_data/ClassIsInterfaceExample.java | 18 ++++++++++++++++++ .../ClassIsInterfaceExample.class | Bin 0 -> 640 bytes .../execution_engine/system_native_table.rs | 3 ++- vm/src/system_native/class.rs | 9 +++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/should_check_if_class_is_interface.rs create mode 100644 tests/test_data/ClassIsInterfaceExample.java create mode 100644 tests/test_data/samples/reflection/trivial/classisinterface/ClassIsInterfaceExample.class diff --git a/tests/should_check_if_class_is_interface.rs b/tests/should_check_if_class_is_interface.rs new file mode 100644 index 00000000..af5c7a4a --- /dev/null +++ b/tests/should_check_if_class_is_interface.rs @@ -0,0 +1,10 @@ +mod utils; +use utils::get_int; +use vm::vm::VM; + +#[test] +fn should_check_if_class_is_interface() { + let last_frame_value = + VM::run("samples.reflection.trivial.classisinterface.ClassIsInterfaceExample").unwrap(); + assert_eq!(1, get_int(last_frame_value)) +} diff --git a/tests/test_data/ClassIsInterfaceExample.java b/tests/test_data/ClassIsInterfaceExample.java new file mode 100644 index 00000000..4c9be684 --- /dev/null +++ b/tests/test_data/ClassIsInterfaceExample.java @@ -0,0 +1,18 @@ +package samples.reflection.trivial.classisinterface; + +import java.lang.annotation.ElementType; +import java.util.AbstractMap; +import java.util.HashMap; + +public class ClassIsInterfaceExample { + public static void main(String[] args) { + boolean runnableIsInterface = Runnable.class.isInterface(); + boolean hashMapIsInterface = HashMap.class.isInterface(); + boolean abstractMapIsInterface = AbstractMap.class.isInterface(); + boolean elementTypeIsInterface = ElementType.class.isInterface(); + + int result = runnableIsInterface && !hashMapIsInterface && !abstractMapIsInterface && !elementTypeIsInterface + ? 1 + : 0; + } +} diff --git a/tests/test_data/samples/reflection/trivial/classisinterface/ClassIsInterfaceExample.class b/tests/test_data/samples/reflection/trivial/classisinterface/ClassIsInterfaceExample.class new file mode 100644 index 0000000000000000000000000000000000000000..500506f8b96674e2f9cd802e13b6b8d04e77883b GIT binary patch literal 640 zcmZ{hK~EDw7>3`k-FE19q3uFJq*e3;9N6fAi$Ej|L=x1*5)N{jt^*Ekr_Jt^z{USz z|A1#MkZ|A!_@mTsHX(b_IlSMu^S%4dGnrq%?;im4u0+PM&alsGRp{cogEsh(lZ&W zJnFMRlhDeI6RVTxoy@+zmt%pD&@Q(32ANG|Z1d3qVYP^s#*D1AdSs&gL=Ba($Jb*e zut?}-ayU*@7NzPe;cGaLP4%UgNfbLGo#{uMC`UXj`1Y^zp9x-X^o3~~N7|?lli@(6 z$L_K`7)osji)*Jx1x}w2Fz0X9PYAPpE8}mBocjg3g!zA134VVxNn>@OU25k)Ae-(C zyg-}Z%B>Q}y%y^de|5PM*UH8n=!P4@6IM3{1Zr5uQ|_JhJJ%}L*P*yYZ98nR?u0GY z-Ef|D%Uk(@h0YYs?i4}Wn?iU$;gz>4F1M8DzQhdoHO}!7ppKAr8FSb`1Dj~#6qL literal 0 HcmV?d00001 diff --git a/vm/src/execution_engine/system_native_table.rs b/vm/src/execution_engine/system_native_table.rs index 82840013..11f73c14 100644 --- a/vm/src/execution_engine/system_native_table.rs +++ b/vm/src/execution_engine/system_native_table.rs @@ -3,7 +3,7 @@ use crate::execution_engine::system_native_table::NativeMethod::{Basic, WithStac use crate::helper::i64_to_vec; use crate::stack::stack_frame::StackFrames; use crate::system_native::class::{ - get_modifiers_wrp, get_primitive_class_wrp, is_array_wrp, is_primitive_wrp, + get_modifiers_wrp, get_primitive_class_wrp, is_array_wrp, is_interface_wrp, is_primitive_wrp, }; use crate::system_native::file_descriptor::file_descriptor_close0_wrp; use crate::system_native::file_output_stream::{ @@ -73,6 +73,7 @@ static SYSTEM_NATIVE_TABLE: Lazy> = Lazy::ne ); table.insert("java/lang/Class:isPrimitive:()Z", Basic(is_primitive_wrp)); table.insert("java/lang/Class:isArray:()Z", Basic(is_array_wrp)); + table.insert("java/lang/Class:isInterface:()Z", Basic(is_interface_wrp)); table.insert( "jdk/internal/misc/Unsafe:registerNatives:()V", Basic(void_stub as BasicNativeMethod), diff --git a/vm/src/system_native/class.rs b/vm/src/system_native/class.rs index 7a86cd14..fec8744e 100644 --- a/vm/src/system_native/class.rs +++ b/vm/src/system_native/class.rs @@ -105,3 +105,12 @@ fn is_array(reference: i32) -> bool { name.starts_with('[') }) } + +pub(crate) fn is_interface_wrp(args: &[i32]) -> crate::error::Result> { + let interface = is_interface(args[0])?; + + Ok(vec![interface as i32]) +} +fn is_interface(reference: i32) -> crate::error::Result { + Ok((get_modifiers(reference)? as u16 & INTERFACE) != 0) +}