Skip to content

Commit

Permalink
Add support of Class.isInterface()
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Dec 2, 2024
1 parent f7b87a2 commit 5afe379
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tests/should_check_if_class_is_interface.rs
Original file line number Diff line number Diff line change
@@ -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))
}
18 changes: 18 additions & 0 deletions tests/test_data/ClassIsInterfaceExample.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Binary file not shown.
3 changes: 2 additions & 1 deletion vm/src/execution_engine/system_native_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -73,6 +73,7 @@ static SYSTEM_NATIVE_TABLE: Lazy<HashMap<&'static str, NativeMethod>> = 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),
Expand Down
9 changes: 9 additions & 0 deletions vm/src/system_native/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,12 @@ fn is_array(reference: i32) -> bool {
name.starts_with('[')
})
}

pub(crate) fn is_interface_wrp(args: &[i32]) -> crate::error::Result<Vec<i32>> {
let interface = is_interface(args[0])?;

Ok(vec![interface as i32])
}
fn is_interface(reference: i32) -> crate::error::Result<bool> {
Ok((get_modifiers(reference)? as u16 & INTERFACE) != 0)
}

0 comments on commit 5afe379

Please sign in to comment.