Path: blob/master/test/jdk/java/io/Serializable/checkModifiers/CheckModifiers.java
41153 views
/*1* Copyright (c) 1999, 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 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 {38private static final long serialVersionUID = 1L;3940// Missing the "final" modifier41@SuppressWarnings("serial") /* Incorrect declarations are being tested */42private static ObjectStreamField[] serialPersistentFields = {43new ObjectStreamField("field1", Integer.class),44new ObjectStreamField("field2", Double.TYPE),45};4647Integer field1;48double field2;49int field3;50String field4;5152public TestClass1(Integer f1, double f2, int f3, String f4) {53field1 = f1;54field2 = f2;55field3 = f3;56field4 = f4;57}5859private void readObject(ObjectInputStream ois)60throws IOException, ClassNotFoundException {61ObjectInputStream.GetField pfields = ois.readFields();6263field1 = (Integer) pfields.get("field1", Integer.valueOf(100));64field2 = pfields.get("field2", 99.99);6566/* These fields must be present in the stream */67try {68field3 = pfields.get("field3", 99);69System.out.println("Passes test 1a");70} catch(IllegalArgumentException e) {71throw new Error("data field: field3 not in the persistent stream");72}73try {74field4 = (String) pfields.get("field4", "Default string");75System.out.println("Passes test 1b");76} catch(IllegalArgumentException e) {77throw new Error("data field: field4 not in the persistent stream");78}79}80};818283class TestClass2 implements Serializable {84private static final long serialVersionUID = 1L;8586// public instead of private87@SuppressWarnings("serial") /* Incorrect declarations are being tested */88public static final ObjectStreamField[] serialPersistentFields = {89new ObjectStreamField("field1", Integer.class),90new ObjectStreamField("field2", Double.TYPE),91};9293Integer field1;94double field2;95int field3;96String field4;9798public TestClass2(Integer f1, double f2, int f3, String f4) {99field1 = f1;100field2 = f2;101field3 = f3;102field4 = f4;103}104105private void readObject(ObjectInputStream ois)106throws IOException, ClassNotFoundException {107ObjectInputStream.GetField pfields = ois.readFields();108109field1 = (Integer) pfields.get("field1", Integer.valueOf(100));110field2 = pfields.get("field2", 99.99);111112/* These fields must be present in the stream */113try {114field3 = pfields.get("field3", 99);115System.out.println("Passes test 2a");116} catch(IllegalArgumentException e) {117throw new Error("data field: field3 not in the persistent stream");118}119try {120field4 = (String) pfields.get("field4", "Default string");121System.out.println("Passes test 2b");122} catch(IllegalArgumentException e) {123throw new Error("data field: field4 not in the persistent stream");124}125}126};127128class TestClass3 implements Serializable{129private static final long serialVersionUID = 1L;130131// Not of type ObjectStreamField132@SuppressWarnings("serial") /* Incorrect declarations are being tested */133private final String[] serialPersistentFields = {"Foo","Foobar"};;134Integer field1;135double field2;136int field3;137String field4;138139public TestClass3(Integer f1, double f2, int f3, String f4) {140field1 = f1;141field2 = f2;142field3 = f3;143field4 = f4;144}145146private void readObject(ObjectInputStream ois)147throws IOException, ClassNotFoundException {148ObjectInputStream.GetField pfields = ois.readFields();149150field1 = (Integer) pfields.get("field1", Integer.valueOf(100));151field2 = pfields.get("field2", 99.99);152field3 = pfields.get("field3", 99);153field4 = (String) pfields.get("field4", "Default string");154155try {156String[] tserialPersistentFields =157(String[])pfields.get("serialPersistentFields", null);158System.out.println("Passes test 3");159} catch(IllegalArgumentException e) {160throw new Error("non-static field: " +161"serialPersistentFields must be in the persistent stream");162}163}164};165166class TestClass4 implements Serializable {167private static final long serialVersionUID = 1L;168169// Correct format170private static final ObjectStreamField[] serialPersistentFields = {171new ObjectStreamField("field1", Integer.class),172new ObjectStreamField("field2", Double.TYPE),173};174175Integer field1;176double field2;177int field3;178String field4;179180public TestClass4(Integer f1, double f2, int f3, String f4) {181field1 = f1;182field2 = f2;183field3 = f3;184field4 = f4;185}186187private void readObject(ObjectInputStream ois)188throws IOException, ClassNotFoundException {189ObjectInputStream.GetField pfields = ois.readFields();190191field1 = (Integer) pfields.get("field1", Integer.valueOf(100));192field2 = pfields.get("field2", 99.99);193194try {195field3 = pfields.get("field3", 99);196throw new Error("data field: field3 in the persistent stream");197} catch(IllegalArgumentException e) {198System.out.println("Passes test 4a");199}200try {201field4 = (String) pfields.get("field4", "Default string");202throw new Error("data field: field4 in the persistent stream");203} catch(IllegalArgumentException e) {204System.out.println("Passes test 4b");205}206}207};208209public class CheckModifiers {210public static void main(String[] args)211throws ClassNotFoundException, IOException{212TestClass1 tc1 = new TestClass1(100, 25.56, 2000,213new String("Test modifiers of serialPersistentFields"));214215TestClass2 tc2 = new TestClass2(100, 25.56, 2000,216new String("Test modifiers of serialPersistentFields"));217218TestClass3 tc3 = new TestClass3(100, 25.56, 2000,219new String("Test Type of serialPersistentFields"));220221TestClass4 tc4 = new TestClass4(100, 25.56, 2000,222new String("Test modifiers of serialPersistentFields"));223224225FileOutputStream fos = new FileOutputStream("fields.ser");226try {227ObjectOutputStream oos = new ObjectOutputStream(fos);228System.out.println("Writing obj 1");229oos.writeObject(tc1);230System.out.println("Writing obj 2");231oos.writeObject(tc2);232System.out.println("Writing obj 3");233oos.writeObject(tc3);234System.out.println("Writing obj 4");235oos.writeObject(tc4);236oos.flush();237} finally {238fos.close();239}240241FileInputStream fis = new FileInputStream("fields.ser");242try {243ObjectInputStream ois = new ObjectInputStream(fis);244System.out.println("Test modifiers for serialPeristentFields ");245System.out.println("---------------------------------------- ");246System.out.println("Declaration missing final modifier");247ois.readObject();248System.out.println();249System.out.println("Declaration with public instead of private access");250ois.readObject();251System.out.println();252System.out.println("Declaration with different type");253ois.readObject();254System.out.println();255System.out.println("Declaration as in specification");256ois.readObject();257} finally {258fis.close();259}260}261};262263264