001/* MBeanConstructorInfo.java -- Information about a bean's constructor.
002   Copyright (C) 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package javax.management;
039
040import java.lang.reflect.Constructor;
041import java.lang.reflect.Type;
042
043import java.util.Arrays;
044
045/**
046 * Describes the constructors of a management bean.
047 * The information in this class is immutable as standard.
048 * Of course, subclasses may change this, but this
049 * behaviour is not recommended.
050 *
051 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
052 * @since 1.5
053 */
054public class MBeanConstructorInfo
055  extends MBeanFeatureInfo
056  implements Cloneable
057{
058
059  /**
060   * Compatible with JDK 1.5
061   */
062  private static final long serialVersionUID = 4433990064191844427L;
063
064  /**
065   * The signature of the constructor i.e. the argument types.
066   */
067  private MBeanParameterInfo[] signature;
068
069  /**
070   * Constructs a @link{MBeanConstructorInfo} with the specified
071   * description using the given constructor.  Each parameter is
072   * described merely by its type; the name and description are
073   * <code>null</code>.
074   *
075   * @param desc a description of the attribute.
076   * @param cons the constructor.
077   */
078  // API issue with lack of <?> on Constructor
079  @SuppressWarnings("unchecked")
080  public MBeanConstructorInfo(String desc, Constructor cons)
081  {
082    super(cons.getName(), desc);
083    Type[] paramTypes = cons.getGenericParameterTypes();
084    signature = new MBeanParameterInfo[paramTypes.length];
085    for (int a = 0; a < paramTypes.length; ++a)
086      {
087        Type t = paramTypes[a];
088        if (t instanceof Class)
089          signature[a] = new MBeanParameterInfo(null,
090                                                ((Class<?>) t).getName(),
091                                                null);
092        else
093          signature[a] = new MBeanParameterInfo(null, t.toString(), null);
094      }
095  }
096
097  /**
098   * Constructs a @link{MBeanConstructorInfo} with the specified
099   * name, description and parameter information. A <code>null</code>
100   * value for the parameter information is the same as passing in
101   * an empty array.  A copy of the parameter array is taken, so
102   * later changes have no effect.
103   *
104   * @param name the name of the constructor.
105   * @param desc a description of the constructor.
106   * @param sig the signature of the constructor, as a series
107   *            of {@link MBeanParameterInfo} objects, one for
108   *            each parameter.
109   */
110  public MBeanConstructorInfo(String name, String desc,
111                              MBeanParameterInfo[] sig)
112  {
113    super(name, desc);
114    if (sig == null)
115      signature = new MBeanParameterInfo[0];
116    else
117      {
118        signature = new MBeanParameterInfo[sig.length];
119        System.arraycopy(sig, 0, signature, 0, sig.length);
120      }
121  }
122
123  /**
124   * Returns a clone of this instance.  The clone is created
125   * using just the method provided by {@link java.lang.Object}.
126   * Thus, the clone is just a shallow clone as returned by
127   * that method, and does not contain any deeper cloning based
128   * on the subject of this class.
129   *
130   * @return a clone of this instance.
131   * @see java.lang.Cloneable
132   */
133  public Object clone()
134  {
135    try
136      {
137        return super.clone();
138      }
139    catch (CloneNotSupportedException e)
140      {
141        /* This shouldn't happen; we implement Cloneable */
142        throw new IllegalStateException("clone() called on " +
143                                        "non-cloneable object.");
144      }
145  }
146
147  /**
148   * Compares this feature with the supplied object.  This returns
149   * true iff the object is an instance of {@link
150   * MBeanConstructorInfo}, {@link Object#equals()} returns true for a
151   * comparison of both the name and description of this notification
152   * with that of the specified object (performed by the superclass),
153   * and the two signature arrays contain the same elements in the
154   * same order (but one may be longer than the other).
155   *
156   * @param obj the object to compare.
157   * @return true if the object is a {@link MBeanConstructorInfo}
158   *         instance, 
159   *         <code>name.equals(object.getName())</code>,
160   *         <code>description.equals(object.getDescription())</code>
161   *         and the corresponding elements of the signature arrays are
162   *         equal.
163   */
164  public boolean equals(Object obj)
165  {
166    if (!(obj instanceof MBeanConstructorInfo))
167      return false;
168    if (!(super.equals(obj)))
169      return false;
170    MBeanConstructorInfo o = (MBeanConstructorInfo) obj;
171    MBeanParameterInfo[] sig = o.getSignature();
172    for (int a = 0; a < signature.length; ++a)
173      {
174        if (a == sig.length)
175          return true;
176        if (!(signature[a].equals(sig[a])))
177          return false;
178      }
179    return true;
180  }
181  
182  /**
183   * Returns the constructor's signature, in the form of
184   * information on each parameter.  Each parameter is
185   * described by an instance of {@link MBeanParameterInfo}.
186   * The returned array is a shallow copy of the array used
187   * by this instance, so changing which elements are stored
188   * in the array won't affect the array used by this, but
189   * changing the actual elements will affect the ones used
190   * here.
191   *
192   * @return an array of {@link MBeanParameterInfo} objects,
193   *         describing the constructor parameters.
194   */
195  public MBeanParameterInfo[] getSignature()
196  {
197    return (MBeanParameterInfo[]) signature.clone();
198  }
199
200  /**
201   * Returns the hashcode of the constructor information as the sum
202   * of the hashcode of the superclass and the hashcode of the parameter
203   * array.
204   *
205   * @return the hashcode of the constructor information.
206   */
207  public int hashCode()
208  {
209    return super.hashCode() + Arrays.hashCode(signature);
210  }
211
212  /**
213   * <p>
214   * Returns a textual representation of this instance.  This
215   * is constructed using the class name
216   * (<code>javax.management.MBeanConstructorInfo</code>),
217   * the name and description of the constructor and the 
218   * contents of the array of parameters.
219   * </p>
220   * <p>
221   * As instances of this class are immutable, the return value
222   * is computed just once for each instance and reused
223   * throughout its life.
224   * </p>
225   *
226   * @return a @link{java.lang.String} instance representing
227   *         the instance in textual form.
228   */
229  public String toString()
230  {
231    if (string == null)
232      {
233        super.toString();
234        string = string.substring(0, string.length() - 1) 
235          + ",signature=" + Arrays.toString(signature)
236          + "]";
237      }
238    return string;
239  }
240
241}