Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/noSuchFieldClarification/NoSuchFieldClarification.java
38828 views
/*1* Copyright (c) 2005, 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/* @test24* @bug 632300825* @summary this test verifies that exception from GetField.get method26* will be a more comprehensible27*28* @author Andrey Ozerov29*30*/3132import java.io.*;3334class TwoDPoint implements Serializable {3536private double radius;37private double angle;3839private static final ObjectStreamField[] serialPersistentFields = {40new ObjectStreamField("x", double.class),41new ObjectStreamField("y", double.class),42};4344public TwoDPoint(double x, double y) {45this.radius = Math.sqrt(x*x+y*y);46this.angle = Math.atan2(y, x);47}4849public double getX() {50return radius * Math.cos(angle);51}5253public double getY() {54return radius * Math.sin(angle);55}5657public String toString() {58return "[TwoDPoint:x=" + this.getX() + ", y=" + this.getY() +"]";59}6061private void writeObject(ObjectOutputStream out) throws IOException {62ObjectOutputStream.PutField fields = out.putFields();63fields.put("x", radius * Math.cos(angle));64fields.put("y", radius * Math.sin(angle));65out.writeFields();66}6768private void readObject(ObjectInputStream in)69throws ClassNotFoundException, IOException70{71ObjectInputStream.GetField fields = in.readFields();72double x = fields.get("x", 0);73double y = fields.get("y", 0.0);7475radius = Math.sqrt(x*x + y*y);76angle = Math.atan2(y, x);77}7879}8081public class NoSuchFieldClarification {82private static final String SUBSTRING1 = "x";83private static final String SUBSTRING2 = int.class.toString();8485public static void main(String[] args) throws IOException,86ClassNotFoundException87{88TwoDPoint point = new TwoDPoint(7, 67);89ByteArrayOutputStream bout = new ByteArrayOutputStream();90ObjectOutputStream oout = new ObjectOutputStream(bout);91oout.writeObject(point);92oout.close();93byte[] ser = bout.toByteArray();94ByteArrayInputStream bin = new ByteArrayInputStream(ser);95ObjectInputStream oin = new ObjectInputStream(bin);96try {97point = (TwoDPoint) oin.readObject();98throw new Error();99} catch(IllegalArgumentException exc) {100String msg = exc.getMessage();101System.err.println("\nOriginal message : " + msg);102if (msg.trim().toLowerCase().lastIndexOf(SUBSTRING1) > 0 &&103msg.trim().toLowerCase().lastIndexOf(SUBSTRING2) > 0)104{105System.err.println("\nTEST PASSED");106} else {107throw new Error();108}109}110}111}112113114