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: import java.lang.reflect.Type;
  42: 
  43: /**
  44:  * Describes the attributes of a management bean.
  45:  * The information in this class is immutable as standard.
  46:  * Of course, subclasses may change this, but this
  47:  * behaviour is not recommended.
  48:  *
  49:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  50:  * @since 1.5
  51:  */
  52: public class MBeanAttributeInfo
  53:   extends MBeanFeatureInfo
  54:   implements Cloneable
  55: {
  56: 
  57:   /**
  58:    * Compatible with JDK 1.6
  59:    */
  60:   private static final long serialVersionUID = 8644704819898565848L;
  61: 
  62:   /**
  63:    * The type of the attribute.
  64:    *
  65:    * @serial the attribute type.
  66:    */
  67:   private String attributeType;
  68: 
  69:   /**
  70:    * True if the attribute's value can be changed.
  71:    *
  72:    * @serial true if the value can be changed.
  73:    */
  74:   private boolean isWrite;
  75: 
  76:   /**
  77:    * True if the attribute's value can be read.
  78:    *
  79:    * @serial true if the value can be read.
  80:    */
  81:   private boolean isRead;
  82: 
  83:   /**
  84:    * True if the attribute is a boolean and thus
  85:    * has a isXXX accessor rather than a getXXX accessor.
  86:    *
  87:    * @serial true if the attribute has an isXXX accessor.
  88:    */
  89:   private boolean is;
  90: 
  91:   /**
  92:    * Constructs a new {@link MBeanAttributeInfo} using the specified
  93:    * name and description, with the given accessor and mutator
  94:    * methods.  A <code>null</code> value for the accessor method
  95:    * indicates that the value can not be read.  A <code>null</code>
  96:    * value for the mutator method indicates that the value can not be
  97:    * changed.
  98:    *
  99:    * @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: }