test/jdk/java/io/Serializable/InvalidClassException/noargctor/DefaultPackage.java
author rriggs
Fri, 11 Oct 2019 13:11:56 -0400
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231427: Warning cleanup in tests of java.io.Serializable Reviewed-by: darcy, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4093279
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @compile DefaultPackage.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main DefaultPackage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Raise InvalidClassException if 1st NonSerializable superclass' no-arg constructor is not accessible. This test verifies default package access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class DefaultPackagePublicConstructor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    public DefaultPackagePublicConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
class DefaultPackageProtectedConstructor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    protected DefaultPackageProtectedConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class DefaultPackageDefaultAccessConstructor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    DefaultPackageDefaultAccessConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class DefaultPackagePrivateConstructor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private DefaultPackagePrivateConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* need to have at least one protected constructor to extend this class.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected DefaultPackagePrivateConstructor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
class DefaultPublicSerializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
extends DefaultPackagePublicConstructor implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    59
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    60
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int field1 = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
class DefaultProtectedSerializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
extends DefaultPackageProtectedConstructor implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    67
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    68
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    int field1 = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
class DefaultAccessSerializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
extends DefaultPackageDefaultAccessConstructor implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
{
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    75
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    int field1 = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    80
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
class DefaultPrivateSerializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
extends DefaultPackagePrivateConstructor implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
{
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    84
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    85
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    int field1 = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    DefaultPrivateSerializable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        super(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
class ExternalizablePublicConstructor implements Externalizable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    94
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    95
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public ExternalizablePublicConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public void writeExternal(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   106
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
class ExternalizableProtectedConstructor implements Externalizable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   108
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   109
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected ExternalizableProtectedConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void writeExternal(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   120
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
class ExternalizableAccessConstructor implements Externalizable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   122
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    ExternalizableAccessConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void writeExternal(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   134
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
class ExternalizablePrivateConstructor implements Externalizable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   136
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   137
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private ExternalizablePrivateConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public ExternalizablePrivateConstructor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void writeExternal(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
public class DefaultPackage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            ObjectOutputStream out =   new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            out.writeObject(new DefaultPublicSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            out.writeObject(new DefaultProtectedSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            out.writeObject(new DefaultAccessSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            out.writeObject(new DefaultPrivateSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            InputStream is = new ByteArrayInputStream(baos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            ObjectInputStream in = new ObjectInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            /* (DefaultPublicSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            /* (DefaultProtectedSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            /* (DefaultAcccessSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                /* (DefaultPrivateSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new Error("Expected InvalidClassException reading DefaultPrivateSerialziable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            out.writeObject(new ExternalizablePublicConstructor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            /* (ExternalizablePublicConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            out.writeObject(new ExternalizableProtectedConstructor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                /* (ExternalizableProtectedConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new Error("Expected InvalidClassException reading ExternalizableProtectedConstructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            out.writeObject(new ExternalizableAccessConstructor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                /* (ExternalizableAccessConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                throw new Error("Expected InvalidClassException reading ExternalizableAccessConstructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            out.writeObject(new ExternalizablePrivateConstructor(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                /* (ExternalizablePrivateConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                throw new Error("Expected InvalidClassException reading ExternalizablePrivateConstructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}