KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > ClassesModule


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.lib;
31
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.annotation.ReadOnly;
34 import com.caucho.quercus.env.*;
35 import com.caucho.quercus.expr.Expr;
36 import com.caucho.quercus.module.AbstractQuercusModule;
37 import com.caucho.quercus.program.AbstractFunction;
38 import com.caucho.util.L10N;
39
40 import java.util.Map JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Quercus class information
46  */

47 public class ClassesModule extends AbstractQuercusModule {
48   private static final L10N L = new L10N(ClassesModule.class);
49   private static final Logger JavaDoc log
50     = Logger.getLogger(ClassesModule.class.getName());
51
52   /**
53    * Returns true if the class exists.
54    */

55   public boolean class_exists(Env env,
56                               String JavaDoc className,
57                               @Optional("true") boolean useAutoload)
58   {
59     return env.findClass(className, useAutoload) != null;
60   }
61
62   /**
63    * Returns the object's class name
64    */

65   public Value get_class(Value value)
66   {
67     if (value instanceof ObjectValue) {
68       ObjectValue obj = (ObjectValue) value;
69
70       return new StringValueImpl(obj.getName());
71     }
72     else if (value instanceof JavaValue) {
73       JavaValue obj = (JavaValue) value;
74
75       return new StringValueImpl(obj.getClassName());
76     }
77     else
78       return BooleanValue.FALSE;
79   }
80
81   /**
82    * Returns an array of method names and values
83    *
84    * @param className the name of the class
85    *
86    * @return an array of method names and values
87    *
88    * @throws errorException
89    */

90   public static Value get_class_methods(Env env, String JavaDoc className)
91   {
92     // php/1j11
93

94     QuercusClass cl = null;
95
96     try {
97       cl = env.getClass(className);
98     }
99     catch (Exception JavaDoc t) {
100       log.log(Level.WARNING, t.toString(), t);
101
102       return NullValue.NULL;
103     }
104
105     if (cl == null)
106       return null;
107
108     ArrayValue methArray = new ArrayValueImpl();
109
110     for (Map.Entry JavaDoc<String JavaDoc, AbstractFunction> entry: cl.getClassMethods()) {
111       Value key = StringValue.create(entry.getKey());
112
113       methArray.append(key);
114     }
115
116     return methArray;
117   }
118
119   /**
120    * Returns an array of member names and values
121    *
122    * @param className the name of the class
123    *
124    * @return an array of member names and values
125    */

126   public static Value get_class_vars(Env env, String JavaDoc className)
127   {
128     // php/1j10
129

130     QuercusClass cl = null;
131
132     try {
133       cl = env.getClass(className);
134     } catch (Exception JavaDoc t) {
135       log.log(Level.WARNING, t.toString(), t);
136
137       return NullValue.NULL;
138     }
139
140     if (cl == null)
141       return null;
142
143     ArrayValue varArray = new ArrayValueImpl();
144
145     for (Map.Entry JavaDoc<StringValue,Expr> entry : cl.getClassVars().entrySet()) {
146       Value key = entry.getKey();
147       Value value = entry.getValue().eval(env);
148
149       varArray.append(key, value);
150     }
151
152     return varArray;
153   }
154
155   /**
156    * Returns the declared classes
157    */

158   public static Value get_declared_classes(Env env)
159   {
160     return env.getDeclaredClasses();
161   }
162
163   /**
164    * Returns the object's variables
165    */

166   public static Value get_object_vars(Env env, Value obj)
167   {
168     //
169
ArrayValue result = new ArrayValueImpl();
170
171     for (Value name : obj.getIndices()) {
172       result.put(name, obj.getField(env, name.toString().intern()));
173     }
174
175     return result;
176   }
177
178   /**
179    * Returns the object's class name
180    */

181   public Value get_parent_class(Env env, @ReadOnly Value value)
182   {
183     if (value instanceof ObjectValue) {
184       ObjectValue obj = (ObjectValue) value;
185
186       String JavaDoc parent = obj.getParentName();
187
188       if (parent != null)
189         return new StringValueImpl(parent);
190     }
191     else if (value instanceof StringValue) {
192       String JavaDoc className = value.toString();
193
194       QuercusClass cl = env.findClass(className);
195
196       if (cl != null) {
197         String JavaDoc parent = cl.getParentName();
198
199         if (parent != null)
200           return new StringValueImpl(parent);
201       }
202     }
203
204     return BooleanValue.FALSE;
205   }
206
207   /**
208    * Returns true if the object implements the given class.
209    */

210   public static boolean is_a(@ReadOnly Value value, String JavaDoc name)
211   {
212     return value.isA(name);
213   }
214
215   /**
216    * Returns true if the argument is an object.
217    */

218   public static boolean is_object(@ReadOnly Value value)
219   {
220     return value instanceof ObjectValue;
221   }
222
223   /**
224    * Returns true if the object implements the given class.
225    */

226   public static boolean is_subclass_of(Env env,
227                                        @ReadOnly Value value,
228                                        String JavaDoc name)
229   {
230     QuercusClass cl;
231
232     if (value instanceof StringValue)
233       cl = env.findClass(value.toString());
234     else
235       cl = value.getQuercusClass();
236
237     if (cl == null || cl.getName().equalsIgnoreCase(name))
238       return false;
239     else
240       return cl.isA(name);
241   }
242
243   /**
244    * Returns true if the named method exists on the object.
245    *
246    * @param obj the object to test
247    * @param methodName the name of the method
248    */

249   public static boolean method_exists(Value obj, String JavaDoc methodName)
250   {
251     return obj.findFunction(methodName.intern()) != null;
252   }
253 }
254
Popular Tags