/tutorial/external/Java/example6.e

http://github.com/tybor/Liberty · Specman e · 102 lines · 62 code · 15 blank · 25 comment · 0 complexity · d34133632d7905f5b0e4700d594ed157 MD5 · raw file

  1. class EXAMPLE6
  2. -- Compilation:
  3. -- compile_to_jvm example6
  4. -- Execution:
  5. -- java example6
  6. -- This example demonstrates how to set Java field attributes of Eiffel
  7. -- class attributes, how to declare a method synchronized, and how to
  8. -- construct a synchronized block.
  9. creation {ANY}
  10. make
  11. feature {ANY}
  12. make is
  13. do
  14. field_attributes
  15. synchronized_test
  16. monitor_test
  17. end
  18. feature {ANY}
  19. field_attributes is
  20. -- have to look at dump of classfile to verify that
  21. -- var1 is public
  22. -- var2 is public and transient
  23. -- var3 is public and volatile
  24. -- var4 is public and transient and volatile
  25. -- Note: The field_is_transient and field_is_volatile procedure calls
  26. -- must be in a routine in the Eiffel class that will not be optimized
  27. -- out at compile time. The best place to put them is in a creation
  28. -- procedure that you know will be called at runtime.
  29. do
  30. field_is_transient("var2")
  31. field_is_volatile("var3")
  32. field_is_transient("var4")
  33. field_is_volatile("var4")
  34. var1 := 1
  35. var2 := 1
  36. var3 := 1
  37. var4 := 1
  38. end
  39. feature {ANY}
  40. var1: INTEGER
  41. var2: INTEGER
  42. var3: INTEGER
  43. var4: INTEGER
  44. feature {ANY}
  45. synchronized_test is
  46. -- have to look at dump of classfile to verify that this method is synchronized
  47. -- Note: The routine_is_synchronized procedure call must be within the Eiffel routine
  48. -- to be declared synchronized. It generates no code.
  49. local
  50. i: INTEGER
  51. do
  52. routine_is_synchronized
  53. i := 1
  54. end
  55. monitor_test is
  56. -- have to look at dump of classfile to verify that this Eifel routine has
  57. -- monitorenter and monitorexit calls ( in Java, this would be a
  58. -- synchronized block )
  59. -- the user is responsible for ensuring that each monitorenter is matched
  60. -- by a correspondng monitorexit on the same object
  61. local
  62. i: INTEGER
  63. do
  64. i := 0 -- start of synchronized block
  65. monitor_enter(Current.to_pointer)
  66. i := 1
  67. monitor_exit(Current.to_pointer)
  68. -- end of synchronized block
  69. i := 2
  70. end
  71. feature {ANY} -- externals
  72. field_is_transient (s: STRING) is
  73. external "Java transient"
  74. end
  75. field_is_volatile (s: STRING) is
  76. external "Java volatile"
  77. end
  78. routine_is_synchronized is
  79. external "Java synchronized"
  80. end
  81. monitor_enter (p: POINTER) is
  82. external "Java monitorenter"
  83. end
  84. monitor_exit (p: POINTER) is
  85. external "Java monitorexit"
  86. end
  87. end -- class EXAMPLE6