/tutorial/external/Java/example4.e
Specman e | 114 lines | 82 code | 18 blank | 14 comment | 1 complexity | 72c9b25b9810d201de48f395d7a43e17 MD5 | raw file
1class EXAMPLE4 2 -- Compilation: 3 -- compile_to_jvm example4 4 -- javac BSimple.java 5 -- Execution: 6 -- java example4 7 -- This example demonstrates how to catch exceptions in Eiffel that were 8 -- thrown in an external Java object. 9 10creation {ANY} 11 make 12 13feature {ANY} 14 make is 15 local 16 a1: POINTER; b: BOOLEAN; i: INTEGER; d: DOUBLE 17 do 18 -- create new BSimple instance 19 a1 := new_BSimple -- get the instance variable theInteger 20 i := get_theInteger(checkcast_BSimple(a1)) 21 io.put_string("value of theInteger: " + i.to_string + "%N") 22 -- call proc with argument 13 23 call_proc(a1, 13) 24 -- get the instance variable theInteger 25 i := get_theInteger(checkcast_BSimple(a1)) 26 io.put_string("value of theInteger after calling proc: " + i.to_string + "%N") 27 -- call proc again with argument -11 28 call_proc(a1, -11) 29 -- get the instance variable theInteger 30 i := get_theInteger(checkcast_BSimple(a1)) 31 io.put_string("value of theInteger after calling proc: " + i.to_string + "%N") 32 end 33 34feature {ANY} 35 call_proc (a1: POINTER; i: INTEGER) is 36 local 37 e: POINTER; javaString: POINTER; javaStringBytes: POINTER; string_length: INTEGER; s: STRING 38 flag: BOOLEAN 39 do 40 if flag = False then 41 -- call proc with argument i if not in retry 42 proc(checkcast_BSimple(a1), i) 43 end 44 rescue 45 -- get the exception and print out its message 46 e := get_exception 47 javaString := getMessage(checkcast_java_lang_Exception(e)) 48 string_length := length(checkcast_java_lang_String(javaString)) 49 create s.make_filled('%U', string_length) 50 javaStringBytes := getBytes(checkcast_java_lang_String(javaString)) 51 arraycopy(javaStringBytes, 0, s.to_external, 0, string_length) 52 io.put_string("Exception was thrown: " + s + "%N") 53 flag := True 54 retry 55 end 56 57feature {ANY} -- externals 58 new_BSimple: POINTER is 59 external "Java class BSimple new ()" 60 end 61 62 checkcast_BSimple (p: POINTER): POINTER is 63 external "Java class BSimple checkcast" 64 end 65 66 get_theStaticInteger: INTEGER is 67 external "Java class BSimple get field static int theStaticInteger" 68 end 69 70 set_theStaticInteger (a: INTEGER) is 71 external "Java class BSimple set field static int theStaticInteger" 72 end 73 74 get_theInteger (p: POINTER): INTEGER is 75 external "Java class BSimple get field int theInteger" 76 end 77 78 set_theInteger (p: POINTER; a: INTEGER) is 79 external "Java class BSimple set field int theInteger" 80 end 81 82 proc (p: POINTER; a: INTEGER) is 83 external "Java class BSimple method void proc(int)" 84 end 85 86 get_exception: POINTER is 87 external "Java exception java.lang.Exception get" 88 end 89 90 getMessage (p: POINTER): POINTER is 91 external "Java class java.lang.Exception method java.lang.String getMessage()" 92 end 93 94 checkcast_java_lang_Exception (p: POINTER): POINTER is 95 external "Java class java.lang.Exception checkcast" 96 end 97 98 checkcast_java_lang_String (p: POINTER): POINTER is 99 external "Java class java.lang.String checkcast" 100 end 101 102 length (p: POINTER): INTEGER is 103 external "Java class java.lang.String method int length()" 104 end 105 106 getBytes (p: POINTER): POINTER is 107 external "Java class java.lang.String method byte[] getBytes()" 108 end 109 110 arraycopy (a1: POINTER; a2: INTEGER; a3: POINTER; a4: INTEGER; a5: INTEGER) is 111 external "Java class java.lang.System method static void arraycopy(java.lang.Object,int,java.lang.Object,int,int)" 112 end 113 114end -- class EXAMPLE4