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.

347 lines
12 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
  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. public 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, byte 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, byte value[]);
  43. public static native void setEnv();
  44. //
  45. // Helper routines which encapsulate the native methods
  46. //
  47. public static void setResult(long result, Object value) {
  48. if (value == null) return;
  49. if (value instanceof java.lang.String) {
  50. setResultFromString(result, ((String)value).getBytes());
  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. Throwable lastException = null;
  73. void lastException(long result) {
  74. setResult(result, lastException);
  75. }
  76. void clearException() {
  77. lastException = null;
  78. }
  79. void setException(long result, Throwable e) {
  80. if (e instanceof InvocationTargetException) {
  81. Throwable t = ((InvocationTargetException)e).getTargetException();
  82. if (t!=null) e=t;
  83. }
  84. lastException = e;
  85. setException(result, e.toString().getBytes());
  86. }
  87. //
  88. // Create an new instance of a given class
  89. //
  90. public void CreateObject(String name, Object args[], long result) {
  91. try {
  92. Vector matches = new Vector();
  93. Constructor cons[] = Class.forName(name).getConstructors();
  94. for (int i=0; i<cons.length; i++) {
  95. if (cons[i].getParameterTypes().length == args.length) {
  96. matches.addElement(cons[i]);
  97. }
  98. }
  99. Constructor selected = (Constructor)select(matches, args);
  100. if (selected == null) {
  101. if (args.length > 0) {
  102. throw new InstantiationException("No matching constructor found");
  103. } else {
  104. // for classes which have no visible constructor, return the class
  105. // useful for classes like java.lang.System and java.util.Calendar.
  106. setResult(result, Class.forName(name));
  107. return;
  108. }
  109. }
  110. Object coercedArgs[] = coerce(selected.getParameterTypes(), args);
  111. setResult(result, selected.newInstance(coercedArgs));
  112. } catch (Exception e) {
  113. setException(result, e);
  114. }
  115. }
  116. //
  117. // Select the best match from a list of methods
  118. //
  119. private static Object select(Vector methods, Object args[]) {
  120. if (methods.size() == 1) return methods.firstElement();
  121. Object selected = null;
  122. int best = Integer.MAX_VALUE;
  123. for (Enumeration e = methods.elements(); e.hasMoreElements(); ) {
  124. Object element = e.nextElement();
  125. int weight=0;
  126. Class parms[] = (element instanceof Method) ?
  127. ((Method)element).getParameterTypes() :
  128. ((Constructor)element).getParameterTypes();
  129. for (int i=0; i<parms.length; i++) {
  130. if (parms[i].isInstance(args[i])) {
  131. for (Class c=parms[i]; (c=c.getSuperclass()) != null; ) {
  132. if (!c.isInstance(args[i])) break;
  133. weight++;
  134. }
  135. } else if (parms[i].isInstance("")) {
  136. if (!(args[i] instanceof byte[]))
  137. weight+=9999;
  138. } else if (parms[i].isPrimitive()) {
  139. Class c=parms[i];
  140. if (args[i] instanceof Number) {
  141. if (c==Boolean.TYPE) weight+=5;
  142. if (c==Character.TYPE) weight+=4;
  143. if (c==Byte.TYPE) weight+=3;
  144. if (c==Short.TYPE) weight+=2;
  145. if (c==Integer.TYPE) weight++;
  146. if (c==Float.TYPE) weight++;
  147. } else if (args[i] instanceof Boolean) {
  148. if (c!=Boolean.TYPE) weight+=9999;
  149. } else if (args[i] instanceof String) {
  150. if (c== Character.TYPE || ((String)args[i]).length()>0)
  151. weight+=((String)args[i]).length();
  152. else
  153. weight+=9999;
  154. } else {
  155. weight+=9999;
  156. }
  157. } else {
  158. weight+=9999;
  159. }
  160. }
  161. if (weight < best) {
  162. if (weight == 0) return element;
  163. best = weight;
  164. selected = element;
  165. }
  166. }
  167. return selected;
  168. }
  169. //
  170. // Coerce arguments when possible to conform to the argument list.
  171. // Java's reflection will automatically do widening conversions,
  172. // unfortunately PHP only supports wide formats, so to be practical
  173. // some (possibly lossy) conversions are required.
  174. //
  175. private static Object[] coerce(Class parms[], Object args[]) {
  176. Object result[] = args;
  177. for (int i=0; i<args.length; i++) {
  178. if (args[i] instanceof byte[] && !parms[i].isArray()) {
  179. result[i] = new String((byte[])args[i]);
  180. } else if (args[i] instanceof Number && parms[i].isPrimitive()) {
  181. if (result==args) result=(Object[])result.clone();
  182. Class c = parms[i];
  183. Number n = (Number)args[i];
  184. if (c == Boolean.TYPE) result[i]=new Boolean(0.0!=n.floatValue());
  185. if (c == Byte.TYPE) result[i]=new Byte(n.byteValue());
  186. if (c == Short.TYPE) result[i]=new Short(n.shortValue());
  187. if (c == Integer.TYPE) result[i]=new Integer(n.intValue());
  188. if (c == Float.TYPE) result[i]=new Float(n.floatValue());
  189. if (c == Long.TYPE && !(n instanceof Long))
  190. result[i]=new Long(n.longValue());
  191. }
  192. }
  193. return result;
  194. }
  195. //
  196. // Invoke a method on a given object
  197. //
  198. public void Invoke
  199. (Object object, String method, Object args[], long result)
  200. {
  201. try {
  202. Vector matches = new Vector();
  203. // gather
  204. for (Class jclass = object.getClass();;jclass=(Class)object) {
  205. while (!Modifier.isPublic(jclass.getModifiers())) {
  206. // OK, some joker gave us an instance of a non-public class
  207. // This often occurs in the case of enumerators
  208. // Substitute the first public interface in its place,
  209. // and barring that, try the superclass
  210. Class interfaces[] = jclass.getInterfaces();
  211. jclass=jclass.getSuperclass();
  212. for (int i=interfaces.length; i-->0;) {
  213. if (Modifier.isPublic(interfaces[i].getModifiers())) {
  214. jclass=interfaces[i];
  215. }
  216. }
  217. }
  218. Method methods[] = jclass.getMethods();
  219. for (int i=0; i<methods.length; i++) {
  220. if (methods[i].getName().equalsIgnoreCase(method) &&
  221. methods[i].getParameterTypes().length == args.length) {
  222. matches.addElement(methods[i]);
  223. }
  224. }
  225. // try a second time with the object itself, if it is of type Class
  226. if (!(object instanceof Class) || (jclass==object)) break;
  227. }
  228. Method selected = (Method)select(matches, args);
  229. if (selected == null) throw new NoSuchMethodException(method);
  230. Object coercedArgs[] = coerce(selected.getParameterTypes(), args);
  231. setResult(result, selected.invoke(object, coercedArgs));
  232. } catch (Exception e) {
  233. setException(result, e);
  234. }
  235. }
  236. //
  237. // Get or Set a property
  238. //
  239. public void GetSetProp
  240. (Object object, String prop, Object args[], long result)
  241. {
  242. System.out.println(object + "." + prop);
  243. try {
  244. for (Class jclass = object.getClass();;jclass=(Class)object) {
  245. while (!Modifier.isPublic(jclass.getModifiers())) {
  246. // OK, some joker gave us an instance of a non-public class
  247. // Substitute the first public interface in its place,
  248. // and barring that, try the superclass
  249. Class interfaces[] = jclass.getInterfaces();
  250. jclass=jclass.getSuperclass();
  251. for (int i=interfaces.length; i-->0;) {
  252. if (Modifier.isPublic(interfaces[i].getModifiers())) {
  253. jclass=interfaces[i];
  254. }
  255. }
  256. }
  257. BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
  258. PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
  259. for (int i=0; i<props.length; i++) {
  260. if (props[i].getName().equalsIgnoreCase(prop)) {
  261. Method method;
  262. if (args!=null && args.length>0) {
  263. method=props[i].getWriteMethod();
  264. args = coerce(method.getParameterTypes(), args);
  265. } else {
  266. method=props[i].getReadMethod();
  267. }
  268. setResult(result, method.invoke(object, args));
  269. return;
  270. }
  271. }
  272. Field jfields[] = jclass.getFields();
  273. for (int i=0; i<jfields.length; i++) {
  274. if (jfields[i].getName().equalsIgnoreCase(prop)) {
  275. if (args!=null && args.length>0) {
  276. args = coerce(new Class[] {jfields[i].getType()}, args);
  277. jfields[i].set(object, args[0]);
  278. } else {
  279. setResult(result, jfields[i].get(object));
  280. }
  281. return;
  282. }
  283. }
  284. // try a second time with the object itself, if it is of type Class
  285. if (!(object instanceof Class) || (jclass==object)) break;
  286. }
  287. } catch (Exception e) {
  288. setException(result, e);
  289. }
  290. }
  291. //
  292. // Helper routines for the C implementation
  293. //
  294. public Object MakeArg(boolean b) { return new Boolean(b); }
  295. public Object MakeArg(long l) { return new Long(l); }
  296. public Object MakeArg(double d) { return new Double(d); }
  297. }