1. /*
  2. * @(#)Constructor.java 1.23 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang.reflect;
  11. /**
  12. * <code>Constructor</code> provides information about, and access to, a single
  13. * constructor for a class.
  14. *
  15. * <p><code>Constructor</code> permits widening conversions to occur when matching the
  16. * actual parameters to newInstance() with the underlying
  17. * constructor's formal parameters, but throws an
  18. * <code>IllegalArgumentException</code> if a narrowing conversion would occur.
  19. *
  20. * @see Member
  21. * @see java.lang.Class
  22. * @see java.lang.Class#getConstructors()
  23. * @see java.lang.Class#getConstructor(Class[])
  24. * @see java.lang.Class#getDeclaredConstructors()
  25. *
  26. * @author Nakul Saraiya
  27. */
  28. public final
  29. class Constructor extends AccessibleObject implements Member {
  30. private Class clazz;
  31. private int slot;
  32. private Class[] parameterTypes;
  33. private Class[] exceptionTypes;
  34. private int modifiers;
  35. /**
  36. * Constructor. Only the Java Virtual Machine may construct
  37. * a Constructor.
  38. */
  39. private Constructor() {}
  40. /**
  41. * Returns the <code>Class</code> object representing the class that declares
  42. * the constructor represented by this <code>Constructor</code> object.
  43. */
  44. public Class getDeclaringClass() {
  45. return clazz;
  46. }
  47. /**
  48. * Returns the name of this constructor, as a string. This is
  49. * always the same as the simple name of the constructor's declaring
  50. * class.
  51. */
  52. public String getName() {
  53. return getDeclaringClass().getName();
  54. }
  55. /**
  56. * Returns the Java language modifiers for the constructor
  57. * represented by this <code>Constructor</code> object, as an integer. The
  58. * <code>Modifier</code> class should be used to decode the modifiers.
  59. *
  60. * @see Modifier
  61. */
  62. public int getModifiers() {
  63. return modifiers;
  64. }
  65. /**
  66. * Returns an array of <code>Class</code> objects that represent the formal
  67. * parameter types, in declaration order, of the constructor
  68. * represented by this <code>Constructor</code> object. Returns an array of
  69. * length 0 if the underlying constructor takes no parameters.
  70. */
  71. public Class[] getParameterTypes() {
  72. return Method.copy(parameterTypes);
  73. }
  74. /**
  75. * Returns an array of <code>Class</code> objects that represent the types of
  76. * of exceptions declared to be thrown by the underlying constructor
  77. * represented by this <code>Constructor</code> object. Returns an array of
  78. * length 0 if the constructor declares no exceptions in its <code>throws</code> clause.
  79. */
  80. public Class[] getExceptionTypes() {
  81. return Method.copy(exceptionTypes);
  82. }
  83. /**
  84. * Compares this <code>Constructor</code> against the specified object.
  85. * Returns true if the objects are the same. Two <code>Constructor</code> objects are
  86. * the same if they were declared by the same class and have the
  87. * same formal parameter types.
  88. */
  89. public boolean equals(Object obj) {
  90. if (obj != null && obj instanceof Constructor) {
  91. Constructor other = (Constructor)obj;
  92. if (getDeclaringClass() == other.getDeclaringClass()) {
  93. /* Avoid unnecessary cloning */
  94. Class[] params1 = parameterTypes;
  95. Class[] params2 = other.parameterTypes;
  96. if (params1.length == params2.length) {
  97. for (int i = 0; i < params1.length; i++) {
  98. if (params1[i] != params2[i])
  99. return false;
  100. }
  101. return true;
  102. }
  103. }
  104. }
  105. return false;
  106. }
  107. /**
  108. * Returns a hashcode for this <code>Constructor</code>. The hashcode is
  109. * the same as the hashcode for the underlying constructor's
  110. * declaring class name.
  111. */
  112. public int hashCode() {
  113. return getDeclaringClass().getName().hashCode();
  114. }
  115. /**
  116. * Returns a string describing this <code>Constructor</code>. The string is
  117. * formatted as the constructor access modifiers, if any,
  118. * followed by the fully-qualified name of the declaring class,
  119. * followed by a parenthesized, comma-separated list of the
  120. * constructor's formal parameter types. For example:
  121. * <pre>
  122. * public java.util.Hashtable(int,float)
  123. * </pre>
  124. *
  125. * <p>The only possible modifiers for constructors are the access
  126. * modifiers <tt>public</tt>, <tt>protected</tt> or
  127. * <tt>private</tt>. Only one of these may appear, or none if the
  128. * constructor has default (package) access.
  129. */
  130. public String toString() {
  131. try {
  132. StringBuffer sb = new StringBuffer();
  133. int mod = getModifiers();
  134. if (mod != 0) {
  135. sb.append(Modifier.toString(mod) + " ");
  136. }
  137. sb.append(Field.getTypeName(getDeclaringClass()));
  138. sb.append("(");
  139. Class[] params = parameterTypes; // avoid clone
  140. for (int j = 0; j < params.length; j++) {
  141. sb.append(Field.getTypeName(params[j]));
  142. if (j < (params.length - 1))
  143. sb.append(",");
  144. }
  145. sb.append(")");
  146. Class[] exceptions = exceptionTypes; // avoid clone
  147. if (exceptions.length > 0) {
  148. sb.append(" throws ");
  149. for (int k = 0; k < exceptions.length; k++) {
  150. sb.append(exceptions[k].getName());
  151. if (k < (exceptions.length - 1))
  152. sb.append(",");
  153. }
  154. }
  155. return sb.toString();
  156. } catch (Exception e) {
  157. return "<" + e + ">";
  158. }
  159. }
  160. /**
  161. * Uses the constructor represented by this <code>Constructor</code> object to
  162. * create and initialize a new instance of the constructor's
  163. * declaring class, with the specified initialization parameters.
  164. * Individual parameters are automatically unwrapped to match
  165. * primitive formal parameters, and both primitive and reference
  166. * parameters are subject to method invocation conversions as necessary.
  167. * Returns the newly created and initialized object.
  168. *
  169. * <p>Creation proceeds with the following steps, in order:
  170. *
  171. * <p>If the class that declares the underlying constructor
  172. * represents an abstract class, the creation throws an
  173. * <code>InstantiationException</code>.
  174. *
  175. * <p>If this <code>Constructor</code> object enforces Java language access
  176. * control and the underlying constructor is inaccessible, the
  177. * creation throws an <code>IllegalAccessException</code>.
  178. *
  179. * <p>If the number of actual parameters supplied via <code>initargs</code> is
  180. * different from the number of formal parameters required by the
  181. * underlying constructor, the creation throws an
  182. * <code>IllegalArgumentException</code>.
  183. *
  184. * <p>A new instance of the constructor's declaring class is
  185. * created, and its fields are initialized to their default
  186. * initial values.
  187. *
  188. * <p>For each actual parameter in the supplied <code>initargs</code> array:
  189. *
  190. * <p>If the corresponding formal parameter has a primitive type,
  191. * an unwrapping conversion is attempted to convert the object
  192. * value to a value of the primitive type. If this attempt fails,
  193. * the creation throws an <code>IllegalArgumentException</code>.
  194. *
  195. * <p>If, after possible unwrapping, the parameter value cannot be
  196. * converted to the corresponding formal parameter type by a
  197. * method invocation conversion, the creation throws an
  198. * <code>IllegalArgumentException</code>.
  199. *
  200. * <p> The constructor's declaring class is initialized if it has
  201. * not already been initialized. A new instance of the constructor's
  202. * declaring class is created, and its fields are initialized to
  203. * their default initial values.
  204. *
  205. * <p>Control transfers to the underlying constructor to
  206. * initialize the new instance. If the constructor completes
  207. * abruptly by throwing an exception, the exception is placed in
  208. * an <code>InvocationTargetException</code> and thrown in turn to the caller
  209. * of <code>newInstance</code>.
  210. *
  211. * <p>If the constructor completes normally, returns the newly
  212. * created and initialized instance.
  213. *
  214. * @exception IllegalAccessException if the underlying constructor
  215. * is inaccessible.
  216. * @exception IllegalArgumentException if the number of actual and formal
  217. * parameters differ, or if an unwrapping or method
  218. * invocation conversion fails.
  219. * @exception InstantiationException if the class that declares the
  220. * underlying constructor represents an abstract class.
  221. * @exception InvocationTargetException if the underlying constructor
  222. * throws an exception.
  223. * @exception ExceptionInInitializerError if the initialization provoked
  224. * by this method fails.
  225. */
  226. public native Object newInstance(Object[] initargs)
  227. throws InstantiationException, IllegalAccessException,
  228. IllegalArgumentException, InvocationTargetException;
  229. }