Path: blob/master/src/java.management/share/classes/javax/management/MBeanOperationInfo.java
41154 views
/*1* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.management;2627import com.sun.jmx.mbeanserver.Introspector;28import java.lang.annotation.Annotation;29import java.lang.reflect.Method;30import java.util.Arrays;31import java.util.Objects;3233/**34* Describes a management operation exposed by an MBean. Instances of35* this class are immutable. Subclasses may be mutable but this is36* not recommended.37*38* @since 1.539*/40public class MBeanOperationInfo extends MBeanFeatureInfo implements Cloneable {4142/* Serial version */43static final long serialVersionUID = -6178860474881375330L;4445static final MBeanOperationInfo[] NO_OPERATIONS =46new MBeanOperationInfo[0];4748/**49* Indicates that the operation is read-like:50* it returns information but does not change any state.51*/52public static final int INFO = 0;5354/**55* Indicates that the operation is write-like: it has an effect but does56* not return any information from the MBean.57*/58public static final int ACTION = 1;5960/**61* Indicates that the operation is both read-like and write-like:62* it has an effect, and it also returns information from the MBean.63*/64public static final int ACTION_INFO = 2;6566/**67* Indicates that the impact of the operation is unknown or cannot be68* expressed using one of the other values.69*/70public static final int UNKNOWN = 3;7172/**73* @serial The method's return value.74*/75private final String type;7677/**78* @serial The signature of the method, that is, the class names79* of the arguments.80*/81private final MBeanParameterInfo[] signature;8283/**84* @serial The impact of the method, one of85* {@code INFO, ACTION, ACTION_INFO, UNKNOWN}.86*/87private final int impact;8889/** @see MBeanInfo#arrayGettersSafe */90private final transient boolean arrayGettersSafe;919293/**94* Constructs an {@code MBeanOperationInfo} object. The95* {@link Descriptor} of the constructed object will include96* fields contributed by any annotations on the {@code Method}97* object that contain the {@link DescriptorKey} meta-annotation.98*99* @param method The {@code java.lang.reflect.Method} object100* describing the MBean operation.101* @param description A human readable description of the operation.102*/103public MBeanOperationInfo(String description, Method method) {104this(method.getName(),105description,106methodSignature(method),107method.getReturnType().getName(),108UNKNOWN,109Introspector.descriptorForElement(method));110}111112/**113* Constructs an {@code MBeanOperationInfo} object.114*115* @param name The name of the method.116* @param description A human readable description of the operation.117* @param signature {@code MBeanParameterInfo} objects118* describing the parameters(arguments) of the method. This may be119* null with the same effect as a zero-length array.120* @param type The type of the method's return value.121* @param impact The impact of the method, one of122* {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO},123* {@link #UNKNOWN}.124*/125public MBeanOperationInfo(String name,126String description,127MBeanParameterInfo[] signature,128String type,129int impact) {130this(name, description, signature, type, impact, (Descriptor) null);131}132133/**134* Constructs an {@code MBeanOperationInfo} object.135*136* @param name The name of the method.137* @param description A human readable description of the operation.138* @param signature {@code MBeanParameterInfo} objects139* describing the parameters(arguments) of the method. This may be140* null with the same effect as a zero-length array.141* @param type The type of the method's return value.142* @param impact The impact of the method, one of143* {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO},144* {@link #UNKNOWN}.145* @param descriptor The descriptor for the operation. This may be null146* which is equivalent to an empty descriptor.147*148* @throws IllegalArgumentException if {@code impact} is not one of149* {@linkplain #ACTION}, {@linkplain #ACTION_INFO}, {@linkplain #INFO} or {@linkplain #UNKNOWN}.150*151* @since 1.6152*/153public MBeanOperationInfo(String name,154String description,155MBeanParameterInfo[] signature,156String type,157int impact,158Descriptor descriptor) {159160super(name, description, descriptor);161162if (impact < INFO || impact > UNKNOWN) {163throw new IllegalArgumentException("Argument impact can only be "164+ "one of ACTION, ACTION_INFO, "165+ "INFO, or UNKNOWN" + " given value is :" + impact);166}167168if (signature == null || signature.length == 0)169signature = MBeanParameterInfo.NO_PARAMS;170else171signature = signature.clone();172this.signature = signature;173this.type = type;174this.impact = impact;175this.arrayGettersSafe =176MBeanInfo.arrayGettersSafe(this.getClass(),177MBeanOperationInfo.class);178}179180/**181* <p>Returns a shallow clone of this instance.182* The clone is obtained by simply calling {@code super.clone()},183* thus calling the default native shallow cloning mechanism184* implemented by {@code Object.clone()}.185* No deeper cloning of any internal field is made.</p>186*187* <p>Since this class is immutable, cloning is chiefly of interest188* to subclasses.</p>189*/190@Override191public Object clone () {192try {193return super.clone() ;194} catch (CloneNotSupportedException e) {195// should not happen as this class is cloneable196return null;197}198}199200/**201* Returns the type of the method's return value.202*203* @return the return type.204*/205public String getReturnType() {206return type;207}208209/**210* <p>Returns the list of parameters for this operation. Each211* parameter is described by an {@code MBeanParameterInfo}212* object.</p>213*214* <p>The returned array is a shallow copy of the internal array,215* which means that it is a copy of the internal array of216* references to the {@code MBeanParameterInfo} objects but217* that each referenced {@code MBeanParameterInfo} object is218* not copied.</p>219*220* @return An array of {@code MBeanParameterInfo} objects.221*/222public MBeanParameterInfo[] getSignature() {223// If MBeanOperationInfo was created in our implementation,224// signature cannot be null - because our constructors replace225// null with MBeanParameterInfo.NO_PARAMS;226//227// However, signature could be null if an MBeanOperationInfo is228// deserialized from a byte array produced by another implementation.229// This is not very likely but possible, since the serial form says230// nothing against it. (see 6373150)231//232if (signature == null)233// if signature is null simply return an empty array .234//235return MBeanParameterInfo.NO_PARAMS;236else if (signature.length == 0)237return signature;238else239return signature.clone();240}241242private MBeanParameterInfo[] fastGetSignature() {243if (arrayGettersSafe) {244// if signature is null simply return an empty array .245// see getSignature() above.246//247if (signature == null)248return MBeanParameterInfo.NO_PARAMS;249else return signature;250} else return getSignature();251}252253/**254* Returns the impact of the method, one of255* {@code INFO, ACTION, ACTION_INFO, UNKNOWN}.256*257* @return the impact code.258*/259public int getImpact() {260return impact;261}262263@Override264public String toString() {265String impactString;266switch (getImpact()) {267case ACTION: impactString = "action"; break;268case ACTION_INFO: impactString = "action/info"; break;269case INFO: impactString = "info"; break;270default: impactString = "unknown";271}272return getClass().getName() + "[" +273"description=" + getDescription() + ", " +274"name=" + getName() + ", " +275"returnType=" + getReturnType() + ", " +276"signature=" + Arrays.asList(fastGetSignature()) + ", " +277"impact=" + impactString + ", " +278"descriptor=" + getDescriptor() +279"]";280}281282/**283* Compare this MBeanOperationInfo to another.284*285* @param o the object to compare to.286*287* @return true if and only if {@code o} is an MBeanOperationInfo such288* that its {@link #getName()}, {@link #getReturnType()}, {@link289* #getDescription()}, {@link #getImpact()}, {@link #getDescriptor()}290* and {@link #getSignature()} values are equal (not necessarily identical)291* to those of this MBeanConstructorInfo. Two signature arrays292* are equal if their elements are pairwise equal.293*/294@Override295public boolean equals(Object o) {296if (o == this)297return true;298if (!(o instanceof MBeanOperationInfo))299return false;300MBeanOperationInfo p = (MBeanOperationInfo) o;301return (Objects.equals(p.getName(), getName()) &&302Objects.equals(p.getReturnType(), getReturnType()) &&303Objects.equals(p.getDescription(), getDescription()) &&304p.getImpact() == getImpact() &&305Arrays.equals(p.fastGetSignature(), fastGetSignature()) &&306Objects.equals(p.getDescriptor(), getDescriptor()));307}308309/* We do not include everything in the hashcode. We assume that310if two operations are different they'll probably have different311names or types. The penalty we pay when this assumption is312wrong should be less than the penalty we would pay if it were313right and we needlessly hashed in the description and the314parameter array. */315@Override316public int hashCode() {317return Objects.hash(getName(), getReturnType());318}319320private static MBeanParameterInfo[] methodSignature(Method method) {321final Class<?>[] classes = method.getParameterTypes();322final Annotation[][] annots = method.getParameterAnnotations();323return parameters(classes, annots);324}325326static MBeanParameterInfo[] parameters(Class<?>[] classes,327Annotation[][] annots) {328final MBeanParameterInfo[] params =329new MBeanParameterInfo[classes.length];330assert(classes.length == annots.length);331332for (int i = 0; i < classes.length; i++) {333Descriptor d = Introspector.descriptorForAnnotations(annots[i]);334final String pn = "p" + (i + 1);335params[i] =336new MBeanParameterInfo(pn, classes[i].getName(), "", d);337}338339return params;340}341}342343344