1. /*
  2. * @(#)Constructor.java 1.33 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.reflect;
  8. import sun.reflect.ConstructorAccessor;
  9. import sun.reflect.Reflection;
  10. /**
  11. * <code>Constructor</code> provides information about, and access to, a single
  12. * constructor for a class.
  13. *
  14. * <p><code>Constructor</code> permits widening conversions to occur when matching the
  15. * actual parameters to newInstance() with the underlying
  16. * constructor's formal parameters, but throws an
  17. * <code>IllegalArgumentException</code> if a narrowing conversion would occur.
  18. *
  19. * @see Member
  20. * @see java.lang.Class
  21. * @see java.lang.Class#getConstructors()
  22. * @see java.lang.Class#getConstructor(Class[])
  23. * @see java.lang.Class#getDeclaredConstructors()
  24. *
  25. * @author Kenneth Russell
  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. private volatile ConstructorAccessor constructorAccessor;
  36. // For sharing of ConstructorAccessors. This branching structure
  37. // is currently only two levels deep (i.e., one root Constructor
  38. // and potentially many Constructor objects pointing to it.)
  39. private Constructor root;
  40. /**
  41. * Package-private constructor used by ReflectAccess to enable
  42. * instantiation of these objects in Java code from the java.lang
  43. * package via sun.reflect.LangReflectAccess.
  44. */
  45. Constructor(Class declaringClass,
  46. Class[] parameterTypes,
  47. Class[] checkedExceptions,
  48. int modifiers,
  49. int slot)
  50. {
  51. this.clazz = declaringClass;
  52. this.parameterTypes = parameterTypes;
  53. this.exceptionTypes = checkedExceptions;
  54. this.modifiers = modifiers;
  55. this.slot = slot;
  56. }
  57. /**
  58. * Package-private routine (exposed to java.lang.Class via
  59. * ReflectAccess) which returns a copy of this Constructor. The copy's
  60. * "root" field points to this Constructor.
  61. */
  62. Constructor copy() {
  63. // This routine enables sharing of ConstructorAccessor objects
  64. // among Constructor objects which refer to the same underlying
  65. // method in the VM. (All of this contortion is only necessary
  66. // because of the "accessibility" bit in AccessibleObject,
  67. // which implicitly requires that new java.lang.reflect
  68. // objects be fabricated for each reflective call on Class
  69. // objects.)
  70. Constructor res = new Constructor(clazz, parameterTypes,
  71. exceptionTypes, modifiers, slot);
  72. res.root = this;
  73. // Might as well eagerly propagate this if already present
  74. res.constructorAccessor = constructorAccessor;
  75. return res;
  76. }
  77. /**
  78. * Returns the <code>Class</code> object representing the class that declares
  79. * the constructor represented by this <code>Constructor</code> object.
  80. */
  81. public Class getDeclaringClass() {
  82. return clazz;
  83. }
  84. /**
  85. * Returns the name of this constructor, as a string. This is
  86. * always the same as the simple name of the constructor's declaring
  87. * class.
  88. */
  89. public String getName() {
  90. return getDeclaringClass().getName();
  91. }
  92. /**
  93. * Returns the Java language modifiers for the constructor
  94. * represented by this <code>Constructor</code> object, as an integer. The
  95. * <code>Modifier</code> class should be used to decode the modifiers.
  96. *
  97. * @see Modifier
  98. */
  99. public int getModifiers() {
  100. return modifiers;
  101. }
  102. /**
  103. * Returns an array of <code>Class</code> objects that represent the formal
  104. * parameter types, in declaration order, of the constructor
  105. * represented by this <code>Constructor</code> object. Returns an array of
  106. * length 0 if the underlying constructor takes no parameters.
  107. *
  108. * @return the parameter types for the constructor this object
  109. * represents
  110. */
  111. public Class[] getParameterTypes() {
  112. return Method.copy(parameterTypes);
  113. }
  114. /**
  115. * Returns an array of <code>Class</code> objects that represent the types of
  116. * of exceptions declared to be thrown by the underlying constructor
  117. * represented by this <code>Constructor</code> object. Returns an array of
  118. * length 0 if the constructor declares no exceptions in its <code>throws</code> clause.
  119. *
  120. * @return the exception types declared as being thrown by the
  121. * constructor this object represents
  122. */
  123. public Class[] getExceptionTypes() {
  124. return Method.copy(exceptionTypes);
  125. }
  126. /**
  127. * Compares this <code>Constructor</code> against the specified object.
  128. * Returns true if the objects are the same. Two <code>Constructor</code> objects are
  129. * the same if they were declared by the same class and have the
  130. * same formal parameter types.
  131. */
  132. public boolean equals(Object obj) {
  133. if (obj != null && obj instanceof Constructor) {
  134. Constructor other = (Constructor)obj;
  135. if (getDeclaringClass() == other.getDeclaringClass()) {
  136. /* Avoid unnecessary cloning */
  137. Class[] params1 = parameterTypes;
  138. Class[] params2 = other.parameterTypes;
  139. if (params1.length == params2.length) {
  140. for (int i = 0; i < params1.length; i++) {
  141. if (params1[i] != params2[i])
  142. return false;
  143. }
  144. return true;
  145. }
  146. }
  147. }
  148. return false;
  149. }
  150. /**
  151. * Returns a hashcode for this <code>Constructor</code>. The hashcode is
  152. * the same as the hashcode for the underlying constructor's
  153. * declaring class name.
  154. */
  155. public int hashCode() {
  156. return getDeclaringClass().getName().hashCode();
  157. }
  158. /**
  159. * Returns a string describing this <code>Constructor</code>. The string is
  160. * formatted as the constructor access modifiers, if any,
  161. * followed by the fully-qualified name of the declaring class,
  162. * followed by a parenthesized, comma-separated list of the
  163. * constructor's formal parameter types. For example:
  164. * <pre>
  165. * public java.util.Hashtable(int,float)
  166. * </pre>
  167. *
  168. * <p>The only possible modifiers for constructors are the access
  169. * modifiers <tt>public</tt>, <tt>protected</tt> or
  170. * <tt>private</tt>. Only one of these may appear, or none if the
  171. * constructor has default (package) access.
  172. */
  173. public String toString() {
  174. try {
  175. StringBuffer sb = new StringBuffer();
  176. int mod = getModifiers();
  177. if (mod != 0) {
  178. sb.append(Modifier.toString(mod) + " ");
  179. }
  180. sb.append(Field.getTypeName(getDeclaringClass()));
  181. sb.append("(");
  182. Class[] params = parameterTypes; // avoid clone
  183. for (int j = 0; j < params.length; j++) {
  184. sb.append(Field.getTypeName(params[j]));
  185. if (j < (params.length - 1))
  186. sb.append(",");
  187. }
  188. sb.append(")");
  189. Class[] exceptions = exceptionTypes; // avoid clone
  190. if (exceptions.length > 0) {
  191. sb.append(" throws ");
  192. for (int k = 0; k < exceptions.length; k++) {
  193. sb.append(exceptions[k].getName());
  194. if (k < (exceptions.length - 1))
  195. sb.append(",");
  196. }
  197. }
  198. return sb.toString();
  199. } catch (Exception e) {
  200. return "<" + e + ">";
  201. }
  202. }
  203. /**
  204. * Uses the constructor represented by this <code>Constructor</code> object to
  205. * create and initialize a new instance of the constructor's
  206. * declaring class, with the specified initialization parameters.
  207. * Individual parameters are automatically unwrapped to match
  208. * primitive formal parameters, and both primitive and reference
  209. * parameters are subject to method invocation conversions as necessary.
  210. *
  211. * <p>If the number of formal parameters required by the underlying constructor
  212. * is 0, the supplied <code>initargs</code> array may be of length 0 or null.
  213. *
  214. * <p>If the required access and argument checks succeed and the
  215. * instantiation will proceed, the constructor's declaring class
  216. * is initialized if it has not already been initialized.
  217. *
  218. * <p>If the constructor completes normally, returns the newly
  219. * created and initialized instance.
  220. *
  221. * @param initargs array of objects to be passed as arguments to
  222. * the constructor call; values of primitive types are wrapped in
  223. * a wrapper object of the appropriate type (e.g. a <tt>float</tt>
  224. * in a {@link java.lang.Float Float})
  225. *
  226. * @return a new object created by calling the constructor
  227. * this object represents
  228. *
  229. * @exception IllegalAccessException if this <code>Constructor</code> object
  230. * enforces Java language access control and the underlying
  231. * constructor is inaccessible.
  232. * @exception IllegalArgumentException if the number of actual
  233. * and formal parameters differ; if an unwrapping
  234. * conversion for primitive arguments fails; or if,
  235. * after possible unwrapping, a parameter value
  236. * cannot be converted to the corresponding formal
  237. * parameter type by a method invocation conversion.
  238. * @exception InstantiationException if the class that declares the
  239. * underlying constructor represents an abstract class.
  240. * @exception InvocationTargetException if the underlying constructor
  241. * throws an exception.
  242. * @exception ExceptionInInitializerError if the initialization provoked
  243. * by this method fails.
  244. */
  245. public Object newInstance(Object[] initargs)
  246. throws InstantiationException, IllegalAccessException,
  247. IllegalArgumentException, InvocationTargetException
  248. {
  249. if (!override) {
  250. if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
  251. Class caller = Reflection.getCallerClass(2);
  252. if (securityCheckCache != caller) {
  253. Reflection.ensureMemberAccess(caller, clazz, null, modifiers);
  254. securityCheckCache = caller;
  255. }
  256. }
  257. }
  258. if (constructorAccessor == null) acquireConstructorAccessor();
  259. return constructorAccessor.newInstance(initargs);
  260. }
  261. // NOTE that there is no synchronization used here. It is correct
  262. // (though not efficient) to generate more than one
  263. // ConstructorAccessor for a given Constructor. However, avoiding
  264. // synchronization will probably make the implementation more
  265. // scalable.
  266. private void acquireConstructorAccessor() {
  267. // First check to see if one has been created yet, and take it
  268. // if so.
  269. ConstructorAccessor tmp = null;
  270. if (root != null) tmp = root.getConstructorAccessor();
  271. if (tmp != null) {
  272. constructorAccessor = tmp;
  273. return;
  274. }
  275. // Otherwise fabricate one and propagate it up to the root
  276. tmp = reflectionFactory.newConstructorAccessor(this);
  277. setConstructorAccessor(tmp);
  278. }
  279. // Returns ConstructorAccessor for this Constructor object, not
  280. // looking up the chain to the root
  281. ConstructorAccessor getConstructorAccessor() {
  282. return constructorAccessor;
  283. }
  284. // Sets the ConstructorAccessor for this Constructor object and
  285. // (recursively) its root
  286. void setConstructorAccessor(ConstructorAccessor accessor) {
  287. constructorAccessor = accessor;
  288. // Propagate up
  289. if (root != null) {
  290. root.setConstructorAccessor(accessor);
  291. }
  292. }
  293. int getSlot() {
  294. return slot;
  295. }
  296. }