Path: blob/master/test/jdk/java/io/Serializable/noSuchFieldClarification/NoSuchFieldClarification.java
41153 views
/*1* Copyright (c) 2005, 2019, 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 {35private static final long serialVersionUID = 1L;3637private double radius;38private double angle;3940private static final ObjectStreamField[] serialPersistentFields = {41new ObjectStreamField("x", double.class),42new ObjectStreamField("y", double.class),43};4445public TwoDPoint(double x, double y) {46this.radius = Math.sqrt(x*x+y*y);47this.angle = Math.atan2(y, x);48}4950public double getX() {51return radius * Math.cos(angle);52}5354public double getY() {55return radius * Math.sin(angle);56}5758public String toString() {59return "[TwoDPoint:x=" + this.getX() + ", y=" + this.getY() +"]";60}6162private void writeObject(ObjectOutputStream out) throws IOException {63ObjectOutputStream.PutField fields = out.putFields();64fields.put("x", radius * Math.cos(angle));65fields.put("y", radius * Math.sin(angle));66out.writeFields();67}6869private void readObject(ObjectInputStream in)70throws ClassNotFoundException, IOException71{72ObjectInputStream.GetField fields = in.readFields();73double x = fields.get("x", 0);74double y = fields.get("y", 0.0);7576radius = Math.sqrt(x*x + y*y);77angle = Math.atan2(y, x);78}7980}8182public class NoSuchFieldClarification {83private static final String SUBSTRING1 = "x";84private static final String SUBSTRING2 = int.class.toString();8586public static void main(String[] args) throws IOException,87ClassNotFoundException88{89TwoDPoint point = new TwoDPoint(7, 67);90ByteArrayOutputStream bout = new ByteArrayOutputStream();91ObjectOutputStream oout = new ObjectOutputStream(bout);92oout.writeObject(point);93oout.close();94byte[] ser = bout.toByteArray();95ByteArrayInputStream bin = new ByteArrayInputStream(ser);96ObjectInputStream oin = new ObjectInputStream(bin);97try {98point = (TwoDPoint) oin.readObject();99throw new Error();100} catch(IllegalArgumentException exc) {101String msg = exc.getMessage();102System.err.println("\nOriginal message : " + msg);103if (msg.trim().toLowerCase().lastIndexOf(SUBSTRING1) > 0 &&104msg.trim().toLowerCase().lastIndexOf(SUBSTRING2) > 0)105{106System.err.println("\nTEST PASSED");107} else {108throw new Error();109}110}111}112}113114115