Path: blob/master/src/java.management/share/classes/javax/management/MBeanFeatureInfo.java
41154 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 java.io.IOException;28import java.io.ObjectInputStream;29import java.io.ObjectOutputStream;30import java.io.Serializable;31import java.io.StreamCorruptedException;32import java.util.Objects;3334/**35* <p>Provides general information for an MBean descriptor object.36* The feature described can be an attribute, an operation, a37* parameter, or a notification. Instances of this class are38* immutable. Subclasses may be mutable but this is not39* recommended.</p>40*41* @since 1.542*/43public class MBeanFeatureInfo implements Serializable, DescriptorRead {444546/* Serial version */47static final long serialVersionUID = 3952882688968447265L;4849/**50* The name of the feature. It is recommended that subclasses call51* {@link #getName} rather than reading this field, and that they52* not change it.53*54* @serial The name of the feature.55*/56protected String name;5758/**59* The human-readable description of the feature. It is60* recommended that subclasses call {@link #getDescription} rather61* than reading this field, and that they not change it.62*63* @serial The human-readable description of the feature.64*/65protected String description;6667/**68* @serial The Descriptor for this MBeanFeatureInfo. This field69* can be null, which is equivalent to an empty Descriptor.70*/71private transient Descriptor descriptor;727374/**75* Constructs an <CODE>MBeanFeatureInfo</CODE> object. This76* constructor is equivalent to {@code MBeanFeatureInfo(name,77* description, (Descriptor) null}.78*79* @param name The name of the feature.80* @param description A human readable description of the feature.81*/82public MBeanFeatureInfo(String name, String description) {83this(name, description, null);84}8586/**87* Constructs an <CODE>MBeanFeatureInfo</CODE> object.88*89* @param name The name of the feature.90* @param description A human readable description of the feature.91* @param descriptor The descriptor for the feature. This may be null92* which is equivalent to an empty descriptor.93*94* @since 1.695*/96public MBeanFeatureInfo(String name, String description,97Descriptor descriptor) {98this.name = name;99this.description = description;100this.descriptor = descriptor;101}102103/**104* Returns the name of the feature.105*106* @return the name of the feature.107*/108public String getName() {109return name;110}111112/**113* Returns the human-readable description of the feature.114*115* @return the human-readable description of the feature.116*/117public String getDescription() {118return description;119}120121/**122* Returns the descriptor for the feature. Changing the returned value123* will have no affect on the original descriptor.124*125* @return a descriptor that is either immutable or a copy of the original.126*127* @since 1.6128*/129public Descriptor getDescriptor() {130return (Descriptor) ImmutableDescriptor.nonNullDescriptor(descriptor).clone();131}132133/**134* Compare this MBeanFeatureInfo to another.135*136* @param o the object to compare to.137*138* @return true if and only if <code>o</code> is an MBeanFeatureInfo such139* that its {@link #getName()}, {@link #getDescription()}, and140* {@link #getDescriptor()}141* values are equal (not necessarily identical) to those of this142* MBeanFeatureInfo.143*/144public boolean equals(Object o) {145if (o == this)146return true;147if (!(o instanceof MBeanFeatureInfo))148return false;149MBeanFeatureInfo p = (MBeanFeatureInfo) o;150return (Objects.equals(p.getName(), getName()) &&151Objects.equals(p.getDescription(), getDescription()) &&152Objects.equals(p.getDescriptor(), getDescriptor()));153}154155public int hashCode() {156return getName().hashCode() ^ getDescription().hashCode() ^157getDescriptor().hashCode();158}159160/**161* Serializes an {@link MBeanFeatureInfo} to an {@link ObjectOutputStream}.162* @serialData163* For compatibility reasons, an object of this class is serialized as follows.164* <p>165* The method {@link ObjectOutputStream#defaultWriteObject defaultWriteObject()}166* is called first to serialize the object except the field {@code descriptor}167* which is declared as transient. The field {@code descriptor} is serialized168* as follows:169* <ul>170* <li>If {@code descriptor} is an instance of the class171* {@link ImmutableDescriptor}, the method {@link ObjectOutputStream#write172* write(int val)} is called to write a byte with the value {@code 1},173* then the method {@link ObjectOutputStream#writeObject writeObject(Object obj)}174* is called twice to serialize the field names and the field values of the175* {@code descriptor}, respectively as a {@code String[]} and an176* {@code Object[]};</li>177* <li>Otherwise, the method {@link ObjectOutputStream#write write(int val)}178* is called to write a byte with the value {@code 0}, then the method179* {@link ObjectOutputStream#writeObject writeObject(Object obj)} is called180* to serialize directly the field {@code descriptor}.181* </ul>182*183* @since 1.6184*/185private void writeObject(ObjectOutputStream out) throws IOException {186out.defaultWriteObject();187188if (descriptor != null &&189descriptor.getClass() == ImmutableDescriptor.class) {190191out.write(1);192193final String[] names = descriptor.getFieldNames();194195out.writeObject(names);196out.writeObject(descriptor.getFieldValues(names));197} else {198out.write(0);199200out.writeObject(descriptor);201}202}203204/**205* Deserializes an {@link MBeanFeatureInfo} from an {@link ObjectInputStream}.206* @serialData207* For compatibility reasons, an object of this class is deserialized as follows.208* <p>209* The method {@link ObjectInputStream#defaultReadObject defaultReadObject()}210* is called first to deserialize the object except the field211* {@code descriptor}, which is not serialized in the default way. Then the method212* {@link ObjectInputStream#read read()} is called to read a byte, the field213* {@code descriptor} is deserialized according to the value of the byte value:214* <ul>215* <li>1. The method {@link ObjectInputStream#readObject readObject()}216* is called twice to obtain the field names (a {@code String[]}) and217* the field values (an {@code Object[]}) of the {@code descriptor}.218* The two obtained values then are used to construct219* an {@link ImmutableDescriptor} instance for the field220* {@code descriptor};</li>221* <li>0. The value for the field {@code descriptor} is obtained directly222* by calling the method {@link ObjectInputStream#readObject readObject()}.223* If the obtained value is null, the field {@code descriptor} is set to224* {@link ImmutableDescriptor#EMPTY_DESCRIPTOR EMPTY_DESCRIPTOR};</li>225* <li>-1. This means that there is no byte to read and that the object is from226* an earlier version of the JMX API. The field {@code descriptor} is set227* to {@link ImmutableDescriptor#EMPTY_DESCRIPTOR EMPTY_DESCRIPTOR}</li>228* <li>Any other value. A {@link StreamCorruptedException} is thrown.</li>229* </ul>230*231* @since 1.6232*/233private void readObject(ObjectInputStream in)234throws IOException, ClassNotFoundException {235236in.defaultReadObject();237238switch (in.read()) {239case 1:240final String[] names = (String[])in.readObject();241242final Object[] values = (Object[]) in.readObject();243descriptor = (names.length == 0) ?244ImmutableDescriptor.EMPTY_DESCRIPTOR :245new ImmutableDescriptor(names, values);246247break;248case 0:249descriptor = (Descriptor)in.readObject();250251if (descriptor == null) {252descriptor = ImmutableDescriptor.EMPTY_DESCRIPTOR;253}254255break;256case -1: // from an earlier version of the JMX API257descriptor = ImmutableDescriptor.EMPTY_DESCRIPTOR;258259break;260default:261throw new StreamCorruptedException("Got unexpected byte.");262}263}264}265266267