KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > program > InterpretedClassDef


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.program;
31
32 import com.caucho.quercus.env.Env;
33 import com.caucho.quercus.env.ObjectValue;
34 import com.caucho.quercus.env.QuercusClass;
35 import com.caucho.quercus.env.Value;
36 import com.caucho.quercus.expr.Expr;
37
38 import java.util.HashMap JavaDoc;
39 import java.util.LinkedHashMap JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42
43 /**
44  * Represents an interpreted PHP class definition.
45  */

46 public class InterpretedClassDef extends ClassDef
47   implements InstanceInitializer
48 {
49   protected boolean _isAbstract;
50   protected boolean _isInterface;
51   
52   protected final HashMap JavaDoc<String JavaDoc,AbstractFunction> _functionMap
53     = new HashMap JavaDoc<String JavaDoc,AbstractFunction>();
54
55   protected final HashMap JavaDoc<String JavaDoc,Expr> _fieldMap
56     = new LinkedHashMap JavaDoc<String JavaDoc,Expr>();
57
58   protected final HashMap JavaDoc<String JavaDoc,Expr> _staticFieldMap
59     = new LinkedHashMap JavaDoc<String JavaDoc,Expr>();
60
61   protected final HashMap JavaDoc<String JavaDoc,Expr> _constMap
62     = new HashMap JavaDoc<String JavaDoc,Expr>();
63
64   protected AbstractFunction _constructor;
65   protected AbstractFunction _destructor;
66   protected AbstractFunction _getField;
67   protected AbstractFunction _setField;
68   protected AbstractFunction _call;
69
70   public InterpretedClassDef(String JavaDoc name,
71                  String JavaDoc parentName,
72                  String JavaDoc []ifaceList)
73   {
74     super(name, parentName, ifaceList);
75   }
76
77   /**
78    * true for an abstract class.
79    */

80   public void setAbstract(boolean isAbstract)
81   {
82     _isAbstract = isAbstract;
83   }
84
85   /**
86    * True for an abstract class.
87    */

88   public boolean isAbstract()
89   {
90     return _isAbstract;
91   }
92
93   /**
94    * true for an interface class.
95    */

96   public void setInterface(boolean isInterface)
97   {
98     _isInterface = isInterface;
99   }
100
101   /**
102    * True for an interface class.
103    */

104   public boolean isInterface()
105   {
106     return _isInterface;
107   }
108
109   /**
110    * Initialize the quercus class.
111    */

112   public void initClass(QuercusClass cl)
113   {
114     if (_constructor != null)
115       cl.setConstructor(_constructor);
116     
117     if (_getField != null)
118       cl.setGet(_getField);
119     
120     if (_setField != null)
121       cl.setSet(_setField);
122     
123     if (_call != null)
124       cl.setCall(_call);
125     
126     cl.addInitializer(this);
127     
128     for (Map.Entry JavaDoc<String JavaDoc,AbstractFunction> entry : _functionMap.entrySet()) {
129       cl.addMethod(entry.getKey(), entry.getValue());
130     }
131     
132     for (Map.Entry JavaDoc<String JavaDoc,Expr> entry : _fieldMap.entrySet()) {
133       cl.addField(entry.getKey(), 0, entry.getValue());
134     }
135
136     for (Map.Entry JavaDoc<String JavaDoc,Expr> entry : _staticFieldMap.entrySet()) {
137       cl.addStaticField(entry.getKey(), entry.getValue());
138     }
139
140     for (Map.Entry JavaDoc<String JavaDoc,Expr> entry : _constMap.entrySet()) {
141       cl.addConstant(entry.getKey(), entry.getValue());
142     }
143   }
144
145   /**
146    * Sets the constructor.
147    */

148   public void setConstructor(AbstractFunction fun)
149   {
150     _constructor = fun;
151   }
152   
153   /**
154    * Adds a function.
155    */

156   public void addFunction(String JavaDoc name, Function fun)
157   {
158     _functionMap.put(name.intern(), fun);
159
160     if (name.equals("__construct"))
161       _constructor = fun;
162     else if (name.equals("__destruct"))
163       _destructor = fun;
164     else if (name.equals("__get"))
165       _getField = fun;
166     else if (name.equals("__set"))
167       _setField = fun;
168     else if (name.equals("__call"))
169       _call = fun;
170     else if (name.equalsIgnoreCase(getName()) && _constructor == null)
171       _constructor = fun;
172   }
173
174   /**
175    * Adds a static value.
176    */

177   public void addStaticValue(Value name, Expr value)
178   {
179     _staticFieldMap.put(getName() + "::" + name.toString(), value);
180   }
181
182   /**
183    * Adds a const value.
184    */

185   public void addConstant(String JavaDoc name, Expr value)
186   {
187     _constMap.put(name.intern(), value);
188   }
189
190   /**
191    * Return a const value.
192    */

193   public Expr findConstant(String JavaDoc name)
194   {
195     return _constMap.get(name);
196   }
197
198   /**
199    * Adds a value.
200    */

201   public void addValue(Value name, Expr value)
202   {
203     _fieldMap.put(name.toString().intern(), value);
204   }
205
206   /**
207    * Adds a value.
208    */

209   public Expr get(Value name)
210   {
211     return _fieldMap.get(name.toString().intern());
212   }
213
214   /**
215    * Return true for a declared field.
216    */

217   public boolean isDeclaredField(String JavaDoc name)
218   {
219     return _fieldMap.get(name) != null;
220   }
221
222   /**
223    * Initialize the class.
224    */

225   public void init(Env env)
226   {
227     for (Map.Entry JavaDoc<String JavaDoc,Expr> var : _staticFieldMap.entrySet()) {
228       String JavaDoc name = getName() + "::" + var.getKey();
229
230       env.setGlobalValue(name.intern(), var.getValue().eval(env).copy());
231     }
232   }
233
234   /**
235    * Initialize the fields
236    */

237   public void initInstance(Env env, Value value)
238   {
239     ObjectValue object = (ObjectValue) value;
240     
241     for (Map.Entry JavaDoc<String JavaDoc,Expr> entry : _fieldMap.entrySet())
242       object.putFieldInit(env, entry.getKey(), entry.getValue().eval(env).copy());
243   }
244
245   /**
246    * Returns the constructor
247    */

248   public AbstractFunction findConstructor()
249   {
250     return _constructor;
251   }
252
253   public String JavaDoc toString()
254   {
255     return "Class[" + getName() + "]";
256   }
257   
258   public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc, Expr>> fieldSet()
259   {
260     return _fieldMap.entrySet();
261   }
262   
263   public Set JavaDoc<Map.Entry JavaDoc<String JavaDoc, AbstractFunction>> functionSet()
264   {
265     return _functionMap.entrySet();
266   }
267 }
268
269
Popular Tags