src/java.management/share/classes/javax/management/openmbean/OpenMBeanAttributeInfoSupport.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3446
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3446
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3446
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3446
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3446
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package javax.management.openmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
// java import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.DescriptorRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import com.sun.jmx.remote.util.EnvHelp;
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
    48
import sun.reflect.misc.MethodUtil;
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
    49
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Describes an attribute of an open MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class OpenMBeanAttributeInfoSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    extends MBeanAttributeInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    implements OpenMBeanAttributeInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final long serialVersionUID = -4867215622149721849L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @serial The open mbean attribute's <i>open type</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
    67
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private OpenType<?> openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @serial The open mbean attribute's default value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
    73
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private final Object defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @serial The open mbean attribute's legal values. This {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Set} is unmodifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
    80
    @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final Set<?> legalValues;  // to be constructed unmodifiable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @serial The open mbean attribute's min value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
    86
    @SuppressWarnings("serial") // Conditionally serializable
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    87
    private final Comparable<?> minValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @serial The open mbean attribute's max value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 52902
diff changeset
    92
    @SuppressWarnings("serial") // Conditionally serializable
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
    93
    private final Comparable<?> maxValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // As this instance is immutable, these two values need only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // be calculated once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private transient Integer myHashCode = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private transient String  myToString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * specified {@code name}, {@code openType} and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * description}, and the specified read/write access properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param isIs {@code true} if the attribute's getter is of the
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   121
     * form <code>is<i>XXX</i></code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public OpenMBeanAttributeInfoSupport(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                         String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                         OpenType<?> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                         boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                         boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                         boolean isIs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
             (Descriptor) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * specified {@code name}, {@code openType}, {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * description}, read/write access properties, and {@code Descriptor}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>The {@code descriptor} can contain entries that will define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * the values returned by certain methods of this class, as
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 18220
diff changeset
   145
     * explained in the <a href="package-summary.html#constraints">
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 18220
diff changeset
   146
     * package description</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param isIs {@code true} if the attribute's getter is of the
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   161
     * form <code>is<i>XXX</i></code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param descriptor The descriptor for the attribute.  This may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * which is equivalent to an empty descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * null, or the descriptor entries are invalid as described in the
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 18220
diff changeset
   169
     * <a href="package-summary.html#constraints">package description</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public OpenMBeanAttributeInfoSupport(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                         String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                         OpenType<?> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                         boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                         boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                         boolean isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                         Descriptor descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // Construct parent's state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
              (openType==null) ? null : openType.getClassName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
              description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
              isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
              isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
              isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
              ImmutableDescriptor.union(descriptor, (openType==null)?null:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                openType.getDescriptor()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // Initialize this instance's specific state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.openType = openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        descriptor = getDescriptor();  // replace null by empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this.defaultValue = valueFrom(descriptor, "defaultValue", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this.legalValues = valuesFrom(descriptor, "legalValues", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this.minValue = comparableValueFrom(descriptor, "minValue", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.maxValue = comparableValueFrom(descriptor, "maxValue", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            check(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new IllegalArgumentException(e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * specified {@code name}, {@code openType}, {@code description}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * and {@code defaultValue}, and the specified read/write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param isIs {@code true} if the attribute's getter is of the
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   228
     * form <code>is<i>XXX</i></code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param defaultValue must be a valid value for the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * openType} specified for this attribute; default value not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * supported for {@code ArrayType} and {@code TabularType}; can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * be null, in which case it means that no default value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param <T> allows the compiler to check that the {@code defaultValue},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * if non-null, has the correct Java type for the given {@code openType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @throws OpenDataException if {@code defaultValue} is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * valid value for the specified {@code openType}, or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * defaultValue} is non null and {@code openType} is an {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * ArrayType} or a {@code TabularType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public <T> OpenMBeanAttributeInfoSupport(String   name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                             String   description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                             OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                             boolean  isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                             boolean  isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                             boolean  isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                             T        defaultValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
             defaultValue, (T[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * which describes the attribute of an open MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * specified {@code name}, {@code openType}, {@code description},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * {@code defaultValue} and {@code legalValues}, and the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * read/write access properties.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>The contents of {@code legalValues} are copied, so subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * modifications of the array referenced by {@code legalValues}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * have no impact on this {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * instance.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param isIs {@code true} if the attribute's getter is of the
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   285
     * form <code>is<i>XXX</i></code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param defaultValue must be a valid value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * for the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * openType} specified for this attribute; default value not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * supported for {@code ArrayType} and {@code TabularType}; can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * be null, in which case it means that no default value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param legalValues each contained value must be valid for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * {@code openType} specified for this attribute; legal values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * not supported for {@code ArrayType} and {@code TabularType};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * can be null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param <T> allows the compiler to check that the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * defaultValue} and {@code legalValues}, if non-null, have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * correct Java type for the given {@code openType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @throws OpenDataException if {@code defaultValue} is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * valid value for the specified {@code openType}, or one value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * {@code legalValues} is not valid for the specified {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * openType}, or {@code defaultValue} is non null and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * openType} is an {@code ArrayType} or a {@code TabularType}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * {@code legalValues} is non null and non empty and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * openType} is an {@code ArrayType} or a {@code TabularType}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * {@code legalValues} is non null and non empty and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * defaultValue} is not contained in {@code legalValues}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public <T> OpenMBeanAttributeInfoSupport(String   name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                             String   description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                             OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                             boolean  isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                             boolean  isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                             boolean  isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                             T        defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                             T[]      legalValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
             defaultValue, legalValues, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Constructs an {@code OpenMBeanAttributeInfoSupport} instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * which describes the attribute of an open MBean, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * specified {@code name}, {@code openType}, {@code description},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * {@code defaultValue}, {@code minValue} and {@code maxValue}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * It is possible to specify minimal and maximal values only for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * an open type whose values are {@code Comparable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param name  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param description  cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param openType  cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param isReadable {@code true} if the attribute has a getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param isWritable {@code true} if the attribute has a setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * exposed for management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param isIs {@code true} if the attribute's getter is of the
32034
05676cfd40b5 8133040: docs: replace <tt> tags (obsolete in html5) for java.management
avstepan
parents: 25859
diff changeset
   352
     * form <code>is<i>XXX</i></code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param defaultValue must be a valid value for the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * openType} specified for this attribute; default value not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * supported for {@code ArrayType} and {@code TabularType}; can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * null, in which case it means that no default value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param minValue must be valid for the {@code openType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * specified for this attribute; can be null, in which case it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * means that no minimal value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param maxValue must be valid for the {@code openType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * specified for this attribute; can be null, in which case it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * means that no maximal value is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param <T> allows the compiler to check that the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * defaultValue}, {@code minValue}, and {@code maxValue}, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * non-null, have the correct Java type for the given {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * openType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @throws IllegalArgumentException if {@code name} or {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * description} are null or empty string, or {@code openType} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @throws OpenDataException if {@code defaultValue}, {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * minValue} or {@code maxValue} is not a valid value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * specified {@code openType}, or {@code defaultValue} is non null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * and {@code openType} is an {@code ArrayType} or a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * TabularType}, or both {@code minValue} and {@code maxValue} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * non-null and {@code minValue.compareTo(maxValue) > 0} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * true}, or both {@code defaultValue} and {@code minValue} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * non-null and {@code minValue.compareTo(defaultValue) > 0} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * {@code true}, or both {@code defaultValue} and {@code maxValue}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * are non-null and {@code defaultValue.compareTo(maxValue) > 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public <T> OpenMBeanAttributeInfoSupport(String     name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                             String     description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                             OpenType<T>   openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                             boolean    isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                             boolean    isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                             boolean    isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                             T          defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                             Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                             Comparable<T> maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        this(name, description, openType, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
             defaultValue, null, minValue, maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private <T> OpenMBeanAttributeInfoSupport(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                                              String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                              OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                              boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                              boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                              boolean isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                                              T defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                              T[] legalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                              Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                              Comparable<T> maxValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        super(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
              (openType==null) ? null : openType.getClassName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
              description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
              isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
              isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
              isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
              makeDescriptor(openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                             defaultValue, legalValues, minValue, maxValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        this.openType = openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        Descriptor d = getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        this.defaultValue = defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        this.minValue = minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        this.maxValue = maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // We already converted the array into an unmodifiable Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // in the descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        this.legalValues = (Set<?>) d.getFieldValue("legalValues");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        check(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * An object serialized in a version of the API before Descriptors were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * added to this class will have an empty or null Descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * For consistency with our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * behavior in this version, we must replace the object with one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * where the Descriptors reflect the same values of openType, defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (getDescriptor().getFieldNames().length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            OpenType<Object> xopenType = cast(openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            Set<Object> xlegalValues = cast(legalValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            Comparable<Object> xminValue = cast(minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            Comparable<Object> xmaxValue = cast(maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return new OpenMBeanAttributeInfoSupport(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    name, description, openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    isReadable(), isWritable(), isIs(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    makeDescriptor(xopenType, defaultValue, xlegalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                   xminValue, xmaxValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    static void check(OpenMBeanParameterInfo info) throws OpenDataException {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   459
        OpenType<?> openType = info.getOpenType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (openType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            throw new IllegalArgumentException("OpenType cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (info.getName() == null ||
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   464
                info.getName().trim().isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            throw new IllegalArgumentException("Name cannot be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (info.getDescription() == null ||
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   468
                info.getDescription().trim().isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            throw new IllegalArgumentException("Description cannot be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // Check and initialize defaultValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (info.hasDefaultValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // Default value not supported for ArrayType and TabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            // Cast to Object because "OpenType<T> instanceof" is illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (openType.isArray() || (Object)openType instanceof TabularType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                throw new OpenDataException("Default value not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                                            "for ArrayType and TabularType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            // Check defaultValue's class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (!openType.isValue(info.getDefaultValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    "Argument defaultValue's class [\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    info.getDefaultValue().getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    "\"] does not match the one defined in openType[\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    openType.getClassName() +"\"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // Check that we don't have both legalValues and min or max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (info.hasLegalValues() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                (info.hasMinValue() || info.hasMaxValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new OpenDataException("cannot have both legalValue and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                                        "minValue or maxValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // Check minValue and maxValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (info.hasMinValue() && !openType.isValue(info.getMinValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                "Type of minValue [" + info.getMinValue().getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                "] does not match OpenType [" + openType.getClassName() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (info.hasMaxValue() && !openType.isValue(info.getMaxValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                "Type of maxValue [" + info.getMaxValue().getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                "] does not match OpenType [" + openType.getClassName() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        // Check that defaultValue is a legal value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (info.hasDefaultValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            Object defaultValue = info.getDefaultValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (info.hasLegalValues() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    !info.getLegalValues().contains(defaultValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                throw new OpenDataException("defaultValue is not contained " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                            "in legalValues");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            // Check that minValue <= defaultValue <= maxValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (info.hasMinValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if (compare(info.getMinValue(), defaultValue) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    throw new OpenDataException("minValue cannot be greater " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                                "than defaultValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (info.hasMaxValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if (compare(info.getMaxValue(), defaultValue) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    throw new OpenDataException("maxValue cannot be less " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                                                "than defaultValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        // Check legalValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (info.hasLegalValues()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            // legalValues not supported for TabularType and arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if ((Object)openType instanceof TabularType || openType.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                throw new OpenDataException("Legal values not supported " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                            "for TabularType and arrays");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            // Check legalValues are valid with openType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            for (Object v : info.getLegalValues()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (!openType.isValue(v)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        "Element of legalValues [" + v +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        "] is not a valid value for the specified openType [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        openType.toString() +"]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    throw new OpenDataException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        // Check that, if both specified, minValue <= maxValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (info.hasMinValue() && info.hasMaxValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            if (compare(info.getMinValue(), info.getMaxValue()) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                throw new OpenDataException("minValue cannot be greater " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                            "than maxValue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   571
    @SuppressWarnings({"unchecked", "rawtypes"})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    static int compare(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return ((Comparable) x).compareTo(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    static <T> Descriptor makeDescriptor(OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                         T defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                                         T[] legalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                                         Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                         Comparable<T> maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        Map<String, Object> map = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (defaultValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            map.put("defaultValue", defaultValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (legalValues != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            Set<T> set = new HashSet<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            for (T v : legalValues)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                set.add(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            set = Collections.unmodifiableSet(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            map.put("legalValues", set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (minValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            map.put("minValue", minValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (maxValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            map.put("maxValue", maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (map.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            return openType.getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            map.put("openType", openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            return new ImmutableDescriptor(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    static <T> Descriptor makeDescriptor(OpenType<T> openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                         T defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                         Set<T> legalValues,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                         Comparable<T> minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                                         Comparable<T> maxValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        T[] legals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if (legalValues == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            legals = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            legals = cast(new Object[legalValues.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            legalValues.toArray(legals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return makeDescriptor(openType, defaultValue, legals, minValue, maxValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    static <T> T valueFrom(Descriptor d, String name, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        Object x = d.getFieldValue(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return convertFrom(x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                "Cannot convert descriptor field " + name + "  to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                openType.getTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            throw EnvHelp.initCause(new IllegalArgumentException(msg), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    static <T> Set<T> valuesFrom(Descriptor d, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                 OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Object x = d.getFieldValue(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        Collection<?> coll;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (x instanceof Set<?>) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            Set<?> set = (Set<?>) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            boolean asis = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            for (Object element : set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                if (!openType.isValue(element)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    asis = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if (asis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                return cast(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            coll = set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        } else if (x instanceof Object[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            coll = Arrays.asList((Object[]) x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                "Descriptor value for " + name + " must be a Set or " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                "an array: " + x.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        Set<T> result = new HashSet<T>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        for (Object element : coll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            result.add(convertFrom(element, openType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   666
    static <T> Comparable<?> comparableValueFrom(Descriptor d, String name,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   667
                                                 OpenType<T> openType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        T t = valueFrom(d, name, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (t == null || t instanceof Comparable<?>)
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   670
            return (Comparable<?>) t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            "Descriptor field " + name + " with value " + t +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            " is not Comparable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    private static <T> T convertFrom(Object x, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (openType.isValue(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            T t = OpenMBeanAttributeInfoSupport.<T>cast(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return convertFromStrings(x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    private static <T> T convertFromStrings(Object x, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (openType instanceof ArrayType<?>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            return convertFromStringArray(x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        else if (x instanceof String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            return convertFromString((String) x, openType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            "Cannot convert value " + x + " of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            x.getClass().getName() + " to type " + openType.getTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    private static <T> T convertFromString(String s, OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        Class<T> c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        try {
33279
ddef2c0a5c2a 8078440: Safer managed types
sjiang
parents: 32034
diff changeset
   699
            String className = openType.safeGetClassName();
ddef2c0a5c2a 8078440: Safer managed types
sjiang
parents: 32034
diff changeset
   700
            ReflectUtil.checkPackageAccess(className);
ddef2c0a5c2a 8078440: Safer managed types
sjiang
parents: 32034
diff changeset
   701
            c = cast(Class.forName(className));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            throw new NoClassDefFoundError(e.toString());  // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // Look for: public static T valueOf(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        Method valueOf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        try {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   709
            // It is safe to call this plain Class.getMethod because the class "c"
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   710
            // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            valueOf = c.getMethod("valueOf", String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (!Modifier.isStatic(valueOf.getModifiers()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    valueOf.getReturnType() != c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                valueOf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            valueOf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (valueOf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            try {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   720
                return c.cast(MethodUtil.invoke(valueOf, null, new Object[] {s}));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    "Could not convert \"" + s + "\" using method: " + valueOf;
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   724
                throw new IllegalArgumentException(msg, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        // Look for: public T(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        Constructor<T> con;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        try {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   731
            // It is safe to call this plain Class.getConstructor because the class "c"
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   732
            // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            con = c.getConstructor(String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            con = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (con != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                return con.newInstance(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    "Could not convert \"" + s + "\" using constructor: " + con;
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   743
                throw new IllegalArgumentException(msg, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        throw new IllegalArgumentException("Don't know how to convert " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                                           "string to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                                           openType.getTypeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /* A Descriptor contained an array value encoded as Strings.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
       Strings must be organized in an array corresponding to the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
       array.  If the desired array has n dimensions, so must the String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
       array.  We will convert element by element from String to desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
       component type. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private static <T> T convertFromStringArray(Object x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                                OpenType<T> openType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        ArrayType<?> arrayType = (ArrayType<?>) openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        OpenType<?> baseType = arrayType.getElementOpenType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        int dim = arrayType.getDimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        String squareBrackets = "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        for (int i = 1; i < dim; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            squareBrackets += "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        Class<?> stringArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        Class<?> targetArrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        try {
33286
71c4bed3de03 8087350: Improve array conversions
jbachorik
parents: 33279
diff changeset
   769
            String baseClassName = baseType.safeGetClassName();
71c4bed3de03 8087350: Improve array conversions
jbachorik
parents: 33279
diff changeset
   770
71c4bed3de03 8087350: Improve array conversions
jbachorik
parents: 33279
diff changeset
   771
            // check access to the provided base type class name and bail out early
71c4bed3de03 8087350: Improve array conversions
jbachorik
parents: 33279
diff changeset
   772
            ReflectUtil.checkPackageAccess(baseClassName);
71c4bed3de03 8087350: Improve array conversions
jbachorik
parents: 33279
diff changeset
   773
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            stringArrayClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                Class.forName(squareBrackets + "Ljava.lang.String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            targetArrayClass =
33286
71c4bed3de03 8087350: Improve array conversions
jbachorik
parents: 33279
diff changeset
   777
                Class.forName(squareBrackets + "L" + baseClassName + ";");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            throw new NoClassDefFoundError(e.toString());  // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (!stringArrayClass.isInstance(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                "Value for " + dim + "-dimensional array of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                baseType.getTypeName() + " must be same type or a String " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                "array with same dimensions";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        OpenType<?> componentOpenType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (dim == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            componentOpenType = baseType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                componentOpenType = new ArrayType<T>(dim - 1, baseType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                throw new IllegalArgumentException(e.getMessage(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                // can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        int n = Array.getLength(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        Object[] targetArray = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            Array.newInstance(targetArrayClass.getComponentType(), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            Object stringish = Array.get(x, i);  // String or String[] etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            Object converted =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                convertFromStrings(stringish, componentOpenType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            Array.set(targetArray, i, converted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        return OpenMBeanAttributeInfoSupport.<T>cast(targetArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    static <T> T cast(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        return (T) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Returns the open type for the values of the attribute described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * by this {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    public OpenType<?> getOpenType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        return openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * Returns the default value for the attribute described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * {@code OpenMBeanAttributeInfoSupport} instance, if specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    public Object getDefaultValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        // Special case for ArrayType and TabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        // [JF] TODO: clone it so that it cannot be altered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // [JF] TODO: if we decide to support defaultValue as an array itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        // [JF] As of today (oct 2000) it is not supported so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        // defaultValue is null for arrays. Nothing to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Returns an unmodifiable Set of legal values for the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * described by this {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * instance, if specified, or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public Set<?> getLegalValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        // Special case for ArrayType and TabularType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        // [JF] TODO: clone values so that they cannot be altered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        // [JF] TODO: if we decide to support LegalValues as an array itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        // [JF] As of today (oct 2000) it is not supported so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        // legalValues is null for arrays. Nothing to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        // Returns our legalValues Set (set was constructed unmodifiable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return (legalValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Returns the minimal value for the attribute described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * {@code OpenMBeanAttributeInfoSupport} instance, if specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    public Comparable<?> getMinValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        // Note: only comparable values have a minValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        // so that's not the case of arrays and tabulars (always null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        return minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Returns the maximal value for the attribute described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * {@code OpenMBeanAttributeInfoSupport} instance, if specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * or {@code null} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    public Comparable<?> getMaxValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        // Note: only comparable values have a maxValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        // so that's not the case of arrays and tabulars (always null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        return maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * default value for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    public boolean hasDefaultValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        return (defaultValue != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * set of legal values for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    public boolean hasLegalValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        return (legalValues != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * minimal value for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    public boolean hasMinValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        return (minValue != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * Returns {@code true} if this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * OpenMBeanAttributeInfoSupport} instance specifies a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * maximal value for the described attribute, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public boolean hasMaxValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        return (maxValue != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Tests whether {@code obj} is a valid value for the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * described by this {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @param obj the object to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @return {@code true} if {@code obj} is a valid value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * the parameter described by this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * OpenMBeanAttributeInfoSupport} instance, {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    public boolean isValue(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        return isValue(this, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   944
    @SuppressWarnings({"unchecked", "rawtypes"})  // cast to Comparable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    static boolean isValue(OpenMBeanParameterInfo info, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        if (info.hasDefaultValue() && obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            info.getOpenType().isValue(obj) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            (!info.hasLegalValues() || info.getLegalValues().contains(obj)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            (!info.hasMinValue() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            ((Comparable) info.getMinValue()).compareTo(obj) <= 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            (!info.hasMaxValue() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            ((Comparable) info.getMaxValue()).compareTo(obj) >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    /* ***  Commodity methods from java.lang.Object  *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Compares the specified {@code obj} parameter with this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * OpenMBeanAttributeInfoSupport} instance for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * Returns {@code true} if and only if all of the following statements are true:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * <li>{@code obj} is non null,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * <li>{@code obj} also implements the {@code OpenMBeanAttributeInfo} interface,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * <li>their names are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * <li>their open types are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * <li>their access properties (isReadable, isWritable and isIs) are equal</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * <li>their default, min, max and legal values are equal.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * This ensures that this {@code equals} method works properly for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * {@code obj} parameters which are different implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * the {@code OpenMBeanAttributeInfo} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * <p>If {@code obj} also implements {@link DescriptorRead}, then its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * {@link DescriptorRead#getDescriptor() getDescriptor()} method must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * also return the same value as for this object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @param obj the object to be compared for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @return {@code true} if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        if (!(obj instanceof OpenMBeanAttributeInfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        OpenMBeanAttributeInfo other = (OpenMBeanAttributeInfo) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            this.isReadable() == other.isReadable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            this.isWritable() == other.isWritable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            this.isIs() == other.isIs() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            equal(this, other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    static boolean equal(OpenMBeanParameterInfo x1, OpenMBeanParameterInfo x2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        if (x1 instanceof DescriptorRead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            if (!(x2 instanceof DescriptorRead))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            Descriptor d1 = ((DescriptorRead) x1).getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            Descriptor d2 = ((DescriptorRead) x2).getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            if (!d1.equals(d2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        } else if (x2 instanceof DescriptorRead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            x1.getName().equals(x2.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            x1.getOpenType().equals(x2.getOpenType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            (x1.hasDefaultValue() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                x1.getDefaultValue().equals(x2.getDefaultValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                !x2.hasDefaultValue()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            (x1.hasMinValue() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                x1.getMinValue().equals(x2.getMinValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                !x2.hasMinValue()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            (x1.hasMaxValue() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                x1.getMaxValue().equals(x2.getMaxValue()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                !x2.hasMaxValue()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            (x1.hasLegalValues() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                x1.getLegalValues().equals(x2.getLegalValues()) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                !x2.hasLegalValues());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * <p>Returns the hash code value for this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * OpenMBeanAttributeInfoSupport} instance.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * <p>The hash code of an {@code OpenMBeanAttributeInfoSupport}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * instance is the sum of the hash codes of all elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * information used in {@code equals} comparisons (ie: its name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * its <i>open type</i>, its default, min, max and legal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * values, and its Descriptor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * <p>This ensures that {@code t1.equals(t2)} implies that {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * t1.hashCode()==t2.hashCode()} for any two {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * OpenMBeanAttributeInfoSupport} instances {@code t1} and {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * t2}, as required by the general contract of the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Object#hashCode() Object.hashCode()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * <p>However, note that another instance of a class implementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * the {@code OpenMBeanAttributeInfo} interface may be equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * this {@code OpenMBeanAttributeInfoSupport} instance as defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * by {@link #equals(java.lang.Object)}, but may have a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * hash code if it is calculated differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * <p>As {@code OpenMBeanAttributeInfoSupport} instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * immutable, the hash code for this instance is calculated once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * on the first call to {@code hashCode}, and then the same value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * is returned for subsequent calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @return the hash code value for this {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * OpenMBeanAttributeInfoSupport} instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        // Calculate the hash code value if it has not yet been done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        // (ie 1st call to hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        if (myHashCode == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            myHashCode = hashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        // return always the same hash code for this instance (immutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        return myHashCode.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    static int hashCode(OpenMBeanParameterInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        value += info.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        value += info.getOpenType().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        if (info.hasDefaultValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            value += info.getDefaultValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        if (info.hasMinValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            value += info.getMinValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if (info.hasMaxValue())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            value += info.getMaxValue().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (info.hasLegalValues())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            value += info.getLegalValues().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if (info instanceof DescriptorRead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            value += ((DescriptorRead) info).getDescriptor().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * Returns a string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * The string representation consists of the name of this class (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * {@code javax.management.openmbean.OpenMBeanAttributeInfoSupport}),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * the string representation of the name and open type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * described parameter, the string representation of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * default, min, max and legal values and the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * representation of its descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * <p>As {@code OpenMBeanAttributeInfoSupport} instances are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * immutable, the string representation for this instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * calculated once, on the first call to {@code toString}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * then the same value is returned for subsequent calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @return a string representation of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * {@code OpenMBeanAttributeInfoSupport} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        // Calculate the string value if it has not yet been done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        // (ie 1st call to toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        if (myToString == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            myToString = toString(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        // return always the same string representation for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        // instance (immutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        return myToString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    static String toString(OpenMBeanParameterInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        Descriptor d = (info instanceof DescriptorRead) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            ((DescriptorRead) info).getDescriptor() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            info.getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            "(name=" + info.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            ",openType=" + info.getOpenType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            ",default=" + info.getDefaultValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            ",minValue=" + info.getMinValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            ",maxValue=" + info.getMaxValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            ",legalValues=" + info.getLegalValues() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            ((d == null) ? "" : ",descriptor=" + d) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
}