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