001 /* MBeanAttributeInfo.java -- Information about an attribute of a bean.
002 Copyright (C) 2006 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package javax.management;
039
040 import java.lang.reflect.Method;
041 import java.lang.reflect.Type;
042
043 /**
044 * Describes the attributes of a management bean.
045 * The information in this class is immutable as standard.
046 * Of course, subclasses may change this, but this
047 * behaviour is not recommended.
048 *
049 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
050 * @since 1.5
051 */
052 public class MBeanAttributeInfo
053 extends MBeanFeatureInfo
054 implements Cloneable
055 {
056
057 /**
058 * Compatible with JDK 1.6
059 */
060 private static final long serialVersionUID = 8644704819898565848L;
061
062 /**
063 * The type of the attribute.
064 *
065 * @serial the attribute type.
066 */
067 private String attributeType;
068
069 /**
070 * True if the attribute's value can be changed.
071 *
072 * @serial true if the value can be changed.
073 */
074 private boolean isWrite;
075
076 /**
077 * True if the attribute's value can be read.
078 *
079 * @serial true if the value can be read.
080 */
081 private boolean isRead;
082
083 /**
084 * True if the attribute is a boolean and thus
085 * has a isXXX accessor rather than a getXXX accessor.
086 *
087 * @serial true if the attribute has an isXXX accessor.
088 */
089 private boolean is;
090
091 /**
092 * Constructs a new {@link MBeanAttributeInfo} using the specified
093 * name and description, with the given accessor and mutator
094 * methods. A <code>null</code> value for the accessor method
095 * indicates that the value can not be read. A <code>null</code>
096 * value for the mutator method indicates that the value can not be
097 * changed.
098 *
099 * @param name the name of the attribute.
100 * @param desc a description of the attribute.
101 * @param getter the accessor method, or <code>null</code> if the value
102 * can not be read.
103 * @param setter the mutator method, or <code>null</code> if the value
104 * can not be changed.
105 * @throws IntrospectionException if both the accessor and mutator method
106 * are <code>null</code>.
107 */
108 public MBeanAttributeInfo(String name, String desc,
109 Method getter, Method setter)
110 throws IntrospectionException
111 {
112 super(name, desc);
113 if (getter == null && setter == null)
114 throw new IntrospectionException("Both the getter and setter methods can " +
115 "not be null.");
116 if (getter == null)
117 {
118 Type t = setter.getGenericParameterTypes()[0];
119 if (t instanceof Class)
120 attributeType = ((Class<?>) t).getName();
121 else
122 attributeType = t.toString();
123 isRead = false;
124 is = false;
125 }
126 else
127 {
128 Type t = getter.getGenericReturnType();
129 if (t instanceof Class)
130 attributeType = ((Class<?>) t).getName();
131 else
132 attributeType = t.toString();
133 isRead = true;
134 is = getter.getName().startsWith("is");
135 }
136 if (setter != null)
137 isWrite = true;
138 }
139
140 /**
141 * Constructs a new {@link MBeanAttributeInfo} using the specified
142 * name, description and type with the given settings for the accessor
143 * and mutator methods.
144 *
145 * @param name the name of the attribute.
146 * @param type the type of the attribute, in the form of its class name.
147 * @param desc a description of the attribute.
148 * @param isReadable true if the attribute's value can be read.
149 * @param isWritable true if the attribute's value can be changed.
150 * @param isIs true if the attribute uses an accessor of the form isXXX.
151 * @throws IllegalArgumentException if the attribute is both unreadable
152 * and unwritable.
153 */
154 public MBeanAttributeInfo(String name, String type, String desc,
155 boolean isReadable, boolean isWritable,
156 boolean isIs)
157 {
158 super(name, desc);
159 if (!isReadable && !isWritable)
160 throw new IllegalArgumentException("The attribute can not be both " +
161 "unreadable and unwritable.");
162 attributeType = type;
163 isRead = isReadable;
164 isWrite = isWritable;
165 is = isIs;
166 }
167
168 /**
169 * Returns a clone of this instance. The clone is created
170 * using just the method provided by {@link java.lang.Object}.
171 * Thus, the clone is just a shallow clone as returned by
172 * that method, and does not contain any deeper cloning based
173 * on the subject of this class.
174 *
175 * @return a clone of this instance.
176 * @see java.lang.Cloneable
177 */
178 public Object clone()
179 {
180 try
181 {
182 return super.clone();
183 }
184 catch (CloneNotSupportedException e)
185 {
186 /* This shouldn't happen; we implement Cloneable */
187 throw new IllegalStateException("clone() called on " +
188 "non-cloneable object.");
189 }
190 }
191
192 /**
193 * Compares this feature with the supplied object. This
194 * returns true iff the object is an instance of
195 * {@link MBeanAttributeInfo}, {@link Object#equals()}
196 * returns true for a comparison of both the name and
197 * description of this attribute with that of the specified
198 * object (performed by the superclass), and the type and
199 * boolean flags of the two instances are equal.
200 *
201 * @param obj the object to compare.
202 * @return true if the object is a {@link MBeanAttributeInfo}
203 * instance,
204 * <code>name.equals(object.getName())</code>,
205 * <code>description.equals(object.getDescription())</code>,
206 * <code>attributeType.equals(object.getType())</code>,
207 * <code>isRead == object.isReadable()</code>,
208 * <code>isWrite == object.isWritable()</code>,
209 * <code>is == object.isIs()</code>
210 */
211 public boolean equals(Object obj)
212 {
213 if (!(obj instanceof MBeanAttributeInfo))
214 return false;
215 if (!(super.equals(obj)))
216 return false;
217 MBeanAttributeInfo o = (MBeanAttributeInfo) obj;
218 return (attributeType.equals(o.getType()) &&
219 isRead == o.isReadable() &&
220 isWrite == o.isWritable() &&
221 is == o.isIs());
222 }
223
224 /**
225 * Returns the type of this attribute, in the form of its class name.
226 *
227 * @return the type of this attribute.
228 */
229 public String getType()
230 {
231 return attributeType;
232 }
233
234 /**
235 * Returns the hashcode of the attribute information as the sum of
236 * the hashcode of the superclass, the hashcode of the type,
237 * the hashcode of {@link #isReadable()}, twice the hashcode
238 * of {@link #isWritable()} and four times the hashcode
239 * of {@link #isIs()}.
240 *
241 * @return the hashcode of the attribute information.
242 */
243 public int hashCode()
244 {
245 return super.hashCode() + attributeType.hashCode()
246 + Boolean.valueOf(isRead).hashCode()
247 + (2 * Boolean.valueOf(isWrite).hashCode())
248 + (4 * Boolean.valueOf(is).hashCode());
249 }
250
251 /**
252 * Returns true if the accessor method of this attribute
253 * is of the form <code>isXXX</code>.
254 *
255 * @return true if the accessor takes the form <code>isXXX</code>.
256 */
257 public boolean isIs()
258 {
259 return is;
260 }
261
262 /**
263 * Returns true if value of this attribute can be read.
264 *
265 * @return true if the value of the attribute can be read.
266 */
267 public boolean isReadable()
268 {
269 return isRead;
270 }
271
272 /**
273 * Returns true if the value of this attribute can be changed.
274 *
275 * @return true if the value of the attribute can be changed.
276 */
277 public boolean isWritable()
278 {
279 return isWrite;
280 }
281
282 /**
283 * <p>
284 * Returns a textual representation of this instance. This
285 * is constructed using the class name
286 * (<code>javax.management.MBeanAttributeInfo</code>),
287 * the name, description and type of the attribute and the
288 * current settings of the {@link #isReadable()},
289 * {@link #isWritable()} and {@link #isIs()} properties.
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 super.toString();
305 string = string.substring(0, string.length() - 1)
306 + ",type=" + attributeType
307 + ",isReadable=" + (isRead ? "yes" : "no")
308 + ",isWritable=" + (isWrite ? "yes" : "no")
309 + ",isIs=" + (is ? "yes" : "no")
310 + "]";
311 }
312 return string;
313 }
314
315 }