KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > core > client > JavaScriptObject


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.core.client;
17
18 /**
19  * An opaque handle to a native JavaScript object. A
20  * <code>JavaScriptObject</code> cannot be created directly.
21  * <code>JavaScriptObject</code> should be declared as the return type of a
22  * JSNI method that returns native (non-Java) objects. A
23  * <code>JavaScriptObject</code> passed back into JSNI from Java becomes the
24  * original object, and can be accessed in JavaScript as expected.
25  *
26  * <p>
27  * <b>SUBCLASSING IS NOT SUPPORTED EXCEPT FOR THE EXISTING SUBCLASSES.</b>
28  * </p>
29  */

30 public class JavaScriptObject {
31
32   private static native boolean equalsImpl(JavaScriptObject o,
33       JavaScriptObject other) /*-{
34     return o === other;
35   }-*/
;
36
37   private static native String JavaDoc toStringImpl(JavaScriptObject o) /*-{
38     if (o.toString)
39       return o.toString();
40     return "[object]";
41   }-*/
;
42
43   /**
44    * The underlying JavaScript object. This is used internally and should never
45    * be accessed by client code.
46    */

47   protected Object JavaDoc hostedModeReference;
48
49   /**
50    * Not directly instantiable. Subclasses should also define a protected
51    * no-arg constructor to prevent client code from directly instantiating
52    * the class.
53    */

54   protected JavaScriptObject() {
55   };
56
57   public boolean equals(Object JavaDoc other) {
58     if (!(other instanceof JavaScriptObject)) {
59       return false;
60     }
61     return equalsImpl(this, (JavaScriptObject) other);
62   }
63
64   public int hashCode() {
65     return Impl.getHashCode(this);
66   }
67   
68   public String JavaDoc toString() {
69     /*
70      * Hosted mode will marshal an explicit argument from a JavaScriptObject
71      * back to its underlying object, but it won't currently do that for the
72      * implicit "this" arg. For now, can't implement instance methods on JSO
73      * directly as natives, so use a delegator.
74      */

75     return toStringImpl(this);
76   }
77 }
78
Popular Tags