home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Constructor.java < prev    next >
Text File  |  1997-05-20  |  9KB  |  244 lines

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