Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of Class.isInterface() #122

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading