PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/quercus/src/com/caucho/quercus/lib/ClassesModule.java

https://github.com/GEFFROY/Quercus
Java | 372 lines | 211 code | 64 blank | 97 comment | 55 complexity | 12eb654d17d11801e6003ec988c1b0fb MD5 | raw file
  1. /*
  2. * Copyright (c) 1998-2010 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. package com.caucho.quercus.lib;
  30. import com.caucho.quercus.annotation.Optional;
  31. import com.caucho.quercus.annotation.ReadOnly;
  32. import com.caucho.quercus.annotation.ReturnNullAsFalse;
  33. import com.caucho.quercus.env.*;
  34. import com.caucho.quercus.expr.Expr;
  35. import com.caucho.quercus.module.AbstractQuercusModule;
  36. import com.caucho.quercus.function.AbstractFunction;
  37. import com.caucho.util.L10N;
  38. import java.util.HashSet;
  39. import java.util.Iterator;
  40. import java.util.Map;
  41. import java.util.Set;
  42. import java.util.TreeSet;
  43. import java.util.logging.Logger;
  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 log
  50. = Logger.getLogger(ClassesModule.class.getName());
  51. /**
  52. * Calls an object method.
  53. */
  54. public static Value call_user_method(Env env,
  55. StringValue name,
  56. Value obj,
  57. Value []args)
  58. {
  59. if (obj.isObject()) {
  60. return obj.callMethod(env, name, args);
  61. }
  62. else {
  63. QuercusClass cls = env.findClass(obj.toString());
  64. return cls.callMethod(
  65. env, env.getThis(), name, name.hashCode(), args).copyReturn();
  66. }
  67. }
  68. /*
  69. * Calls a object method with arguments in an array.
  70. */
  71. public static Value call_user_method_array(Env env,
  72. StringValue methodName,
  73. Value obj,
  74. ArrayValue params)
  75. {
  76. Value []args = params.valuesToArray();
  77. return call_user_method(env, methodName, obj, args);
  78. }
  79. /**
  80. * returns true if the class exists.
  81. */
  82. public boolean class_exists(Env env,
  83. String className,
  84. @Optional("true") boolean useAutoload)
  85. {
  86. if (className == null)
  87. return false;
  88. QuercusClass cl = env.findClass(className, useAutoload, true);
  89. // php/[03]9m1
  90. return cl != null && ! cl.isInterface();
  91. }
  92. /**
  93. * Returns the object's class name
  94. */
  95. public Value get_class(Env env, Value value)
  96. {
  97. if (value instanceof ObjectValue) {
  98. ObjectValue obj = (ObjectValue) value;
  99. return env.createString(obj.getName());
  100. }
  101. else if (value instanceof JavaValue) {
  102. JavaValue obj = (JavaValue) value;
  103. return env.createString(obj.getClassName());
  104. }
  105. else
  106. return BooleanValue.FALSE;
  107. }
  108. /*
  109. * Returns the calling class name.
  110. */
  111. @ReturnNullAsFalse
  112. public String get_called_class(Env env)
  113. {
  114. Value qThis = env.getThis();
  115. if (qThis == null || qThis.getQuercusClass() == null) {
  116. env.warning("get_called_class was not called from a class method");
  117. return null;
  118. }
  119. return qThis.getQuercusClass().getName();
  120. }
  121. /**
  122. * Returns an array of method names
  123. *
  124. * @param clss the name of the class, or an instance of a class
  125. *
  126. * @return an array of method names
  127. */
  128. public static Value get_class_methods(Env env, Value cls)
  129. {
  130. // php/1j11
  131. QuercusClass cl;
  132. if (cls.isObject())
  133. cl = ((ObjectValue) cls).getQuercusClass();
  134. else
  135. cl = env.findClass(cls.toString());
  136. if (cl == null)
  137. return NullValue.NULL;
  138. ArrayValue array = new ArrayValueImpl();
  139. HashSet<String> set = new HashSet<String>();
  140. // to combine __construct and class name constructors
  141. for (AbstractFunction fun : cl.getClassMethods()) {
  142. if (fun.isPublic())
  143. set.add(fun.getName());
  144. }
  145. for (String name : set) {
  146. array.put(name);
  147. }
  148. return array;
  149. }
  150. /**
  151. * Returns an array of member names and values
  152. *
  153. * @param clss the name of the class, or an instance of a class
  154. *
  155. * @return an array of member names and values
  156. */
  157. public static Value get_class_vars(Env env, Value obj)
  158. {
  159. // php/1j10
  160. QuercusClass cl;
  161. if (obj instanceof ObjectValue)
  162. cl = ((ObjectValue) obj).getQuercusClass();
  163. else
  164. cl = env.findClass(obj.toString());
  165. if (cl == null)
  166. return BooleanValue.FALSE;
  167. ArrayValue varArray = new ArrayValueImpl();
  168. for (ClassField field : cl.getClassFields().values()) {
  169. if (field.isPublic()) {
  170. StringValue name = field.getName();
  171. Expr initValue = field.getInitValue();
  172. Value value = initValue.eval(env);
  173. varArray.append(name, value);
  174. }
  175. }
  176. ArrayModule.ksort(env, varArray, ArrayModule.SORT_STRING);
  177. return varArray;
  178. }
  179. /**
  180. * Returns the declared classes
  181. */
  182. public static Value get_declared_classes(Env env)
  183. {
  184. return env.getDeclaredClasses();
  185. }
  186. // XXX: get_declared_interfaces
  187. /**
  188. * Returns the object's variables
  189. */
  190. public static Value get_object_vars(Env env, Value obj)
  191. {
  192. ArrayValue result = new ArrayValueImpl();
  193. // #3253, php/4as7 - XXX: needs cleanup
  194. if (obj instanceof ObjectValue) {
  195. for (Map.Entry<Value,Value> entry : ((ObjectValue) obj).entrySet()) {
  196. result.put(entry.getKey(), entry.getValue());
  197. }
  198. }
  199. else {
  200. Iterator<Map.Entry<Value,Value>> iter = obj.getIterator(env);
  201. while (iter.hasNext()) {
  202. Map.Entry<Value,Value> entry = iter.next();
  203. result.put(entry.getKey(), entry.getValue());
  204. }
  205. }
  206. return result;
  207. }
  208. /**
  209. * Returns the object's class name
  210. */
  211. public Value get_parent_class(Env env, @ReadOnly Value value)
  212. {
  213. if (value instanceof ObjectValue) {
  214. ObjectValue obj = (ObjectValue) value;
  215. String parent = obj.getParentClassName();
  216. if (parent != null)
  217. return env.createString(parent);
  218. }
  219. else if (value.isString()) {
  220. String className = value.toString();
  221. QuercusClass cl = env.findClass(className);
  222. if (cl != null) {
  223. String parent = cl.getParentName();
  224. if (parent != null)
  225. return env.createString(parent);
  226. }
  227. }
  228. return BooleanValue.FALSE;
  229. }
  230. /**
  231. * Returns true if the class exists.
  232. */
  233. public boolean interface_exists(Env env,
  234. String interfaceName,
  235. @Optional("true") boolean useAutoload)
  236. {
  237. QuercusClass cl = env.findClass(interfaceName, useAutoload, true);
  238. // php/[03]9m0
  239. return cl != null && cl.isInterface();
  240. }
  241. /**
  242. * Returns true if the object implements the given class.
  243. */
  244. public static boolean is_a(@ReadOnly Value value, String name)
  245. {
  246. return value.isA(name);
  247. }
  248. /**
  249. * Returns true if the argument is an object.
  250. */
  251. public static boolean is_object(@ReadOnly Value value)
  252. {
  253. return value.isObject();
  254. }
  255. /**
  256. * Returns true if the object implements the given class.
  257. */
  258. public static boolean is_subclass_of(Env env,
  259. @ReadOnly Value value,
  260. String name)
  261. {
  262. if (value instanceof StringValue) {
  263. QuercusClass cl = env.findClass(value.toString());
  264. return cl.isA(name) && !cl.getName().equalsIgnoreCase(name);
  265. }
  266. else
  267. return value.isA(name) && !value.getClassName().equalsIgnoreCase(name);
  268. }
  269. /**
  270. * Returns true if the named method exists on the object.
  271. *
  272. * @param obj the object to test
  273. * @param methodName the name of the method
  274. */
  275. public static boolean method_exists(Env env,
  276. Value obj,
  277. StringValue methodName)
  278. {
  279. QuercusClass qClass = obj.getQuercusClass();
  280. if (qClass == null)
  281. qClass = env.findClass(obj.toString());
  282. if (qClass != null)
  283. return qClass.findFunction(methodName) != null;
  284. else
  285. return false;
  286. }
  287. /**
  288. * Returns true if the named property exists on the object.
  289. */
  290. public static Value property_exists(Env env,
  291. Value obj,
  292. StringValue name)
  293. {
  294. QuercusClass cls;
  295. if (obj.isString())
  296. cls = env.findClass(obj.toString());
  297. else if (obj.isObject())
  298. cls = ((ObjectValue) obj.toValue()).getQuercusClass();
  299. else {
  300. env.warning("must pass in object or name of class");
  301. return NullValue.NULL;
  302. }
  303. if (cls != null && cls.getClassField(name) != null)
  304. return BooleanValue.TRUE;
  305. else
  306. return BooleanValue.FALSE;
  307. }
  308. }