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.

279 lines
9.0 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 setException(long result, String value);
  41. public static native void setEnv();
  42. //
  43. // Helper routines which encapsulate the native methods
  44. //
  45. static void setResult(long result, Object value) {
  46. if (value == null) return;
  47. if (value instanceof java.lang.String) {
  48. setResultFromString(result, (String)value);
  49. } else if (value instanceof java.lang.Number) {
  50. if (value instanceof java.lang.Integer ||
  51. value instanceof java.lang.Short ||
  52. value instanceof java.lang.Byte) {
  53. setResultFromLong(result, ((Number)value).longValue());
  54. } else {
  55. /* Float, Double, BigDecimal, BigInteger, Double, Long, ... */
  56. setResultFromDouble(result, ((Number)value).doubleValue());
  57. }
  58. } else if (value instanceof java.lang.Boolean) {
  59. setResultFromBoolean(result, ((Boolean)value).booleanValue());
  60. } else {
  61. setResultFromObject(result, value);
  62. }
  63. }
  64. static void setException(long result, Throwable e) {
  65. if (e instanceof InvocationTargetException) {
  66. Throwable t = ((InvocationTargetException)e).getTargetException();
  67. if (t!=null) e=t;
  68. }
  69. setException(result, e.toString());
  70. }
  71. //
  72. // Create an new instance of a given class
  73. //
  74. public static void CreateObject(String name, Object args[], long result) {
  75. try {
  76. Constructor cons[] = Class.forName(name).getConstructors();
  77. for (int i=0; i<cons.length; i++) {
  78. if (cons[i].getParameterTypes().length == args.length) {
  79. setResult(result, cons[i].newInstance(args));
  80. return;
  81. }
  82. }
  83. // for classes which have no visible constructor, return the class
  84. // useful for classes like java.lang.System and java.util.Calendar.
  85. if (args.length == 0) {
  86. setResult(result, Class.forName(name));
  87. return;
  88. }
  89. throw new InstantiationException("No matching constructor found");
  90. } catch (Exception e) {
  91. setException(result, e);
  92. }
  93. }
  94. //
  95. // Select the best match from a list of methods
  96. //
  97. private static Method select(Vector methods, Object args[]) {
  98. if (methods.size() == 1) return (Method) methods.firstElement();
  99. Method selected = null;
  100. int best = Integer.MAX_VALUE;
  101. for (Enumeration e = methods.elements(); e.hasMoreElements(); ) {
  102. Method method = (Method)e.nextElement();
  103. int weight=0;
  104. Class parms[] = method.getParameterTypes();
  105. for (int i=0; i<parms.length; i++) {
  106. if (parms[i].isInstance(args[i])) {
  107. for (Class c=parms[i]; (c=c.getSuperclass()) != null; ) {
  108. if (!c.isInstance(args[i])) break;
  109. weight++;
  110. }
  111. } else if (parms[i].isPrimitive()) {
  112. Class c=parms[i];
  113. if (args[i] instanceof Number) {
  114. if (c==Boolean.TYPE) weight+=5;
  115. if (c==Character.TYPE) weight+=4;
  116. if (c==Byte.TYPE) weight+=3;
  117. if (c==Short.TYPE) weight+=2;
  118. if (c==Integer.TYPE) weight++;
  119. if (c==Float.TYPE) weight++;
  120. } else if (args[i] instanceof Boolean) {
  121. if (c!=Boolean.TYPE) weight+=9999;
  122. } else if (args[i] instanceof String) {
  123. if (c== Character.TYPE || ((String)args[i]).length()>0)
  124. weight+=((String)args[i]).length();
  125. else
  126. weight+=9999;
  127. } else {
  128. weight+=9999;
  129. }
  130. } else {
  131. weight+=9999;
  132. }
  133. }
  134. if (weight < best) {
  135. if (weight == 0) return method;
  136. best = weight;
  137. selected = method;
  138. }
  139. }
  140. return selected;
  141. }
  142. //
  143. // Select the best match from a list of methods
  144. //
  145. private static Object[] coerce(Method method, Object args[]) {
  146. Object result[] = args;
  147. Class parms[] = method.getParameterTypes();
  148. for (int i=0; i<args.length; i++) {
  149. if (parms[i].isInstance(args[i])) continue;
  150. if (args[i] instanceof Number && parms[i].isPrimitive()) {
  151. if (result==args) result=(Object[])result.clone();
  152. Class c = parms[i];
  153. Number n = (Number)args[i];
  154. if (c == Boolean.TYPE) result[i]=new Boolean(0.0!=n.floatValue());
  155. if (c == Byte.TYPE) result[i]=new Byte(n.byteValue());
  156. if (c == Short.TYPE) result[i]=new Short(n.shortValue());
  157. if (c == Integer.TYPE) result[i]=new Integer(n.intValue());
  158. if (c == Float.TYPE) result[i]=new Float(n.floatValue());
  159. if (c == Long.TYPE && !(n instanceof Long))
  160. result[i]=new Long(n.longValue());
  161. }
  162. }
  163. return result;
  164. }
  165. //
  166. // Invoke a method on a given object
  167. //
  168. public static void Invoke
  169. (Object object, String method, Object args[], long result)
  170. {
  171. try {
  172. Vector matches = new Vector();
  173. // gather
  174. for (Class jclass = object.getClass();;jclass=(Class)object) {
  175. Method methods[] = jclass.getMethods();
  176. for (int i=0; i<methods.length; i++) {
  177. if (methods[i].getName().equalsIgnoreCase(method) &&
  178. methods[i].getParameterTypes().length == args.length) {
  179. matches.addElement(methods[i]);
  180. }
  181. }
  182. // try a second time with the object itself, if it is of type Class
  183. if (!(object instanceof Class) || (jclass==object)) break;
  184. }
  185. Method selected = select(matches, args);
  186. if (selected == null) throw new NoSuchMethodException(method);
  187. Object coercedArgs[] = coerce(selected, args);
  188. setResult(result, selected.invoke(object, coercedArgs));
  189. } catch (Exception e) {
  190. setException(result, e);
  191. }
  192. }
  193. //
  194. // Get or Set a property
  195. //
  196. public static void GetSetProp
  197. (Object object, String prop, Object args[], long result)
  198. {
  199. try {
  200. for (Class jclass = object.getClass();;jclass=(Class)object) {
  201. BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
  202. PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
  203. for (int i=0; i<props.length; i++) {
  204. if (props[i].getName().equalsIgnoreCase(prop)) {
  205. Method method;
  206. if (args!=null && args.length>0) {
  207. method=props[i].getWriteMethod();
  208. } else {
  209. method=props[i].getReadMethod();
  210. }
  211. setResult(result, method.invoke(object, args));
  212. return;
  213. }
  214. }
  215. Field jfields[] = jclass.getFields();
  216. for (int i=0; i<jfields.length; i++) {
  217. if (jfields[i].getName().equalsIgnoreCase(prop)) {
  218. if (args!=null && args.length>0) {
  219. jfields[i].set(object, args[0]);
  220. } else {
  221. setResult(result, jfields[i].get(object));
  222. }
  223. return;
  224. }
  225. }
  226. // try a second time with the object itself, if it is of type Class
  227. if (!(object instanceof Class) || (jclass==object)) break;
  228. }
  229. } catch (Exception e) {
  230. setException(result, e);
  231. }
  232. }
  233. //
  234. // Helper routines for the C implementation
  235. //
  236. public static Object MakeArg(boolean b) { return new Boolean(b); }
  237. public static Object MakeArg(long l) { return new Long(l); }
  238. public static Object MakeArg(double d) { return new Double(d); }
  239. }