1. /*
  2. * @(#)Statement.java 1.29 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.beans;
  8. import java.lang.reflect.AccessibleObject;
  9. import java.lang.reflect.Array;
  10. import java.lang.reflect.Constructor;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.lang.reflect.Method;
  13. import com.sun.beans.ObjectHandler;
  14. /**
  15. * A <code>Statement</code> object represents a primitive statement
  16. * in which a single method is applied to a target and
  17. * a set of arguments - as in <code>"a.setFoo(b)"</code>.
  18. * Note that where this example uses names
  19. * to denote the target and its argument, a statement
  20. * object does not require a name space and is constructed with
  21. * the values themselves.
  22. * The statement object associates the named method
  23. * with its environment as a simple set of values:
  24. * the target and an array of argument values.
  25. *
  26. * @since 1.4
  27. *
  28. * @version 1.29 12/19/03
  29. * @author Philip Milne
  30. */
  31. public class Statement {
  32. private static Object[] emptyArray = new Object[]{};
  33. static ExceptionListener defaultExceptionListener = new ExceptionListener() {
  34. public void exceptionThrown(Exception e) {
  35. System.err.println(e);
  36. // e.printStackTrace();
  37. System.err.println("Continuing ...");
  38. }
  39. };
  40. Object target;
  41. String methodName;
  42. Object[] arguments;
  43. /**
  44. * Creates a new <code>Statement</code> object with a <code>target</code>,
  45. * <code>methodName</code> and <code>arguments</code> as per the parameters.
  46. *
  47. * @param target The target of this statement.
  48. * @param methodName The methodName of this statement.
  49. * @param arguments The arguments of this statement. If <code>null</code> then an empty array will be used.
  50. *
  51. */
  52. public Statement(Object target, String methodName, Object[] arguments) {
  53. this.target = target;
  54. this.methodName = methodName;
  55. this.arguments = (arguments == null) ? emptyArray : arguments;
  56. }
  57. /**
  58. * Returns the target of this statement.
  59. *
  60. * @return The target of this statement.
  61. */
  62. public Object getTarget() {
  63. return target;
  64. }
  65. /**
  66. * Returns the name of the method.
  67. *
  68. * @return The name of the method.
  69. */
  70. public String getMethodName() {
  71. return methodName;
  72. }
  73. /**
  74. * Returns the arguments of this statement.
  75. *
  76. * @return the arguments of this statement.
  77. */
  78. public Object[] getArguments() {
  79. return arguments;
  80. }
  81. /**
  82. * The execute method finds a method whose name is the same
  83. * as the methodName property, and invokes the method on
  84. * the target.
  85. *
  86. * When the target's class defines many methods with the given name
  87. * the implementation should choose the most specific method using
  88. * the algorithm specified in the Java Language Specification
  89. * (15.11). The dynamic class of the target and arguments are used
  90. * in place of the compile-time type information and, like the
  91. * <code>java.lang.reflect.Method</code> class itself, conversion between
  92. * primitive values and their associated wrapper classes is handled
  93. * internally.
  94. * <p>
  95. * The following method types are handled as special cases:
  96. * <ul>
  97. * <li>
  98. * Static methods may be called by using a class object as the target.
  99. * <li>
  100. * The reserved method name "new" may be used to call a class's constructor
  101. * as if all classes defined static "new" methods. Constructor invocations
  102. * are typically considered <code>Expression</code>s rather than <code>Statement</code>s
  103. * as they return a value.
  104. * <li>
  105. * The method names "get" and "set" defined in the <code>java.util.List</code>
  106. * interface may also be applied to array instances, mapping to
  107. * the static methods of the same name in the <code>Array</code> class.
  108. * </ul>
  109. */
  110. public void execute() throws Exception {
  111. invoke();
  112. }
  113. Object invoke() throws Exception {
  114. Object target = getTarget();
  115. String methodName = getMethodName();
  116. if (target == null || methodName == null) {
  117. throw new NullPointerException((target == null ? "target" :
  118. "methodName") + " should not be null");
  119. }
  120. Object[] arguments = getArguments();
  121. // Class.forName() won't load classes outside
  122. // of core from a class inside core. Special
  123. // case this method.
  124. if (target == Class.class && methodName.equals("forName")) {
  125. return ObjectHandler.classForName((String)arguments[0]);
  126. }
  127. Class[] argClasses = new Class[arguments.length];
  128. for(int i = 0; i < arguments.length; i++) {
  129. argClasses[i] = (arguments[i] == null) ? null : arguments[i].getClass();
  130. }
  131. AccessibleObject m = null;
  132. if (target instanceof Class) {
  133. /*
  134. For class methods, simluate the effect of a meta class
  135. by taking the union of the static methods of the
  136. actual class, with the instance methods of "Class.class"
  137. and the overloaded "newInstance" methods defined by the
  138. constructors.
  139. This way "System.class", for example, will perform both
  140. the static method getProperties() and the instance method
  141. getSuperclass() defined in "Class.class".
  142. */
  143. if (methodName.equals("new")) {
  144. methodName = "newInstance";
  145. }
  146. // Provide a short form for array instantiation by faking an nary-constructor.
  147. if (methodName.equals("newInstance") && ((Class)target).isArray()) {
  148. Object result = Array.newInstance(((Class)target).getComponentType(), arguments.length);
  149. for(int i = 0; i < arguments.length; i++) {
  150. Array.set(result, i, arguments[i]);
  151. }
  152. return result;
  153. }
  154. if (methodName.equals("newInstance") && arguments.length != 0) {
  155. // The Character class, as of 1.4, does not have a constructor
  156. // which takes a String. All of the other "wrapper" classes
  157. // for Java's primitive types have a String constructor so we
  158. // fake such a constructor here so that this special case can be
  159. // ignored elsewhere.
  160. if (target == Character.class && arguments.length == 1 &&
  161. argClasses[0] == String.class) {
  162. return new Character(((String)arguments[0]).charAt(0));
  163. }
  164. m = ReflectionUtils.getConstructor((Class)target, argClasses);
  165. }
  166. if (m == null) {
  167. m = ReflectionUtils.getMethod((Class)target, methodName, argClasses);
  168. }
  169. if (m == null) {
  170. m = ReflectionUtils.getMethod(Class.class, methodName, argClasses);
  171. }
  172. }
  173. else {
  174. /*
  175. This special casing of arrays is not necessary, but makes files
  176. involving arrays much shorter and simplifies the archiving infrastrcure.
  177. The Array.set() method introduces an unusual idea - that of a static method
  178. changing the state of an instance. Normally statements with side
  179. effects on objects are instance methods of the objects themselves
  180. and we reinstate this rule (perhaps temporarily) by special-casing arrays.
  181. */
  182. if (target.getClass().isArray() &&
  183. (methodName.equals("set") || methodName.equals("get"))) {
  184. int index = ((Integer)arguments[0]).intValue();
  185. if (methodName.equals("get")) {
  186. return Array.get(target, index);
  187. }
  188. else {
  189. Array.set(target, index, arguments[1]);
  190. return null;
  191. }
  192. }
  193. m = ReflectionUtils.getMethod(target.getClass(), methodName, argClasses);
  194. }
  195. if (m != null) {
  196. try {
  197. if (m instanceof Method) {
  198. return ((Method)m).invoke(target, arguments);
  199. }
  200. else {
  201. return ((Constructor)m).newInstance(arguments);
  202. }
  203. }
  204. catch (IllegalAccessException iae) {
  205. throw new Exception("Statement cannot invoke: " +
  206. methodName + " on " + target.getClass(),
  207. iae);
  208. }
  209. catch (InvocationTargetException ite) {
  210. Throwable te = ite.getTargetException();
  211. if (te instanceof Exception) {
  212. throw (Exception)te;
  213. }
  214. else {
  215. throw ite;
  216. }
  217. }
  218. }
  219. throw new NoSuchMethodException(toString());
  220. }
  221. String instanceName(Object instance) {
  222. if (instance == null) {
  223. return "null";
  224. } else if (instance.getClass() == String.class) {
  225. return "\""+(String)instance + "\"";
  226. } else {
  227. // Note: there is a minor problem with using the non-caching
  228. // NameGenerator method. The return value will not have
  229. // specific information about the inner class name. For example,
  230. // In 1.4.2 an inner class would be represented as JList$1 now
  231. // would be named Class.
  232. return NameGenerator.unqualifiedClassName(instance.getClass());
  233. }
  234. }
  235. /**
  236. * Prints the value of this statement using a Java-style syntax.
  237. */
  238. public String toString() {
  239. // Respect a subclass's implementation here.
  240. Object target = getTarget();
  241. String methodName = getMethodName();
  242. Object[] arguments = getArguments();
  243. StringBuffer result = new StringBuffer(instanceName(target) + "." + methodName + "(");
  244. int n = arguments.length;
  245. for(int i = 0; i < n; i++) {
  246. result.append(instanceName(arguments[i]));
  247. if (i != n -1) {
  248. result.append(", ");
  249. }
  250. }
  251. result.append(");");
  252. return result.toString();
  253. }
  254. }