Source for javax.management.MBeanAttributeInfo

   1: /* MBeanAttributeInfo.java -- Information about an attribute of a bean.
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.management;
  39: 
  40: import java.lang.reflect.Method;
  41: 
  42: /**
  43:  * Describes the attributes of a management bean.
  44:  * The information in this class is immutable as standard.
  45:  * Of course, subclasses may change this, but this
  46:  * behaviour is not recommended.
  47:  *
  48:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  49:  * @since 1.5
  50:  */
  51: public class MBeanAttributeInfo
  52:   extends MBeanFeatureInfo
  53:   implements Cloneable
  54: {
  55: 
  56:   /**
  57:    * The type of the attribute.
  58:    *
  59:    * @serial the attribute type.
  60:    */
  61:   private String attributeType;
  62: 
  63:   /**
  64:    * True if the attribute's value can be changed.
  65:    *
  66:    * @serial true if the value can be changed.
  67:    */
  68:   private boolean isWrite;
  69: 
  70:   /**
  71:    * True if the attribute's value can be read.
  72:    *
  73:    * @serial true if the value can be read.
  74:    */
  75:   private boolean isRead;
  76: 
  77:   /**
  78:    * True if the attribute is a boolean and thus
  79:    * has a isXXX accessor rather than a getXXX accessor.
  80:    *
  81:    * @serial true if the attribute has an isXXX accessor.
  82:    */
  83:   private boolean is;
  84: 
  85:   /**
  86:    * Constructs a new {@link MBeanAttributeInfo} using the specified
  87:    * name and description, with the given accessor and mutator
  88:    * methods.  A <code>null</code> value for the accessor method
  89:    * indicates that the value can not be read.  A <code>null</code>
  90:    * value for the mutator method indicates that the value can not be
  91:    * changed.
  92:    *
  93:    * @param name the name of the attribute.
  94:    * @param desc a description of the attribute.
  95:    * @param getter the accessor method, or <code>null</code> if the value
  96:    *               can not be read.
  97:    * @param setter the mutator method, or <code>null</code> if the value
  98:    *               can not be changed.
  99:    * @throws IntrospectionException if both the accessor and mutator method
 100:    *                                are <code>null</code>.
 101:    */
 102:   public MBeanAttributeInfo(String name, String desc, 
 103:                 Method getter, Method setter)
 104:     throws IntrospectionException
 105:   {
 106:     super(name, desc);
 107:     if (getter == null && setter == null)
 108:       throw new IntrospectionException("Both the getter and setter methods can " +
 109:                        "not be null.");
 110:     if (getter == null)
 111:       {
 112:     attributeType = setter.getParameterTypes()[0].getName();
 113:     isRead = false;
 114:     is = false;
 115:       }
 116:     else
 117:       {
 118:     attributeType = getter.getReturnType().getName();
 119:     isRead = true;
 120:     is = getter.getName().startsWith("is");
 121:       }
 122:     if (setter != null)
 123:       isWrite = true;
 124:   }
 125: 
 126:   /**
 127:    * Constructs a new {@link MBeanAttributeInfo} using the specified
 128:    * name, description and type with the given settings for the accessor
 129:    * and mutator methods.  
 130:    *
 131:    * @param name the name of the attribute.
 132:    * @param type the type of the attribute, in the form of its class name.
 133:    * @param desc a description of the attribute.
 134:    * @param isReadable true if the attribute's value can be read.
 135:    * @param isWritable true if the attribute's value can be changed.
 136:    * @param isIs true if the attribute uses an accessor of the form isXXX.
 137:    * @throws IllegalArgumentException if the attribute is both unreadable
 138:    *                                  and unwritable.
 139:    */
 140:   public MBeanAttributeInfo(String name, String type, String desc,
 141:                 boolean isReadable, boolean isWritable,
 142:                 boolean isIs)
 143:   {
 144:     super(name, desc);
 145:     if (!isReadable && !isWritable)
 146:       throw new IllegalArgumentException("The attribute can not be both " +
 147:                      "unreadable and unwritable.");
 148:     attributeType = type;
 149:     isRead = isReadable;
 150:     isWrite = isWritable;
 151:     is = isIs;
 152:   }
 153: 
 154:   /**
 155:    * Returns a clone of this instance.  The clone is created
 156:    * using just the method provided by {@link java.lang.Object}.
 157:    * Thus, the clone is just a shallow clone as returned by
 158:    * that method, and does not contain any deeper cloning based
 159:    * on the subject of this class.
 160:    *
 161:    * @return a clone of this instance.
 162:    * @see java.lang.Cloneable
 163:    */
 164:   public Object clone()
 165:   {
 166:     try
 167:       {
 168:     return super.clone();
 169:       }
 170:     catch (CloneNotSupportedException e)
 171:       {
 172:     /* This shouldn't happen; we implement Cloneable */
 173:     throw new IllegalStateException("clone() called on " +
 174:                     "non-cloneable object.");
 175:       }
 176:   }
 177: 
 178:   /**
 179:    * Compares this feature with the supplied object.  This
 180:    * returns true iff the object is an instance of
 181:    * {@link MBeanAttributeInfo}, {@link Object#equals()}
 182:    * returns true for a comparison of both the name and
 183:    * description of this attribute  with that of the specified
 184:    * object (performed by the superclass), and the type and
 185:    * boolean flags of the two instances are equal.
 186:    *
 187:    * @param obj the object to compare.
 188:    * @return true if the object is a {@link MBeanAttributeInfo}
 189:    *         instance, 
 190:    *         <code>name.equals(object.getName())</code>,
 191:    *         <code>description.equals(object.getDescription())</code>,
 192:    *         <code>attributeType.equals(object.getType())</code>,
 193:    *         <code>isRead == object.isReadable()</code>,
 194:    *         <code>isWrite == object.isWritable()</code>,
 195:    *         <code>is == object.isIs()</code>
 196:    */
 197:   public boolean equals(Object obj)
 198:   {
 199:     if (!(obj instanceof MBeanAttributeInfo))
 200:       return false;
 201:     if (!(super.equals(obj)))
 202:       return false;
 203:     MBeanAttributeInfo o = (MBeanAttributeInfo) obj;
 204:     return (attributeType.equals(o.getType()) &&
 205:         isRead == o.isReadable() &&
 206:         isWrite == o.isWritable() &&
 207:         is == o.isIs());
 208:   }
 209: 
 210:   /**
 211:    * Returns the type of this attribute, in the form of its class name.
 212:    *
 213:    * @return the type of this attribute.
 214:    */
 215:   public String getType()
 216:   {
 217:     return attributeType;
 218:   }
 219: 
 220:   /**
 221:    * Returns the hashcode of the attribute information as the sum of
 222:    * the hashcode of the superclass, the hashcode of the type,
 223:    * the hashcode of {@link #isReadable()}, twice the hashcode
 224:    * of {@link #isWritable()} and four times the hashcode
 225:    * of {@link #isIs()}.
 226:    *
 227:    * @return the hashcode of the attribute information.
 228:    */
 229:   public int hashCode()
 230:   {
 231:     return super.hashCode() + attributeType.hashCode()
 232:       + Boolean.valueOf(isRead).hashCode()
 233:       + (2 * Boolean.valueOf(isWrite).hashCode())
 234:       + (4 * Boolean.valueOf(is).hashCode());
 235:   }
 236: 
 237:   /**
 238:    * Returns true if the accessor method of this attribute
 239:    * is of the form <code>isXXX</code>.
 240:    *
 241:    * @return true if the accessor takes the form <code>isXXX</code>.
 242:    */
 243:   public boolean isIs()
 244:   {
 245:     return is;
 246:   }
 247: 
 248:   /**
 249:    * Returns true if value of this attribute can be read.
 250:    *
 251:    * @return true if the value of the attribute can be read.
 252:    */
 253:   public boolean isReadable()
 254:   {
 255:     return isRead;
 256:   }
 257: 
 258:   /**
 259:    * Returns true if the value of this attribute can be changed.
 260:    *
 261:    * @return true if the value of the attribute can be changed.
 262:    */
 263:   public boolean isWritable()
 264:   {
 265:     return isWrite;
 266:   }
 267: 
 268:   /**
 269:    * <p>
 270:    * Returns a textual representation of this instance.  This
 271:    * is constructed using the class name
 272:    * (<code>javax.management.MBeanAttributeInfo</code>),
 273:    * the name, description and type of the attribute and the 
 274:    * current settings of the {@link #isReadable()}, 
 275:    * {@link #isWritable()} and {@link #isIs()} properties.
 276:    * </p>
 277:    * <p>
 278:    * As instances of this class are immutable, the return value
 279:    * is computed just once for each instance and reused
 280:    * throughout its life.
 281:    * </p>
 282:    *
 283:    * @return a @link{java.lang.String} instance representing
 284:    *         the instance in textual form.
 285:    */
 286:   public String toString()
 287:   {
 288:     if (string == null)
 289:       {
 290:     super.toString();
 291:     string = string.substring(0, string.length() - 1) 
 292:       + ",type=" + attributeType
 293:       + ",isReadable=" + (isRead ? "yes" : "no")
 294:       + ",isWritable=" + (isWrite ? "yes" : "no")
 295:       + ",isIs=" + (is ? "yes" : "no")
 296:       + "]";
 297:       }
 298:     return string;
 299:   }
 300: 
 301: }