Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/InvalidClassException/noargctor/DefaultPackage.java
38890 views
/*1* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 409327925* @compile DefaultPackage.java26* @run main DefaultPackage27* @summary Raise InvalidClassException if 1st NonSerializable superclass' no-arg constructor is not accessible. This test verifies default package access.28*/29import java.io.*;3031class DefaultPackagePublicConstructor {32public DefaultPackagePublicConstructor() {33}34};3536class DefaultPackageProtectedConstructor {37protected DefaultPackageProtectedConstructor() {38}39};4041class DefaultPackageDefaultAccessConstructor {42DefaultPackageDefaultAccessConstructor() {43}44};4546class DefaultPackagePrivateConstructor {47private DefaultPackagePrivateConstructor() {48}4950/* need to have at least one protected constructor to extend this class.*/51protected DefaultPackagePrivateConstructor(int i) {52}53};5455class DefaultPublicSerializable56extends DefaultPackagePublicConstructor implements Serializable57{58int field1 = 5;59};6061class DefaultProtectedSerializable62extends DefaultPackageProtectedConstructor implements Serializable63{64int field1 = 5;65};6667class DefaultAccessSerializable68extends DefaultPackageDefaultAccessConstructor implements Serializable69{70int field1 = 5;71};7273class DefaultPrivateSerializable74extends DefaultPackagePrivateConstructor implements Serializable75{76int field1 = 5;7778DefaultPrivateSerializable() {79super(1);80}81};8283class ExternalizablePublicConstructor implements Externalizable {84public ExternalizablePublicConstructor() {85}86public void writeExternal(ObjectOutput out) throws IOException {87}88public void readExternal(ObjectInput in)89throws IOException, ClassNotFoundException90{91}92};9394class ExternalizableProtectedConstructor implements Externalizable {95protected ExternalizableProtectedConstructor() {96}97public void writeExternal(ObjectOutput out) throws IOException {98}99public void readExternal(ObjectInput in)100throws IOException, ClassNotFoundException101{102}103};104105class ExternalizableAccessConstructor implements Externalizable {106ExternalizableAccessConstructor() {107}108public void writeExternal(ObjectOutput out) throws IOException {109}110public void readExternal(ObjectInput in)111throws IOException, ClassNotFoundException112{113}114};115116class ExternalizablePrivateConstructor implements Externalizable {117private ExternalizablePrivateConstructor() {118}119public ExternalizablePrivateConstructor(int i) {120}121public void writeExternal(ObjectOutput out) throws IOException {122}123public void readExternal(ObjectInput in)124throws IOException, ClassNotFoundException125{126}127};128129130public class DefaultPackage {131public static void main(String args[])132throws IOException, ClassNotFoundException133{134ByteArrayOutputStream baos = new ByteArrayOutputStream();135ObjectOutputStream out = new ObjectOutputStream(baos);136out.writeObject(new DefaultPublicSerializable());137out.writeObject(new DefaultProtectedSerializable());138out.writeObject(new DefaultAccessSerializable());139out.writeObject(new DefaultPrivateSerializable());140141InputStream is = new ByteArrayInputStream(baos.toByteArray());142ObjectInputStream in = new ObjectInputStream(is);143/* (DefaultPublicSerializable) */ in.readObject();144/* (DefaultProtectedSerializable) */ in.readObject();145/* (DefaultAcccessSerializable) */ in.readObject();146try {147/* (DefaultPrivateSerializable) */ in.readObject();148throw new Error("Expected InvalidClassException reading DefaultPrivateSerialziable");149} catch (InvalidClassException e) {150}151in.close();152153baos.reset();154out = new ObjectOutputStream(baos);155out.writeObject(new ExternalizablePublicConstructor());156157in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));158/* (ExternalizablePublicConstructor) */ in.readObject();159in.close();160161baos.reset();162out = new ObjectOutputStream(baos);163out.writeObject(new ExternalizableProtectedConstructor());164165166in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));167try {168/* (ExternalizableProtectedConstructor) */ in.readObject();169throw new Error("Expected InvalidClassException reading ExternalizableProtectedConstructor");170} catch (InvalidClassException e) {171}172in.close();173174baos.reset();175out = new ObjectOutputStream(baos);176out.writeObject(new ExternalizableAccessConstructor());177178in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));179try {180/* (ExternalizableAccessConstructor) */ in.readObject();181throw new Error("Expected InvalidClassException reading ExternalizableAccessConstructor");182} catch (InvalidClassException e) {183}184in.close();185186baos.reset();187out = new ObjectOutputStream(baos);188out.writeObject(new ExternalizablePrivateConstructor(2));189190in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));191try {192/* (ExternalizablePrivateConstructor) */ in.readObject();193throw new Error("Expected InvalidClassException reading ExternalizablePrivateConstructor");194} catch (InvalidClassException e) {195}196out.close();197in.close();198}199}200201202