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.

186 lines
6.0 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.beans.*;
  21. class reflect {
  22. static { System.loadLibrary("php_java"); }
  23. //
  24. // Native methods
  25. //
  26. private static native void setResultFromString(long result, String value);
  27. private static native void setResultFromLong(long result, long value);
  28. private static native void setResultFromDouble(long result, double value);
  29. private static native void setResultFromBoolean(long result, boolean value);
  30. private static native void setResultFromObject(long result, Object value);
  31. private static native void setException(long result, String value);
  32. //
  33. // Helper routines which encapsulate the native methods
  34. //
  35. static void setResult(long result, Object value) {
  36. if (value == null) return;
  37. if (value instanceof java.lang.String) {
  38. setResultFromString(result, (String)value);
  39. } else if (value instanceof java.lang.Number) {
  40. if (value instanceof java.lang.Integer ||
  41. value instanceof java.lang.Short ||
  42. value instanceof java.lang.Byte) {
  43. setResultFromLong(result, ((Number)value).longValue());
  44. } else {
  45. /* Float, Double, BigDecimal, BigInteger, Double, Long, ... */
  46. setResultFromDouble(result, ((Number)value).doubleValue());
  47. }
  48. } else if (value instanceof java.lang.Boolean) {
  49. setResultFromBoolean(result, ((Boolean)value).booleanValue());
  50. } else {
  51. setResultFromObject(result, value);
  52. }
  53. }
  54. static void setException(long result, Throwable e) {
  55. if (e instanceof InvocationTargetException) {
  56. Throwable t = ((InvocationTargetException)e).getTargetException();
  57. if (t!=null) e=t;
  58. }
  59. setException(result, e.toString());
  60. }
  61. //
  62. // Create an new instance of a given class
  63. //
  64. public static void CreateObject(String name, Object args[], long result) {
  65. try {
  66. Constructor cons[] = Class.forName(name).getConstructors();
  67. for (int i=0; i<cons.length; i++) {
  68. if (cons[i].getParameterTypes().length == args.length) {
  69. setResult(result, cons[i].newInstance(args));
  70. return;
  71. }
  72. }
  73. // for classes which have no visible constructor, return the class
  74. // useful for classes like java.lang.System and java.util.Calendar.
  75. if (args.length == 0) {
  76. setResult(result, Class.forName(name));
  77. return;
  78. }
  79. throw new InstantiationException("No matching constructor found");
  80. } catch (Exception e) {
  81. setException(result, e);
  82. }
  83. }
  84. //
  85. // Invoke a method on a given object
  86. //
  87. public static void Invoke
  88. (Object object, String method, Object args[], long result)
  89. {
  90. try {
  91. for (Class jclass = object.getClass();;jclass=(Class)object) {
  92. Method methods[] = jclass.getMethods();
  93. for (int i=0; i<methods.length; i++) {
  94. if (methods[i].getName().equalsIgnoreCase(method) &&
  95. methods[i].getParameterTypes().length == args.length) {
  96. setResult(result, methods[i].invoke(object, args));
  97. return;
  98. }
  99. }
  100. // try a second time with the object itself, if it is of type Class
  101. if (!(jclass instanceof Class) || (jclass==object)) break;
  102. }
  103. throw new NoSuchMethodException(method);
  104. } catch (Exception e) {
  105. setException(result, e);
  106. }
  107. }
  108. //
  109. // Get or Set a property
  110. //
  111. public static void GetSetProp
  112. (Object object, String prop, Object args[], long result)
  113. {
  114. try {
  115. for (Class jclass = object.getClass();;jclass=(Class)object) {
  116. BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
  117. PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
  118. for (int i=0; i<props.length; i++) {
  119. if (props[i].getName().equalsIgnoreCase(prop)) {
  120. Method method;
  121. if (args!=null && args.length>0) {
  122. method=props[i].getWriteMethod();
  123. } else {
  124. method=props[i].getReadMethod();
  125. }
  126. setResult(result, method.invoke(object, args));
  127. return;
  128. }
  129. }
  130. Field jfields[] = jclass.getFields();
  131. for (int i=0; i<jfields.length; i++) {
  132. if (jfields[i].getName().equalsIgnoreCase(prop)) {
  133. if (args!=null && args.length>0) {
  134. jfields[i].set(object, args[0]);
  135. } else {
  136. setResult(result, jfields[i].get(object));
  137. }
  138. return;
  139. }
  140. }
  141. // try a second time with the object itself, if it is of type Class
  142. if (!(jclass instanceof Class) || (jclass==object)) break;
  143. }
  144. } catch (Exception e) {
  145. setException(result, e);
  146. }
  147. }
  148. //
  149. // Helper routines for the C implementation
  150. //
  151. public static Object MakeArg(boolean b) { return new Boolean(b); }
  152. public static Object MakeArg(long l) { return new Long(l); }
  153. public static Object MakeArg(double d) { return new Double(d); }
  154. }