Eiffel2Java Sample

This example shows how to create an instance of the Java Virtual Machine and the creation of an object of type test, a java class reproduced below: (Java Code) class test { test () { my_integer = 10; } public int my_integer; public static int my_static_integer; public void my_method (int arg_int, String arg_string) { my_static_integer = arg_int; my_integer = arg_int; } }

In order to run properly you need to read the library requirement .

Compiling the example

Since the example is using the test.java class, the first step is to compile the java class using the javac command line utility from the JDK. Once it is done, copy the test.class either into $ISE_EIFFEL\examples\Eiffel2Java\EIFGENs\classic\W_code or else into $ISE_EIFFEL\examples\Eiffel2Java\EIFGENs\classic\F_code, depending on your compilation mode (freezing vs. finalizing).

Running the example

Once the example is compiled, you can run it and here is the expected output: $ sample Creating instance of class `test' Value of `my_integer' is 10 Value of `my_static_integer' is 0 Calling `my_method' with (2, "String test") Value of `my_integer' after call to `my_method' is 2

Code description

(Eiffel Code) class EIFFEL_TO_JAVA inherit SHARED_JNI_ENVIRONMENT create make feature -- Creation make local class_test: JAVA_CLASS instance_of_class_test: JAVA_OBJECT fid: POINTER value: INTEGER j_args: JAVA_ARGS do --| Creation of the Java object class_test := jni.find_class ("test") create instance_of_class_test.create_instance (class_test, "()V", Void) --| Access to a public attribute fid := instance_of_class_test.field_id ("my_integer", "I") -- 'fid' contains the id of the field 'my_integer' -- 'value' contains the value of the field referenced -- by 'fid' value := instance_of_class_test.integer_attribute (fid) --| Access to a static attribute using directly the JAVA_CLASS fid := class_test.field_id ("my_static_integer", "I") value := class_test.integer_attribute (fid) --| Access to a static attribute using the attribute 'jclass' fid := instance_of_class_test.jclass.field_id ("my_static_integer", "I") value := instance_of_class_test.jclass.integer_attribute (fid) --| Access to the method 'my_method' -- Get the id of 'my_method' fid := instance_of_class_test.method_id ("my_method", "(ILjava/lang/String;)V") -- Create the set of arguments for 'my_method' create j_args.make(2) j_args.push_int (2) j_args.push_string("String test") -- Create the set of arguments for 'my_method' -- Call to the void method referenced by 'fid' instance_of_class_test.void_method (fid, j_args) end -- make end -- class EIFFEL_TO_JAVA