Path: blob/master/test/jdk/java/io/Serializable/GetField/Read2.java
41153 views
/*1* Copyright (c) 2001, 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/*24* @bug 442788125* @summary Verify that the ObjectInputStream.GetField API works properly for26* serialized fields which exist in the receiving object.27*/2829import java.io.*;3031class Foo implements Serializable {32private static final long serialVersionUID = 0L;3334boolean z;35byte b;36char c;37short s;38int i;39long j;40float f;41double d;42String str;43@SuppressWarnings("serial") /* Incorrect declarations are being tested */44Object extra;4546private void readObject(ObjectInputStream in)47throws IOException, ClassNotFoundException48{49ObjectInputStream.GetField fields = in.readFields();50if ((fields.get("z", false) != true) ||51(fields.get("b", (byte) 0) != 5) ||52(fields.get("c", '0') != '5') ||53(fields.get("s", (short) 0) != 5) ||54(fields.get("i", 0) != 5) ||55(fields.get("j", 0l) != 5) ||56(fields.get("f", 0.0f) != 5.0f) ||57(fields.get("d", 0.0) != 5.0) ||58(! fields.get("str", null).equals("5")))59{60throw new Error();61}62}63}6465public class Read2 {66public static void main(String[] args) throws Exception {67ObjectInputStream oin =68new ObjectInputStream(new FileInputStream("tmp.ser"));69oin.readObject();70oin.close();71}72}737475