1 7 8 package javax.management; 9 10 import java.lang.reflect.Method ; 11 import java.security.AccessController ; 12 import java.security.PrivilegedAction ; 13 14 import com.sun.jmx.mbeanserver.GetPropertyAction; 15 16 23 public class MBeanAttributeInfo extends MBeanFeatureInfo implements java.io.Serializable , Cloneable { 24 25 26 private static final long serialVersionUID; 27 static { 28 37 long uid = 8644704819898565848L; 38 try { 39 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 40 String form = (String ) AccessController.doPrivileged(act); 41 if ("1.0".equals(form)) 42 uid = 7043855487133450673L; 43 } catch (Exception e) { 44 } 46 serialVersionUID = uid; 47 } 48 49 static final MBeanAttributeInfo [] NO_ATTRIBUTES = 50 new MBeanAttributeInfo [0]; 51 52 55 private final String attributeType; 56 57 60 private final boolean isWrite; 61 62 65 private final boolean isRead; 66 67 70 private final boolean is; 71 72 73 83 public MBeanAttributeInfo(String name, 84 String type, 85 String description, 86 boolean isReadable, 87 boolean isWritable, 88 boolean isIs) 89 throws IllegalArgumentException { 90 91 super(name, description); 92 93 this.attributeType = type; 94 this.isRead= isReadable; 95 this.isWrite = isWritable; 96 if (isIs && !isReadable) { 97 throw new IllegalArgumentException ("Cannot have an \"is\" getter for a non-readable attribute."); 98 } 99 if (isIs && (!type.equals("java.lang.Boolean") && (!type.equals("boolean")))) { 100 throw new IllegalArgumentException ("Cannot have an \"is\" getter for a non-boolean attribute."); 101 } 102 this.is = isIs; 103 } 104 105 118 public MBeanAttributeInfo(String name, 119 String description, 120 Method getter, 121 Method setter) throws IntrospectionException { 122 this(name, 123 attributeType(getter, setter), 124 description, 125 (getter != null), 126 (setter != null), 127 isIs(getter)); 128 } 129 130 140 public Object clone () { 141 try { 142 return super.clone() ; 143 } catch (CloneNotSupportedException e) { 144 return null; 146 } 147 } 148 149 154 public String getType() { 155 return attributeType; 156 } 157 158 163 public boolean isReadable() { 164 return isRead; 165 } 166 167 172 public boolean isWritable() { 173 return isWrite; 174 } 175 176 181 public boolean isIs() { 182 return is; 183 } 184 185 196 public boolean equals(Object o) { 197 if (o == this) 198 return true; 199 if (!(o instanceof MBeanAttributeInfo )) 200 return false; 201 MBeanAttributeInfo p = (MBeanAttributeInfo ) o; 202 return (p.getName().equals(getName()) && 203 p.getType().equals(getType()) && 204 p.getDescription().equals(getDescription()) && 205 p.isReadable() == isReadable() && 206 p.isWritable() == isWritable() && 207 p.isIs() == isIs()); 208 } 209 210 216 public int hashCode() { 217 return getName().hashCode() ^ getType().hashCode(); 218 } 219 220 private static boolean isIs(Method getter) { 221 return (getter != null && 222 getter.getName().startsWith("is") && 223 (getter.getReturnType().equals(Boolean.TYPE) || 224 getter.getReturnType().equals(Boolean .class))); 225 } 226 227 230 private static String attributeType(Method getter, Method setter) 231 throws IntrospectionException { 232 Class type = null; 233 234 if (getter != null) { 235 if (getter.getParameterTypes().length != 0) { 236 throw new IntrospectionException ("bad getter arg count"); 237 } 238 type = getter.getReturnType(); 239 if (type == Void.TYPE) { 240 throw new IntrospectionException ("getter " + getter.getName() + 241 " returns void"); 242 } 243 } 244 245 if (setter != null) { 246 Class params[] = setter.getParameterTypes(); 247 if (params.length != 1) { 248 throw new IntrospectionException ("bad setter arg count"); 249 } 250 if (type == null) 251 type = params[0]; 252 else if (type != params[0]) { 253 throw new IntrospectionException ("type mismatch between " + 254 "getter and setter"); 255 } 256 } 257 258 if (type == null) { 259 throw new IntrospectionException ("getter and setter cannot " + 260 "both be null"); 261 } 262 263 return type.getName(); 264 } 265 266 } 267 | Popular Tags |