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. package javax.management;
  8. import java.lang.reflect.Constructor;
  9. import java.util.Arrays;
  10. /**
  11. * Describes a constructor exposed by an MBean. Instances of this
  12. * class are immutable. Subclasses may be mutable but this is not
  13. * recommended.
  14. *
  15. * @since 1.5
  16. */
  17. public class MBeanConstructorInfo extends MBeanFeatureInfo implements java.io.Serializable, Cloneable {
  18. /* Serial version */
  19. static final long serialVersionUID = 4433990064191844427L;
  20. static final MBeanConstructorInfo[] NO_CONSTRUCTORS =
  21. new MBeanConstructorInfo[0];
  22. /** @see MBeanInfo#immutable */
  23. private final transient boolean immutable;
  24. /**
  25. * @serial The signature of the method, that is, the class names of the arguments.
  26. */
  27. private final MBeanParameterInfo[] signature;
  28. /**
  29. * Constructs an <CODE>MBeanConstructorInfo</CODE> object.
  30. *
  31. * @param description A human readable description of the operation.
  32. * @param constructor The <CODE>java.lang.reflect.Constructor</CODE>
  33. * object describing the MBean constructor.
  34. */
  35. public MBeanConstructorInfo(String description, Constructor constructor) {
  36. this(constructor.getName(), description,
  37. constructorSignature(constructor));
  38. }
  39. /**
  40. * Constructs an <CODE>MBeanConstructorInfo</CODE> object.
  41. *
  42. * @param name The name of the constructor.
  43. * @param signature <CODE>MBeanParameterInfo</CODE> objects
  44. * describing the parameters(arguments) of the constructor. This
  45. * may be null with the same effect as a zero-length array.
  46. * @param description A human readable description of the constructor.
  47. */
  48. public MBeanConstructorInfo(String name,
  49. String description,
  50. MBeanParameterInfo[] signature)
  51. throws IllegalArgumentException {
  52. super(name, description);
  53. if (signature == null || signature.length == 0)
  54. signature = MBeanParameterInfo.NO_PARAMS;
  55. else
  56. signature = (MBeanParameterInfo[]) signature.clone();
  57. this.signature = signature;
  58. this.immutable =
  59. MBeanInfo.isImmutableClass(this.getClass(),
  60. MBeanConstructorInfo.class);
  61. }
  62. /**
  63. * <p>Returns a shallow clone of this instance. The clone is
  64. * obtained by simply calling <tt>super.clone()</tt>, thus calling
  65. * the default native shallow cloning mechanism implemented by
  66. * <tt>Object.clone()</tt>. No deeper cloning of any internal
  67. * field is made.</p>
  68. *
  69. * <p>Since this class is immutable, cloning is chiefly of
  70. * interest to subclasses.</p>
  71. */
  72. public Object clone () {
  73. try {
  74. return super.clone() ;
  75. } catch (CloneNotSupportedException e) {
  76. // should not happen as this class is cloneable
  77. return null;
  78. }
  79. }
  80. /**
  81. * <p>Returns the list of parameters for this constructor. Each
  82. * parameter is described by an <CODE>MBeanParameterInfo</CODE>
  83. * object.</p>
  84. *
  85. * <p>The returned array is a shallow copy of the internal array,
  86. * which means that it is a copy of the internal array of
  87. * references to the <CODE>MBeanParameterInfo</CODE> objects but
  88. * that each referenced <CODE>MBeanParameterInfo</CODE> object is
  89. * not copied.</p>
  90. *
  91. * @return An array of <CODE>MBeanParameterInfo</CODE> objects.
  92. */
  93. public MBeanParameterInfo[] getSignature() {
  94. if (signature.length == 0)
  95. return signature;
  96. else
  97. return (MBeanParameterInfo[]) signature.clone();
  98. }
  99. private MBeanParameterInfo[] fastGetSignature() {
  100. if (immutable)
  101. return signature;
  102. else
  103. return getSignature();
  104. }
  105. /**
  106. * Compare this MBeanConstructorInfo to another.
  107. *
  108. * @param o the object to compare to.
  109. *
  110. * @return true iff <code>o</code> is an MBeanConstructorInfo such
  111. * that its {@link #getName()}, {@link #getDescription()}, and
  112. * {@link #getSignature()} values are equal (not necessarily
  113. * identical) to those of this MBeanConstructorInfo. Two
  114. * signature arrays are equal if their elements are pairwise
  115. * equal.
  116. */
  117. public boolean equals(Object o) {
  118. if (o == this)
  119. return true;
  120. if (!(o instanceof MBeanConstructorInfo))
  121. return false;
  122. MBeanConstructorInfo p = (MBeanConstructorInfo) o;
  123. return (p.getName().equals(getName()) &&
  124. p.getDescription().equals(getDescription()) &&
  125. Arrays.equals(p.fastGetSignature(), fastGetSignature()));
  126. }
  127. /* Unlike attributes and operations, it's quite likely we'll have
  128. more than one constructor with the same name and even
  129. description, so we include the parameter array in the hashcode.
  130. We don't include the description, though, because it could be
  131. quite long and yet the same between constructors. */
  132. public int hashCode() {
  133. int hash = getName().hashCode();
  134. MBeanParameterInfo[] sig = fastGetSignature();
  135. for (int i = 0; i < sig.length; i++)
  136. hash ^= sig[i].hashCode();
  137. return hash;
  138. }
  139. private static MBeanParameterInfo[] constructorSignature(Constructor cn) {
  140. final Class[] classes = cn.getParameterTypes();
  141. final MBeanParameterInfo[] params =
  142. new MBeanParameterInfo[classes.length];
  143. for (int i = 0; i < classes.length; i++) {
  144. final String pn = "p" + (i + 1);
  145. params[i] = new MBeanParameterInfo(pn, classes[i].getName(), "");
  146. }
  147. return params;
  148. }
  149. }