KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > lang > Cast


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.lang;
17
18 import com.google.gwt.core.client.JavaScriptObject;
19
20 // CHECKSTYLE_NAMING_OFF: Uses legacy conventions of underscore prefixes.
21

22 /**
23  * This is a magic class the compiler uses to perform any cast operations that
24  * require code.
25  */

26 final class Cast {
27
28   // magic magic magic
29
protected static Object JavaDoc typeIdArray;
30
31   protected static native boolean canCast(int srcId, int dstId) /*-{
32     // either a null or 0 both will be false, short circuit
33     if (!srcId)
34       return false;
35     
36     // force to boolean
37     return !!@com.google.gwt.lang.Cast::typeIdArray[srcId][dstId];
38   }-*/
;
39
40   static native String JavaDoc charToString(char x) /*-{
41     return String.fromCharCode(x);
42   }-*/
;
43
44   static native Object JavaDoc dynamicCast(Object JavaDoc src, int dstId) /*-{
45     if (src != null)
46       @com.google.gwt.lang.Cast::canCast(II)(src.@java.lang.Object::typeId,dstId)
47       || @com.google.gwt.lang.Cast::throwClassCastException()();
48
49     return src;
50   }-*/
;
51
52   static native boolean instanceOf(Object JavaDoc src, int dstId) /*-{
53     if (src==null)
54       return false;
55
56     return @com.google.gwt.lang.Cast::canCast(II)(src.@java.lang.Object::typeId,dstId);
57   }-*/
;
58
59   /**
60    * See JLS 5.1.3.
61    */

62   static native byte narrow_byte(Object JavaDoc x) /*-{
63     return x << 24 >> 24;
64   }-*/
;
65
66   /**
67    * See JLS 5.1.3.
68    */

69   static native char narrow_char(Object JavaDoc x) /*-{
70     return x & 0xFFFF;
71   }-*/
;
72
73   /**
74    * See JLS 5.1.3.
75    */

76   static native int narrow_int(Object JavaDoc x) /*-{
77     return ~~x;
78   }-*/
;
79
80   /**
81    * See JLS 5.1.3.
82    */

83   static native short narrow_short(Object JavaDoc x) /*-{
84     return x << 16 >> 16;
85   }-*/
;
86
87   /**
88    * See JLS 5.1.3 for why we do a two-step cast. First we rount to int, then
89    * narrow to byte.
90    */

91   static byte round_byte(Object JavaDoc x) {
92     return narrow_byte(floatToInt(x));
93   }
94
95   /**
96    * See JLS 5.1.3 for why we do a two-step cast. First we rount to int, then
97    * narrow to char.
98    */

99   static char round_char(Object JavaDoc x) {
100     return narrow_char(floatToInt(x));
101   }
102
103   /**
104    * See JLS 5.1.3.
105    */

106   static native int round_int(Object JavaDoc x) /*-{
107     if (x > @java.lang.Integer::MAX_VALUE) return @java.lang.Integer::MAX_VALUE;
108     if (x < @java.lang.Integer::MIN_VALUE) return @java.lang.Integer::MIN_VALUE;
109     return x >= 0 ? Math.floor(x) : Math.ceil(x);
110   }-*/
;
111
112   /**
113    * See JLS 5.1.3.
114    */

115   static native long round_long(Object JavaDoc x) /*-{
116     if (x > @java.lang.Long::MAX_VALUE) return @java.lang.Long::MAX_VALUE;
117     if (x < @java.lang.Long::MIN_VALUE) return @java.lang.Long::MIN_VALUE;
118     return x >= 0 ? Math.floor(x) : Math.ceil(x);
119   }-*/
;
120
121   /**
122    * See JLS 5.1.3 for why we do a two-step cast. First we rount to int, then
123    * narrow to short.
124    */

125   static short round_short(Object JavaDoc x) {
126     return narrow_short(floatToInt(x));
127   }
128
129   /**
130    * Unconditionally throw a {@link ClassCastException}. Called from {#link
131    * {@link #dynamicCast(Object, int)}.
132    */

133   static Object JavaDoc throwClassCastException() throws ClassCastException JavaDoc {
134     throw new ClassCastException JavaDoc();
135   }
136
137   /**
138    * Check a statically false cast, which can succeed if the argument is null.
139    * Called by compiler-generated code based on static type information.
140    */

141   static Object JavaDoc throwClassCastExceptionUnlessNull(Object JavaDoc o)
142       throws ClassCastException JavaDoc {
143     if (o != null) {
144       throw new ClassCastException JavaDoc();
145     }
146     return o;
147   }
148
149   static native JavaScriptObject wrapJSO(JavaScriptObject jso, Object JavaDoc seed) /*-{
150     _ = seed.prototype;
151
152     // WEIRD: The inequality below represents the fact that superclasses always
153     // have typeId < any subclass typeId. This code lets us wrap the same JSO
154     // "tighter" but never "looser". This would break if the compiler did not
155     // ensure that superclass ids are less than subclass ids.
156     //
157     // Note also that the inequality is false (and thus allows wrapping) if
158     // jso's typeId is undefined, because (undefined < positive int).
159
160     if (jso && !(jso.@java.lang.Object::typeId >= _.@java.lang.Object::typeId)) {
161       // don't clobber toString
162       var oldToString = jso.toString;
163       for (var i in _) {
164         jso[i] = _[i];
165       }
166       jso.toString = oldToString;
167     }
168     return jso;
169   }-*/
;
170
171   /**
172    * See JLS 5.1.3.
173    */

174   private static native Object JavaDoc floatToInt(Object JavaDoc x) /*-{
175     if (x > @java.lang.Integer::MAX_VALUE) return @java.lang.Integer::MAX_VALUE;
176     if (x < @java.lang.Integer::MIN_VALUE) return @java.lang.Integer::MIN_VALUE;
177     return x >= 0 ? Math.floor(x) : Math.ceil(x);
178   }-*/
;
179
180 }
181
182 // CHECKSTYLE_NAMING_ON
183
Popular Tags