PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/external/Java/example3.e

http://github.com/tybor/Liberty
Specman e | 519 lines | 428 code | 63 blank | 28 comment | 0 complexity | ab06eba2be7c016f7ee4d1d407221000 MD5 | raw file
  1class EXAMPLE3
  2	-- Compilation:
  3	--    compile_to_jvm example3
  4	-- Execution:
  5	--    java example3
  6	-- This example demonstrates how to use Java arrays from Eiffel.
  7
  8creation {ANY}
  9	make
 10
 11feature {ANY}
 12	make is
 13		do
 14			boolean_array
 15			byte_array
 16			char_array
 17			short_array
 18			int_array
 19			long_array
 20			float_array
 21			double_array
 22			java_lang_String_array
 23		end
 24
 25feature {ANY}
 26	boolean_array is
 27		local
 28			a: POINTER; i: INTEGER; length: INTEGER; z: BOOLEAN
 29		do
 30			-- create an array of booleans
 31			a := new_aZ(5) -- fill the array
 32			length := arraylength_aZ(checkcast_aZ(a))
 33			from
 34				i := 0
 35			until
 36				i >= length
 37			loop
 38				put_aZ(checkcast_aZ(a), i, i.even)
 39				i := i + 1
 40			end
 41			-- read the array
 42			length := arraylength_aZ(checkcast_aZ(a))
 43			io.put_string("array of booleans: ")
 44			from
 45				i := 0
 46			until
 47				i >= length
 48			loop
 49				z := item_aZ(checkcast_aZ(a), i)
 50				io.put_string(z.to_string + " ")
 51				i := i + 1
 52			end
 53			io.put_string("%N")
 54		end
 55
 56	byte_array is
 57		local
 58			a: POINTER; i: INTEGER; length: INTEGER; b: INTEGER_8
 59		do
 60			-- create an array of bytes
 61			a := new_aB(5) -- fill the array
 62			length := arraylength_aB(checkcast_aB(a))
 63			from
 64				i := 0
 65			until
 66				i >= length
 67			loop
 68				put_aB(checkcast_aB(a), i, i.to_integer_8)
 69				i := i + 1
 70			end
 71			-- read the array
 72			length := arraylength_aB(checkcast_aB(a))
 73			io.put_string("array of bytes: ")
 74			from
 75				i := 0
 76			until
 77				i >= length
 78			loop
 79				b := item_aB(checkcast_aB(a), i)
 80				io.put_string(b.to_string + " ")
 81				i := i + 1
 82			end
 83			io.put_string("%N")
 84		end
 85
 86	char_array is
 87			-- note that Java characters are 16 bits long and are
 88			-- not equivalent to Eiffel characters
 89			-- Eiffel integers are used to hold Java characters
 90		local
 91			a: POINTER; i: INTEGER; length: INTEGER; c: INTEGER; s: STRING; javaString: POINTER
 92		do
 93			-- create an array of chars
 94			a := new_aC(5) -- fill the array
 95			s := "Meyer"
 96			javaString := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
 97			length := arraylength_aC(checkcast_aC(a))
 98			from
 99				i := 0
100			until
101				i >= length
102			loop
103				c := charAt(checkcast_java_lang_String(javaString), i)
104				put_aC(checkcast_aC(a), i, c)
105				i := i + 1
106			end
107			-- read the array
108			length := arraylength_aC(checkcast_aC(a))
109			io.put_string("array of chars: ")
110			from
111				i := 0
112			until
113				i >= length
114			loop
115				c := item_aC(checkcast_aC(a), i)
116				io.put_string(c.to_string + " ")
117				i := i + 1
118			end
119			io.put_string("%N")
120		end
121
122	short_array is
123		local
124			a: POINTER; i: INTEGER; length: INTEGER; s: INTEGER_16
125		do
126			-- create an array of shorts
127			a := new_aS(5) -- fill the array
128			length := arraylength_aS(checkcast_aS(a))
129			from
130				i := 0
131			until
132				i >= length
133			loop
134				put_aS(checkcast_aS(a), i, i.to_integer_16)
135				i := i + 1
136			end
137			-- read the array
138			length := arraylength_aS(checkcast_aS(a))
139			io.put_string("array of shorts: ")
140			from
141				i := 0
142			until
143				i >= length
144			loop
145				s := item_aS(checkcast_aS(a), i)
146				io.put_string(s.out + " ")
147				i := i + 1
148			end
149			io.put_string("%N")
150		end
151
152	int_array is
153		local
154			a: POINTER; i: INTEGER; length: INTEGER; i1: INTEGER
155		do
156			-- create an array of ints
157			a := new_aI(5) -- fill the array
158			length := arraylength_aI(checkcast_aI(a))
159			from
160				i := 0
161			until
162				i >= length
163			loop
164				put_aI(checkcast_aI(a), i, i)
165				i := i + 1
166			end
167			-- read the array
168			length := arraylength_aI(checkcast_aI(a))
169			io.put_string("array of ints: ")
170			from
171				i := 0
172			until
173				i >= length
174			loop
175				i1 := item_aI(checkcast_aI(a), i)
176				io.put_string(i1.to_string + " ")
177				i := i + 1
178			end
179			io.put_string("%N")
180		end
181
182	long_array is
183		local
184			a: POINTER; i: INTEGER; length: INTEGER; j: INTEGER_64
185		do
186			-- create an array of longs
187			a := new_aJ(5) -- fill the array
188			length := arraylength_aJ(checkcast_aJ(a))
189			from
190				i := 0
191			until
192				i >= length
193			loop
194				put_aJ(checkcast_aJ(a), i, i.to_integer_64)
195				i := i + 1
196			end
197			-- read the array
198			length := arraylength_aJ(checkcast_aJ(a))
199			io.put_string("array of longs: ")
200			from
201				i := 0
202			until
203				i >= length
204			loop
205				j := item_aJ(checkcast_aJ(a), i)
206				io.put_string(j.to_string + " ")
207				i := i + 1
208			end
209			io.put_string("%N")
210		end
211
212	float_array is
213		local
214			a: POINTER; i: INTEGER; length: INTEGER; f: REAL
215		do
216			-- create an array of floats
217			a := new_aF(5) -- fill the array
218			length := arraylength_aF(checkcast_aF(a))
219			from
220				i := 0
221			until
222				i >= length
223			loop
224				put_aF(checkcast_aF(a), i, i * 1.0)
225				i := i + 1
226			end
227			-- read the array
228			length := arraylength_aF(checkcast_aF(a))
229			io.put_string("array of floats: ")
230			from
231				i := 0
232			until
233				i >= length
234			loop
235				f := item_aF(checkcast_aF(a), i)
236				io.put_string(f.to_string + " ")
237				i := i + 1
238			end
239			io.put_string("%N")
240		end
241
242	double_array is
243		local
244			a: POINTER; i: INTEGER; length: INTEGER; d: DOUBLE
245		do
246			-- create an array of doubles
247			a := new_aD(5) -- fill the array
248			length := arraylength_aD(checkcast_aD(a))
249			from
250				i := 0
251			until
252				i >= length
253			loop
254				put_aD(checkcast_aD(a), i, i * 1.0)
255				i := i + 1
256			end
257			-- read the array
258			length := arraylength_aD(checkcast_aD(a))
259			io.put_string("array of doubles: ")
260			from
261				i := 0
262			until
263				i >= length
264			loop
265				d := item_aD(checkcast_aD(a), i)
266				io.put_string(d.to_string + " ")
267				i := i + 1
268			end
269			io.put_string("%N")
270		end
271
272	java_lang_String_array is
273		local
274			a: POINTER; i: INTEGER; length: INTEGER; s: STRING; javaString: POINTER; javaStringBytes: POINTER
275			string_length: INTEGER
276		do
277			-- create an array of java.lang.Strings
278			a := new_a_java_lang_String(5) -- fill the array
279			length := arraylength_a_java_lang_String(checkcast_a_java_lang_String(a))
280			from
281				i := 0
282			until
283				i >= length
284			loop
285				create s.make(3)
286				s.append("<")
287				s.append(i.out)
288				s.append(">")
289				javaString := new_java_lang_String(checkcast_aB(s.to_external), 0, s.count)
290				put_a_java_lang_String(checkcast_a_java_lang_String(a), i, checkcast_java_lang_String(javaString))
291				i := i + 1
292			end
293			-- read the array
294			length := arraylength_a_java_lang_String(checkcast_a_java_lang_String(a))
295			io.put_string("array of strings: ")
296			from
297				i := 0
298			until
299				i >= length
300			loop
301				javaString := item_a_java_lang_String(checkcast_a_java_lang_String(a), i)
302				string_length := length_java_lang_String(checkcast_java_lang_String(javaString))
303				create s.make_filled('%U', string_length)
304				javaStringBytes := getBytes(checkcast_java_lang_String(javaString))
305				arraycopy(javaStringBytes, 0, s.to_external, 0, string_length)
306				io.put_string(s.to_string + " ")
307				i := i + 1
308			end
309			io.put_string("%N")
310		end
311
312feature {ANY}
313	-- externals
314	-- boolean
315	new_aZ (a_length: INTEGER): POINTER is
316		external "Java array boolean new"
317		end
318
319	checkcast_aZ (p: POINTER): POINTER is
320		external "Java class boolean[] checkcast"
321		end
322
323	arraylength_aZ (p: POINTER): INTEGER is
324		external "Java array boolean length"
325		end
326
327	put_aZ (p: POINTER; i: INTEGER; a: BOOLEAN) is
328		external "Java array boolean set"
329		end
330
331	item_aZ (p: POINTER; i: INTEGER): BOOLEAN is
332		external "Java array boolean get"
333		end -- byte
334
335	new_aB (a_length: INTEGER): POINTER is
336		external "Java array byte new"
337		end
338
339	checkcast_aB (p: POINTER): POINTER is
340		external "Java class byte[] checkcast"
341		end
342
343	arraylength_aB (p: POINTER): INTEGER is
344		external "Java array byte length"
345		end
346
347	put_aB (p: POINTER; i: INTEGER; a: INTEGER_8) is
348		external "Java array byte set"
349		end
350
351	item_aB (p: POINTER; i: INTEGER): INTEGER_8 is
352		external "Java array byte get"
353		end -- char
354
355	new_aC (a_length: INTEGER): POINTER is
356		external "Java array char new"
357		end
358
359	checkcast_aC (p: POINTER): POINTER is
360		external "Java class char[] checkcast"
361		end
362
363	arraylength_aC (p: POINTER): INTEGER is
364		external "Java array char length"
365		end
366
367	put_aC (p: POINTER; i: INTEGER; a: INTEGER) is
368		external "Java array char set"
369		end
370
371	item_aC (p: POINTER; i: INTEGER): INTEGER is
372		external "Java array char get"
373		end
374
375	new_java_lang_String (a1: POINTER; a2: INTEGER; a3: INTEGER): POINTER is
376		external "Java class java.lang.String new (byte[],int,int)"
377		end
378
379	checkcast_java_lang_String (p: POINTER): POINTER is
380		external "Java class java.lang.String checkcast"
381		end
382
383	charAt (p: POINTER; a1: INTEGER): INTEGER is
384		external "Java class java.lang.String method char charAt(int)"
385		end -- short
386
387	new_aS (a_length: INTEGER): POINTER is
388		external "Java array short new"
389		end
390
391	checkcast_aS (p: POINTER): POINTER is
392		external "Java class short[] checkcast"
393		end
394
395	arraylength_aS (p: POINTER): INTEGER is
396		external "Java array short length"
397		end
398
399	put_aS (p: POINTER; i: INTEGER; a: INTEGER_16) is
400		external "Java array short set"
401		end
402
403	item_aS (p: POINTER; i: INTEGER): INTEGER_16 is
404		external "Java array short get"
405		end -- int
406
407	new_aI (a_length: INTEGER): POINTER is
408		external "Java array int new"
409		end
410
411	checkcast_aI (p: POINTER): POINTER is
412		external "Java class int[] checkcast"
413		end
414
415	arraylength_aI (p: POINTER): INTEGER is
416		external "Java array int length"
417		end
418
419	put_aI (p: POINTER; i: INTEGER; a: INTEGER) is
420		external "Java array int set"
421		end
422
423	item_aI (p: POINTER; i: INTEGER): INTEGER is
424		external "Java array int get"
425		end -- long
426
427	new_aJ (a_length: INTEGER): POINTER is
428		external "Java array long new"
429		end
430
431	checkcast_aJ (p: POINTER): POINTER is
432		external "Java class long[] checkcast"
433		end
434
435	arraylength_aJ (p: POINTER): INTEGER is
436		external "Java array long length"
437		end
438
439	put_aJ (p: POINTER; i: INTEGER; a: INTEGER_64) is
440		external "Java array long set"
441		end
442
443	item_aJ (p: POINTER; i: INTEGER): INTEGER_64 is
444		external "Java array long get"
445		end -- float
446
447	new_aF (a_length: INTEGER): POINTER is
448		external "Java array float new"
449		end
450
451	checkcast_aF (p: POINTER): POINTER is
452		external "Java class float[] checkcast"
453		end
454
455	arraylength_aF (p: POINTER): INTEGER is
456		external "Java array float length"
457		end
458
459	put_aF (p: POINTER; i: INTEGER; a: REAL) is
460		external "Java array float set"
461		end
462
463	item_aF (p: POINTER; i: INTEGER): REAL is
464		external "Java array float get"
465		end -- double
466
467	new_aD (a_length: INTEGER): POINTER is
468		external "Java array double new"
469		end
470
471	checkcast_aD (p: POINTER): POINTER is
472		external "Java class double[] checkcast"
473		end
474
475	arraylength_aD (p: POINTER): INTEGER is
476		external "Java array double length"
477		end
478
479	put_aD (p: POINTER; i: INTEGER; a: DOUBLE) is
480		external "Java array double set"
481		end
482
483	item_aD (p: POINTER; i: INTEGER): DOUBLE is
484		external "Java array double get"
485		end -- java.lang.String
486
487	length_java_lang_String (p: POINTER): INTEGER is
488		external "Java class java.lang.String method int length()"
489		end
490
491	new_a_java_lang_String (a_length: INTEGER): POINTER is
492		external "Java array java.lang.String new"
493		end
494
495	checkcast_a_java_lang_String (p: POINTER): POINTER is
496		external "Java class java.lang.String[] checkcast"
497		end
498
499	arraylength_a_java_lang_String (p: POINTER): INTEGER is
500		external "Java array java.lang.String length"
501		end
502
503	put_a_java_lang_String (p: POINTER; i: INTEGER; a: POINTER) is
504		external "Java array java.lang.String set"
505		end
506
507	item_a_java_lang_String (p: POINTER; i: INTEGER): POINTER is
508		external "Java array java.lang.String get"
509		end
510
511	getBytes (p: POINTER): POINTER is
512		external "Java class java.lang.String method byte[] getBytes()"
513		end
514
515	arraycopy (a1: POINTER; a2: INTEGER; a3: POINTER; a4: INTEGER; a5: INTEGER) is
516		external "Java class java.lang.System method static void arraycopy(java.lang.Object,int,java.lang.Object,int,int)"
517		end
518
519end -- class EXAMPLE3