1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Method.java,v 1.7 2004/02/17 04:24:21 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils.synthetic.reflection;
  20. import com.sun.org.apache.xml.internal.utils.synthetic.SynthesisException;
  21. /**
  22. * A Method provides information about, and access to, a
  23. * single method on a class or interface. The reflected
  24. * method may be a class method or an instance method
  25. * (including an abstract method).
  26. * <p>
  27. * A Method permits widening conversions to occur when
  28. * matching the actual parameters to invokewith the
  29. * underlying method's formal parameters, but it throws an
  30. * IllegalArgumentException if a narrowing conversion
  31. * would occur.
  32. * <p>
  33. * Need to add method body, a la Matt's codebuffer.
  34. * That may or may not imply retaining the final return value
  35. * separately and passing in a how-to-use-it mechanism...?
  36. *
  37. * @xsl.usage internal
  38. */
  39. public class Method extends EntryPoint implements Member
  40. {
  41. /**
  42. * Insert the method's description here.
  43. * <p>
  44. * Creation date: (12-27-99 2:31:39 PM)
  45. * @param realConstructor java.lang.reflect.Constructor
  46. *
  47. * @param name
  48. * @param declaringclass
  49. */
  50. public Method(String name,
  51. com.sun.org.apache.xml.internal.utils.synthetic.Class declaringclass)
  52. {
  53. super(declaringclass);
  54. this.name = name;
  55. }
  56. /**
  57. * Insert the method's description here.
  58. * <p>
  59. * Creation date: (12-27-99 2:31:39 PM)
  60. * @param realConstructor java.lang.reflect.Constructor
  61. *
  62. * @param ctor
  63. * @param declaringclass
  64. */
  65. public Method(java.lang.reflect.Method ctor,
  66. com.sun.org.apache.xml.internal.utils.synthetic.Class declaringclass)
  67. {
  68. super(ctor, declaringclass);
  69. }
  70. /**
  71. * Insert the method's description here.
  72. * <p>
  73. * Creation date: (12-27-99 2:31:39 PM)
  74. * @param realConstructor java.lang.reflect.Constructor
  75. *
  76. * @param realmethod
  77. */
  78. public Method(java.lang.reflect.Method realmethod)
  79. {
  80. super(realmethod);
  81. }
  82. /**
  83. * Returns a hashcode for this Method. The hashcode
  84. * is computed as the exclusive-or of the hashcodes
  85. * for the underlying method's declaring class name
  86. * and the method's name.
  87. *
  88. */
  89. /**
  90. * Returns a hashcode for this Constructor. The
  91. * hashcode for a Method is the hashcode for the
  92. * underlying constructor's declaring class name,
  93. * XORed with the name of this method.
  94. */
  95. public int hashCode()
  96. {
  97. return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
  98. }
  99. /**
  100. * Invokes the underlying method represented by this
  101. * Method object, on the specified object with the
  102. * specified parameters. Individual parameters are
  103. * automatically unwrapped to match primitive
  104. * formal parameters, and both primitive and
  105. * reference parameters are subject to widening
  106. * conversions as necessary. The value returned by
  107. * the underlying method is automatically wrapped
  108. * in an object if it has a primitive type.
  109. *
  110. * Method invocation proceeds with the following
  111. * steps, in order:
  112. *
  113. * If the underlying method is static, then the
  114. * specified object argument is ignored. It may be
  115. * null.
  116. *
  117. * Otherwise, the method is an instance method. If
  118. * the specified object argument is null, the
  119. * invocation throws a NullPointerException.
  120. * Otherwise, if the specified object argument is not
  121. * an instance of the class or interface declaring the
  122. * underlying method, the invocation throws an
  123. * IllegalArgumentException.
  124. *
  125. * If this Method object enforces Java language access
  126. * control and the underlying method is inaccessible,
  127. * the invocation throws an IllegalAccessException.
  128. *
  129. * If the number of actual parameters supplied via
  130. * args is different from the number of formal
  131. * parameters required by the underlying method, the
  132. * invocation throws an IllegalArgumentException.
  133. *
  134. * For each actual parameter in the supplied args
  135. * array:
  136. *
  137. * If the corresponding formal parameter has a
  138. * primitive type, an unwrapping conversion is
  139. * attempted to convert the object value to a value of
  140. * a primitive type. If this attempt fails, the
  141. * invocation throws an IllegalArgumentException.
  142. *
  143. * If, after possible unwrapping, the parameter value
  144. * cannot be converted to the corresponding formal
  145. * parameter type by an identity or widening
  146. * conversion, the invocation throws an
  147. * IllegalArgumentException.
  148. *
  149. * If the underlying method is an instance method, it
  150. * is invoked using dynamic method lookup as
  151. * documented in The Java Language Specification,
  152. * section 15.11.4.4; in particular, overriding based
  153. * on the runtime type of the target object will occur.
  154. *
  155. * If the underlying method is static, it is invoked as
  156. * exactly the method on the declaring class.
  157. *
  158. * Control transfers to the underlying method. If the
  159. * method completes abruptly by throwing an
  160. * exception, the exception is placed in an
  161. * InvocationTargetException and thrown in turn to
  162. * the caller of invoke.
  163. *
  164. * If the method completes normally, the value it
  165. * returns is returned to the caller of invoke; if the
  166. * value has a primitive type, it is first appropriately
  167. * wrapped in an object. If the underlying method
  168. * return type is void, the invocation returns null.
  169. *
  170. * Throws: IllegalAccessException
  171. * if the underlying method is inaccessible.
  172. * Throws: IllegalArgumentException
  173. * if the number of actual and formal
  174. * parameters differ, or if an unwrapping
  175. * conversion fails.
  176. * Throws: InvocationTargetException
  177. * if the underlying method throws an
  178. * exception.
  179. * Throws: NullPointerException
  180. * if the specified object is null.
  181. *
  182. * @param obj
  183. * @param args
  184. *
  185. *
  186. * @throws IllegalAccessException
  187. * @throws IllegalArgumentException
  188. * @throws java.lang.reflect.InvocationTargetException
  189. */
  190. public Object invoke(Object obj, Object args[])
  191. throws IllegalAccessException, IllegalArgumentException,
  192. java.lang.reflect.InvocationTargetException
  193. {
  194. if (realep != null)
  195. return ((java.lang.reflect.Method) realep).invoke(obj, args);
  196. else
  197. throw new IllegalAccessException(
  198. "Un-reified com.sun.org.apache.xml.internal.utils.synthetic.Class doesn't yet support invocation");
  199. }
  200. /**
  201. * Method setReturnType
  202. *
  203. *
  204. * @param returntype
  205. *
  206. * @throws SynthesisException
  207. */
  208. public void setReturnType(com.sun.org.apache.xml.internal.utils.synthetic.Class returntype)
  209. throws SynthesisException
  210. {
  211. if (realep != null)
  212. throw new SynthesisException(SynthesisException.REIFIED);
  213. this.returntype = returntype;
  214. }
  215. }