Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/management/MBeanOperationInfo.java
38829 views
/*1* Copyright (c) 1999, 2013, 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</CODE>,86* <CODE>ACTION</CODE>,87* <CODE>ACTION_INFO</CODE>,88* <CODE>UNKNOWN</CODE>89*/90private final int impact;9192/** @see MBeanInfo#arrayGettersSafe */93private final transient boolean arrayGettersSafe;949596/**97* Constructs an <CODE>MBeanOperationInfo</CODE> object. The98* {@link Descriptor} of the constructed object will include99* fields contributed by any annotations on the {@code Method}100* object that contain the {@link DescriptorKey} meta-annotation.101*102* @param method The <CODE>java.lang.reflect.Method</CODE> object103* describing the MBean operation.104* @param description A human readable description of the operation.105*/106public MBeanOperationInfo(String description, Method method) {107this(method.getName(),108description,109methodSignature(method),110method.getReturnType().getName(),111UNKNOWN,112Introspector.descriptorForElement(method));113}114115/**116* Constructs an <CODE>MBeanOperationInfo</CODE> object.117*118* @param name The name of the method.119* @param description A human readable description of the operation.120* @param signature <CODE>MBeanParameterInfo</CODE> objects121* describing the parameters(arguments) of the method. This may be122* null with the same effect as a zero-length array.123* @param type The type of the method's return value.124* @param impact The impact of the method, one of125* {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO},126* {@link #UNKNOWN}.127*/128public MBeanOperationInfo(String name,129String description,130MBeanParameterInfo[] signature,131String type,132int impact) {133this(name, description, signature, type, impact, (Descriptor) null);134}135136/**137* Constructs an <CODE>MBeanOperationInfo</CODE> object.138*139* @param name The name of the method.140* @param description A human readable description of the operation.141* @param signature <CODE>MBeanParameterInfo</CODE> objects142* describing the parameters(arguments) of the method. This may be143* null with the same effect as a zero-length array.144* @param type The type of the method's return value.145* @param impact The impact of the method, one of146* {@link #INFO}, {@link #ACTION}, {@link #ACTION_INFO},147* {@link #UNKNOWN}.148* @param descriptor The descriptor for the operation. This may be null149* which is equivalent to an empty descriptor.150*151* @since 1.6152*/153public MBeanOperationInfo(String name,154String description,155MBeanParameterInfo[] signature,156String type,157int impact,158Descriptor descriptor) {159160super(name, description, descriptor);161162if (signature == null || signature.length == 0)163signature = MBeanParameterInfo.NO_PARAMS;164else165signature = signature.clone();166this.signature = signature;167this.type = type;168this.impact = impact;169this.arrayGettersSafe =170MBeanInfo.arrayGettersSafe(this.getClass(),171MBeanOperationInfo.class);172}173174/**175* <p>Returns a shallow clone of this instance.176* The clone is obtained by simply calling <tt>super.clone()</tt>,177* thus calling the default native shallow cloning mechanism178* implemented by <tt>Object.clone()</tt>.179* No deeper cloning of any internal field is made.</p>180*181* <p>Since this class is immutable, cloning is chiefly of interest182* to subclasses.</p>183*/184@Override185public Object clone () {186try {187return super.clone() ;188} catch (CloneNotSupportedException e) {189// should not happen as this class is cloneable190return null;191}192}193194/**195* Returns the type of the method's return value.196*197* @return the return type.198*/199public String getReturnType() {200return type;201}202203/**204* <p>Returns the list of parameters for this operation. Each205* parameter is described by an <CODE>MBeanParameterInfo</CODE>206* object.</p>207*208* <p>The returned array is a shallow copy of the internal array,209* which means that it is a copy of the internal array of210* references to the <CODE>MBeanParameterInfo</CODE> objects but211* that each referenced <CODE>MBeanParameterInfo</CODE> object is212* not copied.</p>213*214* @return An array of <CODE>MBeanParameterInfo</CODE> objects.215*/216public MBeanParameterInfo[] getSignature() {217// If MBeanOperationInfo was created in our implementation,218// signature cannot be null - because our constructors replace219// null with MBeanParameterInfo.NO_PARAMS;220//221// However, signature could be null if an MBeanOperationInfo is222// deserialized from a byte array produced by another implementation.223// This is not very likely but possible, since the serial form says224// nothing against it. (see 6373150)225//226if (signature == null)227// if signature is null simply return an empty array .228//229return MBeanParameterInfo.NO_PARAMS;230else if (signature.length == 0)231return signature;232else233return signature.clone();234}235236private MBeanParameterInfo[] fastGetSignature() {237if (arrayGettersSafe) {238// if signature is null simply return an empty array .239// see getSignature() above.240//241if (signature == null)242return MBeanParameterInfo.NO_PARAMS;243else return signature;244} else return getSignature();245}246247/**248* Returns the impact of the method, one of249* <CODE>INFO</CODE>, <CODE>ACTION</CODE>, <CODE>ACTION_INFO</CODE>, <CODE>UNKNOWN</CODE>.250*251* @return the impact code.252*/253public int getImpact() {254return impact;255}256257@Override258public String toString() {259String impactString;260switch (getImpact()) {261case ACTION: impactString = "action"; break;262case ACTION_INFO: impactString = "action/info"; break;263case INFO: impactString = "info"; break;264case UNKNOWN: impactString = "unknown"; break;265default: impactString = "(" + getImpact() + ")";266}267return getClass().getName() + "[" +268"description=" + getDescription() + ", " +269"name=" + getName() + ", " +270"returnType=" + getReturnType() + ", " +271"signature=" + Arrays.asList(fastGetSignature()) + ", " +272"impact=" + impactString + ", " +273"descriptor=" + getDescriptor() +274"]";275}276277/**278* Compare this MBeanOperationInfo to another.279*280* @param o the object to compare to.281*282* @return true if and only if <code>o</code> is an MBeanOperationInfo such283* that its {@link #getName()}, {@link #getReturnType()}, {@link284* #getDescription()}, {@link #getImpact()}, {@link #getDescriptor()}285* and {@link #getSignature()} values are equal (not necessarily identical)286* to those of this MBeanConstructorInfo. Two signature arrays287* are equal if their elements are pairwise equal.288*/289@Override290public boolean equals(Object o) {291if (o == this)292return true;293if (!(o instanceof MBeanOperationInfo))294return false;295MBeanOperationInfo p = (MBeanOperationInfo) o;296return (Objects.equals(p.getName(), getName()) &&297Objects.equals(p.getReturnType(), getReturnType()) &&298Objects.equals(p.getDescription(), getDescription()) &&299p.getImpact() == getImpact() &&300Arrays.equals(p.fastGetSignature(), fastGetSignature()) &&301Objects.equals(p.getDescriptor(), getDescriptor()));302}303304/* We do not include everything in the hashcode. We assume that305if two operations are different they'll probably have different306names or types. The penalty we pay when this assumption is307wrong should be less than the penalty we would pay if it were308right and we needlessly hashed in the description and the309parameter array. */310@Override311public int hashCode() {312return Objects.hash(getName(), getReturnType());313}314315private static MBeanParameterInfo[] methodSignature(Method method) {316final Class<?>[] classes = method.getParameterTypes();317final Annotation[][] annots = method.getParameterAnnotations();318return parameters(classes, annots);319}320321static MBeanParameterInfo[] parameters(Class<?>[] classes,322Annotation[][] annots) {323final MBeanParameterInfo[] params =324new MBeanParameterInfo[classes.length];325assert(classes.length == annots.length);326327for (int i = 0; i < classes.length; i++) {328Descriptor d = Introspector.descriptorForAnnotations(annots[i]);329final String pn = "p" + (i + 1);330params[i] =331new MBeanParameterInfo(pn, classes[i].getName(), "", d);332}333334return params;335}336}337338339