Path: blob/master/test/jdk/java/io/Serializable/InvalidClassException/noargctor/DefaultPackage.java
41159 views
/*1* Copyright (c) 1998, 2019, 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{58private static final long serialVersionUID = 1L;5960int field1 = 5;61};6263class DefaultProtectedSerializable64extends DefaultPackageProtectedConstructor implements Serializable65{66private static final long serialVersionUID = 1L;6768int field1 = 5;69};7071class DefaultAccessSerializable72extends DefaultPackageDefaultAccessConstructor implements Serializable73{74private static final long serialVersionUID = 1L;7576int field1 = 5;77};7879@SuppressWarnings("serial") /* Incorrect declarations are being tested */80class DefaultPrivateSerializable81extends DefaultPackagePrivateConstructor implements Serializable82{83private static final long serialVersionUID = 1L;8485int field1 = 5;8687DefaultPrivateSerializable() {88super(1);89}90};9192class ExternalizablePublicConstructor implements Externalizable {93private static final long serialVersionUID = 1L;9495public ExternalizablePublicConstructor() {96}97public void writeExternal(ObjectOutput out) throws IOException {98}99public void readExternal(ObjectInput in)100throws IOException, ClassNotFoundException101{102}103};104105@SuppressWarnings("serial") /* Incorrect declarations are being tested */106class ExternalizableProtectedConstructor implements Externalizable {107private static final long serialVersionUID = 1L;108109protected ExternalizableProtectedConstructor() {110}111public void writeExternal(ObjectOutput out) throws IOException {112}113public void readExternal(ObjectInput in)114throws IOException, ClassNotFoundException115{116}117};118119@SuppressWarnings("serial") /* Incorrect declarations are being tested */120class ExternalizableAccessConstructor implements Externalizable {121private static final long serialVersionUID = 1L;122123ExternalizableAccessConstructor() {124}125public void writeExternal(ObjectOutput out) throws IOException {126}127public void readExternal(ObjectInput in)128throws IOException, ClassNotFoundException129{130}131};132133@SuppressWarnings("serial") /* Incorrect declarations are being tested */134class ExternalizablePrivateConstructor implements Externalizable {135private static final long serialVersionUID = 1L;136137private ExternalizablePrivateConstructor() {138}139public ExternalizablePrivateConstructor(int i) {140}141public void writeExternal(ObjectOutput out) throws IOException {142}143public void readExternal(ObjectInput in)144throws IOException, ClassNotFoundException145{146}147};148149150public class DefaultPackage {151public static void main(String args[])152throws IOException, ClassNotFoundException153{154ByteArrayOutputStream baos = new ByteArrayOutputStream();155ObjectOutputStream out = new ObjectOutputStream(baos);156out.writeObject(new DefaultPublicSerializable());157out.writeObject(new DefaultProtectedSerializable());158out.writeObject(new DefaultAccessSerializable());159out.writeObject(new DefaultPrivateSerializable());160161InputStream is = new ByteArrayInputStream(baos.toByteArray());162ObjectInputStream in = new ObjectInputStream(is);163/* (DefaultPublicSerializable) */ in.readObject();164/* (DefaultProtectedSerializable) */ in.readObject();165/* (DefaultAcccessSerializable) */ in.readObject();166try {167/* (DefaultPrivateSerializable) */ in.readObject();168throw new Error("Expected InvalidClassException reading DefaultPrivateSerialziable");169} catch (InvalidClassException e) {170}171in.close();172173baos.reset();174out = new ObjectOutputStream(baos);175out.writeObject(new ExternalizablePublicConstructor());176177in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));178/* (ExternalizablePublicConstructor) */ in.readObject();179in.close();180181baos.reset();182out = new ObjectOutputStream(baos);183out.writeObject(new ExternalizableProtectedConstructor());184185186in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));187try {188/* (ExternalizableProtectedConstructor) */ in.readObject();189throw new Error("Expected InvalidClassException reading ExternalizableProtectedConstructor");190} catch (InvalidClassException e) {191}192in.close();193194baos.reset();195out = new ObjectOutputStream(baos);196out.writeObject(new ExternalizableAccessConstructor());197198in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));199try {200/* (ExternalizableAccessConstructor) */ in.readObject();201throw new Error("Expected InvalidClassException reading ExternalizableAccessConstructor");202} catch (InvalidClassException e) {203}204in.close();205206baos.reset();207out = new ObjectOutputStream(baos);208out.writeObject(new ExternalizablePrivateConstructor(2));209210in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));211try {212/* (ExternalizablePrivateConstructor) */ in.readObject();213throw new Error("Expected InvalidClassException reading ExternalizablePrivateConstructor");214} catch (InvalidClassException e) {215}216out.close();217in.close();218}219}220221222