KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > MBeanConstructorInfo


1 /*
2  * @(#)MBeanConstructorInfo.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10 import java.lang.reflect.Constructor JavaDoc;
11 import java.util.Arrays JavaDoc;
12
13 /**
14  * Describes a constructor exposed by an MBean. Instances of this
15  * class are immutable. Subclasses may be mutable but this is not
16  * recommended.
17  *
18  * @since 1.5
19  */

20 public class MBeanConstructorInfo extends MBeanFeatureInfo JavaDoc implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
21
22     /* Serial version */
23     static final long serialVersionUID = 4433990064191844427L;
24         
25     static final MBeanConstructorInfo JavaDoc[] NO_CONSTRUCTORS =
26     new MBeanConstructorInfo JavaDoc[0];
27
28     /** @see MBeanInfo#immutable */
29     private final transient boolean immutable;
30
31     /**
32      * @serial The signature of the method, that is, the class names of the arguments.
33      */

34     private final MBeanParameterInfo JavaDoc[] signature;
35
36     /**
37      * Constructs an <CODE>MBeanConstructorInfo</CODE> object.
38      *
39      * @param description A human readable description of the operation.
40      * @param constructor The <CODE>java.lang.reflect.Constructor</CODE>
41      * object describing the MBean constructor.
42      */

43     public MBeanConstructorInfo(String JavaDoc description, Constructor JavaDoc constructor) {
44     this(constructor.getName(), description,
45          constructorSignature(constructor));
46     }
47
48     /**
49      * Constructs an <CODE>MBeanConstructorInfo</CODE> object.
50      *
51      * @param name The name of the constructor.
52      * @param signature <CODE>MBeanParameterInfo</CODE> objects
53      * describing the parameters(arguments) of the constructor. This
54      * may be null with the same effect as a zero-length array.
55      * @param description A human readable description of the constructor.
56      */

57     public MBeanConstructorInfo(String JavaDoc name,
58                 String JavaDoc description,
59                 MBeanParameterInfo JavaDoc[] signature)
60         throws IllegalArgumentException JavaDoc {
61     super(name, description);
62
63
64     if (signature == null || signature.length == 0)
65         signature = MBeanParameterInfo.NO_PARAMS;
66     else
67         signature = (MBeanParameterInfo JavaDoc[]) signature.clone();
68     this.signature = signature;
69     this.immutable =
70         MBeanInfo.isImmutableClass(this.getClass(),
71                        MBeanConstructorInfo JavaDoc.class);
72     }
73
74
75     /**
76      * <p>Returns a shallow clone of this instance. The clone is
77      * obtained by simply calling <tt>super.clone()</tt>, thus calling
78      * the default native shallow cloning mechanism implemented by
79      * <tt>Object.clone()</tt>. No deeper cloning of any internal
80      * field is made.</p>
81      *
82      * <p>Since this class is immutable, cloning is chiefly of
83      * interest to subclasses.</p>
84      */

85      public Object JavaDoc clone () {
86      try {
87          return super.clone() ;
88      } catch (CloneNotSupportedException JavaDoc e) {
89          // should not happen as this class is cloneable
90
return null;
91      }
92      }
93     
94     /**
95      * <p>Returns the list of parameters for this constructor. Each
96      * parameter is described by an <CODE>MBeanParameterInfo</CODE>
97      * object.</p>
98      *
99      * <p>The returned array is a shallow copy of the internal array,
100      * which means that it is a copy of the internal array of
101      * references to the <CODE>MBeanParameterInfo</CODE> objects but
102      * that each referenced <CODE>MBeanParameterInfo</CODE> object is
103      * not copied.</p>
104      *
105      * @return An array of <CODE>MBeanParameterInfo</CODE> objects.
106      */

107     public MBeanParameterInfo JavaDoc[] getSignature() {
108     if (signature.length == 0)
109             return signature;
110     else
111         return (MBeanParameterInfo JavaDoc[]) signature.clone();
112     }
113
114     private MBeanParameterInfo JavaDoc[] fastGetSignature() {
115     if (immutable)
116         return signature;
117     else
118         return getSignature();
119     }
120
121     /**
122      * Compare this MBeanConstructorInfo to another.
123      *
124      * @param o the object to compare to.
125      *
126      * @return true iff <code>o</code> is an MBeanConstructorInfo such
127      * that its {@link #getName()}, {@link #getDescription()}, and
128      * {@link #getSignature()} values are equal (not necessarily
129      * identical) to those of this MBeanConstructorInfo. Two
130      * signature arrays are equal if their elements are pairwise
131      * equal.
132      */

133     public boolean equals(Object JavaDoc o) {
134     if (o == this)
135         return true;
136     if (!(o instanceof MBeanConstructorInfo JavaDoc))
137         return false;
138     MBeanConstructorInfo JavaDoc p = (MBeanConstructorInfo JavaDoc) o;
139     return (p.getName().equals(getName()) &&
140         p.getDescription().equals(getDescription()) &&
141         Arrays.equals(p.fastGetSignature(), fastGetSignature()));
142     }
143
144     /* Unlike attributes and operations, it's quite likely we'll have
145        more than one constructor with the same name and even
146        description, so we include the parameter array in the hashcode.
147        We don't include the description, though, because it could be
148        quite long and yet the same between constructors. */

149     public int hashCode() {
150     int hash = getName().hashCode();
151     MBeanParameterInfo JavaDoc[] sig = fastGetSignature();
152     for (int i = 0; i < sig.length; i++)
153         hash ^= sig[i].hashCode();
154     return hash;
155     }
156
157     private static MBeanParameterInfo JavaDoc[] constructorSignature(Constructor JavaDoc cn) {
158     final Class JavaDoc[] classes = cn.getParameterTypes();
159     final MBeanParameterInfo JavaDoc[] params =
160         new MBeanParameterInfo JavaDoc[classes.length];
161
162     for (int i = 0; i < classes.length; i++) {
163         final String JavaDoc pn = "p" + (i + 1);
164         params[i] = new MBeanParameterInfo JavaDoc(pn, classes[i].getName(), "");
165     }
166
167     return params;
168     }
169 }
170
Popular Tags