File: example5.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (96 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class EXAMPLE5
-- Compilation: 
--    compile_to_jvm example5
--    del example5.class (Windows), rm example5.class (Unix)
--    javac CSimple.java
-- Execution:
--    java CSimple
-- This example demonstrates how to create and call a compiled Eiffel system 
-- from  Java, how to throw Java exceptions from Eiffel, and how Eiffel 
-- contract assertion violations throw Java exceptions.

creation make

feature

   make is
      do

-- set attribute eiffel_integer

         eiffel_integer := 1
         
-- fake a call to proc1 and proc2 so they won't be optimized out at compile time
         
         if False then
            proc1( 0 )
            proc2( 0 )
         end
         
      end
   
feature

   eiffel_integer: INTEGER
      
feature

   proc1( i: INTEGER ) is
         -- throws a Java exception depending on argument value
      local 
         e: POINTER
         javaString: POINTER
         javaStringBytes: POINTER
         string_length: INTEGER
         s: STRING
         flag: BOOLEAN
      do
         if i >= 0 then
            eiffel_integer := i
         else
            create s.make( 48 )
            s.append( "Exception in example5.proc1: i=" )
            s.append( i.out )
            s.append( " < 0" )
            javaString := new_java_lang_String( checkcast_aB( s.to_external ), 0, s.count )
            e := new_java_lang_Exception( checkcast_java_lang_String( javaString ) )
            throw_exception( checkcast_java_lang_Exception( e ) )
         end
      end

   proc2( i: INTEGER ) is
         -- precondition assertion violation throws Java exception
      require
         i_ok: i >= 0
      do
         eiffel_integer := i
      end
               
feature -- externals

	new_java_lang_String( a1: POINTER; a2: INTEGER; a3: INTEGER ): POINTER is
		external "Java class java.lang.String new (byte[],int,int)"
		end

	checkcast_java_lang_String( p: POINTER ): POINTER is
		external "Java class java.lang.String checkcast"
		end

	checkcast_aB( p: POINTER ): POINTER is
		external "Java class byte[] checkcast"
		end

	new_java_lang_Exception( a1: POINTER ): POINTER is
		external "Java class java.lang.Exception new (java.lang.String)"
		end

	checkcast_java_lang_Exception( p: POINTER ): POINTER is
		external "Java class java.lang.Exception checkcast"
		end

	throw_exception( p: POINTER ) is
		external "Java exception java.lang.Exception throw"
		end


end -- EXAMPLE5