2
|
1 |
/*
|
|
2 |
* Copyright 2005 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 |
/* @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 {
|
|
36 |
|
|
37 |
private double radius;
|
|
38 |
private double angle;
|
|
39 |
|
|
40 |
private static final ObjectStreamField[] serialPersistentFields = {
|
|
41 |
new ObjectStreamField("x", double.class),
|
|
42 |
new ObjectStreamField("y", double.class),
|
|
43 |
};
|
|
44 |
|
|
45 |
public TwoDPoint(double x, double y) {
|
|
46 |
this.radius = Math.sqrt(x*x+y*y);
|
|
47 |
this.angle = Math.atan2(y, x);
|
|
48 |
}
|
|
49 |
|
|
50 |
public double getX() {
|
|
51 |
return radius * Math.cos(angle);
|
|
52 |
}
|
|
53 |
|
|
54 |
public double getY() {
|
|
55 |
return radius * Math.sin(angle);
|
|
56 |
}
|
|
57 |
|
|
58 |
public String toString() {
|
|
59 |
return "[TwoDPoint:x=" + this.getX() + ", y=" + this.getY() +"]";
|
|
60 |
}
|
|
61 |
|
|
62 |
private void writeObject(ObjectOutputStream out) throws IOException {
|
|
63 |
ObjectOutputStream.PutField fields = out.putFields();
|
|
64 |
fields.put("x", radius * Math.cos(angle));
|
|
65 |
fields.put("y", radius * Math.sin(angle));
|
|
66 |
out.writeFields();
|
|
67 |
}
|
|
68 |
|
|
69 |
private void readObject(ObjectInputStream in)
|
|
70 |
throws ClassNotFoundException, IOException
|
|
71 |
{
|
|
72 |
ObjectInputStream.GetField fields = in.readFields();
|
|
73 |
double x = fields.get("x", 0);
|
|
74 |
double y = fields.get("y", 0.0);
|
|
75 |
|
|
76 |
radius = Math.sqrt(x*x + y*y);
|
|
77 |
angle = Math.atan2(y, x);
|
|
78 |
}
|
|
79 |
|
|
80 |
}
|
|
81 |
|
|
82 |
public class NoSuchFieldClarification {
|
|
83 |
private static final String SUBSTRING1 = "x";
|
|
84 |
private static final String SUBSTRING2 = int.class.toString();
|
|
85 |
|
|
86 |
public static void main(String[] args) throws IOException,
|
|
87 |
ClassNotFoundException
|
|
88 |
{
|
|
89 |
TwoDPoint point = new TwoDPoint(7, 67);
|
|
90 |
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
91 |
ObjectOutputStream oout = new ObjectOutputStream(bout);
|
|
92 |
oout.writeObject(point);
|
|
93 |
oout.close();
|
|
94 |
byte[] ser = bout.toByteArray();
|
|
95 |
ByteArrayInputStream bin = new ByteArrayInputStream(ser);
|
|
96 |
ObjectInputStream oin = new ObjectInputStream(bin);
|
|
97 |
try {
|
|
98 |
point = (TwoDPoint) oin.readObject();
|
|
99 |
throw new Error();
|
|
100 |
} catch(IllegalArgumentException exc) {
|
|
101 |
String msg = exc.getMessage();
|
|
102 |
System.err.println("\nOriginal message : " + msg);
|
|
103 |
if (msg.trim().toLowerCase().lastIndexOf(SUBSTRING1) > 0 &&
|
|
104 |
msg.trim().toLowerCase().lastIndexOf(SUBSTRING2) > 0)
|
|
105 |
{
|
|
106 |
System.err.println("\nTEST PASSED");
|
|
107 |
} else {
|
|
108 |
throw new Error();
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|
|
112 |
}
|