KickJava   Java API By Example, From Geeks To Geeks.

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


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.Quercus;
33 import com.caucho.quercus.env.Env;
34 import com.caucho.quercus.env.JavaMethod;
35 import com.caucho.quercus.env.NullValue;
36 import com.caucho.quercus.env.ObjectExtValue;
37 import com.caucho.quercus.env.QuercusClass;
38 import com.caucho.quercus.env.Value;
39 import com.caucho.quercus.expr.Expr;
40 import com.caucho.quercus.expr.LiteralExpr;
41 import com.caucho.quercus.function.Marshal;
42 import com.caucho.quercus.function.MarshalFactory;
43 import com.caucho.quercus.module.ModuleContext;
44 import com.caucho.util.L10N;
45
46 import java.lang.reflect.Field JavaDoc;
47 import java.lang.reflect.Method JavaDoc;
48 import java.lang.reflect.Modifier JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.logging.Level JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53
54 /**
55  * Represents an introspected Java class.
56  */

57 public class JavaImplClassDef extends ClassDef {
58   private final static Logger JavaDoc log
59     = Logger.getLogger(JavaImplClassDef.class.getName());
60   private final static L10N L = new L10N(JavaImplClassDef.class);
61
62   private final ModuleContext _moduleContext;
63
64   private final String JavaDoc _name;
65   private final Class JavaDoc _type;
66
67   private final HashMap JavaDoc<String JavaDoc, Expr> _constMap
68     = new HashMap JavaDoc<String JavaDoc, Expr>();
69
70   private final HashMap JavaDoc<String JavaDoc, JavaMethod> _functionMap
71     = new HashMap JavaDoc<String JavaDoc, JavaMethod>();
72
73   private JavaMethod _cons;
74
75   public JavaImplClassDef(ModuleContext moduleContext,
76                String JavaDoc name,
77                Class JavaDoc type)
78   {
79     super(name, null, new String JavaDoc[0]);
80
81     _moduleContext = moduleContext;
82
83     _name = name;
84
85     _type = type;
86   }
87
88   /**
89    * Returns the class name.
90    */

91   public String JavaDoc getName()
92   {
93     return _name;
94   }
95
96   public Class JavaDoc getType()
97   {
98     return _type;
99   }
100
101   /**
102    * Creates a new instance.
103    */

104   public Value newInstance(Env env, QuercusClass qClass)
105   {
106     return new ObjectExtValue(qClass);
107   }
108
109   /**
110    * Eval a method
111    */

112   public Value callMethod(Env env, Value obj, String JavaDoc name, Expr []args)
113   {
114     JavaMethod method = _functionMap.get(name);
115
116     if (method == null) {
117       env.warning(env.getLocation().getMessagePrefix() + L.l("{0}::{1} is an unknown method.",
118                                           _name, name));
119
120       return NullValue.NULL;
121     }
122
123     return method.call(env, obj, args);
124   }
125
126   /**
127    * Eval a method
128    */

129   public Value callMethod(Env env, Value obj, String JavaDoc name, Value []args)
130   {
131     return getMethod(env, name).call(env, obj, args);
132   }
133
134   /**
135    * Eval a method
136    */

137   public Value callMethod(Env env, Object JavaDoc obj, String JavaDoc name)
138   {
139     return getMethod(env, name).call(env, obj);
140   }
141
142   /**
143    * Eval a method
144    */

145   public Value callMethod(Env env, Object JavaDoc obj, String JavaDoc name, Value a1)
146   {
147     return getMethod(env, name).call(env, obj, a1);
148   }
149
150   /**
151    * Eval a method
152    */

153   public Value callMethod(Env env, Object JavaDoc obj, String JavaDoc name,
154                           Value a1, Value a2)
155   {
156     return getMethod(env, name).call(env, obj, a1, a2);
157   }
158
159   /**
160    * Eval a method
161    */

162   public Value callMethod(Env env, Object JavaDoc obj, String JavaDoc name,
163                           Value a1, Value a2, Value a3)
164   {
165     return getMethod(env, name).call(env, obj, a1, a2, a3);
166   }
167
168   /**
169    * Eval a method
170    */

171   public Value callMethod(Env env, Object JavaDoc obj, String JavaDoc name,
172                           Value a1, Value a2, Value a3, Value a4)
173   {
174     return getMethod(env, name).call(env, obj, a1, a2, a3, a4);
175   }
176
177   /**
178    * Eval a method
179    */

180   public Value callMethod(Env env, Object JavaDoc obj, String JavaDoc name,
181                           Value a1, Value a2, Value a3, Value a4, Value a5)
182   {
183     return getMethod(env, name).call(env, obj, a1, a2, a3, a4, a5);
184   }
185
186   private JavaMethod getMethod(Env env, String JavaDoc name)
187   {
188     JavaMethod method = _functionMap.get(name);
189
190     if (method == null) {
191       env.error("'" + name + "' is an unknown method.");
192     }
193
194     return method;
195   }
196
197   /**
198    * Initialize the quercus class.
199    */

200   public void initClass(QuercusClass cl)
201   {
202     if (_cons != null)
203       cl.setConstructor(_cons);
204
205     for (Map.Entry JavaDoc<String JavaDoc,JavaMethod> entry : _functionMap.entrySet()) {
206       cl.addMethod(entry.getKey(), entry.getValue());
207     }
208
209     for (Map.Entry JavaDoc<String JavaDoc,Expr> entry : _constMap.entrySet()) {
210       cl.addConstant(entry.getKey(), entry.getValue());
211     }
212   }
213
214   /**
215    * Creates a new instance.
216    */

217   public void initInstance(Env env, Value value)
218   {
219   }
220
221   /**
222    * Eval new
223    */

224   @Override JavaDoc
225   public Value callNew(Env env, Expr []args)
226   {
227     return null;
228   }
229
230   /**
231    * Eval new
232    */

233   @Override JavaDoc
234   public Value callNew(Env env, Value []args)
235   {
236     return null;
237   }
238
239   /**
240    * Returns the constructor
241    */

242   public AbstractFunction findConstructor()
243   {
244     return null;
245   }
246
247   /**
248    * Introspects the Java class.
249    */

250   public void introspect(ModuleContext moduleContext)
251   {
252     introspectConstants(_type);
253     introspectMethods(moduleContext, _type);
254     introspectFields(moduleContext, _type);
255   }
256
257   private Method getConsMethod(Class JavaDoc type)
258   {
259     Method []methods = type.getMethods();
260
261     for (int i = 0; i < methods.length; i++) {
262       Method method = methods[i];
263       
264       if (! method.getName().equals("__construct"))
265     continue;
266       if (! Modifier.isStatic(method.getModifiers()))
267     continue;
268       if (! Modifier.isPublic(method.getModifiers()))
269     continue;
270
271       return method;
272     }
273
274     return null;
275   }
276
277   /**
278    * Introspects the Java class.
279    */

280   private void introspectFields(ModuleContext moduleContext, Class JavaDoc type)
281   {
282     MarshalFactory marshalFactory = moduleContext.getMarshalFactory();
283     
284     // Introspect public non-static fields
285
Field JavaDoc[] fields = type.getFields();
286
287     for (Field JavaDoc field : fields) {
288       if (Modifier.isStatic(field.getModifiers()))
289         continue;
290
291       Marshal marshal = marshalFactory.create(field.getType(), false);
292       
293       //_fieldMap.put(field.getName(), new FieldMarshalPair(field, marshal));
294
}
295
296
297    // introspectFields(quercus, type.getSuperclass());
298
}
299
300   /**
301    * Introspects the Java class.
302    */

303   private void introspectConstants(Class JavaDoc type)
304   {
305     if (type == null || type.equals(Object JavaDoc.class))
306       return;
307
308     if (! Modifier.isPublic(type.getModifiers()))
309       return;
310
311     Class JavaDoc []ifcs = type.getInterfaces();
312
313     for (Class JavaDoc ifc : ifcs) {
314       introspectConstants(ifc);
315     }
316
317     Field JavaDoc []fields = type.getDeclaredFields();
318
319     for (Field JavaDoc field : fields) {
320       if (_constMap.get(field.getName()) != null)
321         continue;
322       else if (! Modifier.isPublic(field.getModifiers()))
323         continue;
324       else if (! Modifier.isStatic(field.getModifiers()))
325         continue;
326       else if (! Modifier.isFinal(field.getModifiers()))
327         continue;
328
329       try {
330         Value value = Quercus.objectToValue(field.get(null));
331
332         if (value != null)
333           _constMap.put(field.getName().intern(), new LiteralExpr(value));
334       } catch (Throwable JavaDoc e) {
335         log.log(Level.FINER, e.toString(), e);
336       }
337     }
338
339     introspectConstants(type.getSuperclass());
340   }
341
342   /**
343    * Introspects the Java class.
344    */

345   private void introspectMethods(ModuleContext moduleContext, Class JavaDoc type)
346   {
347     if (type == null || type.equals(Object JavaDoc.class))
348       return;
349
350     Class JavaDoc []ifcs = type.getInterfaces();
351
352     for (Class JavaDoc ifc : ifcs) {
353       introspectMethods(moduleContext, ifc);
354     }
355
356     Method []methods = type.getDeclaredMethods();
357
358     for (Method method : methods) {
359       if (_functionMap.get(method.getName()) != null)
360         continue;
361       else if (! Modifier.isPublic(method.getModifiers()))
362         continue;
363       else if (! Modifier.isStatic(method.getModifiers()))
364         continue;
365
366       JavaMethod javaMethod = new JavaMethod(moduleContext, method);
367
368       if (method.getName().equals("__construct"))
369     _cons = javaMethod;
370
371       _functionMap.put(method.getName(), javaMethod);
372     }
373
374     introspectMethods(moduleContext, type.getSuperclass());
375   }
376
377   private class FieldMarshalPair {
378     public final Field JavaDoc _field;
379     public final Marshal _marshal;
380
381     public FieldMarshalPair(Field JavaDoc field,
382                 Marshal marshal)
383     {
384       _field = field;
385       _marshal = marshal;
386     }
387   }
388 }
389
390
Popular Tags