MBeanOperationInfo.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* MBeanOperationInfo.java -- Information about a bean's operations.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.management;
  32. import java.lang.reflect.Method;
  33. import java.lang.reflect.Type;
  34. import java.util.Arrays;
  35. /**
  36. * Describes the operations of a management bean.
  37. * The information in this class is immutable as standard.
  38. * Of course, subclasses may change this, but this
  39. * behaviour is not recommended.
  40. *
  41. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  42. * @since 1.5
  43. */
  44. public class MBeanOperationInfo
  45. extends MBeanFeatureInfo
  46. implements Cloneable
  47. {
  48. /**
  49. * Compatible with JDK 1.5
  50. */
  51. private static final long serialVersionUID = -6178860474881375330L;
  52. /**
  53. * Used to signify that the operation merely provides information
  54. * (akin to an accessor).
  55. */
  56. public static final int INFO = 0;
  57. /**
  58. * Used to signify that the operation makes some change to the
  59. * state of the bean (akin to a mutator).
  60. */
  61. public static final int ACTION = 1;
  62. /**
  63. * Used to signify that the operation makes some state change
  64. * to the bean and also returns information.
  65. */
  66. public static final int ACTION_INFO = 2;
  67. /**
  68. * Used to signify that the behaviour of the operation is
  69. * unknown.
  70. */
  71. public static final int UNKNOWN = 3;
  72. /**
  73. * The return type of the method, in the form of its class name.
  74. */
  75. private String type;
  76. /**
  77. * The signature of the constructor i.e. the argument types.
  78. */
  79. private MBeanParameterInfo[] signature;
  80. /**
  81. * The impact of the method, as one of {@link #INFO}, {@link #ACTION},
  82. * {@link #ACTION_INFO} and {@link #UNKNOWN}.
  83. */
  84. private int impact;
  85. /**
  86. * Constructs a @link{MBeanOperationInfo} with the specified
  87. * description using the given method. Each parameter is
  88. * described merely by its type; the name and description are
  89. * <code>null</code>. The return type and impact of the
  90. * method are determined from the {@link Method} instance.
  91. *
  92. * @param desc a description of the attribute.
  93. * @param method the method.
  94. */
  95. public MBeanOperationInfo(String desc, Method method)
  96. {
  97. super(method.getName(), desc);
  98. Type[] paramTypes = method.getGenericParameterTypes();
  99. signature = new MBeanParameterInfo[paramTypes.length];
  100. for (int a = 0; a < paramTypes.length; ++a)
  101. {
  102. Type t = paramTypes[a];
  103. if (t instanceof Class)
  104. signature[a] = new MBeanParameterInfo(null,
  105. ((Class<?>) t).getName(),
  106. null);
  107. else
  108. signature[a] = new MBeanParameterInfo(null, t.toString(), null);
  109. }
  110. Type retType = method.getGenericReturnType();
  111. if (retType instanceof Class)
  112. type = ((Class<?>) retType).getName();
  113. else
  114. type = retType.toString();
  115. if (method.getReturnType() == Void.TYPE)
  116. {
  117. if (paramTypes.length == 0)
  118. impact = UNKNOWN;
  119. else
  120. impact = ACTION;
  121. }
  122. else
  123. {
  124. if (paramTypes.length == 0)
  125. impact = INFO;
  126. else
  127. impact = ACTION_INFO;
  128. }
  129. }
  130. /**
  131. * Constructs a @link{MBeanOperationInfo} with the specified name,
  132. * description, parameter information, return type and impact. A
  133. * <code>null</code> value for the parameter information is the same
  134. * as passing in an empty array. A copy of the parameter array is
  135. * taken, so later changes have no effect.
  136. *
  137. * @param name the name of the constructor.
  138. * @param desc a description of the attribute.
  139. * @param sig the signature of the method, as a series
  140. * of {@link MBeanParameterInfo} objects, one for
  141. * each parameter.
  142. * @param type the return type of the method, as the class name.
  143. * @param impact the impact of performing the operation.
  144. */
  145. public MBeanOperationInfo(String name, String desc,
  146. MBeanParameterInfo[] sig, String type,
  147. int impact)
  148. {
  149. super(name, desc);
  150. if (sig == null)
  151. signature = new MBeanParameterInfo[0];
  152. else
  153. {
  154. signature = new MBeanParameterInfo[sig.length];
  155. System.arraycopy(sig, 0, signature, 0, sig.length);
  156. }
  157. this.type = type;
  158. this.impact = impact;
  159. }
  160. /**
  161. * Returns a clone of this instance. The clone is created
  162. * using just the method provided by {@link java.lang.Object}.
  163. * Thus, the clone is just a shallow clone as returned by
  164. * that method, and does not contain any deeper cloning based
  165. * on the subject of this class.
  166. *
  167. * @return a clone of this instance.
  168. * @see java.lang.Cloneable
  169. */
  170. public Object clone()
  171. {
  172. try
  173. {
  174. return super.clone();
  175. }
  176. catch (CloneNotSupportedException e)
  177. {
  178. /* This shouldn't happen; we implement Cloneable */
  179. throw new IllegalStateException("clone() called on " +
  180. "non-cloneable object.");
  181. }
  182. }
  183. /**
  184. * Compares this feature with the supplied object. This returns
  185. * true iff the object is an instance of {@link
  186. * MBeanConstructorInfo}, {@link Object#equals()} returns true for a
  187. * comparison of both the name and description of this notification
  188. * with that of the specified object (performed by the superclass),
  189. * the return type and impact are equal and the two signature arrays
  190. * contain the same elements in the same order (but one may be
  191. * longer than the other).
  192. *
  193. * @param obj the object to compare.
  194. * @return true if the object is a {@link MBeanOperationInfo}
  195. * instance,
  196. * <code>name.equals(object.getName())</code>,
  197. * <code>description.equals(object.getDescription())</code>,
  198. * <code>type.equals(object.getReturnType())</code>,
  199. * <code>impact == object.getImpact()</code>,
  200. * and the corresponding elements of the signature arrays are
  201. * equal.
  202. */
  203. public boolean equals(Object obj)
  204. {
  205. if (!(obj instanceof MBeanOperationInfo))
  206. return false;
  207. if (!(super.equals(obj)))
  208. return false;
  209. MBeanOperationInfo o = (MBeanOperationInfo) obj;
  210. MBeanParameterInfo[] sig = o.getSignature();
  211. for (int a = 0; a < signature.length; ++a)
  212. {
  213. if (a == sig.length)
  214. return true;
  215. if (!(signature[a].equals(sig[a])))
  216. return false;
  217. }
  218. return (type.equals(o.getReturnType()) &&
  219. impact == o.getImpact());
  220. }
  221. /**
  222. * <p>
  223. * Returns the impact of performing this operation.
  224. * The value is equal to one of the following:
  225. * </p>
  226. * <ol>
  227. * <li>{@link #INFO} &mdash; the method just returns
  228. * information (akin to an accessor).</li>
  229. * <li>{@link #ACTION} &mdash; the method just alters
  230. * the state of the bean, without returning a value
  231. * (akin to a mutator).</li>
  232. * <li>{@link #ACTION_INFO} &mdash; the method both makes
  233. * state changes and returns a value.</li>
  234. * <li>{@link #UNKNOWN} &mdash; the behaviour of the operation
  235. * is unknown.</li>
  236. * </ol>
  237. *
  238. * @return the impact of performing the operation.
  239. */
  240. public int getImpact()
  241. {
  242. return impact;
  243. }
  244. /**
  245. * Returns the return type of the operation, as the class
  246. * name.
  247. *
  248. * @return the return type.
  249. */
  250. public String getReturnType()
  251. {
  252. return type;
  253. }
  254. /**
  255. * Returns the operation's signature, in the form of
  256. * information on each parameter. Each parameter is
  257. * described by an instance of {@link MBeanParameterInfo}.
  258. * The returned array is a shallow copy of the array used
  259. * by this instance, so changing which elements are stored
  260. * in the array won't affect the array used by this, but
  261. * changing the actual elements will affect the ones used
  262. * here.
  263. *
  264. * @return an array of {@link MBeanParameterInfo} objects,
  265. * describing the operation parameters.
  266. */
  267. public MBeanParameterInfo[] getSignature()
  268. {
  269. return (MBeanParameterInfo[]) signature.clone();
  270. }
  271. /**
  272. * Returns the hashcode of the operation information as the sum of
  273. * the hashcode of the superclass, the parameter array, the return
  274. * type and the impact factor.
  275. *
  276. * @return the hashcode of the operation information.
  277. */
  278. public int hashCode()
  279. {
  280. return super.hashCode() + Arrays.hashCode(signature)
  281. + type.hashCode() + Integer.valueOf(impact).hashCode();
  282. }
  283. /**
  284. * <p>
  285. * Returns a textual representation of this instance. This
  286. * is constructed using the class name
  287. * (<code>javax.management.MBeanOperationInfo</code>),
  288. * the name, description, return type and impact of the
  289. * operation and the contents of the array of parameters.
  290. * </p>
  291. * <p>
  292. * As instances of this class are immutable, the return value
  293. * is computed just once for each instance and reused
  294. * throughout its life.
  295. * </p>
  296. *
  297. * @return a @link{java.lang.String} instance representing
  298. * the instance in textual form.
  299. */
  300. public String toString()
  301. {
  302. if (string == null)
  303. {
  304. String impactString;
  305. switch (impact)
  306. {
  307. case INFO:
  308. impactString = "INFO";
  309. break;
  310. case ACTION:
  311. impactString = "ACTION";
  312. break;
  313. case ACTION_INFO:
  314. impactString = "ACTION_INFO";
  315. break;
  316. case UNKNOWN:
  317. impactString = "UNKNOWN";
  318. break;
  319. default:
  320. impactString = "ERRONEOUS VALUE";
  321. }
  322. super.toString();
  323. string = string.substring(0, string.length() - 1)
  324. + ",returnType=" + type
  325. + ",impact=" + impactString
  326. + ",signature=" + Arrays.toString(signature)
  327. + "]";
  328. }
  329. return string;
  330. }
  331. }