MBeanAttributeInfo.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* MBeanAttributeInfo.java -- Information about an attribute of a bean.
  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. /**
  35. * Describes the attributes of a management bean.
  36. * The information in this class is immutable as standard.
  37. * Of course, subclasses may change this, but this
  38. * behaviour is not recommended.
  39. *
  40. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  41. * @since 1.5
  42. */
  43. public class MBeanAttributeInfo
  44. extends MBeanFeatureInfo
  45. implements Cloneable
  46. {
  47. /**
  48. * Compatible with JDK 1.6
  49. */
  50. private static final long serialVersionUID = 8644704819898565848L;
  51. /**
  52. * The type of the attribute.
  53. *
  54. * @serial the attribute type.
  55. */
  56. private String attributeType;
  57. /**
  58. * True if the attribute's value can be changed.
  59. *
  60. * @serial true if the value can be changed.
  61. */
  62. private boolean isWrite;
  63. /**
  64. * True if the attribute's value can be read.
  65. *
  66. * @serial true if the value can be read.
  67. */
  68. private boolean isRead;
  69. /**
  70. * True if the attribute is a boolean and thus
  71. * has a isXXX accessor rather than a getXXX accessor.
  72. *
  73. * @serial true if the attribute has an isXXX accessor.
  74. */
  75. private boolean is;
  76. /**
  77. * Constructs a new {@link MBeanAttributeInfo} using the specified
  78. * name and description, with the given accessor and mutator
  79. * methods. A <code>null</code> value for the accessor method
  80. * indicates that the value can not be read. A <code>null</code>
  81. * value for the mutator method indicates that the value can not be
  82. * changed.
  83. *
  84. * @param name the name of the attribute.
  85. * @param desc a description of the attribute.
  86. * @param getter the accessor method, or <code>null</code> if the value
  87. * can not be read.
  88. * @param setter the mutator method, or <code>null</code> if the value
  89. * can not be changed.
  90. * @throws IntrospectionException if both the accessor and mutator method
  91. * are <code>null</code>.
  92. */
  93. public MBeanAttributeInfo(String name, String desc,
  94. Method getter, Method setter)
  95. throws IntrospectionException
  96. {
  97. super(name, desc);
  98. if (getter == null && setter == null)
  99. throw new IntrospectionException("Both the getter and setter methods can " +
  100. "not be null.");
  101. if (getter == null)
  102. {
  103. Type t = setter.getGenericParameterTypes()[0];
  104. if (t instanceof Class)
  105. attributeType = ((Class<?>) t).getName();
  106. else
  107. attributeType = t.toString();
  108. isRead = false;
  109. is = false;
  110. }
  111. else
  112. {
  113. Type t = getter.getGenericReturnType();
  114. if (t instanceof Class)
  115. attributeType = ((Class<?>) t).getName();
  116. else
  117. attributeType = t.toString();
  118. isRead = true;
  119. is = getter.getName().startsWith("is");
  120. }
  121. if (setter != null)
  122. isWrite = true;
  123. }
  124. /**
  125. * Constructs a new {@link MBeanAttributeInfo} using the specified
  126. * name, description and type with the given settings for the accessor
  127. * and mutator methods.
  128. *
  129. * @param name the name of the attribute.
  130. * @param type the type of the attribute, in the form of its class name.
  131. * @param desc a description of the attribute.
  132. * @param isReadable true if the attribute's value can be read.
  133. * @param isWritable true if the attribute's value can be changed.
  134. * @param isIs true if the attribute uses an accessor of the form isXXX.
  135. * @throws IllegalArgumentException if the attribute is both unreadable
  136. * and unwritable.
  137. */
  138. public MBeanAttributeInfo(String name, String type, String desc,
  139. boolean isReadable, boolean isWritable,
  140. boolean isIs)
  141. {
  142. super(name, desc);
  143. if (!isReadable && !isWritable)
  144. throw new IllegalArgumentException("The attribute can not be both " +
  145. "unreadable and unwritable.");
  146. attributeType = type;
  147. isRead = isReadable;
  148. isWrite = isWritable;
  149. is = isIs;
  150. }
  151. /**
  152. * Returns a clone of this instance. The clone is created
  153. * using just the method provided by {@link java.lang.Object}.
  154. * Thus, the clone is just a shallow clone as returned by
  155. * that method, and does not contain any deeper cloning based
  156. * on the subject of this class.
  157. *
  158. * @return a clone of this instance.
  159. * @see java.lang.Cloneable
  160. */
  161. public Object clone()
  162. {
  163. try
  164. {
  165. return super.clone();
  166. }
  167. catch (CloneNotSupportedException e)
  168. {
  169. /* This shouldn't happen; we implement Cloneable */
  170. throw new IllegalStateException("clone() called on " +
  171. "non-cloneable object.");
  172. }
  173. }
  174. /**
  175. * Compares this feature with the supplied object. This
  176. * returns true iff the object is an instance of
  177. * {@link MBeanAttributeInfo}, {@link Object#equals()}
  178. * returns true for a comparison of both the name and
  179. * description of this attribute with that of the specified
  180. * object (performed by the superclass), and the type and
  181. * boolean flags of the two instances are equal.
  182. *
  183. * @param obj the object to compare.
  184. * @return true if the object is a {@link MBeanAttributeInfo}
  185. * instance,
  186. * <code>name.equals(object.getName())</code>,
  187. * <code>description.equals(object.getDescription())</code>,
  188. * <code>attributeType.equals(object.getType())</code>,
  189. * <code>isRead == object.isReadable()</code>,
  190. * <code>isWrite == object.isWritable()</code>,
  191. * <code>is == object.isIs()</code>
  192. */
  193. public boolean equals(Object obj)
  194. {
  195. if (!(obj instanceof MBeanAttributeInfo))
  196. return false;
  197. if (!(super.equals(obj)))
  198. return false;
  199. MBeanAttributeInfo o = (MBeanAttributeInfo) obj;
  200. return (attributeType.equals(o.getType()) &&
  201. isRead == o.isReadable() &&
  202. isWrite == o.isWritable() &&
  203. is == o.isIs());
  204. }
  205. /**
  206. * Returns the type of this attribute, in the form of its class name.
  207. *
  208. * @return the type of this attribute.
  209. */
  210. public String getType()
  211. {
  212. return attributeType;
  213. }
  214. /**
  215. * Returns the hashcode of the attribute information as the sum of
  216. * the hashcode of the superclass, the hashcode of the type,
  217. * the hashcode of {@link #isReadable()}, twice the hashcode
  218. * of {@link #isWritable()} and four times the hashcode
  219. * of {@link #isIs()}.
  220. *
  221. * @return the hashcode of the attribute information.
  222. */
  223. public int hashCode()
  224. {
  225. return super.hashCode() + attributeType.hashCode()
  226. + Boolean.valueOf(isRead).hashCode()
  227. + (2 * Boolean.valueOf(isWrite).hashCode())
  228. + (4 * Boolean.valueOf(is).hashCode());
  229. }
  230. /**
  231. * Returns true if the accessor method of this attribute
  232. * is of the form <code>isXXX</code>.
  233. *
  234. * @return true if the accessor takes the form <code>isXXX</code>.
  235. */
  236. public boolean isIs()
  237. {
  238. return is;
  239. }
  240. /**
  241. * Returns true if value of this attribute can be read.
  242. *
  243. * @return true if the value of the attribute can be read.
  244. */
  245. public boolean isReadable()
  246. {
  247. return isRead;
  248. }
  249. /**
  250. * Returns true if the value of this attribute can be changed.
  251. *
  252. * @return true if the value of the attribute can be changed.
  253. */
  254. public boolean isWritable()
  255. {
  256. return isWrite;
  257. }
  258. /**
  259. * <p>
  260. * Returns a textual representation of this instance. This
  261. * is constructed using the class name
  262. * (<code>javax.management.MBeanAttributeInfo</code>),
  263. * the name, description and type of the attribute and the
  264. * current settings of the {@link #isReadable()},
  265. * {@link #isWritable()} and {@link #isIs()} properties.
  266. * </p>
  267. * <p>
  268. * As instances of this class are immutable, the return value
  269. * is computed just once for each instance and reused
  270. * throughout its life.
  271. * </p>
  272. *
  273. * @return a @link{java.lang.String} instance representing
  274. * the instance in textual form.
  275. */
  276. public String toString()
  277. {
  278. if (string == null)
  279. {
  280. super.toString();
  281. string = string.substring(0, string.length() - 1)
  282. + ",type=" + attributeType
  283. + ",isReadable=" + (isRead ? "yes" : "no")
  284. + ",isWritable=" + (isWrite ? "yes" : "no")
  285. + ",isIs=" + (is ? "yes" : "no")
  286. + "]";
  287. }
  288. return string;
  289. }
  290. }