Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/checkModifiers/CheckModifiers.java
38828 views
/*1* Copyright (c) 1999, 2010, 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 421488825* @clean CheckModifiers TestClass1 TestClass226* @build CheckModifiers27* @run main CheckModifiers28* @summary Make sure that serialpersistentFields data member is used to29* represent tyhe serializable fields only if it has the modfiers30* static, final, private and the type is ObjectStreamField.31* No need to check for static, as ObjectStreamField class is not32* serializable.33*34*/3536import java.io.*;37class TestClass1 implements Serializable {38// Missing the "final" modifier39private static ObjectStreamField[] serialPersistentFields = {40new ObjectStreamField("field1", Integer.class),41new ObjectStreamField("field2", Double.TYPE),42};4344Integer field1;45double field2;46int field3;47String field4;4849public TestClass1(Integer f1, double f2, int f3, String f4) {50field1 = f1;51field2 = f2;52field3 = f3;53field4 = f4;54}5556private void readObject(ObjectInputStream ois)57throws IOException, ClassNotFoundException {58ObjectInputStream.GetField pfields = ois.readFields();5960field1 = (Integer) pfields.get("field1", new Integer(100));61field2 = pfields.get("field2", 99.99);6263/* These fields must be present in the stream */64try {65field3 = pfields.get("field3", 99);66System.out.println("Passes test 1a");67} catch(IllegalArgumentException e) {68throw new Error("data field: field3 not in the persistent stream");69}70try {71field4 = (String) pfields.get("field4", "Default string");72System.out.println("Passes test 1b");73} catch(IllegalArgumentException e) {74throw new Error("data field: field4 not in the persistent stream");75}76}77};787980class TestClass2 implements Serializable {81// public instead of private82public static final ObjectStreamField[] serialPersistentFields = {83new ObjectStreamField("field1", Integer.class),84new ObjectStreamField("field2", Double.TYPE),85};8687Integer field1;88double field2;89int field3;90String field4;9192public TestClass2(Integer f1, double f2, int f3, String f4) {93field1 = f1;94field2 = f2;95field3 = f3;96field4 = f4;97}9899private void readObject(ObjectInputStream ois)100throws IOException, ClassNotFoundException {101ObjectInputStream.GetField pfields = ois.readFields();102103field1 = (Integer) pfields.get("field1", new Integer(100));104field2 = pfields.get("field2", 99.99);105106/* These fields must be present in the stream */107try {108field3 = pfields.get("field3", 99);109System.out.println("Passes test 2a");110} catch(IllegalArgumentException e) {111throw new Error("data field: field3 not in the persistent stream");112}113try {114field4 = (String) pfields.get("field4", "Default string");115System.out.println("Passes test 2b");116} catch(IllegalArgumentException e) {117throw new Error("data field: field4 not in the persistent stream");118}119}120};121122class TestClass3 implements Serializable{123// Not of type ObjectStreamField124private final String[] serialPersistentFields = {"Foo","Foobar"};;125Integer field1;126double field2;127int field3;128String field4;129130public TestClass3(Integer f1, double f2, int f3, String f4) {131field1 = f1;132field2 = f2;133field3 = f3;134field4 = f4;135}136137private void readObject(ObjectInputStream ois)138throws IOException, ClassNotFoundException {139ObjectInputStream.GetField pfields = ois.readFields();140141field1 = (Integer) pfields.get("field1", new Integer(100));142field2 = pfields.get("field2", 99.99);143field3 = pfields.get("field3", 99);144field4 = (String) pfields.get("field4", "Default string");145146try {147String[] tserialPersistentFields =148(String[])pfields.get("serialPersistentFields", null);149System.out.println("Passes test 3");150} catch(IllegalArgumentException e) {151throw new Error("non-static field: " +152"serialPersistentFields must be in the persistent stream");153}154}155};156157class TestClass4 implements Serializable {158// Correct format159private static final ObjectStreamField[] serialPersistentFields = {160new ObjectStreamField("field1", Integer.class),161new ObjectStreamField("field2", Double.TYPE),162};163164Integer field1;165double field2;166int field3;167String field4;168169public TestClass4(Integer f1, double f2, int f3, String f4) {170field1 = f1;171field2 = f2;172field3 = f3;173field4 = f4;174}175176private void readObject(ObjectInputStream ois)177throws IOException, ClassNotFoundException {178ObjectInputStream.GetField pfields = ois.readFields();179180field1 = (Integer) pfields.get("field1", new Integer(100));181field2 = pfields.get("field2", 99.99);182183try {184field3 = pfields.get("field3", 99);185throw new Error("data field: field3 in the persistent stream");186} catch(IllegalArgumentException e) {187System.out.println("Passes test 4a");188}189try {190field4 = (String) pfields.get("field4", "Default string");191throw new Error("data field: field4 in the persistent stream");192} catch(IllegalArgumentException e) {193System.out.println("Passes test 4b");194}195}196};197198public class CheckModifiers {199public static void main(String[] args)200throws ClassNotFoundException, IOException{201TestClass1 tc1 = new TestClass1(new Integer(100), 25.56, 2000,202new String("Test modifiers of serialPersistentFields"));203204TestClass2 tc2 = new TestClass2(new Integer(100), 25.56, 2000,205new String("Test modifiers of serialPersistentFields"));206207TestClass3 tc3 = new TestClass3(new Integer(100), 25.56, 2000,208new String("Test Type of serialPersistentFields"));209210TestClass4 tc4 = new TestClass4(new Integer(100), 25.56, 2000,211new String("Test modifiers of serialPersistentFields"));212213214FileOutputStream fos = new FileOutputStream("fields.ser");215try {216ObjectOutputStream oos = new ObjectOutputStream(fos);217System.out.println("Writing obj 1");218oos.writeObject(tc1);219System.out.println("Writing obj 2");220oos.writeObject(tc2);221System.out.println("Writing obj 3");222oos.writeObject(tc3);223System.out.println("Writing obj 4");224oos.writeObject(tc4);225oos.flush();226} finally {227fos.close();228}229230FileInputStream fis = new FileInputStream("fields.ser");231try {232ObjectInputStream ois = new ObjectInputStream(fis);233System.out.println("Test modifiers for serialPeristentFields ");234System.out.println("---------------------------------------- ");235System.out.println("Declaration missing final modifier");236ois.readObject();237System.out.println();238System.out.println("Declaration with public instead of private access");239ois.readObject();240System.out.println();241System.out.println("Declaration with different type");242ois.readObject();243System.out.println();244System.out.println("Declaration as in specification");245ois.readObject();246} finally {247fis.close();248}249}250};251252253