blob: c4541f2034a2b72545ff3482381f3100095d7771 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @bug 4180735
26 * @summary Make sure that fields that are defaulted can be of primitive and
27 * object type.
28 *
29 */
30
31import java.io.*;
32class TestClass implements Serializable {
33 public static final Integer DEFAULT_OBJECT_I = new Integer(99);
34 public static final Foo DEFAULT_OBJECT_F = new Foo();
35 private static final long serialVersionUID=5748652654655279289L;
36
37 // Fields to be serialized.
38 private final static ObjectStreamField[] serialPersistentFields = {
39 new ObjectStreamField("objectI", Integer.class),
40 new ObjectStreamField("primitiveI", Integer.TYPE),
41 new ObjectStreamField("foo", Foo.class)
42 };
43
44 Integer objectI;
45 int primitiveI;
46 Foo foo;
47
48 public TestClass(Foo f, Integer I, int i) {
49 foo = f;
50 objectI = I;
51 primitiveI = i;
52 }
53
54 /**
55 * Verify GetField.defaulted("fieldName") works for primitive and
56 * object fields.
57 */
58 private void readObject(ObjectInputStream in)
59 throws ClassNotFoundException, IOException
60 {
61 ObjectInputStream.GetField pfields = in.readFields();
62 primitiveI = pfields.get("primitiveI", 99);
63 System.out.println("The primitiveI : " + primitiveI);
64
65 objectI = (Integer)pfields.get("objectI", DEFAULT_OBJECT_I);
66 System.out.println("The ObjectI : " + objectI);
67
68 foo = (Foo)pfields.get("foo", DEFAULT_OBJECT_F);
69 System.out.println("The foo : " + foo);
70
71 try {
72 boolean b = pfields.defaulted("primitiveI");
73 System.out.println("Defaulted prim : " + b);
74 if (b == false) {
75 throw new Error("Bad return value for defaulted() with " +
76 "primitive type fields");
77 }
78
79 b = pfields.defaulted("objectI");
80 System.out.println("Defaulted ObjectI : " + b);
81 if (b == true) {
82 throw new Error("Bad return value for defaulted() with " +
83 "object type fields");
84 }
85
86 b = pfields.defaulted("foo");
87 System.out.println("Defaulted Foo : " + b);
88 if (b == false) {
89 throw new Error("Bad return value for defaulted() with " +
90 "object type fields");
91 }
92
93 } catch (IllegalArgumentException e) {
94 System.out.println("Exception " + e.getMessage() +
95 ": handled calling " +
96 "GetField.defaulted(\"fieldName referring to an object\")");
97 throw e;
98 }
99 }
100};
101
102public class GetFieldRead {
103 public static void main(String[] args)
104 throws ClassNotFoundException, IOException
105 {
106 FileInputStream fis = new FileInputStream("data.ser");
107 ObjectInputStream in = new ObjectInputStream(fis);
108 TestClass obj = (TestClass) in.readObject();
109 in.close();
110 }
111};