/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.management.openmbean;
// java import
//
import java.util.Set;
import javax.management.Descriptor;
import javax.management.DescriptorRead; // for Javadoc
import javax.management.ImmutableDescriptor;
import javax.management.MBeanParameterInfo;
// OpenMBeanAttributeInfoSupport and this class are very similar
// but can't easily be refactored because there's no multiple inheritance.
// The best we can do for refactoring is to put a bunch of static methods
// in OpenMBeanAttributeInfoSupport and import them here.
import static javax.management.openmbean.OpenMBeanAttributeInfoSupport.*;
/**
* Describes a parameter used in one or more operations or
* constructors of an open MBean.
*
*
* @since 1.5
*/
public class OpenMBeanParameterInfoSupport
extends MBeanParameterInfo
implements OpenMBeanParameterInfo {
/* Serial version */
static final long serialVersionUID = -7235016873758443122L;
/**
* @serial The open mbean parameter's open type
*/
@SuppressWarnings("serial") // Not statically typed as Serializable
private OpenType> openType;
/**
* @serial The open mbean parameter's default value
*/
@SuppressWarnings("serial") // Not statically typed as Serializable
private Object defaultValue = null;
/**
* @serial The open mbean parameter's legal values. This {@link
* Set} is unmodifiable
*/
@SuppressWarnings("serial") // Conditionally serializable
private Set> legalValues = null; // to be constructed unmodifiable
/**
* @serial The open mbean parameter's min value
*/
@SuppressWarnings("serial") // Conditionally serializable
private Comparable> minValue = null;
/**
* @serial The open mbean parameter's max value
*/
@SuppressWarnings("serial") // Conditionally serializable
private Comparable> maxValue = null;
// As this instance is immutable, these two values need only
// be calculated once.
private transient Integer myHashCode = null; // As this instance is immutable, these two values
private transient String myToString = null; // need only be calculated once.
/**
* Constructs an {@code OpenMBeanParameterInfoSupport} instance,
* which describes the parameter used in one or more operations or
* constructors of a class of open MBeans, with the specified
* {@code name}, {@code openType} and {@code description}.
*
* @param name cannot be a null or empty string.
*
* @param description cannot be a null or empty string.
*
* @param openType cannot be null.
*
* @throws IllegalArgumentException if {@code name} or {@code
* description} are null or empty string, or {@code openType} is
* null.
*/
public OpenMBeanParameterInfoSupport(String name,
String description,
OpenType> openType) {
this(name, description, openType, (Descriptor) null);
}
/**
* Constructs an {@code OpenMBeanParameterInfoSupport} instance,
* which describes the parameter used in one or more operations or
* constructors of a class of open MBeans, with the specified
* {@code name}, {@code openType}, {@code description},
* and {@code descriptor}.
*
*
The {@code descriptor} can contain entries that will define
* the values returned by certain methods of this class, as
* explained in the
* package description.
*
* @param name cannot be a null or empty string.
*
* @param description cannot be a null or empty string.
*
* @param openType cannot be null.
*
* @param descriptor The descriptor for the parameter. This may be null
* which is equivalent to an empty descriptor.
*
* @throws IllegalArgumentException if {@code name} or {@code
* description} are null or empty string, or {@code openType} is
* null, or the descriptor entries are invalid as described in the
* package
* description.
*
* @since 1.6
*/
public OpenMBeanParameterInfoSupport(String name,
String description,
OpenType> openType,
Descriptor descriptor) {
// Construct parent's state
//
super(name,
(openType==null) ? null : openType.getClassName(),
description,
ImmutableDescriptor.union(descriptor,(openType==null)?null:
openType.getDescriptor()));
// Initialize this instance's specific state
//
this.openType = openType;
descriptor = getDescriptor(); // replace null by empty
this.defaultValue = valueFrom(descriptor, "defaultValue", openType);
this.legalValues = valuesFrom(descriptor, "legalValues", openType);
this.minValue = comparableValueFrom(descriptor, "minValue", openType);
this.maxValue = comparableValueFrom(descriptor, "maxValue", openType);
try {
check(this);
} catch (OpenDataException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
/**
* Constructs an {@code OpenMBeanParameterInfoSupport} instance,
* which describes the parameter used in one or more operations or
* constructors of a class of open MBeans, with the specified
* {@code name}, {@code openType}, {@code description} and {@code
* defaultValue}.
*
* @param name cannot be a null or empty string.
*
* @param description cannot be a null or empty string.
*
* @param openType cannot be null.
*
* @param defaultValue must be a valid value for the {@code
* openType} specified for this parameter; default value not
* supported for {@code ArrayType} and {@code TabularType}; can be
* null, in which case it means that no default value is set.
*
* @param allows the compiler to check that the {@code defaultValue},
* if non-null, has the correct Java type for the given {@code openType}.
*
* @throws IllegalArgumentException if {@code name} or {@code
* description} are null or empty string, or {@code openType} is
* null.
*
* @throws OpenDataException if {@code defaultValue} is not a
* valid value for the specified {@code openType}, or {@code
* defaultValue} is non null and {@code openType} is an {@code
* ArrayType} or a {@code TabularType}.
*/
public OpenMBeanParameterInfoSupport(String name,
String description,
OpenType openType,
T defaultValue)
throws OpenDataException {
this(name, description, openType, defaultValue, (T[]) null);
}
/**
*
Constructs an {@code OpenMBeanParameterInfoSupport} instance,
* which describes the parameter used in one or more operations or
* constructors of a class of open MBeans, with the specified
* {@code name}, {@code openType}, {@code description}, {@code
* defaultValue} and {@code legalValues}.
*
*
The contents of {@code legalValues} are copied, so subsequent
* modifications of the array referenced by {@code legalValues}
* have no impact on this {@code OpenMBeanParameterInfoSupport}
* instance.
*
* @param name cannot be a null or empty string.
*
* @param description cannot be a null or empty string.
*
* @param openType cannot be null.
*
* @param defaultValue must be a valid value for the {@code
* openType} specified for this parameter; default value not
* supported for {@code ArrayType} and {@code TabularType}; can be
* null, in which case it means that no default value is set.
*
* @param legalValues each contained value must be valid for the
* {@code openType} specified for this parameter; legal values not
* supported for {@code ArrayType} and {@code TabularType}; can be
* null or empty.
*
* @param allows the compiler to check that the {@code
* defaultValue} and {@code legalValues}, if non-null, have the
* correct Java type for the given {@code openType}.
*
* @throws IllegalArgumentException if {@code name} or {@code
* description} are null or empty string, or {@code openType} is
* null.
*
* @throws OpenDataException if {@code defaultValue} is not a
* valid value for the specified {@code openType}, or one value in
* {@code legalValues} is not valid for the specified {@code
* openType}, or {@code defaultValue} is non null and {@code
* openType} is an {@code ArrayType} or a {@code TabularType}, or
* {@code legalValues} is non null and non empty and {@code
* openType} is an {@code ArrayType} or a {@code TabularType}, or
* {@code legalValues} is non null and non empty and {@code
* defaultValue} is not contained in {@code legalValues}.
*/
public OpenMBeanParameterInfoSupport(String name,
String description,
OpenType openType,
T defaultValue,
T[] legalValues)
throws OpenDataException {
this(name, description, openType,
defaultValue, legalValues, null, null);
}
/**
* Constructs an {@code OpenMBeanParameterInfoSupport} instance,
* which describes the parameter used in one or more operations or
* constructors of a class of open MBeans, with the specified
* {@code name}, {@code openType}, {@code description}, {@code
* defaultValue}, {@code minValue} and {@code maxValue}.
*
* It is possible to specify minimal and maximal values only for
* an open type whose values are {@code Comparable}.
*
* @param name cannot be a null or empty string.
*
* @param description cannot be a null or empty string.
*
* @param openType cannot be null.
*
* @param defaultValue must be a valid value for the {@code
* openType} specified for this parameter; default value not
* supported for {@code ArrayType} and {@code TabularType}; can be
* null, in which case it means that no default value is set.
*
* @param minValue must be valid for the {@code openType}
* specified for this parameter; can be null, in which case it
* means that no minimal value is set.
*
* @param maxValue must be valid for the {@code openType}
* specified for this parameter; can be null, in which case it
* means that no maximal value is set.
*
* @param allows the compiler to check that the {@code
* defaultValue}, {@code minValue}, and {@code maxValue}, if
* non-null, have the correct Java type for the given {@code
* openType}.
*
* @throws IllegalArgumentException if {@code name} or {@code
* description} are null or empty string, or {@code openType} is
* null.
*
* @throws OpenDataException if {@code defaultValue}, {@code
* minValue} or {@code maxValue} is not a valid value for the
* specified {@code openType}, or {@code defaultValue} is non null
* and {@code openType} is an {@code ArrayType} or a {@code
* TabularType}, or both {@code minValue} and {@code maxValue} are
* non-null and {@code minValue.compareTo(maxValue) > 0} is {@code
* true}, or both {@code defaultValue} and {@code minValue} are
* non-null and {@code minValue.compareTo(defaultValue) > 0} is
* {@code true}, or both {@code defaultValue} and {@code maxValue}
* are non-null and {@code defaultValue.compareTo(maxValue) > 0}
* is {@code true}.
*/
public OpenMBeanParameterInfoSupport(String name,
String description,
OpenType openType,
T defaultValue,
Comparable minValue,
Comparable maxValue)
throws OpenDataException {
this(name, description, openType,
defaultValue, null, minValue, maxValue);
}
private OpenMBeanParameterInfoSupport(String name,
String description,
OpenType openType,
T defaultValue,
T[] legalValues,
Comparable minValue,
Comparable maxValue)
throws OpenDataException {
super(name,
(openType == null) ? null : openType.getClassName(),
description,
makeDescriptor(openType,
defaultValue, legalValues, minValue, maxValue));
this.openType = openType;
Descriptor d = getDescriptor();
this.defaultValue = defaultValue;
this.minValue = minValue;
this.maxValue = maxValue;
// We already converted the array into an unmodifiable Set
// in the descriptor.
this.legalValues = (Set>) d.getFieldValue("legalValues");
check(this);
}
/**
* An object serialized in a version of the API before Descriptors were
* added to this class will have an empty or null Descriptor.
* For consistency with our
* behavior in this version, we must replace the object with one
* where the Descriptors reflect the same values of openType, defaultValue,
* etc.
**/
private Object readResolve() {
if (getDescriptor().getFieldNames().length == 0) {
// This noise allows us to avoid "unchecked" warnings without
// having to suppress them explicitly.
OpenType