PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/external/Java/example5.e

http://github.com/tybor/Liberty
Specman e | 82 lines | 58 code | 12 blank | 12 comment | 2 complexity | b65026ef68a2b0cbbe8503994bb5fc3b MD5 | raw file
 1class EXAMPLE5
 2	-- Compilation:
 3	--    compile_to_jvm example5
 4	--    del example5.class (Windows), rm example5.class (Unix)
 5	--    javac CSimple.java
 6	-- Execution:
 7	--    java CSimple
 8	-- This example demonstrates how to create and call a compiled Eiffel system
 9	-- from  Java, how to throw Java exceptions from Eiffel, and how Eiffel
10	-- contract assertion violations throw Java exceptions.
11
12creation {ANY}
13	make
14
15feature {ANY}
16	make is
17		do
18			-- set attribute eiffel_integer
19			eiffel_integer := 1 -- fake a call to proc1 and proc2 so they won't be optimized out at compile time
20			if False then
21				proc1(0)
22				proc2(0)
23			end
24		end
25
26feature {ANY}
27	eiffel_integer: INTEGER
28
29feature {ANY}
30	proc1 (i: INTEGER) is
31			-- throws a Java exception depending on argument value
32		local
33			e: POINTER; javaString: POINTER; javaStringBytes: POINTER; string_length: INTEGER; s: STRING
34			flag: BOOLEAN
35		do
36			if i >= 0 then
37				eiffel_integer := i
38			else
39				create s.make(48)
40				s.append("Exception in example5.proc1: i=")
41				s.append(i.out)
42				s.append(" < 0")
43				javaString := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
44				e := new_java_lang_Exception(checkcast_java_lang_String(javaString))
45				throw_exception(checkcast_java_lang_Exception(e))
46			end
47		end
48
49	proc2 (i: INTEGER) is
50			-- precondition assertion violation throws Java exception
51		require
52			i_ok: i >= 0
53		do
54			eiffel_integer := i
55		end
56
57feature {ANY} -- externals
58	new_java_lang_String (a1: POINTER; a2: INTEGER; a3: INTEGER): POINTER is
59		external "Java class java.lang.String new (byte[],int,int)"
60		end
61
62	checkcast_java_lang_String (p: POINTER): POINTER is
63		external "Java class java.lang.String checkcast"
64		end
65
66	checkcast_aB (p: POINTER): POINTER is
67		external "Java class byte[] checkcast"
68		end
69
70	new_java_lang_Exception (a1: POINTER): POINTER is
71		external "Java class java.lang.Exception new (java.lang.String)"
72		end
73
74	checkcast_java_lang_Exception (p: POINTER): POINTER is
75		external "Java class java.lang.Exception checkcast"
76		end
77
78	throw_exception (p: POINTER) is
79		external "Java exception java.lang.Exception throw"
80		end
81
82end -- class EXAMPLE5