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.

197 lines
6.3 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999 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.ResourceBundle;
  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. // Invoke a method on a given object
  96. //
  97. public static void Invoke
  98. (Object object, String method, Object args[], long result)
  99. {
  100. try {
  101. for (Class jclass = object.getClass();;jclass=(Class)object) {
  102. Method methods[] = jclass.getMethods();
  103. for (int i=0; i<methods.length; i++) {
  104. if (methods[i].getName().equalsIgnoreCase(method) &&
  105. methods[i].getParameterTypes().length == args.length) {
  106. setResult(result, methods[i].invoke(object, args));
  107. return;
  108. }
  109. }
  110. // try a second time with the object itself, if it is of type Class
  111. if (!(jclass instanceof Class) || (jclass==object)) break;
  112. }
  113. throw new NoSuchMethodException(method);
  114. } catch (Exception e) {
  115. setException(result, e);
  116. }
  117. }
  118. //
  119. // Get or Set a property
  120. //
  121. public static void GetSetProp
  122. (Object object, String prop, Object args[], long result)
  123. {
  124. try {
  125. for (Class jclass = object.getClass();;jclass=(Class)object) {
  126. BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
  127. PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
  128. for (int i=0; i<props.length; i++) {
  129. if (props[i].getName().equalsIgnoreCase(prop)) {
  130. Method method;
  131. if (args!=null && args.length>0) {
  132. method=props[i].getWriteMethod();
  133. } else {
  134. method=props[i].getReadMethod();
  135. }
  136. setResult(result, method.invoke(object, args));
  137. return;
  138. }
  139. }
  140. Field jfields[] = jclass.getFields();
  141. for (int i=0; i<jfields.length; i++) {
  142. if (jfields[i].getName().equalsIgnoreCase(prop)) {
  143. if (args!=null && args.length>0) {
  144. jfields[i].set(object, args[0]);
  145. } else {
  146. setResult(result, jfields[i].get(object));
  147. }
  148. return;
  149. }
  150. }
  151. // try a second time with the object itself, if it is of type Class
  152. if (!(jclass instanceof Class) || (jclass==object)) break;
  153. }
  154. } catch (Exception e) {
  155. setException(result, e);
  156. }
  157. }
  158. //
  159. // Helper routines for the C implementation
  160. //
  161. public static Object MakeArg(boolean b) { return new Boolean(b); }
  162. public static Object MakeArg(long l) { return new Long(l); }
  163. public static Object MakeArg(double d) { return new Double(d); }
  164. }