1 /* 2 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4764280 26 * 27 * @clean Setup Test A B 28 * @compile Setup.java 29 * @run main Setup 30 * @clean Setup A B 31 * @compile Test.java 32 * @run main Test 33 * 34 * @summary Verify that if a serializable class declares multiple 35 * serialPersistentFields that share the same name, calling 36 * ObjectStreamClass.lookup() for that class will not result in an 37 * InternalError, and that attempts at default serialization or 38 * deserialization of such a class will result in 39 * InvalidClassExceptions. 40 */ 41 42 import java.io.*; 43 44 class A implements Serializable { 45 private static final long serialVersionUID = 0L; 46 private static final ObjectStreamField[] serialPersistentFields = { 47 new ObjectStreamField("i", int.class), 48 new ObjectStreamField("i", int.class) 49 }; 50 int i; 51 } 52 53 class B implements Serializable { 54 private static final long serialVersionUID = 0L; 55 private static final ObjectStreamField[] serialPersistentFields = { 56 new ObjectStreamField("i", int.class), 57 new ObjectStreamField("i", String.class) 58 }; 59 int i; 60 } 61 62 public class Test { 63 public static void main(String[] args) throws Exception { 64 65 ObjectStreamClass.lookup(A.class); 66 ObjectStreamClass.lookup(B.class); 67 68 ObjectOutputStream oout = 69 new ObjectOutputStream(new ByteArrayOutputStream()); 70 try { 71 oout.writeObject(new A()); 72 throw new Error( 73 "write of A should fail with InvalidClassException"); 74 } catch (InvalidClassException e) { 75 } 76 77 oout = new ObjectOutputStream(new ByteArrayOutputStream()); 78 try { 79 oout.writeObject(new B()); 80 throw new Error( 81 "write of B should fail with InvalidClassException"); 82 } catch (InvalidClassException e) { 83 } 84 85 FileInputStream in = new FileInputStream("a.ser"); 86 try { 87 ObjectInputStream oin = new ObjectInputStream(in); 88 oin.readObject(); 89 throw new Error( 90 "read of A should fail with InvalidClassException"); 91 } catch (InvalidClassException e) { 92 } finally { 93 in.close(); 94 } 95 96 in = new FileInputStream("b.ser"); 97 try { 98 ObjectInputStream oin = new ObjectInputStream(in); 99 oin.readObject(); 100 throw new Error( 101 "read of B should fail with InvalidClassException"); 102 } catch (InvalidClassException e) { 103 } finally { 104 in.close(); 105 } 106 } 107 }