MBeanOperationInfo.java revision 10444:f08705540498
1153187Spjd/* 2153187Spjd * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 3153187Spjd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4153187Spjd * 5153187Spjd * This code is free software; you can redistribute it and/or modify it 6153187Spjd * under the terms of the GNU General Public License version 2 only, as 7153187Spjd * published by the Free Software Foundation. Oracle designates this 8153187Spjd * particular file as subject to the "Classpath" exception as provided 9153187Spjd * by Oracle in the LICENSE file that accompanied this code. 10153187Spjd * 11153187Spjd * This code is distributed in the hope that it will be useful, but WITHOUT 12153187Spjd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13153187Spjd * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14153187Spjd * version 2 for more details (a copy is included in the LICENSE file that 15153187Spjd * accompanied this code). 16153187Spjd * 17153187Spjd * You should have received a copy of the GNU General Public License version 18153187Spjd * 2 along with this work; if not, write to the Free Software Foundation, 19153187Spjd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20153187Spjd * 21153187Spjd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22153187Spjd * or visit www.oracle.com if you need additional information or have any 23153187Spjd * questions. 24153187Spjd */ 25153187Spjd 26153187Spjdpackage javax.management; 27153187Spjd 28153187Spjdimport com.sun.jmx.mbeanserver.Introspector; 29153187Spjdimport java.lang.annotation.Annotation; 30153187Spjdimport java.lang.reflect.Method; 31153187Spjdimport java.util.Arrays; 32153187Spjdimport java.util.Objects; 33153187Spjd 34153187Spjd/** 35153187Spjd * Describes a management operation exposed by an MBean. Instances of 36153187Spjd * this class are immutable. Subclasses may be mutable but this is 37153187Spjd * not recommended. 38153187Spjd * 39153187Spjd * @since 1.5 40 */ 41public class MBeanOperationInfo extends MBeanFeatureInfo implements Cloneable { 42 43 /* Serial version */ 44 static final long serialVersionUID = -6178860474881375330L; 45 46 static final MBeanOperationInfo[] NO_OPERATIONS = 47 new MBeanOperationInfo[0]; 48 49 /** 50 * Indicates that the operation is read-like: 51 * it returns information but does not change any state. 52 */ 53 public static final int INFO = 0; 54 55 /** 56 * Indicates that the operation is write-like: it has an effect but does 57 * not return any information from the MBean. 58 */ 59 public static final int ACTION = 1; 60 61 /** 62 * Indicates that the operation is both read-like and write-like: 63 * it has an effect, and it also returns information from the MBean. 64 */ 65 public static final int ACTION_INFO = 2; 66 67 /** 68 * Indicates that the impact of the operation is unknown or cannot be 69 * expressed using one of the other values. 70 */ 71 public static final int UNKNOWN = 3; 72 73 /** 74 * @serial The method's return value. 75 */ 76 private final String type; 77 78 /** 79 * @serial The signature of the method, that is, the class names 80 * of the arguments. 81 */ 82 private final MBeanParameterInfo[] signature; 83 84 /** 85 * @serial The impact of the method, one of 86 * <CODE>INFO</CODE>, 87 * <CODE>ACTION</CODE>, 88 * <CODE>ACTION_INFO</CODE>, 89 * <CODE>UNKNOWN</CODE> 90 */ 91 private final int impact; 92 93 /** @see MBeanInfo#arrayGettersSafe */ 94 private final transient boolean arrayGettersSafe; 95 96 97 /** 98 * Constructs an <CODE>MBeanOperationInfo</CODE> object. The 99 * {@link Descriptor} of the constructed object will include 100 * fields contributed by any annotations on the {@code Method} 101 * object that contain the {@link DescriptorKey} meta-annotation. 102 * 103 * @param method The <CODE>java.lang.reflect.Method</CODE> object 104 * describing the MBean operation. 105 * @param description A human readable description of the operation. 106 */ 107 public MBeanOperationInfo(String description, Method method) { 108 this(method.getName(), 109 description, 110 methodSignature(method), 111 method.getReturnType().getName(), 112 UNKNOWN, 113 Introspector.descriptorForElement(method)); 114 } 115 116 /** 117 * Constructs an <CODE>MBeanOperationInfo</CODE> object. 118 * 119 * @param name The name of the method. 120 * @param description A human readable description of the operation. 121 * @param signature <CODE>MBeanParameterInfo</CODE> objects 122 * describing the parameters(arguments) of the method. This may be 123 * null with the same effect as a zero-length array. 124 * @param type The type of the method's return value. 125 * @param impact The impact of the method, one of 126 * {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO}, 127 * {@link #UNKNOWN}. 128 */ 129 public MBeanOperationInfo(String name, 130 String description, 131 MBeanParameterInfo[] signature, 132 String type, 133 int impact) { 134 this(name, description, signature, type, impact, (Descriptor) null); 135 } 136 137 /** 138 * Constructs an <CODE>MBeanOperationInfo</CODE> object. 139 * 140 * @param name The name of the method. 141 * @param description A human readable description of the operation. 142 * @param signature <CODE>MBeanParameterInfo</CODE> objects 143 * describing the parameters(arguments) of the method. This may be 144 * null with the same effect as a zero-length array. 145 * @param type The type of the method's return value. 146 * @param impact The impact of the method, one of 147 * {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO}, 148 * {@link #UNKNOWN}. 149 * @param descriptor The descriptor for the operation. This may be null 150 * which is equivalent to an empty descriptor. 151 * 152 * @since 1.6 153 */ 154 public MBeanOperationInfo(String name, 155 String description, 156 MBeanParameterInfo[] signature, 157 String type, 158 int impact, 159 Descriptor descriptor) { 160 161 super(name, description, descriptor); 162 163 if (signature == null || signature.length == 0) 164 signature = MBeanParameterInfo.NO_PARAMS; 165 else 166 signature = signature.clone(); 167 this.signature = signature; 168 this.type = type; 169 this.impact = impact; 170 this.arrayGettersSafe = 171 MBeanInfo.arrayGettersSafe(this.getClass(), 172 MBeanOperationInfo.class); 173 } 174 175 /** 176 * <p>Returns a shallow clone of this instance. 177 * The clone is obtained by simply calling <tt>super.clone()</tt>, 178 * thus calling the default native shallow cloning mechanism 179 * implemented by <tt>Object.clone()</tt>. 180 * No deeper cloning of any internal field is made.</p> 181 * 182 * <p>Since this class is immutable, cloning is chiefly of interest 183 * to subclasses.</p> 184 */ 185 @Override 186 public Object clone () { 187 try { 188 return super.clone() ; 189 } catch (CloneNotSupportedException e) { 190 // should not happen as this class is cloneable 191 return null; 192 } 193 } 194 195 /** 196 * Returns the type of the method's return value. 197 * 198 * @return the return type. 199 */ 200 public String getReturnType() { 201 return type; 202 } 203 204 /** 205 * <p>Returns the list of parameters for this operation. Each 206 * parameter is described by an <CODE>MBeanParameterInfo</CODE> 207 * object.</p> 208 * 209 * <p>The returned array is a shallow copy of the internal array, 210 * which means that it is a copy of the internal array of 211 * references to the <CODE>MBeanParameterInfo</CODE> objects but 212 * that each referenced <CODE>MBeanParameterInfo</CODE> object is 213 * not copied.</p> 214 * 215 * @return An array of <CODE>MBeanParameterInfo</CODE> objects. 216 */ 217 public MBeanParameterInfo[] getSignature() { 218 // If MBeanOperationInfo was created in our implementation, 219 // signature cannot be null - because our constructors replace 220 // null with MBeanParameterInfo.NO_PARAMS; 221 // 222 // However, signature could be null if an MBeanOperationInfo is 223 // deserialized from a byte array produced by another implementation. 224 // This is not very likely but possible, since the serial form says 225 // nothing against it. (see 6373150) 226 // 227 if (signature == null) 228 // if signature is null simply return an empty array . 229 // 230 return MBeanParameterInfo.NO_PARAMS; 231 else if (signature.length == 0) 232 return signature; 233 else 234 return signature.clone(); 235 } 236 237 private MBeanParameterInfo[] fastGetSignature() { 238 if (arrayGettersSafe) { 239 // if signature is null simply return an empty array . 240 // see getSignature() above. 241 // 242 if (signature == null) 243 return MBeanParameterInfo.NO_PARAMS; 244 else return signature; 245 } else return getSignature(); 246 } 247 248 /** 249 * Returns the impact of the method, one of 250 * <CODE>INFO</CODE>, <CODE>ACTION</CODE>, <CODE>ACTION_INFO</CODE>, <CODE>UNKNOWN</CODE>. 251 * 252 * @return the impact code. 253 */ 254 public int getImpact() { 255 return impact; 256 } 257 258 @Override 259 public String toString() { 260 String impactString; 261 switch (getImpact()) { 262 case ACTION: impactString = "action"; break; 263 case ACTION_INFO: impactString = "action/info"; break; 264 case INFO: impactString = "info"; break; 265 case UNKNOWN: impactString = "unknown"; break; 266 default: impactString = "(" + getImpact() + ")"; 267 } 268 return getClass().getName() + "[" + 269 "description=" + getDescription() + ", " + 270 "name=" + getName() + ", " + 271 "returnType=" + getReturnType() + ", " + 272 "signature=" + Arrays.asList(fastGetSignature()) + ", " + 273 "impact=" + impactString + ", " + 274 "descriptor=" + getDescriptor() + 275 "]"; 276 } 277 278 /** 279 * Compare this MBeanOperationInfo to another. 280 * 281 * @param o the object to compare to. 282 * 283 * @return true if and only if <code>o</code> is an MBeanOperationInfo such 284 * that its {@link #getName()}, {@link #getReturnType()}, {@link 285 * #getDescription()}, {@link #getImpact()}, {@link #getDescriptor()} 286 * and {@link #getSignature()} values are equal (not necessarily identical) 287 * to those of this MBeanConstructorInfo. Two signature arrays 288 * are equal if their elements are pairwise equal. 289 */ 290 @Override 291 public boolean equals(Object o) { 292 if (o == this) 293 return true; 294 if (!(o instanceof MBeanOperationInfo)) 295 return false; 296 MBeanOperationInfo p = (MBeanOperationInfo) o; 297 return (Objects.equals(p.getName(), getName()) && 298 Objects.equals(p.getReturnType(), getReturnType()) && 299 Objects.equals(p.getDescription(), getDescription()) && 300 p.getImpact() == getImpact() && 301 Arrays.equals(p.fastGetSignature(), fastGetSignature()) && 302 Objects.equals(p.getDescriptor(), getDescriptor())); 303 } 304 305 /* We do not include everything in the hashcode. We assume that 306 if two operations are different they'll probably have different 307 names or types. The penalty we pay when this assumption is 308 wrong should be less than the penalty we would pay if it were 309 right and we needlessly hashed in the description and the 310 parameter array. */ 311 @Override 312 public int hashCode() { 313 return Objects.hash(getName(), getReturnType()); 314 } 315 316 private static MBeanParameterInfo[] methodSignature(Method method) { 317 final Class<?>[] classes = method.getParameterTypes(); 318 final Annotation[][] annots = method.getParameterAnnotations(); 319 return parameters(classes, annots); 320 } 321 322 static MBeanParameterInfo[] parameters(Class<?>[] classes, 323 Annotation[][] annots) { 324 final MBeanParameterInfo[] params = 325 new MBeanParameterInfo[classes.length]; 326 assert(classes.length == annots.length); 327 328 for (int i = 0; i < classes.length; i++) { 329 Descriptor d = Introspector.descriptorForAnnotations(annots[i]); 330 final String pn = "p" + (i + 1); 331 params[i] = 332 new MBeanParameterInfo(pn, classes[i].getName(), "", d); 333 } 334 335 return params; 336 } 337} 338