src/java.management/share/classes/javax/management/loading/MLetObjectInputStream.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1999, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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.loading;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
// java import
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This subclass of ObjectInputStream delegates loading of classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * an existing MLetClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class MLetObjectInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private MLet loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Loader must be non-null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public MLetObjectInputStream(InputStream in, MLet loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        throws IOException, StreamCorruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new IllegalArgumentException("Illegal null argument to MLetObjectInputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    58
    private Class<?> primitiveType(char c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        switch(c) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    60
        case 'B':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return Byte.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    63
        case 'C':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return Character.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    66
        case 'D':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return Double.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    69
        case 'F':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return Float.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    72
        case 'I':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return Integer.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    75
        case 'J':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return Long.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    78
        case 'S':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return Short.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    81
        case 'Z':
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return Boolean.TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Use the given ClassLoader rather than using the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    90
    @Override
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    91
    protected Class<?> resolveClass(ObjectStreamClass objectstreamclass)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String s = objectstreamclass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (s.startsWith("[")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            for (i = 1; s.charAt(i) == '['; i++);
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    98
            Class<?> class1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (s.charAt(i) == 'L') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                class1 = loader.loadClass(s.substring(i + 1, s.length() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (s.length() != i + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new ClassNotFoundException(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                class1 = primitiveType(s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            int ai[] = new int[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            for (int j = 0; j < i; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                ai[j] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return Array.newInstance(class1, ai).getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return loader.loadClass(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
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
     * Returns the ClassLoader being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}