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