You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
11 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.0 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license.html. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sam Ruby (rubys@us.ibm.com) |
  16. +----------------------------------------------------------------------+
  17. */
  18. package net.php;
  19. import java.lang.reflect.*;
  20. import java.util.*;
  21. import java.beans.*;
  22. class reflect {
  23. static { loadLibrary("reflect"); }
  24. protected static void loadLibrary(String property) {
  25. try {
  26. ResourceBundle bundle = ResourceBundle.getBundle("net.php."+property);
  27. System.loadLibrary(bundle.getString("library"));
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. //
  33. // Native methods
  34. //
  35. private static native void setResultFromString(long result, String value);
  36. private static native void setResultFromLong(long result, long value);
  37. private static native void setResultFromDouble(long result, double value);
  38. private static native void setResultFromBoolean(long result, boolean value);
  39. private static native void setResultFromObject(long result, Object value);
  40. private static native void setResultFromArray(long result);
  41. private static native long nextElement(long array);
  42. private static native void setException(long result, String value);
  43. public static native void setEnv();
  44. //
  45. // Helper routines which encapsulate the native methods
  46. //
  47. static void setResult(long result, Object value) {
  48. if (value == null) return;
  49. if (value instanceof java.lang.String) {
  50. setResultFromString(result, (String)value);
  51. } else if (value instanceof java.lang.Number) {
  52. if (value instanceof java.lang.Integer ||
  53. value instanceof java.lang.Short ||
  54. value instanceof java.lang.Byte) {
  55. setResultFromLong(result, ((Number)value).longValue());
  56. } else {
  57. /* Float, Double, BigDecimal, BigInteger, Double, Long, ... */
  58. setResultFromDouble(result, ((Number)value).doubleValue());
  59. }
  60. } else if (value instanceof java.lang.Boolean) {
  61. setResultFromBoolean(result, ((Boolean)value).booleanValue());
  62. } else if (value.getClass().isArray()) {
  63. long length = Array.getLength(value);
  64. setResultFromArray(result);
  65. for (int i=0; i<length; i++) {
  66. setResult(nextElement(result), Array.get(value, i));
  67. }
  68. } else {
  69. setResultFromObject(result, value);
  70. }
  71. }
  72. static void setException(long result, Throwable e) {
  73. if (e instanceof InvocationTargetException) {
  74. Throwable t = ((InvocationTargetException)e).getTargetException();
  75. if (t!=null) e=t;
  76. }
  77. setException(result, e.toString());
  78. }
  79. //
  80. // Create an new instance of a given class
  81. //
  82. public static void CreateObject(String name, Object args[], long result) {
  83. try {
  84. Vector matches = new Vector();
  85. Constructor cons[] = Class.forName(name).getConstructors();
  86. for (int i=0; i<cons.length; i++) {
  87. if (cons[i].getParameterTypes().length == args.length) {
  88. matches.addElement(cons[i]);
  89. }
  90. }
  91. Constructor selected = (Constructor)select(matches, args);
  92. if (selected == null) {
  93. if (args.length > 0) {
  94. throw new InstantiationException("No matching constructor found");
  95. } else {
  96. // for classes which have no visible constructor, return the class
  97. // useful for classes like java.lang.System and java.util.Calendar.
  98. setResult(result, Class.forName(name));
  99. return;
  100. }
  101. }
  102. Object coercedArgs[] = coerce(selected.getParameterTypes(), args);
  103. setResult(result, selected.newInstance(coercedArgs));
  104. } catch (Exception e) {
  105. setException(result, e);
  106. }
  107. }
  108. //
  109. // Select the best match from a list of methods
  110. //
  111. private static Object select(Vector methods, Object args[]) {
  112. if (methods.size() == 1) return methods.firstElement();
  113. Object selected = null;
  114. int best = Integer.MAX_VALUE;
  115. for (Enumeration e = methods.elements(); e.hasMoreElements(); ) {
  116. Object element = e.nextElement();
  117. int weight=0;
  118. Class parms[] = (element instanceof Method) ?
  119. ((Method)element).getParameterTypes() :
  120. ((Constructor)element).getParameterTypes();
  121. for (int i=0; i<parms.length; i++) {
  122. if (parms[i].isInstance(args[i])) {
  123. for (Class c=parms[i]; (c=c.getSuperclass()) != null; ) {
  124. if (!c.isInstance(args[i])) break;
  125. weight++;
  126. }
  127. } else if (parms[i].isPrimitive()) {
  128. Class c=parms[i];
  129. if (args[i] instanceof Number) {
  130. if (c==Boolean.TYPE) weight+=5;
  131. if (c==Character.TYPE) weight+=4;
  132. if (c==Byte.TYPE) weight+=3;
  133. if (c==Short.TYPE) weight+=2;
  134. if (c==Integer.TYPE) weight++;
  135. if (c==Float.TYPE) weight++;
  136. } else if (args[i] instanceof Boolean) {
  137. if (c!=Boolean.TYPE) weight+=9999;
  138. } else if (args[i] instanceof String) {
  139. if (c== Character.TYPE || ((String)args[i]).length()>0)
  140. weight+=((String)args[i]).length();
  141. else
  142. weight+=9999;
  143. } else {
  144. weight+=9999;
  145. }
  146. } else {
  147. weight+=9999;
  148. }
  149. }
  150. if (weight < best) {
  151. if (weight == 0) return element;
  152. best = weight;
  153. selected = element;
  154. }
  155. }
  156. return selected;
  157. }
  158. //
  159. // Coerce arguments when possible to conform to the argument list.
  160. // Java's reflection will automatically do widening conversions,
  161. // unfortunately PHP only supports wide formats, so to be practical
  162. // some (possibly lossy) conversions are required.
  163. //
  164. private static Object[] coerce(Class parms[], Object args[]) {
  165. Object result[] = args;
  166. for (int i=0; i<args.length; i++) {
  167. if (parms[i].isInstance(args[i])) continue;
  168. if (args[i] instanceof Number && parms[i].isPrimitive()) {
  169. if (result==args) result=(Object[])result.clone();
  170. Class c = parms[i];
  171. Number n = (Number)args[i];
  172. if (c == Boolean.TYPE) result[i]=new Boolean(0.0!=n.floatValue());
  173. if (c == Byte.TYPE) result[i]=new Byte(n.byteValue());
  174. if (c == Short.TYPE) result[i]=new Short(n.shortValue());
  175. if (c == Integer.TYPE) result[i]=new Integer(n.intValue());
  176. if (c == Float.TYPE) result[i]=new Float(n.floatValue());
  177. if (c == Long.TYPE && !(n instanceof Long))
  178. result[i]=new Long(n.longValue());
  179. }
  180. }
  181. return result;
  182. }
  183. //
  184. // Invoke a method on a given object
  185. //
  186. public static void Invoke
  187. (Object object, String method, Object args[], long result)
  188. {
  189. try {
  190. Vector matches = new Vector();
  191. // gather
  192. for (Class jclass = object.getClass();;jclass=(Class)object) {
  193. while (!Modifier.isPublic(jclass.getModifiers())) {
  194. // OK, some joker gave us an instance of a non-public class
  195. // This often occurs in the case of enumerators
  196. // Substitute the first public interface in its place,
  197. // and barring that, try the superclass
  198. Class interfaces[] = jclass.getInterfaces();
  199. jclass=jclass.getSuperclass();
  200. for (int i=interfaces.length; i-->0;) {
  201. if (Modifier.isPublic(interfaces[i].getModifiers())) {
  202. jclass=interfaces[i];
  203. }
  204. }
  205. }
  206. Method methods[] = jclass.getMethods();
  207. for (int i=0; i<methods.length; i++) {
  208. if (methods[i].getName().equalsIgnoreCase(method) &&
  209. methods[i].getParameterTypes().length == args.length) {
  210. matches.addElement(methods[i]);
  211. }
  212. }
  213. // try a second time with the object itself, if it is of type Class
  214. if (!(object instanceof Class) || (jclass==object)) break;
  215. }
  216. Method selected = (Method)select(matches, args);
  217. if (selected == null) throw new NoSuchMethodException(method);
  218. Object coercedArgs[] = coerce(selected.getParameterTypes(), args);
  219. setResult(result, selected.invoke(object, coercedArgs));
  220. } catch (Exception e) {
  221. setException(result, e);
  222. }
  223. }
  224. //
  225. // Get or Set a property
  226. //
  227. public static void GetSetProp
  228. (Object object, String prop, Object args[], long result)
  229. {
  230. try {
  231. for (Class jclass = object.getClass();;jclass=(Class)object) {
  232. while (!Modifier.isPublic(jclass.getModifiers())) {
  233. // OK, some joker gave us an instance of a non-public class
  234. // Substitute the first public interface in its place,
  235. // and barring that, try the superclass
  236. Class interfaces[] = jclass.getInterfaces();
  237. jclass=jclass.getSuperclass();
  238. for (int i=interfaces.length; i-->0;) {
  239. if (Modifier.isPublic(interfaces[i].getModifiers())) {
  240. jclass=interfaces[i];
  241. }
  242. }
  243. }
  244. BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
  245. PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
  246. for (int i=0; i<props.length; i++) {
  247. if (props[i].getName().equalsIgnoreCase(prop)) {
  248. Method method;
  249. if (args!=null && args.length>0) {
  250. method=props[i].getWriteMethod();
  251. } else {
  252. method=props[i].getReadMethod();
  253. }
  254. setResult(result, method.invoke(object, args));
  255. return;
  256. }
  257. }
  258. Field jfields[] = jclass.getFields();
  259. for (int i=0; i<jfields.length; i++) {
  260. if (jfields[i].getName().equalsIgnoreCase(prop)) {
  261. if (args!=null && args.length>0) {
  262. jfields[i].set(object, args[0]);
  263. } else {
  264. setResult(result, jfields[i].get(object));
  265. }
  266. return;
  267. }
  268. }
  269. // try a second time with the object itself, if it is of type Class
  270. if (!(object instanceof Class) || (jclass==object)) break;
  271. }
  272. } catch (Exception e) {
  273. setException(result, e);
  274. }
  275. }
  276. //
  277. // Helper routines for the C implementation
  278. //
  279. public static Object MakeArg(boolean b) { return new Boolean(b); }
  280. public static Object MakeArg(long l) { return new Long(l); }
  281. public static Object MakeArg(double d) { return new Double(d); }
  282. }