PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/external/Java/example1.e

http://github.com/tybor/Liberty
Specman e | 135 lines | 103 code | 21 blank | 11 comment | 0 complexity | c13d059f53be9d6529f846af14456acd MD5 | raw file
  1class EXAMPLE1
  2	-- This example demonstrates how to call Java class and instance
  3	-- methods from Eiffel code.
  4	-- To try this example:
  5	--
  6	--                       compile_to_jvm example1
  7	--                       java example1
  8
  9creation {ANY}
 10	make
 11
 12feature {ANY}
 13	make is
 14		local
 15			i: INTEGER; s: STRING; b: BOOLEAN; java_string, java_string_bytes, hash_map, obj: POINTER
 16		do
 17			-- get and print this object's Java hash code
 18			i := hashCode(Current.to_pointer)
 19			io.put_string("hash code is: " + i.to_string + "%N")
 20			-- get and print this object's Java string value
 21			java_string := toString(Current.to_pointer)
 22			i := length(checkcast_java_lang_String(java_string))
 23			create s.make_filled('%U', i)
 24			java_string_bytes := getBytes(checkcast_java_lang_String(java_string))
 25			arraycopy(java_string_bytes, 0, s.to_external, 0, i)
 26			io.put_string("string value is: " + s + "%N")
 27			-- create hash map
 28			hash_map := new_java_util_HashMap -- fill hash map with entries using a java.lang.String as key and Eiffel string as value
 29			s := "A"
 30			java_string := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
 31			s := "Number_1"
 32			obj := put(checkcast_java_util_HashMap(hash_map), java_string, s.to_pointer)
 33			s := "B"
 34			java_string := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
 35			s := "Number_2"
 36			obj := put(checkcast_java_util_HashMap(hash_map), java_string, s.to_pointer)
 37			s := "C"
 38			java_string := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
 39			s := "Number_3"
 40			obj := put(checkcast_java_util_HashMap(hash_map), java_string, s.to_pointer) -- get size of hash map
 41			i := size(checkcast_java_util_HashMap(hash_map))
 42			io.put_string("hashMap size is: " + i.to_string + "%N")
 43			-- retrieve hash map entry
 44			s := "B"
 45			java_string := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
 46			b := containsKey(checkcast_java_util_HashMap(hash_map), java_string)
 47			io.put_string("is key " + s + " present in hashMap: " + b.to_string + "%N")
 48			obj := get(checkcast_java_util_HashMap(hash_map), java_string)
 49			s := checkcast_example1_string(obj)
 50			io.put_string("string associated with key is: " + s + "%N")
 51			-- retrieve hash map entry using java.util.Map interface
 52			s := "C"
 53			java_string := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
 54			obj := checkcast_java_util_Map(hash_map)
 55			b := containsKey_interface(checkcast_java_util_Map(hash_map), java_string)
 56			io.put_string("is key " + s + " present in hashMap: " + b.to_string + "%N")
 57			obj := get_interface(checkcast_java_util_Map(hash_map), java_string)
 58			s := checkcast_example1_string(obj)
 59			io.put_string("string associated with key is: " + s + "%N")
 60		end
 61
 62feature {ANY} -- externals
 63	hashCode (p: POINTER): INTEGER is
 64		external "Java class java.lang.Object method int hashCode()"
 65		end
 66
 67	toString (p: POINTER): POINTER is
 68		external "Java class java.lang.Object method java.lang.String toString()"
 69		end
 70
 71	length (p: POINTER): INTEGER is
 72		external "Java class java.lang.String method int length()"
 73		end
 74
 75	checkcast_java_lang_String (p: POINTER): POINTER is
 76		external "Java class java.lang.String checkcast"
 77		end
 78
 79	getBytes (p: POINTER): POINTER is
 80		external "Java class java.lang.String method byte[] getBytes()"
 81		end
 82
 83	arraycopy (a1: POINTER; a2: INTEGER; a3: POINTER; a4: INTEGER; a5: INTEGER) is
 84		external "Java class java.lang.System method static void arraycopy(java.lang.Object,int,java.lang.Object,int,int)"
 85		end
 86
 87	new_java_lang_String (a1: POINTER; a2: INTEGER; a3: INTEGER): POINTER is
 88		external "Java class java.lang.String new (byte[],int,int)"
 89		end
 90
 91	new_java_util_HashMap: POINTER is
 92		external "Java class java.util.HashMap new ()"
 93		end
 94
 95	checkcast_aB (p: POINTER): POINTER is
 96		external "Java class byte[] checkcast"
 97		end
 98
 99	put (p: POINTER; a1: POINTER; a2: POINTER): POINTER is
100		external "Java class java.util.HashMap method java.lang.Object put(java.lang.Object,java.lang.Object)"
101		end
102
103	checkcast_java_util_HashMap (p: POINTER): POINTER is
104		external "Java class java.util.HashMap checkcast"
105		end
106
107	size (p: POINTER): INTEGER is
108		external "Java class java.util.HashMap method int size()"
109		end
110
111	containsKey (p: POINTER; a1: POINTER): BOOLEAN is
112		external "Java class java.util.HashMap method boolean containsKey(java.lang.Object)"
113		end
114
115	get (p: POINTER; a1: POINTER): POINTER is
116		external "Java class java.util.HashMap method java.lang.Object get(java.lang.Object)"
117		end
118
119	checkcast_example1_string (p: POINTER): STRING is
120		external "Java class example1.string checkcast"
121		end
122
123	containsKey_interface (p: POINTER; a1: POINTER): BOOLEAN is
124		external "Java interface java.util.Map method boolean containsKey(java.lang.Object)"
125		end
126
127	get_interface (p: POINTER; a1: POINTER): POINTER is
128		external "Java interface java.util.Map method java.lang.Object get(java.lang.Object)"
129		end
130
131	checkcast_java_util_Map (p: POINTER): POINTER is
132		external "Java class java.util.Map checkcast"
133		end
134
135end -- class EXAMPLE1