jdk/src/share/classes/javax/management/openmbean/ArrayType.java
author rriggs
Tue, 12 Nov 2013 14:03:28 -0500
changeset 21656 d4c777ccb1db
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8028014: Doclint warning/error cleanup in javax.management Summary: Improve generated html by fixing doclint warnings Reviewed-by: sla, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
     2
 * Copyright (c) 2000, 2008, 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: 1510
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: 1510
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: 1510
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1510
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
package javax.management.openmbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The <code>ArrayType</code> class is the <i>open type</i> class whose instances describe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * all <i>open data</i> values which are n-dimensional arrays of <i>open data</i> values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Examples of valid {@code ArrayType} instances are:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
    36
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * // 2-dimension array of java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * ArrayType<String[][]> a1 = new ArrayType<String[][]>(2, SimpleType.STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * // 1-dimension array of int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * ArrayType<int[]> a2 = new ArrayType<int[]>(SimpleType.INTEGER, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * // 1-dimension array of java.lang.Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ArrayType<Integer[]> a3 = new ArrayType<Integer[]>(SimpleType.INTEGER, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * // 4-dimension array of int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * ArrayType<int[][][][]> a4 = new ArrayType<int[][][][]>(3, a2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * // 4-dimension array of java.lang.Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * ArrayType<Integer[][][][]> a5 = new ArrayType<Integer[][][][]>(3, a3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * // 1-dimension array of java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * ArrayType<String[]> a6 = new ArrayType<String[]>(SimpleType.STRING, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * // 1-dimension array of long
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * ArrayType<long[]> a7 = new ArrayType<long[]>(SimpleType.LONG, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * // 1-dimension array of java.lang.Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * ArrayType<Integer[]> a8 = ArrayType.getArrayType(SimpleType.INTEGER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * // 2-dimension array of java.lang.Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * ArrayType<Integer[][]> a9 = ArrayType.getArrayType(a8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * // 2-dimension array of int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * ArrayType<int[][]> a10 = ArrayType.getPrimitiveArrayType(int[][].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * // 3-dimension array of int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * ArrayType<int[][][]> a11 = ArrayType.getArrayType(a10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * // 1-dimension array of float
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * ArrayType<float[]> a12 = ArrayType.getPrimitiveArrayType(float[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * // 2-dimension array of float
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * ArrayType<float[][]> a13 = ArrayType.getArrayType(a12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * // 1-dimension array of javax.management.ObjectName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * ArrayType<ObjectName[]> a14 = ArrayType.getArrayType(SimpleType.OBJECTNAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * // 2-dimension array of javax.management.ObjectName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * ArrayType<ObjectName[][]> a15 = ArrayType.getArrayType(a14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * // 3-dimension array of java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * ArrayType<String[][][]> a16 = new ArrayType<String[][][]>(3, SimpleType.STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * // 1-dimension array of java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * ArrayType<String[]> a17 = new ArrayType<String[]>(1, SimpleType.STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * // 2-dimension array of java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * ArrayType<String[][]> a18 = new ArrayType<String[][]>(1, a17);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * // 3-dimension array of java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * ArrayType<String[][][]> a19 = new ArrayType<String[][][]>(1, a18);
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
    93
 * }</pre>
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
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  Generification note: we could have defined a type parameter that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  element type, with class ArrayType<E> extends OpenType<E[]>.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  that doesn't buy us all that much.  We can't say
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public OpenType<E> getElementOpenType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  because this ArrayType could be a multi-dimensional array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  For example, if we had
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    ArrayType(2, SimpleType.INTEGER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  then E would have to be Integer[], while getElementOpenType() would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  return SimpleType.INTEGER, which is an OpenType<Integer>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  Furthermore, we would like to support int[] (as well as Integer[]) as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  an Open Type (RFE 5045358).  We would want this to be an OpenType<int[]>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  which can't be expressed as <E[]> because E can't be a primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  like int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
public class ArrayType<T> extends OpenType<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static final long serialVersionUID = 720504429830309770L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @serial The dimension of arrays described by this {@link ArrayType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private int dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @serial The <i>open type</i> of element values contained in the arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *         described by this {@link ArrayType} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private OpenType<?> elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @serial This flag indicates whether this {@link ArrayType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         describes a primitive array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private boolean primitiveArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private transient Integer  myHashCode = null;       // As this instance is immutable, these two values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private transient String   myToString = null;       // need only be calculated once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // indexes refering to columns in the PRIMITIVE_ARRAY_TYPES table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static final int PRIMITIVE_WRAPPER_NAME_INDEX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static final int PRIMITIVE_TYPE_NAME_INDEX = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private static final int PRIMITIVE_TYPE_KEY_INDEX  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static final int PRIMITIVE_OPEN_TYPE_INDEX  = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static final Object[][] PRIMITIVE_ARRAY_TYPES = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        { Boolean.class.getName(),   boolean.class.getName(), "Z", SimpleType.BOOLEAN },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        { Character.class.getName(), char.class.getName(),    "C", SimpleType.CHARACTER },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        { Byte.class.getName(),      byte.class.getName(),    "B", SimpleType.BYTE },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        { Short.class.getName(),     short.class.getName(),   "S", SimpleType.SHORT },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        { Integer.class.getName(),   int.class.getName(),     "I", SimpleType.INTEGER },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        { Long.class.getName(),      long.class.getName(),    "J", SimpleType.LONG },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        { Float.class.getName(),     float.class.getName(),   "F", SimpleType.FLOAT },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        { Double.class.getName(),    double.class.getName(),  "D", SimpleType.DOUBLE }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static boolean isPrimitiveContentType(final String primitiveKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (typeDescr[PRIMITIVE_TYPE_KEY_INDEX].equals(primitiveKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Return the key used to identify the element type in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * arrays - e.g. "Z" for boolean, "C" for char etc...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param elementClassName the wrapper class name of the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *        element ("Boolean",  "Character", etc...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return the key corresponding to the given type ("Z", "C", etc...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *         return null if the given elementClassName is not a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *         wrapper class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    static String getPrimitiveTypeKey(String elementClassName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (elementClassName.equals(typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                return (String)typeDescr[PRIMITIVE_TYPE_KEY_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Return the primitive type name corresponding to the given wrapper class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * e.g. "boolean" for "Boolean", "char" for "Character" etc...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param elementClassName the type of the array element ("Boolean",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *        "Character", etc...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the primitive type name corresponding to the given wrapper class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *         ("boolean", "char", etc...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *         return null if the given elementClassName is not a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *         wrapper type name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static String getPrimitiveTypeName(String elementClassName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (elementClassName.equals(typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                return (String)typeDescr[PRIMITIVE_TYPE_NAME_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Return the primitive open type corresponding to the given primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * e.g. SimpleType.BOOLEAN for "boolean", SimpleType.CHARACTER for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * "char", etc...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param primitiveTypeName the primitive type of the array element ("boolean",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *        "char", etc...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return the OpenType corresponding to the given primitive type name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *         (SimpleType.BOOLEAN, SimpleType.CHARACTER, etc...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         return null if the given elementClassName is not a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *         type name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    static SimpleType<?> getPrimitiveOpenType(String primitiveTypeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (primitiveTypeName.equals(typeDescr[PRIMITIVE_TYPE_NAME_INDEX]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return (SimpleType<?>)typeDescr[PRIMITIVE_OPEN_TYPE_INDEX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /* *** Constructor *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Constructs an <tt>ArrayType</tt> instance describing <i>open data</i> values which are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * arrays with dimension <var>dimension</var> of elements whose <i>open type</i> is <var>elementType</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * When invoked on an <tt>ArrayType</tt> instance, the {@link OpenType#getClassName() getClassName} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * returns the class name of the array instances it describes (following the rules defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * {@link Class#getName() getName} method of <code>java.lang.Class</code>), not the class name of the array elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * (which is returned by a call to <tt>getElementOpenType().getClassName()</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * The internal field corresponding to the type name of this <code>ArrayType</code> instance is also set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * the class name of the array instances it describes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * In other words, the methods <code>getClassName</code> and <code>getTypeName</code> return the same string value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * The internal field corresponding to the description of this <code>ArrayType</code> instance is set to a string value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * which follows the following template:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <li>if non-primitive array: <tt><i>&lt;dimension&gt;</i>-dimension array of <i>&lt;element_class_name&gt;</i></tt></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <li>if primitive array: <tt><i>&lt;dimension&gt;</i>-dimension array of <i>&lt;primitive_type_of_the_element_class_name&gt;</i></tt></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * As an example, the following piece of code:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   244
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * ArrayType<String[][][]> t = new ArrayType<String[][][]>(3, SimpleType.STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * System.out.println("array class name       = " + t.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * System.out.println("element class name     = " + t.getElementOpenType().getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * System.out.println("array type name        = " + t.getTypeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * System.out.println("array type description = " + t.getDescription());
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   250
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * would produce the following output:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   252
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * array class name       = [[[Ljava.lang.String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * element class name     = java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * array type name        = [[[Ljava.lang.String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * array type description = 3-dimension array of java.lang.String
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   257
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * And the following piece of code which is equivalent to the one listed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * above would also produce the same output:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   260
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * ArrayType<String[]> t1 = new ArrayType<String[]>(1, SimpleType.STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * ArrayType<String[][]> t2 = new ArrayType<String[][]>(1, t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * ArrayType<String[][][]> t3 = new ArrayType<String[][][]>(1, t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * System.out.println("array class name       = " + t3.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * System.out.println("element class name     = " + t3.getElementOpenType().getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * System.out.println("array type name        = " + t3.getTypeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * System.out.println("array type description = " + t3.getDescription());
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   268
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param  dimension  the dimension of arrays described by this <tt>ArrayType</tt> instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *                    must be greater than or equal to 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param  elementType  the <i>open type</i> of element values contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *                      in the arrays described by this <tt>ArrayType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *                      instance; must be an instance of either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *                      <tt>SimpleType</tt>, <tt>CompositeType</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *                      <tt>TabularType</tt> or another <tt>ArrayType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *                      with a <tt>SimpleType</tt>, <tt>CompositeType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *                      or <tt>TabularType</tt> as its <tt>elementType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @throws IllegalArgumentException if {@code dimension} is not a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *                                  integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @throws OpenDataException  if <var>elementType's className</var> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *                            one of the allowed Java class names for open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *                            data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public ArrayType(int dimension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                     OpenType<?> elementType) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // Check and construct state defined by parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // We can't use the package-private OpenType constructor because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // we don't know if the elementType parameter is sane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        super(buildArrayClassName(dimension, elementType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
              buildArrayClassName(dimension, elementType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
              buildArrayDescription(dimension, elementType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // Check and construct state specific to ArrayType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (elementType.isArray()) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   299
            ArrayType<?> at = (ArrayType<?>) elementType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            this.dimension = at.getDimension() + dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            this.elementType = at.getElementOpenType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            this.primitiveArray = at.isPrimitiveArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            this.dimension = dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            this.elementType = elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            this.primitiveArray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Constructs a unidimensional {@code ArrayType} instance for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * supplied {@code SimpleType}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * This constructor supports the creation of arrays of primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * types when {@code primitiveArray} is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * For primitive arrays the {@link #getElementOpenType()} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * returns the {@link SimpleType} corresponding to the wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * type of the primitive type of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * When invoked on an <tt>ArrayType</tt> instance, the {@link OpenType#getClassName() getClassName} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * returns the class name of the array instances it describes (following the rules defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * {@link Class#getName() getName} method of <code>java.lang.Class</code>), not the class name of the array elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * (which is returned by a call to <tt>getElementOpenType().getClassName()</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * The internal field corresponding to the type name of this <code>ArrayType</code> instance is also set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * the class name of the array instances it describes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * In other words, the methods <code>getClassName</code> and <code>getTypeName</code> return the same string value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * The internal field corresponding to the description of this <code>ArrayType</code> instance is set to a string value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * which follows the following template:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * <li>if non-primitive array: <tt>1-dimension array of <i>&lt;element_class_name&gt;</i></tt></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <li>if primitive array: <tt>1-dimension array of <i>&lt;primitive_type_of_the_element_class_name&gt;</i></tt></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * As an example, the following piece of code:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   337
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * ArrayType<int[]> t = new ArrayType<int[]>(SimpleType.INTEGER, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * System.out.println("array class name       = " + t.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * System.out.println("element class name     = " + t.getElementOpenType().getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * System.out.println("array type name        = " + t.getTypeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * System.out.println("array type description = " + t.getDescription());
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   343
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * would produce the following output:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   345
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * array class name       = [I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * element class name     = java.lang.Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * array type name        = [I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * array type description = 1-dimension array of int
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   350
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @param elementType the {@code SimpleType} of the element values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *                    contained in the arrays described by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *                    {@code ArrayType} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param primitiveArray {@code true} when this array describes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *                       primitive arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws IllegalArgumentException if {@code dimension} is not a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @throws OpenDataException if {@code primitiveArray} is {@code true} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * {@code elementType} is not a valid {@code SimpleType} for a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public ArrayType(SimpleType<?> elementType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                     boolean primitiveArray) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // Check and construct state defined by parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // We can call the package-private OpenType constructor because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // set of SimpleTypes is fixed and SimpleType can't be subclassed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        super(buildArrayClassName(1, elementType, primitiveArray),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
              buildArrayClassName(1, elementType, primitiveArray),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
              buildArrayDescription(1, elementType, primitiveArray),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
              true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Check and construct state specific to ArrayType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        this.dimension = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        this.elementType = elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        this.primitiveArray = primitiveArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /* Package-private constructor for callers we trust to get it right. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    ArrayType(String className, String typeName, String description,
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   387
              int dimension, OpenType<?> elementType,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
              boolean primitiveArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        super(className, typeName, description, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        this.dimension = dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        this.elementType = elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        this.primitiveArray = primitiveArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private static String buildArrayClassName(int dimension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                              OpenType<?> elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        boolean isPrimitiveArray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (elementType.isArray()) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   400
            isPrimitiveArray = ((ArrayType<?>) elementType).isPrimitiveArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return buildArrayClassName(dimension, elementType, isPrimitiveArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private static String buildArrayClassName(int dimension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                              OpenType<?> elementType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                              boolean isPrimitiveArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (dimension < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                "Value of argument dimension must be greater than 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        String elementClassName = elementType.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // Add N (= dimension) additional '[' characters to the existing array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        for (int i = 1; i <= dimension; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            result.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (elementType.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            result.append(elementClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (isPrimitiveArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                final String key = getPrimitiveTypeKey(elementClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                // Ideally we should throw an IllegalArgumentException here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                // but for compatibility reasons we throw an OpenDataException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // (used to be thrown by OpenType() constructor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    throw new OpenDataException("Element type is not primitive: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            + elementClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                result.append(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                result.append("L");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                result.append(elementClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                result.append(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private static String buildArrayDescription(int dimension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                                OpenType<?> elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        boolean isPrimitiveArray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (elementType.isArray()) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   446
            isPrimitiveArray = ((ArrayType<?>) elementType).isPrimitiveArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return buildArrayDescription(dimension, elementType, isPrimitiveArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private static String buildArrayDescription(int dimension,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                                OpenType<?> elementType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                                boolean isPrimitiveArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (elementType.isArray()) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   456
            ArrayType<?> at = (ArrayType<?>) elementType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            dimension += at.getDimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            elementType = at.getElementOpenType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            isPrimitiveArray = at.isPrimitiveArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        StringBuilder result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            new StringBuilder(dimension + "-dimension array of ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        final String elementClassName = elementType.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (isPrimitiveArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            // Convert from wrapper type to primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            final String primitiveType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    getPrimitiveTypeName(elementClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            // Ideally we should throw an IllegalArgumentException here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // but for compatibility reasons we throw an OpenDataException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // (used to be thrown by OpenType() constructor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (primitiveType == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                throw new OpenDataException("Element is not a primitive type: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        elementClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            result.append(primitiveType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            result.append(elementClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /* *** ArrayType specific information methods *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Returns the dimension of arrays described by this <tt>ArrayType</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return the dimension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public int getDimension() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        return dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Returns the <i>open type</i> of element values contained in the arrays described by this <tt>ArrayType</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return the element type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public OpenType<?> getElementOpenType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Returns <code>true</code> if the open data values this open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * type describes are primitive arrays, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @return true if this is a primitive array type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public boolean isPrimitiveArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return primitiveArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Tests whether <var>obj</var> is a value for this <code>ArrayType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * This method returns <code>true</code> if and only if <var>obj</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * is not null, <var>obj</var> is an array and any one of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * is <tt>true</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <li>if this <code>ArrayType</code> instance describes an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <tt>SimpleType</tt> elements or their corresponding primitive types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <var>obj</var>'s class name is the same as the className field defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * for this <code>ArrayType</code> instance (i.e. the class name returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * by the {@link OpenType#getClassName() getClassName} method, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * includes the dimension information),<br>&nbsp;</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * <li>if this <code>ArrayType</code> instance describes an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * classes implementing the {@code TabularData} interface or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * {@code CompositeData} interface, <var>obj</var> is assignable to
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   536
     * such a declared array, and each element contained in {<var>obj</var>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * is either null or a valid value for the element's open type specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * by this <code>ArrayType</code> instance.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param obj the object to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return <code>true</code> if <var>obj</var> is a value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * <code>ArrayType</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public boolean isValue(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        // if obj is null, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   554
        Class<?> objClass = obj.getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        String objClassName = objClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        // if obj is not an array, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if ( ! objClass.isArray() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // Test if obj's class name is the same as for the array values that this instance describes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        // (this is fine if elements are of simple types, which are final classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if ( this.getClassName().equals(objClassName) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        // In case this ArrayType instance describes an array of classes implementing the TabularData or CompositeData interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // we first check for the assignability of obj to such an array of TabularData or CompositeData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        // which ensures that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        //  . obj is of the the same dimension as this ArrayType instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        //  . it is declared as an array of elements which are either all TabularData or all CompositeData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        // If the assignment check is positive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        // then we have to check that each element in obj is of the same TabularType or CompositeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // as the one described by this ArrayType instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // [About assignment check, note that the call below returns true: ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        // [Class.forName("[Lpackage.CompositeData;").isAssignableFrom(Class.forName("[Lpackage.CompositeDataImpl;)")); ]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        if ( (this.elementType.getClassName().equals(TabularData.class.getName()))  ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
             (this.elementType.getClassName().equals(CompositeData.class.getName()))   ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            boolean isTabular =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                (elementType.getClassName().equals(TabularData.class.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            int[] dims = new int[getDimension()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            Class<?> elementClass = isTabular ? TabularData.class : CompositeData.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            Class<?> targetClass = Array.newInstance(elementClass, dims).getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // assignment check: return false if negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if  ( ! targetClass.isAssignableFrom(objClass) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            // check that all elements in obj are valid values for this ArrayType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if ( ! checkElementsType( (Object[]) obj, this.dimension) ) { // we know obj's dimension is this.dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        // if previous tests did not return, then obj is not a value for this ArrayType instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Returns true if and only if all elements contained in the array argument x_dim_Array of dimension dim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * are valid values (ie either null or of the right openType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * for the element open type specified by this ArrayType instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * This method's implementation uses recursion to go down the dimensions of the array argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private boolean checkElementsType(Object[] x_dim_Array, int dim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // if the elements of x_dim_Array are themselves array: go down recursively....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if ( dim > 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            for (int i=0; i<x_dim_Array.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                if ( ! checkElementsType((Object[])x_dim_Array[i], dim-1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // ...else, for a non-empty array, each element must be a valid value: either null or of the right openType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            for (int i=0; i<x_dim_Array.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                if ( (x_dim_Array[i] != null) && (! this.getElementOpenType().isValue(x_dim_Array[i])) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    @Override
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   639
    boolean isAssignableFrom(OpenType<?> ot) {
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   640
        if (!(ot instanceof ArrayType<?>))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        ArrayType<?> at = (ArrayType<?>) ot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return (at.getDimension() == getDimension() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                at.isPrimitiveArray() == isPrimitiveArray() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                at.getElementOpenType().isAssignableFrom(getElementOpenType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    /* *** Methods overriden from class Object *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Compares the specified <code>obj</code> parameter with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * <code>ArrayType</code> instance for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Two <code>ArrayType</code> instances are equal if and only if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * describe array instances which have the same dimension, elements'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * open type and primitive array flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @param obj the object to be compared for equality with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *            <code>ArrayType</code> instance; if <var>obj</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *            is <code>null</code> or is not an instance of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *            class <code>ArrayType</code> this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *            <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @return <code>true</code> if the specified object is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *         this <code>ArrayType</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // if obj is null, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        // if obj is not an ArrayType, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        //
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   678
        if (!(obj instanceof ArrayType<?>))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            return false;
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   680
        ArrayType<?> other = (ArrayType<?>) obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        // if other's dimension is different than this instance's, return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        if (this.dimension != other.dimension) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        // Test if other's elementType field is the same as for this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (!this.elementType.equals(other.elementType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        // Test if other's primitiveArray flag is the same as for this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return this.primitiveArray == other.primitiveArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * Returns the hash code value for this <code>ArrayType</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * The hash code of an <code>ArrayType</code> instance is the sum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * hash codes of all the elements of information used in <code>equals</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * comparisons (i.e. dimension, elements' open type and primitive array flag).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * The hashcode for a primitive value is the hashcode of the corresponding boxed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * object (e.g. the hashcode for <tt>true</tt> is <tt>Boolean.TRUE.hashCode()</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * This ensures that <code> t1.equals(t2) </code> implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * <code> t1.hashCode()==t2.hashCode() </code> for any two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <code>ArrayType</code> instances <code>t1</code> and <code>t2</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * as required by the general contract of the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * {@link Object#hashCode() Object.hashCode()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * As <code>ArrayType</code> instances are immutable, the hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * code for this instance is calculated once, on the first call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * to <code>hashCode</code>, and then the same value is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * for subsequent calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @return  the hash code value for this <code>ArrayType</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (myHashCode == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            value += dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            value += elementType.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            value += Boolean.valueOf(primitiveArray).hashCode();
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   729
            myHashCode = Integer.valueOf(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        // return always the same hash code for this instance (immutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        return myHashCode.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Returns a string representation of this <code>ArrayType</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * The string representation consists of the name of this class (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * <code>javax.management.openmbean.ArrayType</code>), the type name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * the dimension, the elements' open type and the primitive array flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * defined for this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * As <code>ArrayType</code> instances are immutable, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * string representation for this instance is calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * once, on the first call to <code>toString</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * then the same value is returned for subsequent calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @return a string representation of this <code>ArrayType</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // Calculate the string representation if it has not yet been done (ie 1st call to toString())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        if (myToString == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            myToString = getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                         "(name=" + getTypeName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                         ",dimension=" + dimension +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                         ",elementType=" + elementType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                         ",primitiveArray=" + primitiveArray + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // return always the same string representation for this instance (immutable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return myToString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Create an {@code ArrayType} instance in a type-safe manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * Multidimensional arrays can be built up by calling this method as many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * times as necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Calling this method twice with the same parameters may return the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * object or two equal but not identical objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * As an example, the following piece of code:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   779
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * ArrayType<String[]> t1 = ArrayType.getArrayType(SimpleType.STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * ArrayType<String[][]> t2 = ArrayType.getArrayType(t1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * ArrayType<String[][][]> t3 = ArrayType.getArrayType(t2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * System.out.println("array class name       = " + t3.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * System.out.println("element class name     = " + t3.getElementOpenType().getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * System.out.println("array type name        = " + t3.getTypeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * System.out.println("array type description = " + t3.getDescription());
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   787
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * would produce the following output:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   789
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * array class name       = [[[Ljava.lang.String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * element class name     = java.lang.String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * array type name        = [[[Ljava.lang.String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * array type description = 3-dimension array of java.lang.String
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   794
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @param  elementType  the <i>open type</i> of element values contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *                      in the arrays described by this <tt>ArrayType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *                      instance; must be an instance of either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *                      <tt>SimpleType</tt>, <tt>CompositeType</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *                      <tt>TabularType</tt> or another <tt>ArrayType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *                      with a <tt>SimpleType</tt>, <tt>CompositeType</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *                      or <tt>TabularType</tt> as its <tt>elementType</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @throws OpenDataException if <var>elementType's className</var> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *                           one of the allowed Java class names for open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *                           data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public static <E> ArrayType<E[]> getArrayType(OpenType<E> elementType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        return new ArrayType<E[]>(1, elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Create an {@code ArrayType} instance in a type-safe manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Calling this method twice with the same parameters may return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * same object or two equal but not identical objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * As an example, the following piece of code:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   822
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * ArrayType<int[][][]> t = ArrayType.getPrimitiveArrayType(int[][][].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * System.out.println("array class name       = " + t.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * System.out.println("element class name     = " + t.getElementOpenType().getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * System.out.println("array type name        = " + t.getTypeName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * System.out.println("array type description = " + t.getDescription());
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   828
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * would produce the following output:
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   830
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * array class name       = [[[I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * element class name     = java.lang.Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * array type name        = [[[I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * array type description = 3-dimension array of int
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 5506
diff changeset
   835
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param arrayClass a primitive array class such as {@code int[].class},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *                   {@code boolean[][].class}, etc. The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *                   #getElementOpenType()} method of the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *                   {@code ArrayType} returns the {@link SimpleType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *                   corresponding to the wrapper type of the primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *                   type of the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @throws IllegalArgumentException if <var>arrayClass</var> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *                                  a primitive array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    @SuppressWarnings("unchecked")  // can't get appropriate T for primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    public static <T> ArrayType<T> getPrimitiveArrayType(Class<T> arrayClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        // Check if the supplied parameter is an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        if (!arrayClass.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            throw new IllegalArgumentException("arrayClass must be an array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        // Calculate array dimension and component type name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        int n = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        Class<?> componentType = arrayClass.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        while (componentType.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            componentType = componentType.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        String componentTypeName = componentType.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        // Check if the array's component type is a primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if (!componentType.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                "component type of the array must be a primitive type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        // Map component type name to corresponding SimpleType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        final SimpleType<?> simpleType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                getPrimitiveOpenType(componentTypeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        // Build primitive array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        try {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   882
            @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            ArrayType at = new ArrayType(simpleType, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            if (n > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                at = new ArrayType<T>(n - 1, at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            throw new IllegalArgumentException(e); // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * Replace/resolve the object read from the stream before it is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * to the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @serialData The new serial form of this class defines a new serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * {@code boolean} field {@code primitiveArray}. In order to guarantee the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * interoperability with previous versions of this class the new serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * form must continue to refer to primitive wrapper types even when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * {@code ArrayType} instance describes a primitive type array. So when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * {@code primitiveArray} is {@code true} the {@code className},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * {@code typeName} and {@code description} serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * are converted into primitive types before the deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * {@code ArrayType} instance is return to the caller. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * {@code elementType} field always returns the {@code SimpleType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * corresponding to the primitive wrapper type of the array's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Therefore the following serializable fields are deserialized as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *   <li>if {@code primitiveArray} is {@code true} the {@code className}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *       field is deserialized by replacing the array's component primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *       wrapper type by the corresponding array's component primitive type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *       e.g. {@code "[[Ljava.lang.Integer;"} will be deserialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *       {@code "[[I"}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *   <li>if {@code primitiveArray} is {@code true} the {@code typeName}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *       field is deserialized by replacing the array's component primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *       wrapper type by the corresponding array's component primitive type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *       e.g. {@code "[[Ljava.lang.Integer;"} will be deserialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *       {@code "[[I"}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *   <li>if {@code primitiveArray} is {@code true} the {@code description}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *       field is deserialized by replacing the array's component primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *       wrapper type by the corresponding array's component primitive type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *       e.g. {@code "2-dimension array of java.lang.Integer"} will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     *       deserialized as {@code "2-dimension array of int"}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    private Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        if (primitiveArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            return convertFromWrapperToPrimitiveTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   938
    private <T> ArrayType<T> convertFromWrapperToPrimitiveTypes() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        String cn = getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        String tn = getTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        String d = getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (cn.indexOf((String)typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX]) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                cn = cn.replaceFirst(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                    "L" + typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX] + ";",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    (String) typeDescr[PRIMITIVE_TYPE_KEY_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                tn = tn.replaceFirst(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    "L" + typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX] + ";",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    (String) typeDescr[PRIMITIVE_TYPE_KEY_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                d = d.replaceFirst(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    (String) typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    (String) typeDescr[PRIMITIVE_TYPE_NAME_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   956
        return new ArrayType<T>(cn, tn, d,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   957
                                dimension, elementType, primitiveArray);
2
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
     * Nominate a replacement for this object in the stream before the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @serialData The new serial form of this class defines a new serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * {@code boolean} field {@code primitiveArray}. In order to guarantee the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * interoperability with previous versions of this class the new serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * form must continue to refer to primitive wrapper types even when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * {@code ArrayType} instance describes a primitive type array. So when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * {@code primitiveArray} is {@code true} the {@code className},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * {@code typeName} and {@code description} serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * are converted into wrapper types before the serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * {@code ArrayType} instance is written to the stream. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * {@code elementType} field always returns the {@code SimpleType}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * corresponding to the primitive wrapper type of the array's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Therefore the following serializable fields are serialized as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *   <li>if {@code primitiveArray} is {@code true} the {@code className}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *       field is serialized by replacing the array's component primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *       type by the corresponding array's component primitive wrapper type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *       e.g. {@code "[[I"} will be serialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *       {@code "[[Ljava.lang.Integer;"}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *   <li>if {@code primitiveArray} is {@code true} the {@code typeName}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *       field is serialized by replacing the array's component primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *       type by the corresponding array's component primitive wrapper type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *       e.g. {@code "[[I"} will be serialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *       {@code "[[Ljava.lang.Integer;"}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *   <li>if {@code primitiveArray} is {@code true} the {@code description}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     *       field is serialized by replacing the array's component primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *       type by the corresponding array's component primitive wrapper type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *       e.g. {@code "2-dimension array of int"} will be serialized as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *       {@code "2-dimension array of java.lang.Integer"}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    private Object writeReplace() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        if (primitiveArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            return convertFromPrimitiveToWrapperTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
  1006
    private <T> ArrayType<T> convertFromPrimitiveToWrapperTypes() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        String cn = getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        String tn = getTypeName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        String d = getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        for (Object[] typeDescr : PRIMITIVE_ARRAY_TYPES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            if (cn.indexOf((String) typeDescr[PRIMITIVE_TYPE_KEY_INDEX]) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                cn = cn.replaceFirst(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    (String) typeDescr[PRIMITIVE_TYPE_KEY_INDEX],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    "L" + typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX] + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                tn = tn.replaceFirst(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                    (String) typeDescr[PRIMITIVE_TYPE_KEY_INDEX],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    "L" + typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX] + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                d = d.replaceFirst(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    (String) typeDescr[PRIMITIVE_TYPE_NAME_INDEX],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    (String) typeDescr[PRIMITIVE_WRAPPER_NAME_INDEX]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
  1024
        return new ArrayType<T>(cn, tn, d,
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
  1025
                                dimension, elementType, primitiveArray);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
}