/tutorial/external/Java/example4.e

http://github.com/tybor/Liberty · Specman e · 114 lines · 82 code · 18 blank · 14 comment · 1 complexity · 72c9b25b9810d201de48f395d7a43e17 MD5 · raw file

  1. class 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. creation {ANY}
  10. make
  11. feature {ANY}
  12. make is
  13. local
  14. a1: POINTER; b: BOOLEAN; i: INTEGER; d: DOUBLE
  15. do
  16. -- create new BSimple instance
  17. a1 := new_BSimple -- get the instance variable theInteger
  18. i := get_theInteger(checkcast_BSimple(a1))
  19. io.put_string("value of theInteger: " + i.to_string + "%N")
  20. -- call proc with argument 13
  21. call_proc(a1, 13)
  22. -- get the instance variable theInteger
  23. i := get_theInteger(checkcast_BSimple(a1))
  24. io.put_string("value of theInteger after calling proc: " + i.to_string + "%N")
  25. -- call proc again with argument -11
  26. call_proc(a1, -11)
  27. -- get the instance variable theInteger
  28. i := get_theInteger(checkcast_BSimple(a1))
  29. io.put_string("value of theInteger after calling proc: " + i.to_string + "%N")
  30. end
  31. feature {ANY}
  32. call_proc (a1: POINTER; i: INTEGER) is
  33. local
  34. e: POINTER; javaString: POINTER; javaStringBytes: POINTER; string_length: INTEGER; s: STRING
  35. flag: BOOLEAN
  36. do
  37. if flag = False then
  38. -- call proc with argument i if not in retry
  39. proc(checkcast_BSimple(a1), i)
  40. end
  41. rescue
  42. -- get the exception and print out its message
  43. e := get_exception
  44. javaString := getMessage(checkcast_java_lang_Exception(e))
  45. string_length := length(checkcast_java_lang_String(javaString))
  46. create s.make_filled('%U', string_length)
  47. javaStringBytes := getBytes(checkcast_java_lang_String(javaString))
  48. arraycopy(javaStringBytes, 0, s.to_external, 0, string_length)
  49. io.put_string("Exception was thrown: " + s + "%N")
  50. flag := True
  51. retry
  52. end
  53. feature {ANY} -- externals
  54. new_BSimple: POINTER is
  55. external "Java class BSimple new ()"
  56. end
  57. checkcast_BSimple (p: POINTER): POINTER is
  58. external "Java class BSimple checkcast"
  59. end
  60. get_theStaticInteger: INTEGER is
  61. external "Java class BSimple get field static int theStaticInteger"
  62. end
  63. set_theStaticInteger (a: INTEGER) is
  64. external "Java class BSimple set field static int theStaticInteger"
  65. end
  66. get_theInteger (p: POINTER): INTEGER is
  67. external "Java class BSimple get field int theInteger"
  68. end
  69. set_theInteger (p: POINTER; a: INTEGER) is
  70. external "Java class BSimple set field int theInteger"
  71. end
  72. proc (p: POINTER; a: INTEGER) is
  73. external "Java class BSimple method void proc(int)"
  74. end
  75. get_exception: POINTER is
  76. external "Java exception java.lang.Exception get"
  77. end
  78. getMessage (p: POINTER): POINTER is
  79. external "Java class java.lang.Exception method java.lang.String getMessage()"
  80. end
  81. checkcast_java_lang_Exception (p: POINTER): POINTER is
  82. external "Java class java.lang.Exception checkcast"
  83. end
  84. checkcast_java_lang_String (p: POINTER): POINTER is
  85. external "Java class java.lang.String checkcast"
  86. end
  87. length (p: POINTER): INTEGER is
  88. external "Java class java.lang.String method int length()"
  89. end
  90. getBytes (p: POINTER): POINTER is
  91. external "Java class java.lang.String method byte[] getBytes()"
  92. end
  93. arraycopy (a1: POINTER; a2: INTEGER; a3: POINTER; a4: INTEGER; a5: INTEGER) is
  94. external "Java class java.lang.System method static void arraycopy(java.lang.Object,int,java.lang.Object,int,int)"
  95. end
  96. end -- class EXAMPLE4