Debian Codegraph @

test/jnitest.ml

raw
1 open Jni
2
3 class cls = object
4 method f = print_string "cls.f called"; print_newline()
5 method g x =
6 print_string "cls.g called with "; print_int x; print_newline();
7 x+42
8 end
9
10 let wrap_caml_object() =
11 let camlobj = new cls in
12 (* Wrap caml object into instance of Testcb (see Testcb.java) *)
13 let c = find_class "Testcb" in
14 let i = get_methodID c "<init>" "(Lfr/inria/caml/camljava/Callback;)V" in
15 let o = alloc_object c in
16 call_nonvirtual_void_method o c i [|Obj(wrap_object camlobj)|];
17 o
18
19 let test() =
20 (* Static method invocation *)
21 let c = find_class "Test" in
22 let f = get_static_methodID c "f" "()V" in
23 print_string "Calling Test.f()"; print_newline();
24 call_static_void_method c f [||];
25 let g = get_static_methodID c "g" "(II)I" in
26 print_string "Calling Test.g(12,45)"; print_newline();
27 let r = call_static_int_method c g [|Camlint 12; Camlint 45|] in
28 print_string "Result is: "; print_string (Int32.to_string r);
29 print_newline();
30 (* Static field access *)
31 let a = get_static_fieldID c "a" "I" in
32 print_string "Current value of Test.a is: ";
33 print_string (Int32.to_string (get_static_int_field c a));
34 print_newline();
35 print_string "Setting Test.a to 12"; print_newline();
36 set_static_int_field c a (Int32.of_int 12);
37 print_string "Current value of Test.a is: ";
38 print_string (Int32.to_string (get_static_int_field c a));
39 print_newline();
40 (* Object creation *)
41 print_string "Creating an instance of Test..."; print_newline();
42 let o = alloc_object c in
43 let init = get_methodID c "<init>" "()V" in
44 call_nonvirtual_void_method o c init [||];
45 (* Virtual method invocation *)
46 let h = get_methodID c "h" "()I" in
47 print_string "Calling testinstance.h()"; print_newline();
48 let r = call_int_method o h [||] in
49 print_string "Result is: "; print_string (Int32.to_string r);
50 print_newline();
51 (* Instance field access *)
52 let b = get_fieldID c "b" "I" in
53 print_string "Setting testinstance.b to 45"; print_newline();
54 set_int_field o b (Int32.of_int 45);
55 print_string "Calling testinstance.h()"; print_newline();
56 let r = call_int_method o h [||] in
57 print_string "Result is: "; print_string (Int32.to_string r);
58 print_newline();
59 print_string "Current value of testinstance.b is: ";
60 print_string (Int32.to_string (get_int_field o b));
61 print_newline();
62 print_string "Wrapping Caml object into Java object..."; print_newline();
63 let cb = wrap_caml_object() in
64 let k = get_static_methodID c "k" "(LTestcb;I)I" in
65 print_string "Calling Test.k(<caml object>, 2)"; print_newline();
66 let r = call_static_int_method c k [|Obj cb; Camlint 2|] in
67 print_string "Result is: "; print_string (Int32.to_string r);
68 print_newline()
69
70 let _ =
71 test()