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.

303 lines
9.9 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. Constructor cons[] = Class.forName(name).getConstructors();
  85. for (int i=0; i<cons.length; i++) {
  86. if (cons[i].getParameterTypes().length == args.length) {
  87. setResult(result, cons[i].newInstance(args));
  88. return;
  89. }
  90. }
  91. // for classes which have no visible constructor, return the class
  92. // useful for classes like java.lang.System and java.util.Calendar.
  93. if (args.length == 0) {
  94. setResult(result, Class.forName(name));
  95. return;
  96. }
  97. throw new InstantiationException("No matching constructor found");
  98. } catch (Exception e) {
  99. setException(result, e);
  100. }
  101. }
  102. //
  103. // Select the best match from a list of methods
  104. //
  105. private static Method select(Vector methods, Object args[]) {
  106. if (methods.size() == 1) return (Method) methods.firstElement();
  107. Method selected = null;
  108. int best = Integer.MAX_VALUE;
  109. for (Enumeration e = methods.elements(); e.hasMoreElements(); ) {
  110. Method method = (Method)e.nextElement();
  111. int weight=0;
  112. Class parms[] = method.getParameterTypes();
  113. for (int i=0; i<parms.length; i++) {
  114. if (parms[i].isInstance(args[i])) {
  115. for (Class c=parms[i]; (c=c.getSuperclass()) != null; ) {
  116. if (!c.isInstance(args[i])) break;
  117. weight++;
  118. }
  119. } else if (parms[i].isPrimitive()) {
  120. Class c=parms[i];
  121. if (args[i] instanceof Number) {
  122. if (c==Boolean.TYPE) weight+=5;
  123. if (c==Character.TYPE) weight+=4;
  124. if (c==Byte.TYPE) weight+=3;
  125. if (c==Short.TYPE) weight+=2;
  126. if (c==Integer.TYPE) weight++;
  127. if (c==Float.TYPE) weight++;
  128. } else if (args[i] instanceof Boolean) {
  129. if (c!=Boolean.TYPE) weight+=9999;
  130. } else if (args[i] instanceof String) {
  131. if (c== Character.TYPE || ((String)args[i]).length()>0)
  132. weight+=((String)args[i]).length();
  133. else
  134. weight+=9999;
  135. } else {
  136. weight+=9999;
  137. }
  138. } else {
  139. weight+=9999;
  140. }
  141. }
  142. if (weight < best) {
  143. if (weight == 0) return method;
  144. best = weight;
  145. selected = method;
  146. }
  147. }
  148. return selected;
  149. }
  150. //
  151. // Select the best match from a list of methods
  152. //
  153. private static Object[] coerce(Method method, Object args[]) {
  154. Object result[] = args;
  155. Class parms[] = method.getParameterTypes();
  156. for (int i=0; i<args.length; i++) {
  157. if (parms[i].isInstance(args[i])) continue;
  158. if (args[i] instanceof Number && parms[i].isPrimitive()) {
  159. if (result==args) result=(Object[])result.clone();
  160. Class c = parms[i];
  161. Number n = (Number)args[i];
  162. if (c == Boolean.TYPE) result[i]=new Boolean(0.0!=n.floatValue());
  163. if (c == Byte.TYPE) result[i]=new Byte(n.byteValue());
  164. if (c == Short.TYPE) result[i]=new Short(n.shortValue());
  165. if (c == Integer.TYPE) result[i]=new Integer(n.intValue());
  166. if (c == Float.TYPE) result[i]=new Float(n.floatValue());
  167. if (c == Long.TYPE && !(n instanceof Long))
  168. result[i]=new Long(n.longValue());
  169. }
  170. }
  171. return result;
  172. }
  173. //
  174. // Invoke a method on a given object
  175. //
  176. public static void Invoke
  177. (Object object, String method, Object args[], long result)
  178. {
  179. try {
  180. Vector matches = new Vector();
  181. // gather
  182. for (Class jclass = object.getClass();;jclass=(Class)object) {
  183. while (!Modifier.isPublic(jclass.getModifiers())) {
  184. // OK, some joker gave us an instance of a non-public class
  185. // This often occurs in the case of enumerators
  186. // Substitute the first public interface in its place,
  187. // and barring that, try the superclass
  188. Class interfaces[] = jclass.getInterfaces();
  189. jclass=jclass.getSuperclass();
  190. for (int i=interfaces.length; i-->0;) {
  191. if (Modifier.isPublic(interfaces[i].getModifiers())) {
  192. jclass=interfaces[i];
  193. }
  194. }
  195. }
  196. Method methods[] = jclass.getMethods();
  197. for (int i=0; i<methods.length; i++) {
  198. if (methods[i].getName().equalsIgnoreCase(method) &&
  199. methods[i].getParameterTypes().length == args.length) {
  200. matches.addElement(methods[i]);
  201. }
  202. }
  203. // try a second time with the object itself, if it is of type Class
  204. if (!(object instanceof Class) || (jclass==object)) break;
  205. }
  206. Method selected = select(matches, args);
  207. if (selected == null) throw new NoSuchMethodException(method);
  208. Object coercedArgs[] = coerce(selected, args);
  209. setResult(result, selected.invoke(object, coercedArgs));
  210. } catch (Exception e) {
  211. setException(result, e);
  212. }
  213. }
  214. //
  215. // Get or Set a property
  216. //
  217. public static void GetSetProp
  218. (Object object, String prop, Object args[], long result)
  219. {
  220. try {
  221. for (Class jclass = object.getClass();;jclass=(Class)object) {
  222. BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
  223. PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
  224. for (int i=0; i<props.length; i++) {
  225. if (props[i].getName().equalsIgnoreCase(prop)) {
  226. Method method;
  227. if (args!=null && args.length>0) {
  228. method=props[i].getWriteMethod();
  229. } else {
  230. method=props[i].getReadMethod();
  231. }
  232. setResult(result, method.invoke(object, args));
  233. return;
  234. }
  235. }
  236. Field jfields[] = jclass.getFields();
  237. for (int i=0; i<jfields.length; i++) {
  238. if (jfields[i].getName().equalsIgnoreCase(prop)) {
  239. if (args!=null && args.length>0) {
  240. jfields[i].set(object, args[0]);
  241. } else {
  242. setResult(result, jfields[i].get(object));
  243. }
  244. return;
  245. }
  246. }
  247. // try a second time with the object itself, if it is of type Class
  248. if (!(object instanceof Class) || (jclass==object)) break;
  249. }
  250. } catch (Exception e) {
  251. setException(result, e);
  252. }
  253. }
  254. //
  255. // Helper routines for the C implementation
  256. //
  257. public static Object MakeArg(boolean b) { return new Boolean(b); }
  258. public static Object MakeArg(long l) { return new Long(l); }
  259. public static Object MakeArg(double d) { return new Double(d); }
  260. }