author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
31 |
import java.io.*; |
|
32 |
class 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. |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
5506
diff
changeset
|
38 |
private static final ObjectStreamField[] serialPersistentFields = { |
2 | 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 |
||
102 |
public 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 |
}; |