KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > ESBeanWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 import com.caucho.util.IntMap;
32
33 import java.util.HashMap JavaDoc;
34
35 /**
36  * Implementation class serving as the base for wrapped Java objects.
37  */

38 public class ESBeanWrapper extends ESBase {
39   static ESId CALL = ESId.intern("call");
40   protected static ESId LENGTH = ESId.intern("length");
41
42   public int set;
43   public IntMap hasDispatch;
44   public IntMap setDispatch;
45   public IntMap []subGets;
46   public IntMap []subSets;
47   public HashMap JavaDoc methods;
48   public IntMap methodDispatch;
49
50   protected Object JavaDoc value;
51   protected String JavaDoc name;
52   ESString []formals;
53   int length;
54   public int n = -3;
55   int newN;
56
57   protected ESBeanWrapper()
58   {
59   }
60
61   public long getVersionId()
62   {
63     return 0;
64   }
65
66   private String JavaDoc decompile()
67   {
68     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
69
70     sbuf.append("function ");
71     sbuf.append(name);
72     sbuf.append("(");
73     for (int i = 0; formals != null && i < formals.length; i++) {
74       if (i != 0)
75     sbuf.append(", ");
76       sbuf.append(formals[i]);
77     }
78       
79     sbuf.append(") ");
80
81     sbuf.append("{ ");
82     sbuf.append("[native code]");
83     sbuf.append(" }");
84
85     return sbuf.toString();
86   }
87
88   public ESString toStr() throws ESException
89   {
90     if (n == -1)
91       return ESString.create(value == null ? "null" : value.toString());
92     else
93       return ESString.create(decompile());
94   }
95
96   public double toNum() throws ESException
97   {
98     if (value instanceof Number JavaDoc)
99       return ((Number JavaDoc) value).doubleValue();
100     else
101       throw new ESException("no number: " + getClass().getName());
102   }
103
104   public ESBase getProperty(ESString name) throws Throwable JavaDoc
105   {
106     ESBase value = hasProperty(name);
107
108     if (value != null)
109       return value;
110     else
111       return esEmpty;
112   }
113
114   public ESBase hasProperty(ESString name) throws Throwable JavaDoc
115   {
116     return null;
117   }
118
119   public ESString toSource(IntMap map, boolean isLoopPath) throws ESException
120   {
121     if (isLoopPath)
122       return null;
123     else
124       return toStr();
125   }
126
127   public ESBase toPrimitive(int hint) throws ESException
128   {
129     if (value instanceof ESBase)
130       return (ESBase) value;
131     else
132       return toStr();
133   }
134
135   public Object JavaDoc toJavaObject()
136   {
137     return value != null ? value : this;
138   }
139
140   public boolean toBoolean()
141   {
142     return true;
143   }
144   
145   protected ESBeanWrapper dup()
146   {
147     throw new UnsupportedOperationException JavaDoc();
148   }
149
150   protected ESBeanWrapper dup(int set)
151   {
152     ESBeanWrapper child = dup();
153
154     child.value = value;
155     child.set = set;
156     child.hasDispatch = subGets[set];
157     child.setDispatch = subSets[set];
158     child.subGets = subGets;
159     child.subSets = subSets;
160     child.methods = methods;
161
162     return child;
163   }
164
165   public ESBeanWrapper wrap(Object JavaDoc value)
166   {
167     throw new RuntimeException JavaDoc();
168   }
169
170   public ESBeanWrapper wrapStatic()
171   {
172     throw new RuntimeException JavaDoc();
173   }
174
175   public boolean ecmaEquals(ESBase b)
176   {
177     if (! (b instanceof ESBeanWrapper))
178       return false;
179     else
180       return value.equals(((ESBeanWrapper) b).value);
181   }
182
183   public Object JavaDoc copy(HashMap JavaDoc refs)
184   {
185     return this;
186   }
187
188   public ESBase typeof()
189   {
190     return ESString.create("object");
191   }
192
193   public ESBase call(Call eval, int length, int n) throws Throwable JavaDoc
194   {
195     throw new ESNullException(toStr() + " is not a function");
196   }
197
198   public ESBase call(Call eval, int length) throws Throwable JavaDoc
199   {
200     return call(eval, length, n);
201   }
202
203   public ESBase call(Call eval, int length, ESString key) throws Throwable JavaDoc
204   {
205     int n = methodDispatch.get(key);
206     if (n < 0)
207       throw new ESUndefinedException(getClass().getName() + ": undefined call `" + key + "'");
208
209     return call(eval, length, n);
210   }
211
212   public ESBase construct(Call eval, int length) throws Throwable JavaDoc
213   {
214     if (n != newN) {
215       throw new ESException("cannot create " + name);
216     }
217
218     ESBase value = call(eval, length);
219
220     if (value == esUndefined || value == null)
221       throw new ESException("cannot create " + name);
222
223     return value;
224   }
225
226   public boolean isModified()
227   {
228     return true;
229   }
230 }
231
Popular Tags