test/jdk/java/io/Serializable/InvalidClassException/noargctor/DefaultPackage.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 5506 jdk/test/java/io/Serializable/InvalidClassException/noargctor/DefaultPackage.java@202f599c92aa
child 58565 baa5969ecf34
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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: 2
diff changeset
     2
 * Copyright (c) 1998, 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
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    int field1 = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
class DefaultProtectedSerializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
extends DefaultPackageProtectedConstructor implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    int field1 = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
class DefaultAccessSerializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
extends DefaultPackageDefaultAccessConstructor implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    int field1 = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
class DefaultPrivateSerializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
extends DefaultPackagePrivateConstructor implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
{
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
    DefaultPrivateSerializable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        super(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
class ExternalizablePublicConstructor implements Externalizable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public ExternalizablePublicConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void writeExternal(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
class ExternalizableProtectedConstructor implements Externalizable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected ExternalizableProtectedConstructor() {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
class ExternalizableAccessConstructor implements Externalizable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    ExternalizableAccessConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public void writeExternal(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        throws IOException, ClassNotFoundException
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
class ExternalizablePrivateConstructor implements Externalizable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private ExternalizablePrivateConstructor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public ExternalizablePrivateConstructor(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void writeExternal(ObjectOutput out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void readExternal(ObjectInput in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
public class DefaultPackage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ObjectOutputStream out =   new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            out.writeObject(new DefaultPublicSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            out.writeObject(new DefaultProtectedSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            out.writeObject(new DefaultAccessSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            out.writeObject(new DefaultPrivateSerializable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            InputStream is = new ByteArrayInputStream(baos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            ObjectInputStream in = new ObjectInputStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            /* (DefaultPublicSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            /* (DefaultProtectedSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            /* (DefaultAcccessSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                /* (DefaultPrivateSerializable) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throw new Error("Expected InvalidClassException reading DefaultPrivateSerialziable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            out.writeObject(new ExternalizablePublicConstructor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            /* (ExternalizablePublicConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            out.writeObject(new ExternalizableProtectedConstructor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                /* (ExternalizableProtectedConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                throw new Error("Expected InvalidClassException reading ExternalizableProtectedConstructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            out.writeObject(new ExternalizableAccessConstructor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                /* (ExternalizableAccessConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                throw new Error("Expected InvalidClassException reading ExternalizableAccessConstructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            baos.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            out = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            out.writeObject(new ExternalizablePrivateConstructor(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                /* (ExternalizablePrivateConstructor) */ in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                throw new Error("Expected InvalidClassException reading ExternalizablePrivateConstructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } catch (InvalidClassException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}