KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > debug > meta > JsObjectStub


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.debug.meta;
57
58 import java.rmi.RemoteException JavaDoc;
59 import java.io.*;
60 import java.net.*;
61
62 import org.apache.bsf.debug.jsdi.*;
63 import org.apache.bsf.debug.util.*;
64  
65 public class JsObjectStub extends Stub implements JsObject {
66     public JsObjectStub(
67         SocketConnection con,
68         int tid,
69         int uid) {
70         super(con,tid, uid);
71     }
72
73     /**
74      * True if this object represents the "NOT_FOUND" value
75      * for a code. This is potentially returned from
76      * <code>get</code> if the property is not found.
77      */

78     public boolean isNotFound() {
79         return this == Stub.NOT_FOUND;
80     }
81
82     /**
83      * Value returned when a property is undefined.
84      */

85     public boolean isUndefined() {
86         return this == Stub.UNDEFINED;
87     }
88
89     /**
90     * The value can be any of the following type:
91     *
92     * java.lang.Boolean
93     * java.lang.Number
94     * java.lang.String
95     * org.apache.bsf.debug.jsdi.JsObject
96     */

97     public void define(String JavaDoc propertyName, Object JavaDoc value, int attributes)
98         throws RemoteException JavaDoc {
99         ResultCell cell;
100         try {
101             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_DEFINE);
102             cell.writeObject(propertyName);
103             cell.writeObject(value);
104             cell.writeInt(attributes);
105
106             cell.waitForCompletion();
107         } catch (Exception JavaDoc ex) {
108             throw new RemoteException JavaDoc("Marshalling error", ex);
109         }
110     }
111     /**
112      * Removes a property from this object.
113      * The prototype chain is not walked to find the
114      * property.
115     *
116      * This operation corresponds to the ECMA [[Delete]] except that
117      * the no result is returned. The runtime will guarantee that this
118      * method is called only if the property exists. After this method
119      * is called, the runtime will call Scriptable.has to see if the
120      * property has been removed in order to determine the boolean
121      * result of the delete operator as defined by ECMA 11.4.1.
122      * <p>
123      * A property can be made permanent by ignoring calls to remove
124      * it.
125      *
126      * The property is specified by an integral index
127      * as defined for <code>get</code>.
128      * <p>
129      * To delete properties defined in a prototype chain,
130      * first find the owner object of the property and then
131      * call deleteProperty on that owner object.
132      *
133      * Identical to <code>delete(String)</code> except that
134      * an integral index is used to select the property.
135      *
136      * @param index the numeric index for the property
137      * @see org.mozilla.javascript.Scriptable#get
138      * @see org.mozilla.javascript.ScriptableObject#deleteProperty
139      */

140     public void delete(int index) throws RemoteException JavaDoc {
141         ResultCell cell;
142
143         try {
144             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_DELETE_BY_INDEX);
145             cell.writeInt(index);
146
147             cell.waitForCompletion();
148
149         } catch (Exception JavaDoc ex) {
150             throw new RemoteException JavaDoc("Marshalling error", ex);
151         }
152     }
153     /**
154      * Removes a property from this object.
155      * This operation corresponds to the ECMA [[Delete]] except that
156      * the no result is returned. The runtime will guarantee that this
157      * method is called only if the property exists. After this method
158      * is called, the runtime will call Scriptable.has to see if the
159      * property has been removed in order to determine the boolean
160      * result of the delete operator as defined by ECMA 11.4.1.
161      * <p>
162      * A property can be made permanent by ignoring calls to remove
163      * it.<p>
164      * The property is specified by a String name
165      * as defined for <code>get</code>.
166      * <p>
167      * To delete properties defined in a prototype chain,
168      * first find the owner object of the property and then
169      * call deleteProperty on that owner object.
170      *
171      * @param name the identifier for the property
172      * @see org.mozilla.javascript.Scriptable#get
173      * @see org.mozilla.javascript.ScriptableObject#deleteProperty
174      */

175     public void delete(String JavaDoc name) throws RemoteException JavaDoc {
176         ResultCell cell;
177
178         try {
179             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_DELETE_BY_NAME);
180             cell.writeObject(name);
181             
182             cell.waitForCompletion();
183             
184         } catch (Exception JavaDoc ex) {
185             throw new RemoteException JavaDoc("Marshalling error", ex);
186         }
187     }
188
189     /**
190      * Get a named property from the object.
191      * Does not walk the prototype chain.
192          *
193          * Looks property up in this object and returns the associated value
194          * if found. Returns NOT_FOUND if not found.
195          * Note that this method is not expected to traverse the prototype
196          * chain. This is different from the ECMA [[Get]] operation.
197          *
198          * Depending on the property selector, the runtime will call
199          * this method or the form of <code>get</code> that takes an
200          * integer:
201          * <table>
202          * <tr><th>JavaScript code</th><th>Java code</th></tr>
203          * <tr><td>a.b </td><td>a.get("b", a)</td></tr>
204          * <tr><td>a["foo"] </td><td>a.get("foo", a)</td></tr>
205          * <tr><td>a[3] </td><td>a.get(3, a)</td></tr>
206          * <tr><td>a["3"] </td><td>a.get(3, a)</td></tr>
207          * <tr><td>a[3.0] </td><td>a.get(3, a)</td></tr>
208          * <tr><td>a["3.0"] </td><td>a.get("3.0", a)</td></tr>
209          * <tr><td>a[1.1] </td><td>a.get("1.1", a)</td></tr>
210          * <tr><td>a[-4] </td><td>a.get(-4, a)</td></tr>
211          * </table>
212          * <p>
213          * The values that may be returned are limited to the following:
214          * <UL>
215          * <LI>java.lang.Boolean objects</LI>
216          * <LI>java.lang.String objects</LI>
217          * <LI>java.lang.Number objects</LI>
218          * <LI>org.apache.bsf.debug.jsdi.JsObject objects</LI>
219          * <LI>null</LI>
220          * <LI>The value returned by Context.getUndefinedValue()</LI>
221          * <LI>NOT_FOUND</LI>
222          * </UL>
223          * @param name the name of the property
224          * @return the value of the property (may be null), or NOT_FOUND
225          */

226     public Object JavaDoc get(String JavaDoc name) throws RemoteException JavaDoc {
227         ResultCell cell;
228
229         try {
230             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_GET_BY_NAME);
231             cell.writeObject(name);
232
233             return cell.waitForObject();
234
235         } catch (IOException ex) {
236             throw new RemoteException JavaDoc("Marshalling error", ex);
237         } catch (Exception JavaDoc ex) {
238             throw new RemoteException JavaDoc("Error at server", ex);
239         }
240     }
241     public Object JavaDoc get(int index) throws RemoteException JavaDoc {
242         ResultCell cell;
243         try {
244             cell= m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_GET_BY_INDEX);
245             cell.writeInt(index);
246
247             return cell.waitForObject();
248
249         } catch (IOException ex) {
250             throw new RemoteException JavaDoc("Marshalling error", ex);
251         } catch (Exception JavaDoc ex) {
252             throw new RemoteException JavaDoc("Error at server", ex);
253         }
254     }
255
256     /**
257      * Get the name of the set of objects implemented by this Java class.
258      * This corresponds to the [[Class]] operation in ECMA and is used
259      * by Object.prototype.toString() in ECMA.<p>
260      * See ECMA 8.6.2 and 15.2.4.2.
261      */

262     public String JavaDoc getClassName() throws RemoteException JavaDoc {
263         ResultCell cell;
264         try {
265             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_GET_CLASSNAME);
266             return (String JavaDoc) cell.waitForValueObject();
267         } catch (IOException ex) {
268             throw new RemoteException JavaDoc("Marshalling error", ex);
269         } catch (Exception JavaDoc ex) {
270             throw new RemoteException JavaDoc("Error at server", ex);
271         }
272     }
273
274     /**
275      * Get the default value of the object with a given hint.
276      * The hints are String.class for type String, Number.class for type
277      * Number, Scriptable.class for type Object, and Boolean.class for
278      * type Boolean. <p>
279      *
280      * A <code>hint</code> of null means "no hint".
281      *
282      * See ECMA 8.6.2.6.
283      *
284      * @param hint the type hint
285      * @return the default value
286      */

287     public Object JavaDoc getDefaultValue(Class JavaDoc hint) throws RemoteException JavaDoc {
288         ResultCell cell;
289         try {
290             cell= m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_GET_DEFAULT_VALUE);
291             cell.writeObject(hint.getName());
292
293             return (JsObject) cell.waitForObject();
294
295         } catch (IOException ex) {
296             throw new RemoteException JavaDoc("Marshalling error", ex);
297         } catch (Exception JavaDoc ex) {
298             throw new RemoteException JavaDoc("Error at server", ex);
299         }
300     }
301
302     /**
303     * Returns an array of property ids defined on this object.
304     * The prototype chain is not walked.
305     * However, modified properties that were prototype properties
306     * will be seen.
307     * The parameter indicates to enumerate the "DONTENUM" properties
308     * or not.
309     * <p>
310     * @return an array of all ids from all object in the prototype chain.
311     * If a given id occurs multiple times in the prototype chain,
312     * it will occur only once in this list.
313     * The elements are limited to:
314     * - java.lang.String for the property name if it has one.
315     * - java.lang.Integer for properties without a name, it is their index.
316     * @since 1.5R2
317     */

318     public Object JavaDoc[] getIds(boolean all) throws RemoteException JavaDoc {
319         ResultCell cell;
320         try {
321             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_GET_IDS);
322             cell.writeBoolean(all);
323
324             return (Object JavaDoc[]) cell.waitForValueObject();
325
326         } catch (IOException ex) {
327             throw new RemoteException JavaDoc("Marshalling error", ex);
328         } catch (Exception JavaDoc ex) {
329             throw new RemoteException JavaDoc("Error at server", ex);
330         }
331     }
332
333     /**
334     * Get the prototype of the object.
335     * @return the prototype
336     */

337     public JsObject getPrototype() throws RemoteException JavaDoc {
338         ResultCell cell;
339         try {
340             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_GET_PROTOTYPE);
341             return (JsObject) cell.waitForObject();
342         } catch (IOException ex) {
343             throw new RemoteException JavaDoc("Marshalling error", ex);
344         } catch (Exception JavaDoc ex) {
345             throw new RemoteException JavaDoc("Error at server", ex);
346         }
347     }
348
349     /**
350      * The scope is for supporting two things.
351      *
352      * If the object is a function, the scope is the "execution" scope
353      * of the function. It will be used when calling the function to
354      * initialize the Context scope.
355      *
356      * If the object is not a function, the scope is used for the scope
357      * chain of execution context.
358      */

359     public JsObject getScope() throws RemoteException JavaDoc {
360         ResultCell cell;
361         try {
362             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_GET_SCOPE);
363             return (JsObject) cell.waitForObject();
364
365         } catch (IOException ex) {
366             throw new RemoteException JavaDoc("Marshalling error", ex);
367         } catch (Exception JavaDoc ex) {
368             throw new RemoteException JavaDoc("Error at server", ex);
369         }
370     }
371
372     /**
373      * Indicates whether or not an indexed property is defined in an object.
374      * Does not traverse the prototype chain.<p>
375      *
376      * The property is specified by an integral index
377      * as defined for the <code>get</code> method.<p>
378      *
379      * @param index the numeric index for the property
380      * @return true if and only if the indexed property is found in the object
381      * @see org.mozilla.javascript.Scriptable#get
382      * @see org.mozilla.javascript.ScriptableObject#getProperty
383      */

384     public boolean has(int index) throws RemoteException JavaDoc {
385         ResultCell cell;
386         try {
387             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_HAS_BY_INDEX);
388             cell.writeInt(index);
389             return cell.waitForBooleanValue();
390         } catch (IOException ex) {
391             throw new RemoteException JavaDoc("Marshalling error", ex);
392         } catch (Exception JavaDoc ex) {
393             throw new RemoteException JavaDoc("Error at server", ex);
394         }
395     }
396
397     /**
398     * Indicates whether or not a named property is defined in an object.
399     * Does not traverse the prototype chain.<p>
400     *
401     * The property is specified by a String name
402     * as defined for the <code>get</code> method.<p>
403     *
404     * @param name the name of the property
405     * @return true if and only if the named property is found in the object
406     * @see org.mozilla.javascript.Scriptable#get
407     * @see org.mozilla.javascript.ScriptableObject#getProperty
408     */

409     public boolean has(String JavaDoc name) throws RemoteException JavaDoc {
410         ResultCell cell;
411         try {
412             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_HAS_BY_NAME);
413             cell.writeObject(name);
414
415             return cell.waitForBooleanValue();
416
417         } catch (IOException ex) {
418             throw new RemoteException JavaDoc("Marshalling error", ex);
419         } catch (Exception JavaDoc ex) {
420             throw new RemoteException JavaDoc("Error at server", ex);
421         }
422     }
423
424     /**
425          * The instanceof operator.
426          *
427          * <p>
428          * The JavaScript code "lhs instanceof rhs" causes rhs.hasInstance(lhs) to
429          * be called.
430          *
431          * <p>
432          * The return value is implementation dependent so that embedded host objects can
433          * return an appropriate value. See the JS 1.3 language documentation for more
434          * detail.
435          *
436          * <p>This operator corresponds to the proposed EMCA [[HasInstance]] operator.
437          *
438          * @param instance The value that appeared on the LHS of the instanceof
439          * operator
440          *
441          * @return an implementation dependent value
442          */

443     public boolean hasInstance(JsObject instance) throws RemoteException JavaDoc {
444         ResultCell cell;
445         JsObjectStub stub = (JsObjectStub) instance;
446         try {
447             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_HAS_INSTANCE);
448             cell.writeObject(stub);
449             return cell.waitForBooleanValue();
450
451         } catch (IOException ex) {
452             throw new RemoteException JavaDoc("Marshalling error", ex);
453         } catch (Exception JavaDoc ex) {
454             throw new RemoteException JavaDoc("Error at server", ex);
455         }
456     }
457     public boolean isA(int cmd) throws RemoteException JavaDoc {
458         ResultCell cell;
459         try {
460             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,cmd);
461             return cell.waitForBooleanValue();
462
463         } catch (IOException ex) {
464             throw new RemoteException JavaDoc("Marshalling error", ex);
465         } catch (Exception JavaDoc ex) {
466             throw new RemoteException JavaDoc("Error at server", ex);
467         }
468     }
469     public boolean isFunction() throws RemoteException JavaDoc {
470         return isA(DebugConstants.JO_FUNCTION);
471     }
472     public boolean isScript() throws RemoteException JavaDoc {
473         return isA(DebugConstants.JO_SCRIPT);
474     }
475
476     /**
477          * Sets an indexed property in this object.
478          * <p>
479          * The property is specified by an integral index
480          * as defined for <code>get</code>.<p>
481          *
482          * @param index the numeric index for the property
483          * @param value value to set the property to
484          */

485     public void put(int index, Object JavaDoc value) throws RemoteException JavaDoc {
486         ResultCell cell;
487         try {
488             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_PUT_BY_INDEX);
489             cell.writeInt(index);
490             cell.writeObject(value);
491             
492             cell.waitForCompletion();
493
494         } catch (IOException ex) {
495             throw new RemoteException JavaDoc("Marshalling error", ex);
496         } catch (Exception JavaDoc ex) {
497             throw new RemoteException JavaDoc("Error at server", ex);
498         }
499     }
500
501     /**
502     * Sets a named property in this object.
503     * <p>
504     * The property is specified by a string name
505     * as defined for <code>get</code>.
506     * <p>
507     * Note that if a property <i>a</i> is defined in the prototype <i>p</i>
508     * of an object <i>o</i>, then evaluating <code>o.a = 23</code> will cause
509     * <code>set</code> to be called on the prototype <i>p</i> with
510     * <i>o</i> as the <i>start</i> parameter.
511     * To preserve JavaScript semantics, it is the Scriptable
512     * object's responsibility to modify <i>o</i>. <p>
513     * This design allows properties to be defined in prototypes and implemented
514     * in terms of getters and setters of Java values without consuming slots
515     * in each instance.<p>
516     * <p>
517     * The values that may be set are limited to the following:
518     * <UL>
519     * <LI>java.lang.Boolean objects</LI>
520     * <LI>java.lang.String objects</LI>
521     * <LI>java.lang.Number objects</LI>
522     * <LI>org.apache.bsf.debug.jsdi.JsObject</LI>
523     * <LI>null</LI>
524     * <LI>The value returned by Context.getUndefinedValue()</LI>
525     * </UL><p>
526     *
527     * IMPORTANT: JAVA OBJECTS.
528     * The wrapping is not yet supported.
529     */

530     public void put(String JavaDoc name, Object JavaDoc value) throws RemoteException JavaDoc {
531         ResultCell cell;
532         try {
533             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_PUT_BY_NAME);
534             cell.writeObject(name);
535             cell.writeObject(value);
536
537             cell.waitForCompletion();
538         } catch (IOException ex) {
539             throw new RemoteException JavaDoc("Marshalling error", ex);
540         } catch (Exception JavaDoc ex) {
541             throw new RemoteException JavaDoc("Error at server", ex);
542         }
543     }
544
545     /**
546     * Set the prototype of the object.
547     * @param prototype the prototype to set
548     */

549     public void setPrototype(JsObject prototype) throws RemoteException JavaDoc {
550         ResultCell cell;
551         try {
552             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_SET_PROTOTYPE);
553             cell.writeObject(prototype);
554             cell.waitForCompletion();
555             
556         } catch (IOException ex) {
557             throw new RemoteException JavaDoc("Marshalling error", ex);
558         } catch (Exception JavaDoc ex) {
559             throw new RemoteException JavaDoc("Error at server", ex);
560         }
561     }
562
563     /**
564     * Set the prototype of the object.
565     * @param prototype the prototype to set
566     */

567     public void setScope(JsObject scope) throws RemoteException JavaDoc {
568         ResultCell cell;
569         try {
570             cell = m_con.prepareOutgoingInvoke(this,DebugConstants.JS_OBJECT_TID,DebugConstants.JO_SET_SCOPE);
571             cell.writeObject(scope);
572             cell.waitForCompletion();
573             
574         } catch (IOException ex) {
575             throw new RemoteException JavaDoc("Marshalling error", ex);
576         } catch (Exception JavaDoc ex) {
577             throw new RemoteException JavaDoc("Error at server", ex);
578         }
579     }
580 }
581
Popular Tags