author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 58565 | baa5969ecf34 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, 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 |
/* @test |
|
25 |
* @bug 6323008 |
|
26 |
* @summary this test verifies that exception from GetField.get method |
|
27 |
* will be a more comprehensible |
|
28 |
* |
|
29 |
* @author Andrey Ozerov |
|
30 |
* |
|
31 |
*/ |
|
32 |
||
33 |
import java.io.*; |
|
34 |
||
35 |
class TwoDPoint implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
36 |
private static final long serialVersionUID = 1L; |
2 | 37 |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
38 |
private double radius; |
2 | 39 |
private double angle; |
40 |
||
41 |
private static final ObjectStreamField[] serialPersistentFields = { |
|
42 |
new ObjectStreamField("x", double.class), |
|
43 |
new ObjectStreamField("y", double.class), |
|
44 |
}; |
|
45 |
||
46 |
public TwoDPoint(double x, double y) { |
|
47 |
this.radius = Math.sqrt(x*x+y*y); |
|
48 |
this.angle = Math.atan2(y, x); |
|
49 |
} |
|
50 |
||
51 |
public double getX() { |
|
52 |
return radius * Math.cos(angle); |
|
53 |
} |
|
54 |
||
55 |
public double getY() { |
|
56 |
return radius * Math.sin(angle); |
|
57 |
} |
|
58 |
||
59 |
public String toString() { |
|
60 |
return "[TwoDPoint:x=" + this.getX() + ", y=" + this.getY() +"]"; |
|
61 |
} |
|
62 |
||
63 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
64 |
ObjectOutputStream.PutField fields = out.putFields(); |
|
65 |
fields.put("x", radius * Math.cos(angle)); |
|
66 |
fields.put("y", radius * Math.sin(angle)); |
|
67 |
out.writeFields(); |
|
68 |
} |
|
69 |
||
70 |
private void readObject(ObjectInputStream in) |
|
71 |
throws ClassNotFoundException, IOException |
|
72 |
{ |
|
73 |
ObjectInputStream.GetField fields = in.readFields(); |
|
74 |
double x = fields.get("x", 0); |
|
75 |
double y = fields.get("y", 0.0); |
|
76 |
||
77 |
radius = Math.sqrt(x*x + y*y); |
|
78 |
angle = Math.atan2(y, x); |
|
79 |
} |
|
80 |
||
81 |
} |
|
82 |
||
83 |
public class NoSuchFieldClarification { |
|
84 |
private static final String SUBSTRING1 = "x"; |
|
85 |
private static final String SUBSTRING2 = int.class.toString(); |
|
86 |
||
87 |
public static void main(String[] args) throws IOException, |
|
88 |
ClassNotFoundException |
|
89 |
{ |
|
90 |
TwoDPoint point = new TwoDPoint(7, 67); |
|
91 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
92 |
ObjectOutputStream oout = new ObjectOutputStream(bout); |
|
93 |
oout.writeObject(point); |
|
94 |
oout.close(); |
|
95 |
byte[] ser = bout.toByteArray(); |
|
96 |
ByteArrayInputStream bin = new ByteArrayInputStream(ser); |
|
97 |
ObjectInputStream oin = new ObjectInputStream(bin); |
|
98 |
try { |
|
99 |
point = (TwoDPoint) oin.readObject(); |
|
100 |
throw new Error(); |
|
101 |
} catch(IllegalArgumentException exc) { |
|
102 |
String msg = exc.getMessage(); |
|
103 |
System.err.println("\nOriginal message : " + msg); |
|
104 |
if (msg.trim().toLowerCase().lastIndexOf(SUBSTRING1) > 0 && |
|
105 |
msg.trim().toLowerCase().lastIndexOf(SUBSTRING2) > 0) |
|
106 |
{ |
|
107 |
System.err.println("\nTEST PASSED"); |
|
108 |
} else { |
|
109 |
throw new Error(); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
} |