src/java.management/share/classes/javax/management/openmbean/OpenType.java
author rriggs
Fri, 07 Dec 2018 11:51:17 -0500
changeset 52902 e3398b2e1ab0
parent 47216 71c04702a3d5
permissions -rw-r--r--
8214971: Replace use of string.equals("") with isEmpty() Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad
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: 4156
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: 4156
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: 4156
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: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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 com.sun.jmx.mbeanserver.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The <code>OpenType</code> class is the parent abstract class of all classes which describe the actual <i>open type</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of open data values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * An <i>open type</i> is defined by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *  <li>the fully qualified Java class name of the open data values this type describes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      note that only a limited set of Java classes is allowed for open data values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      (see {@link #ALLOWED_CLASSNAMES_LIST ALLOWED_CLASSNAMES_LIST}),</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *  <li>its name,</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *  <li>its description.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @param <T> the Java type that instances described by this type must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * have.  For example, {@link SimpleType#INTEGER} is a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * SimpleType<Integer>} which is a subclass of {@code OpenType<Integer>},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * meaning that an attribute, parameter, or return value that is described
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * as a {@code SimpleType.INTEGER} must have Java type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * {@link Integer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public abstract class OpenType<T> implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final long serialVersionUID = -9195195325186646468L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * List of the fully qualified names of the Java classes allowed for open
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * data values. A multidimensional array of any one of these classes or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * their corresponding primitive types is also an allowed class for open
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * data values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
       <pre>ALLOWED_CLASSNAMES_LIST = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "java.lang.Void",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        "java.lang.Boolean",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        "java.lang.Character",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "java.lang.Byte",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        "java.lang.Short",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        "java.lang.Integer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        "java.lang.Long",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        "java.lang.Float",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        "java.lang.Double",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        "java.lang.String",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        "java.math.BigDecimal",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        "java.math.BigInteger",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        "java.util.Date",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        "javax.management.ObjectName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        CompositeData.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        TabularData.class.getName() } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
       </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static final List<String> ALLOWED_CLASSNAMES_LIST =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      Collections.unmodifiableList(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Arrays.asList(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
          "java.lang.Void",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
          "java.lang.Boolean",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
          "java.lang.Character",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
          "java.lang.Byte",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
          "java.lang.Short",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
          "java.lang.Integer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
          "java.lang.Long",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
          "java.lang.Float",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
          "java.lang.Double",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
          "java.lang.String",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
          "java.math.BigDecimal",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
          "java.math.BigInteger",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
          "java.util.Date",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
          "javax.management.ObjectName",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
          CompositeData.class.getName(),        // better refer to these two class names like this, rather than hardcoding a string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
          TabularData.class.getName()) );       // in case the package of these classes should change (who knows...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @deprecated Use {@link #ALLOWED_CLASSNAMES_LIST ALLOWED_CLASSNAMES_LIST} instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public static final String[] ALLOWED_CLASSNAMES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ALLOWED_CLASSNAMES_LIST.toArray(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @serial The fully qualified Java class name of open data values this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *         type describes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @serial The type description (should not be null or empty).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private String description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @serial The name given to this type (should not be null or empty).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Tells if this type describes an array (checked in constructor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private transient boolean isArray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Cached Descriptor for this OpenType, constructed on demand.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private transient Descriptor descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /* *** Constructor *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Constructs an <code>OpenType</code> instance (actually a subclass instance as <code>OpenType</code> is abstract),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * checking for the validity of the given parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The validity constraints are described below for each parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <br>&nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param  className  The fully qualified Java class name of the open data values this open type describes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *                    The valid Java class names allowed for open data values are listed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *                    {@link #ALLOWED_CLASSNAMES_LIST ALLOWED_CLASSNAMES_LIST}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *                    A multidimensional array of any one of these classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                    or their corresponding primitive types is also an allowed class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                    in which case the class name follows the rules defined by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *                    {@link Class#getName() getName()} of <code>java.lang.Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *                    For example, a 3-dimensional array of Strings has for class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *                    &quot;<code>[[[Ljava.lang.String;</code>&quot; (without the quotes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <br>&nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param  typeName  The name given to the open type this instance represents; cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <br>&nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param  description  The human readable description of the open type this instance represents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *                      cannot be a null or empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <br>&nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @throws IllegalArgumentException  if <var>className</var>, <var>typeName</var> or <var>description</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *                                   is a null or empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <br>&nbsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws OpenDataException  if <var>className</var> is not one of the allowed Java class names for open data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected OpenType(String  className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                       String  typeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                       String  description) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        checkClassNameOverride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        this.typeName = valid("typeName", typeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        this.description = valid("description", description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this.className = validClassName(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this.isArray = (this.className != null && this.className.startsWith("["));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /* Package-private constructor for callers we trust to get it right. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    OpenType(String className, String typeName, String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
             boolean isArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this.className   = valid("className",className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        this.typeName    = valid("typeName", typeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        this.description = valid("description", description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.isArray     = isArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private void checkClassNameOverride() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (this.getClass().getClassLoader() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return;  // We trust bootstrap classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (overridesGetClassName(this.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            final GetPropertyAction getExtendOpenTypes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                new GetPropertyAction("jmx.extend.open.types");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (AccessController.doPrivileged(getExtendOpenTypes) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throw new SecurityException("Cannot override getClassName() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        "unless -Djmx.extend.open.types");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   209
    private static boolean overridesGetClassName(final Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            public Boolean run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    return (c.getMethod("getClassName").getDeclaringClass() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            OpenType.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    return true;  // fail safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 3446
diff changeset
   222
    private static String validClassName(String className) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        className   = valid("className", className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Check if className describes an array class, and determines its elements' class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // (eg: a 3-dimensional array of Strings has for class name: "[[[Ljava.lang.String;")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        while (className.startsWith("[", n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        String eltClassName; // class name of array elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        boolean isPrimitiveArray = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (className.startsWith("L", n) && className.endsWith(";")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                // removes the n leading '[' + the 'L' characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                // and the last ';' character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                eltClassName = className.substring(n+1, className.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } else if (n == className.length() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                // removes the n leading '[' characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                eltClassName = className.substring(n, className.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                isPrimitiveArray = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                throw new OpenDataException("Argument className=\"" + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        "\" is not a valid class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            eltClassName = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // Check that eltClassName's value is one of the allowed basic data types for open data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (isPrimitiveArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            ok = ArrayType.isPrimitiveContentType(eltClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            ok = ALLOWED_CLASSNAMES_LIST.contains(eltClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if ( ! ok ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new OpenDataException("Argument className=\""+ className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                        "\" is not one of the allowed Java class names for open data.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /* Return argValue.trim() provided argValue is neither null nor empty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
       otherwise throw IllegalArgumentException.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static String valid(String argName, String argValue) {
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   271
        if (argValue == null || (argValue = argValue.trim()).isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            throw new IllegalArgumentException("Argument " + argName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                               " cannot be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return argValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /* Package-private access to a Descriptor containing this OpenType. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    synchronized Descriptor getDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (descriptor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            descriptor = new ImmutableDescriptor(new String[] {"openType"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                 new Object[] {this});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /* *** Open type information methods *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns the fully qualified Java class name of the open data values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * this open type describes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * The only possible Java class names for open data values are listed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * {@link #ALLOWED_CLASSNAMES_LIST ALLOWED_CLASSNAMES_LIST}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * A multidimensional array of any one of these classes or their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * corresponding primitive types is also an allowed class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * in which case the class name follows the rules defined by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * {@link Class#getName() getName()} of <code>java.lang.Class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * For example, a 3-dimensional array of Strings has for class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * &quot;<code>[[[Ljava.lang.String;</code>&quot; (without the quotes),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * a 3-dimensional array of Integers has for class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * &quot;<code>[[[Ljava.lang.Integer;</code>&quot; (without the quotes),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * and a 3-dimensional array of int has for class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * &quot;<code>[[[I</code>&quot; (without the quotes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return the class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public String getClassName() {
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   307
        return className;
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   308
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
3446
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   310
    // A version of getClassName() that can only be called from within this
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   311
    // package and that cannot be overridden.
4a07252d2e92 6736293: OpenType checks can be bypassed through finalizer resurrection
emcmanus
parents: 1510
diff changeset
   312
    String safeGetClassName() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Returns the name of this <code>OpenType</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @return the type name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public String getTypeName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns the text description of this <code>OpenType</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return the description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns <code>true</code> if the open data values this open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * type describes are arrays, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return true if this is an array type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public boolean isArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return isArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Tests whether <var>obj</var> is a value for this open type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param obj the object to be tested for validity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return <code>true</code> if <var>obj</var> is a value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * open type, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public abstract boolean isValue(Object obj) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Tests whether values of the given type can be assigned to this open type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * The default implementation of this method returns true only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * types are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @param ot the type to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return true if {@code ot} is assignable to this open type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    boolean isAssignableFrom(OpenType<?> ot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return this.equals(ot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /* *** Methods overriden from class Object *** */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Compares the specified <code>obj</code> parameter with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * open type instance for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param obj the object to compare to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return true if this object and <code>obj</code> are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public abstract boolean equals(Object obj) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public abstract int hashCode() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Returns a string representation of this open type instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @return the string representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public abstract String toString() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Deserializes an {@link OpenType} from an {@link java.io.ObjectInputStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        checkClassNameOverride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        ObjectInputStream.GetField fields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        final String classNameField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        final String descriptionField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        final String typeNameField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            classNameField =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                validClassName((String) fields.get("className", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            descriptionField =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                valid("description", (String) fields.get("description", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            typeNameField =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                valid("typeName", (String) fields.get("typeName", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            IOException e2 = new InvalidObjectException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            e2.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throw e2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        className = classNameField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        description = descriptionField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        typeName = typeNameField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        isArray = (className.startsWith("["));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
}