📄 testserialfields.java
字号:
/* * Tests of ObjectInputStream.GetField and ObjectOutputStream.PutField. * Doesn't test too aggressively (for example, none of the tests make * sure what they say gets written out, actually gets written out.) * See SerializationCompatibility for more aggressive testing. * * Copyright (c) 2002 Pat Tullmann * All Rights Reserved. * * This file is released into the public domain. * * @author Pat Tullmann <pat_kaffe@tullmann.org> */import java.io.*;import java.util.*;public class TestSerialFields{ // Set to true to get stack traces and byte-dumps: private static final boolean DEBUG = false; public static final long constantUID = 0x42L; private static final String[] allFieldNames = { "x", "y", "xd", "yd", "xodd", "yodd", }; private static void checkGetBool(ObjectInputStream.GetField inputFields, String fname) throws IOException { try { boolean val = inputFields.get(fname, false); System.out.print(" bool=" +val); } catch (IllegalArgumentException iae) { // ignore if (DEBUG) System.out.print(" /*not boolean*/"); } } private static void checkGetObject(ObjectInputStream.GetField inputFields, String fname) throws IOException { try { Object val = inputFields.get(fname, null); System.out.print(" Object=" +val); } catch (IllegalArgumentException iae) { // ignore if (DEBUG) System.out.print(" /*not Object*/"); } } private static void checkGetInt(ObjectInputStream.GetField inputFields, String fname) throws IOException { try { int val = inputFields.get(fname, 0); System.out.print(" int=" +val); } catch (IllegalArgumentException iae) { // ignore if (DEBUG) System.out.print(" /*not int*/"); } } private static void checkGetDouble(ObjectInputStream.GetField inputFields, String fname) throws IOException { try { double val = inputFields.get(fname, 0.0); System.out.print(" double=" +val+ " "); } catch (IllegalArgumentException iae) { // ignore if (DEBUG) System.out.print(" /*not double*/"); } } private static boolean checkGetDefaulted(ObjectInputStream.GetField inputFields, String fname) throws IOException { try { boolean defaulted = inputFields.defaulted(fname); System.out.print(" " +fname+ ":"); if (defaulted) System.out.print(" (defaulted)"); return true; } catch (IllegalArgumentException iae) { return false; } } private static void checkGetFields(ObjectInputStream.GetField inputFields) throws IOException { for (int i = 0; i < allFieldNames.length; i++) { String fname = allFieldNames[i]; if (!checkGetDefaulted(inputFields, fname)) continue; checkGetBool(inputFields, fname); checkGetObject(inputFields, fname); checkGetInt(inputFields, fname); checkGetDouble(inputFields, fname); System.out.println(); } } private static String checkPutBool(ObjectOutputStream.PutField outputFields, String fname) throws IOException { try { outputFields.put(fname, true); return "bool=true "; } catch (IllegalArgumentException iae) { if (DEBUG) return "/*not boolean*/ "; return ""; } } private static String checkPutString(ObjectOutputStream.PutField outputFields, String fname) throws IOException { try { outputFields.put(fname, new String("putfield test string")); return "string='putfield test string' "; } catch (IllegalArgumentException iae) { if (DEBUG) return "/*not Object*/ "; return ""; } } private static String checkPutInt(ObjectOutputStream.PutField outputFields, String fname) throws IOException { try { outputFields.put(fname, 42); return "int=42 "; } catch (IllegalArgumentException iae) { if (DEBUG) return "/*not int*/ "; return ""; } } private static String checkPutDouble(ObjectOutputStream.PutField outputFields, String fname) throws IOException { try { outputFields.put(fname, 6.9); return "double=6.9 "; } catch (IllegalArgumentException iae) { if (DEBUG) return "/*not double*/ "; return ""; } } private static void checkPutFields(ObjectOutputStream.PutField outputFields) throws IOException { for (int i = 0; i < allFieldNames.length; i++) { String fname = allFieldNames[i]; String msg = ""; msg += checkPutBool(outputFields, fname); msg += checkPutString(outputFields, fname); msg += checkPutInt(outputFields, fname); msg += checkPutDouble(outputFields, fname); if (!msg.equals("")) { System.out.print(" put " +fname+ ": "); System.out.print(msg); System.out.println(); } } } private static class Test0001 // "original" (pristine) version implements Serializable { private static final long serialVersionUID = constantUID; private int x = 0x042; private int y = 0x069; // Garbage fields that won't be read by anyone private int[] iarray = new int[] { 1, 2, 3}; private Integer X = new Integer(44); private long foo = 11; private double xf = 32.45; public String toString() { return (this.getClass().getName() + " x=" +x+ "; y=" +y+ "; foo=" +foo); } private void writeObject(ObjectOutputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkPutFields(stream.putFields()); stream.writeFields(); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkGetFields(stream.readFields()); } } private static class Test0002 // contains doubles implements Serializable { private static final long serialVersionUID = constantUID; private double xd = 0; private double yd = 0; public String toString() { return (this.getClass().getName() + " instance"); } private void writeObject(ObjectOutputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkPutFields(stream.putFields()); stream.writeFields(); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkGetFields(stream.readFields()); } } private static class Test0005 // missing x,y fields implements Serializable { private static final long serialVersionUID = constantUID; public String toString() { return (this.getClass().getName() + " instance"); } private void writeObject(ObjectOutputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkPutFields(stream.putFields()); stream.writeFields(); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkGetFields(stream.readFields()); } } private static class Test0008 // Compatible via serialPersistentFields... implements Serializable { private static final long serialVersionUID = constantUID; private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("x", int.class), new ObjectStreamField("y", int.class) }; public String toString() { return (this.getClass().getName() + " instance"); } private void writeObject(ObjectOutputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkPutFields(stream.putFields()); stream.writeFields(); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkGetFields(stream.readFields()); } } private static class Test0010 // Compatible, but different (and invalid) serialPersistentFields... implements Serializable { private static final long serialVersionUID = constantUID; private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField("xodd", String.class), new ObjectStreamField("yodd", String.class) }; private int x; private int y; public String toString() { return (this.getClass().getName() + " instance"); } private void writeObject(ObjectOutputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkPutFields(stream.putFields()); stream.writeFields(); } private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { TestSerialFields.checkGetFields(stream.readFields()); } } private static class Test0011 // Compatible, broken writeObject implements Serializable { private static final long serialVersionUID = constantUID; private int x; private int y; public String toString() { return (this.getClass().getName() + " instance"); } private void writeObject(ObjectOutputStream stream)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -