1. /*
  2. * @(#)MBeanOperationInfo.java 1.31 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.Method;
  9. import java.util.Arrays;
  10. /**
  11. * Describes a management operation exposed by an MBean. Instances of
  12. * this class are immutable. Subclasses may be mutable but this is
  13. * not recommended.
  14. *
  15. * @since 1.5
  16. */
  17. public class MBeanOperationInfo extends MBeanFeatureInfo implements java.io.Serializable, Cloneable {
  18. /* Serial version */
  19. static final long serialVersionUID = -6178860474881375330L;
  20. static final MBeanOperationInfo[] NO_OPERATIONS =
  21. new MBeanOperationInfo[0];
  22. /**
  23. * Indicates that the operation is read-like,
  24. * it basically returns information.
  25. */
  26. public static final int INFO = 0;
  27. /**
  28. * Indicates that the operation is a write-like,
  29. * and would modify the MBean in some way, typically by writing some value
  30. * or changing a configuration.
  31. */
  32. public static final int ACTION = 1 ;
  33. /**
  34. * Indicates that the operation is both read-like and write-like.
  35. */
  36. public static final int ACTION_INFO = 2;
  37. /**
  38. * Indicates that the operation has an "unknown" nature.
  39. */
  40. public static final int UNKNOWN = 3;
  41. /**
  42. * @serial The method's return value.
  43. */
  44. private final String type;
  45. /**
  46. * @serial The signature of the method, that is, the class names
  47. * of the arguments.
  48. */
  49. private final MBeanParameterInfo[] signature;
  50. /**
  51. * @serial The impact of the method, one of
  52. * <CODE>INFO</CODE>,
  53. * <CODE>ACTION</CODE>,
  54. * <CODE>ACTION_INFO</CODE>,
  55. * <CODE>UNKNOWN</CODE>
  56. */
  57. private final int impact;
  58. /** @see MBeanInfo#immutable */
  59. private final transient boolean immutable;
  60. /**
  61. * Constructs an <CODE>MBeanOperationInfo</CODE> object.
  62. *
  63. * @param method The <CODE>java.lang.reflect.Method</CODE> object
  64. * describing the MBean operation.
  65. * @param description A human readable description of the operation.
  66. */
  67. public MBeanOperationInfo(String description,
  68. Method method)
  69. throws IllegalArgumentException {
  70. this(method.getName(),
  71. description,
  72. methodSignature(method),
  73. method.getReturnType().getName(),
  74. UNKNOWN);
  75. }
  76. /**
  77. * Constructs an <CODE>MBeanOperationInfo</CODE> object.
  78. *
  79. * @param name The name of the method.
  80. * @param description A human readable description of the operation.
  81. * @param signature <CODE>MBeanParameterInfo</CODE> objects
  82. * describing the parameters(arguments) of the method. This may be
  83. * null with the same effect as a zero-length array.
  84. * @param type The type of the method's return value.
  85. * @param impact The impact of the method, one of <CODE>INFO,
  86. * ACTION, ACTION_INFO, UNKNOWN</CODE>.
  87. */
  88. public MBeanOperationInfo(String name,
  89. String description,
  90. MBeanParameterInfo[] signature,
  91. String type,
  92. int impact)
  93. throws IllegalArgumentException {
  94. super(name, description);
  95. if (signature == null || signature.length == 0)
  96. signature = MBeanParameterInfo.NO_PARAMS;
  97. else
  98. signature = (MBeanParameterInfo[]) signature.clone();
  99. this.signature = signature;
  100. this.type = type;
  101. this.impact = impact;
  102. this.immutable =
  103. MBeanInfo.isImmutableClass(this.getClass(),
  104. MBeanOperationInfo.class);
  105. }
  106. /**
  107. * <p>Returns a shallow clone of this instance.
  108. * The clone is obtained by simply calling <tt>super.clone()</tt>,
  109. * thus calling the default native shallow cloning mechanism
  110. * implemented by <tt>Object.clone()</tt>.
  111. * No deeper cloning of any internal field is made.</p>
  112. *
  113. * <p>Since this class is immutable, cloning is chiefly of interest
  114. * to subclasses.</p>
  115. */
  116. public Object clone () {
  117. try {
  118. return super.clone() ;
  119. } catch (CloneNotSupportedException e) {
  120. // should not happen as this class is cloneable
  121. return null;
  122. }
  123. }
  124. /**
  125. * Returns the type of the method's return value.
  126. *
  127. * @return the return type.
  128. */
  129. public String getReturnType() {
  130. return type;
  131. }
  132. /**
  133. * <p>Returns the list of parameters for this operation. Each
  134. * parameter is described by an <CODE>MBeanParameterInfo</CODE>
  135. * object.</p>
  136. *
  137. * <p>The returned array is a shallow copy of the internal array,
  138. * which means that it is a copy of the internal array of
  139. * references to the <CODE>MBeanParameterInfo</CODE> objects but
  140. * that each referenced <CODE>MBeanParameterInfo</CODE> object is
  141. * not copied.</p>
  142. *
  143. * @return An array of <CODE>MBeanParameterInfo</CODE> objects.
  144. */
  145. public MBeanParameterInfo[] getSignature() {
  146. if (signature.length == 0)
  147. return signature;
  148. else
  149. return (MBeanParameterInfo[]) signature.clone();
  150. }
  151. private MBeanParameterInfo[] fastGetSignature() {
  152. if (immutable)
  153. return signature;
  154. else
  155. return getSignature();
  156. }
  157. /**
  158. * Returns the impact of the method, one of
  159. * <CODE>INFO</CODE>, <CODE>ACTION</CODE>, <CODE>ACTION_INFO</CODE>, <CODE>UNKNOWN</CODE>.
  160. *
  161. * @return the impact code.
  162. */
  163. public int getImpact() {
  164. return impact;
  165. }
  166. /**
  167. * Compare this MBeanOperationInfo to another.
  168. *
  169. * @param o the object to compare to.
  170. *
  171. * @return true iff <code>o</code> is an MBeanOperationInfo such
  172. * that its {@link #getName()}, {@link #getReturnType()}, {@link
  173. * #getDescription()}, {@link #getImpact()}, and {@link
  174. * #getSignature()} values are equal (not necessarily identical)
  175. * to those of this MBeanConstructorInfo. Two signature arrays
  176. * are equal if their elements are pairwise equal.
  177. */
  178. public boolean equals(Object o) {
  179. if (o == this)
  180. return true;
  181. if (!(o instanceof MBeanOperationInfo))
  182. return false;
  183. MBeanOperationInfo p = (MBeanOperationInfo) o;
  184. return (p.getName().equals(getName()) &&
  185. p.getReturnType().equals(getReturnType()) &&
  186. p.getDescription().equals(getDescription()) &&
  187. p.getImpact() == getImpact() &&
  188. Arrays.equals(p.fastGetSignature(), fastGetSignature()));
  189. }
  190. /* We do not include everything in the hashcode. We assume that
  191. if two operations are different they'll probably have different
  192. names or types. The penalty we pay when this assumption is
  193. wrong should be less than the penalty we would pay if it were
  194. right and we needlessly hashed in the description and the
  195. parameter array. */
  196. public int hashCode() {
  197. return getName().hashCode() ^ getReturnType().hashCode();
  198. }
  199. private static MBeanParameterInfo[] methodSignature(Method method) {
  200. final Class[] classes = method.getParameterTypes();
  201. final MBeanParameterInfo[] params =
  202. new MBeanParameterInfo[classes.length];
  203. for (int i = 0; i < classes.length; i++) {
  204. final String pn = "p" + (i + 1);
  205. params[i] = new MBeanParameterInfo(pn, classes[i].getName(), "");
  206. }
  207. return params;
  208. }
  209. }