/[eiffelstudio]/trunk/Src/library/Eiffel2Java/java_object.e
ViewVC logotype

Contents of /trunk/Src/library/Eiffel2Java/java_object.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38228 - (show annotations)
Tue Jul 29 05:31:36 2003 UTC (16 years, 6 months ago) by manus
File size: 11272 byte(s)
In `method_id' fixed VDRD error that was not previously detected by the
compiler. Use `feature_name' instead of `method_name' to follow inherited
argument name.

1 indexing
2 description: "This class gives Eiffel access to Java objects. You %
3 %can use it directly or inherit from to and create a %
4 %more convienient Eiffel class that makes the Java %
5 %object look like an Eiffel object"
6 date: "$Date$"
7 revision: "$Revision$"
8
9 class
10 JAVA_OBJECT
11
12 inherit
13 JAVA_ENTITY
14 redefine
15 is_equal
16 end
17
18 SHARED_JNI_ENVIRONMENT
19 redefine
20 is_equal
21 end
22
23 create
24 create_instance,
25 make_from_pointer
26
27 feature -- Initialization
28
29 make_from_pointer (jobject: POINTER) is
30 -- Create an Eiffel proxy, give a pointer to a Java object
31 -- (Java object already exists)
32 require
33 valid_java_id: jobject /= default_pointer
34 local
35 clsp: POINTER
36 do
37 java_object_id := jobject
38
39 -- Figure out out class
40 clsp := jni.get_class (jobject)
41 jclass := jni.find_class_by_pointer (clsp)
42
43 -- Add to the Eiffel/java object table
44 jni.java_object_table.put (Current, java_object_id)
45 end
46
47 create_instance (my_cls: JAVA_CLASS; sig: STRING; args: JAVA_ARGS) is
48 -- Create an instance of the class by calling the
49 -- constructor with the specified arguments. If "sig" is
50 -- void then we assume that the contructor has no
51 -- arguments. In that case "args" can be void
52 require
53 class_valid: my_cls /= Void
54 sig_and_args_consistent: (sig = Void) implies (args = Void)
55 local
56 constructor_id, argsp: POINTER
57 lsig: STRING
58 do
59 jclass := my_cls
60 lsig := sig
61 if lsig = Void then
62 lsig := "()V"
63 end
64 constructor_id := method_id ("<init>", lsig)
65 if args /= Void then
66 argsp := args.to_c
67 end
68 if constructor_id /= default_pointer then
69 debug ("java")
70 io.putstring ("JAVA_OBJECT: creating new object Class=")
71 io.putstring (jclass.java_class_id.out)
72 io.putstring (" Constructor ID=")
73 io.putstring (constructor_id.out)
74 io.new_line
75 end
76 java_object_id := jni.new_object (jclass.java_class_id,
77 constructor_id, argsp)
78 end
79 if java_object_id = default_pointer then
80 io.putstring ("Failed to get constructor for class:")
81 io.putstring (my_cls.name)
82 io.new_line
83 else
84 jni.java_object_table.put (Current, java_object_id)
85 end
86 ensure
87 created: java_object_id /= default_pointer
88 end
89
90 feature -- Access
91
92 java_object_id: POINTER
93 -- Reference to java object.
94
95 jclass: JAVA_CLASS
96 -- Associated java class.
97
98 feature -- Status report
99
100 exists: BOOLEAN is
101 -- Is Current java object alive?
102 do
103 Result := java_object_id /= default_pointer
104 end
105
106 feature -- Comparison
107
108 is_equal (other: like Current): BOOLEAN is
109 -- Is `other' attached to an object considered
110 -- equal to current object?
111 do
112 Result := java_object_id = other.java_object_id
113 end
114
115 feature -- Reflection
116
117 method_id (feature_name: STRING; signature: STRING): POINTER is
118 -- Find the method_id for `feature_name' with signature
119 -- encoded by "signature"
120 local
121 method_name_to_c, signature_to_c: C_STRING
122 do
123 create method_name_to_c.make (feature_name)
124 create signature_to_c.make (signature)
125 Result := jni.get_method_id (jclass.java_class_id,
126 method_name_to_c.item, signature_to_c.item)
127 end
128
129 field_id (lname: STRING; sig: STRING): POINTER is
130 -- Get the java field id used to set/get this field
131 local
132 lname_to_c, sig_to_c: C_STRING
133 do
134 create lname_to_c.make (lname)
135 create sig_to_c.make (sig)
136 Result := jni.get_field_id (jclass.java_class_id,
137 lname_to_c.item, sig_to_c.item)
138 end
139
140 feature -- Calls
141
142 void_method (mid: POINTER; args: JAVA_ARGS) is
143 -- Call a Java procedure with method_id "mid" and
144 -- arguments "args.
145 local
146 argsp: POINTER
147 do
148 if args /= Void then
149 argsp := args.to_c
150 end
151 jni.call_void_method (java_object_id, mid, argsp)
152 end
153
154 string_method (mid: POINTER; args: JAVA_ARGS): STRING is
155 -- Call an instance function that returns a STRING.
156 local
157 argsp: POINTER
158 do
159 if args /= Void then
160 argsp := args.to_c
161 end
162 Result := jni.call_string_method (java_object_id, mid, argsp)
163 end
164
165 integer_method (mid: POINTER; args: JAVA_ARGS): INTEGER is
166 -- Call an instance function that returns an INTEGER.
167 local
168 argsp: POINTER
169 do
170 if args /= Void then
171 argsp := args.to_c
172 end
173 Result := jni.call_int_method (java_object_id, mid, argsp)
174 end
175
176 short_method (mid: POINTER; args: JAVA_ARGS): INTEGER_16 is
177 -- Call an instance function that returns a Short (in
178 -- Eiffel we still return an INTEGER).
179 local
180 argsp: POINTER
181 do
182 if args /= Void then
183 argsp := args.to_c
184 end
185 Result := jni.call_short_method (java_object_id, mid, argsp)
186 end
187
188 long_method (mid: POINTER; args: JAVA_ARGS): INTEGER_64 is
189 -- Call an instance function that returns an Long. This
190 -- function is not implemented.
191 local
192 argsp: POINTER
193 do
194 if args /= Void then
195 argsp := args.to_c
196 end
197 Result := jni.call_long_method (java_object_id, mid, argsp)
198 end
199
200 double_method (mid: POINTER; args: JAVA_ARGS): DOUBLE is
201 -- Call an instance function that returns a DOUBLE.
202 local
203 argsp: POINTER
204 do
205 if args /= Void then
206 argsp := args.to_c
207 end
208 Result := jni.call_double_method (java_object_id, mid, argsp)
209 end
210
211 float_method (mid: POINTER; args: JAVA_ARGS): REAL is
212 -- Call an instance function that returns a REAL.
213 local
214 argsp: POINTER
215 do
216 if args /= Void then
217 argsp := args.to_c
218 end
219 Result := jni.call_float_method (java_object_id, mid, argsp)
220 end
221
222 char_method (mid: POINTER; args: JAVA_ARGS): CHARACTER is
223 -- Call an instance function that returns a char
224 local
225 argsp: POINTER
226 do
227 if args /= Void then
228 argsp := args.to_c
229 end
230 Result := jni.call_char_method (java_object_id, mid, argsp)
231 end
232
233 boolean_method (mid: POINTER; args: JAVA_ARGS): BOOLEAN is
234 -- Call an instance function that returns a boolean
235 local
236 argsp: POINTER
237 do
238 if args /= Void then
239 argsp := args.to_c
240 end
241 Result := jni.call_boolean_method (java_object_id, mid, argsp)
242 end
243
244 object_method (lmethod_id: POINTER; args: JAVA_ARGS): JAVA_OBJECT is
245 -- Call an instance function that returns a java object
246 local
247 argp: POINTER
248 jo: POINTER
249 do
250 if args /= Void then
251 argp := args.to_c
252 end
253 jo := jni.call_object_method (java_object_id, lmethod_id, argp)
254 if jo /= default_pointer then
255 -- Check global table to see if we have an Eiffel
256 -- proxy for this object
257 Result := jni.java_object_table.item (jo)
258 if Result = Void then
259 create Result.make_from_pointer (jo)
260 end
261 end
262 end
263
264 byte_method (mid: POINTER; args: JAVA_ARGS): INTEGER_8 is
265 -- Call an instance function that return a byte
266 -- ( 8-bit integer (signed)), in Eiffel return
267 -- a INTEGER_8
268 local
269 argsp: POINTER
270 do
271 if args /= Void then
272 argsp := args.to_c
273 end
274 Result := jni.call_byte_method (java_object_id, mid, argsp)
275 end
276
277 feature -- Attributes
278
279 integer_attribute (fid: POINTER): INTEGER is
280 -- Access to an integer attribute
281 do
282 Result := jni.get_integer_field (java_object_id, fid)
283 end
284
285 string_attribute (fid: POINTER): STRING is
286 -- Access to a String attribute
287 do
288 Result := jni.get_string_field (java_object_id, fid)
289 end
290
291 object_attribute (fid: POINTER): JAVA_OBJECT is
292 -- Access to a java object attribute
293 local
294 jo: POINTER
295 do
296 jo := jni.get_object_field (java_object_id, fid)
297 if jo /= default_pointer then
298 -- Check global table to see if we have an Eiffel
299 -- proxy for this object
300 Result := jni.java_object_table.item (jo)
301 if Result = Void then
302 create Result.make_from_pointer (jo)
303 end
304 end
305 end
306
307 boolean_attribute (fid: POINTER): BOOLEAN is
308 -- Access to a boolean attribute
309 do
310 Result := jni.get_boolean_field (java_object_id, fid)
311 end
312
313 char_attribute (fid: POINTER): CHARACTER is
314 -- Access to a 'char' attribute
315 do
316 Result := jni.get_char_field (java_object_id, fid)
317 end
318
319 float_attribute (fid: POINTER): REAL is
320 -- Access to a 'float' attribute, returns a REAL
321 do
322 Result := jni.get_float_field (java_object_id, fid)
323 end
324
325 double_attribute (fid: POINTER): DOUBLE is
326 -- Access to a double attribute
327 do
328 Result := jni.get_double_field (java_object_id, fid)
329 end
330
331 byte_attribute (fid: POINTER): INTEGER_8 is
332 -- Access to a 'byte' attribute, returns a INTEGER_8
333 do
334 Result := jni.get_byte_field (java_object_id, fid)
335 end
336
337 short_attribute (fid: POINTER): INTEGER_16 is
338 -- Access to a 'short' attribute, returns a INTEGER_16
339 do
340 Result := jni.get_short_field (java_object_id, fid)
341 end
342
343 long_attribute (fid: POINTER): INTEGER_64 is
344 -- Access to a 'long' attribute, returns a INTEGER_64
345 do
346 Result := jni.get_long_field (java_object_id, fid)
347 end
348
349 feature -- Attributes setting
350
351 set_integer_attribute (fid: POINTER; value: INTEGER) is
352 -- Set an 'integer' attribute to 'value'
353 do
354 jni.set_integer_field (java_object_id, fid, value)
355 end
356
357 set_string_attribute (fid: POINTER; value: STRING) is
358 -- Set a 'String' attribute to 'value'
359 do
360 jni.set_string_field (java_object_id, fid, value)
361 end
362
363 set_object_attribute (fid: POINTER; value: JAVA_OBJECT) is
364 -- Set a java object attribute to 'value'
365 do
366 jni.set_object_field (java_object_id, fid, value.java_object_id)
367 end
368
369 set_boolean_attribute (fid: POINTER; value: BOOLEAN) is
370 -- Set a 'boolean' attribute to 'value'
371 do
372 jni.set_boolean_field (java_object_id, fid, value)
373 end
374
375 set_char_attribute (fid: POINTER; value: CHARACTER) is
376 -- Set a 'char' attribute to 'value'
377 do
378 jni.set_char_field (java_object_id, fid, value)
379 end
380
381 set_float_attribute (fid: POINTER; value: REAL) is
382 -- Set a 'float' attribute to 'value'
383 do
384 jni.set_float_field (java_object_id, fid, value)
385 end
386
387 set_double_attribute (fid: POINTER; value: DOUBLE) is
388 -- Set a 'double' attribute to 'value'
389 do
390 jni.set_double_field (java_object_id, fid, value)
391 end
392
393 set_byte_attribute (fid: POINTER; value: INTEGER_8) is
394 -- Set a 'byte' attribute to 'value'
395 do
396 jni.set_byte_field (java_object_id, fid, value)
397 end
398
399 set_short_attribute (fid: POINTER; value: INTEGER_16) is
400 -- Set a 'short' attribute to 'value'
401 do
402 jni.set_short_field (java_object_id, fid, value)
403 end
404
405 set_long_attribute (fid: POINTER; value: INTEGER_64) is
406 -- Set a 'short' attribute to 'value'
407 do
408 jni.set_long_field (java_object_id, fid, value)
409 end
410
411 invariant
412 valid_proxy: java_object_id /= default_pointer
413
414 end
415
416 --|----------------------------------------------------------------
417 --| Eiffel2Java: library of reusable components for ISE Eiffel.
418 --| Copyright (C) 1986-2001 Interactive Software Engineering Inc.
419 --| All rights reserved. Duplication and distribution prohibited.
420 --| May be used only with ISE Eiffel, under terms of user license.
421 --| Contact ISE for any other use.
422 --|
423 --| Interactive Software Engineering Inc.
424 --| ISE Building
425 --| 360 Storke Road, Goleta, CA 93117 USA
426 --| Telephone 805-685-1006, Fax 805-685-6869
427 --| Electronic mail <info@eiffel.com>
428 --| Customer support: http://support.eiffel.com>
429 --| For latest info see award-winning pages: http://www.eiffel.com
430 --|----------------------------------------------------------------
431

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23