undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @bug 4088176
|
||||
* @summary Evolution: read evolved class with new field of a non-existing Externalizable class.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class A implements Serializable {
|
||||
// Version 1.0 of class A.
|
||||
private static final long serialVersionUID = 1L;
|
||||
public int bar;
|
||||
public D zoo;
|
||||
}
|
||||
|
||||
class D implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public int x;
|
||||
D(int y) {
|
||||
x = y;
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadAddedField {
|
||||
public static void main(String args[])
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
File f = new File("tmp.ser");
|
||||
ObjectInput in =
|
||||
new ObjectInputStream(new FileInputStream(f));
|
||||
A a = (A)in.readObject();
|
||||
A b = (A)in.readObject();
|
||||
if (a.bar != 4)
|
||||
throw new RuntimeException("a.bar does not equal 4, it equals " +
|
||||
a.bar);
|
||||
if (a.zoo.x != 22)
|
||||
throw new RuntimeException("a.zoo.x does not equal 22 equals " +
|
||||
a.zoo.x);
|
||||
if (b.bar != 4)
|
||||
throw new RuntimeException("b.bar does not equal 4, it equals " +
|
||||
b.bar);
|
||||
if (b.zoo.x != 22)
|
||||
throw new RuntimeException("b.zoo.x does not equal 22 equals " +
|
||||
b.zoo.x);
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4088176
|
||||
*
|
||||
* @clean A D NewExternFieldClass ReadAddedField WriteAddedField
|
||||
* @compile WriteAddedField.java
|
||||
* @run main WriteAddedField
|
||||
* @clean A D NewExternFieldClass ReadAddedField WriteAddedField
|
||||
* @compile ReadAddedField.java
|
||||
* @run main ReadAddedField
|
||||
*
|
||||
* @summary Evolution: read evolved class with new field of a non-existing Externalizable class.
|
||||
*/
|
||||
import java.io.*;
|
||||
|
||||
class NewExternFieldClass implements Externalizable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
byte l;
|
||||
|
||||
public NewExternFieldClass() {
|
||||
l = 0;
|
||||
}
|
||||
|
||||
public NewExternFieldClass(byte value) {
|
||||
l = value;
|
||||
}
|
||||
|
||||
public void readExternal(ObjectInput s)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
l = s.readByte();
|
||||
System.out.println("readExternal read " + l);
|
||||
}
|
||||
|
||||
public void writeExternal(ObjectOutput s) throws IOException
|
||||
{
|
||||
s.writeByte(l);
|
||||
}
|
||||
}
|
||||
|
||||
class D implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public int x;
|
||||
D(int y) {
|
||||
x = y;
|
||||
}
|
||||
}
|
||||
|
||||
class A implements Serializable {
|
||||
// Version 1.1 of class A. Added superclass NewSerializableSuper.
|
||||
private static final long serialVersionUID = 1L;
|
||||
NewExternFieldClass foo;
|
||||
D zoo;
|
||||
|
||||
int bar;
|
||||
A() {
|
||||
bar = 4;
|
||||
foo = new NewExternFieldClass((byte)66);
|
||||
zoo = new D(22);
|
||||
}
|
||||
}
|
||||
|
||||
public class WriteAddedField {
|
||||
public static void main(String args[]) throws IOException {
|
||||
A a = new A();
|
||||
File f = new File("tmp.ser");
|
||||
ObjectOutput out =
|
||||
new ObjectOutputStream(new FileOutputStream(f));
|
||||
out.writeObject(a);
|
||||
out.writeObject(new A());
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @bug 4088176
|
||||
* @build WriteAddedField ReadAddedField
|
||||
* @run main ReadAddedField
|
||||
*
|
||||
* @summary Deserialize an evolved class with a new field, field type is new.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class IncompatibleFieldClass implements Serializable {
|
||||
private static final long serialVersionUID = 3L;
|
||||
int x = 5;
|
||||
};
|
||||
|
||||
class A implements Serializable {
|
||||
// Version 1.0 of class A.
|
||||
private static final long serialVersionUID = 1L;
|
||||
public int bar;
|
||||
}
|
||||
|
||||
/** Test serial persistent fields w/o using Alternate API.
|
||||
*/
|
||||
class B implements Serializable {
|
||||
private static final long serialVersionUID = 2L;
|
||||
int bar;
|
||||
|
||||
B() {
|
||||
bar = 4;
|
||||
}
|
||||
}
|
||||
|
||||
/** Test serial persistent fields using Alternate API.
|
||||
* Also make sure that optional data to non-existent classes can be skipped.
|
||||
*/
|
||||
class C implements Serializable {
|
||||
private static final long serialVersionUID = 3L;
|
||||
int bar;
|
||||
|
||||
C() {
|
||||
bar = 4;
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream s)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
s.defaultReadObject();
|
||||
}
|
||||
|
||||
private void writeObject(ObjectOutputStream s)
|
||||
throws IOException
|
||||
{
|
||||
s.defaultWriteObject();
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadAddedField {
|
||||
public static void main(String args[])
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
File f = new File("tmp.ser");
|
||||
ObjectInput in =
|
||||
new ObjectInputStream(new FileInputStream(f));
|
||||
A a = (A)in.readObject();
|
||||
if (a.bar != 4)
|
||||
throw new RuntimeException("a.bar does not equal 4, it equals " +
|
||||
a.bar);
|
||||
B b = (B)in.readObject();
|
||||
if (b.bar != 4)
|
||||
throw new RuntimeException("b.bar does not equal 4, it equals " +
|
||||
b.bar);
|
||||
C c = (C)in.readObject();
|
||||
if (c.bar != 4)
|
||||
throw new RuntimeException("c.bar does not equal 4, it equals " +
|
||||
c.bar);
|
||||
A aa = (A)in.readObject();
|
||||
if (aa.bar != 4)
|
||||
throw new RuntimeException("a.bar does not equal 4, it equals " +
|
||||
aa.bar);
|
||||
B bb = (B)in.readObject();
|
||||
if (bb.bar != 4)
|
||||
throw new RuntimeException("b.bar does not equal 4, it equals " +
|
||||
bb.bar);
|
||||
C cc = (C)in.readObject();
|
||||
if (cc.bar != 4)
|
||||
throw new RuntimeException("c.bar does not equal 4, it equals " +
|
||||
cc.bar);
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,211 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4088176
|
||||
* @summary Deserialize an evolved class with a new field, field type is new.
|
||||
*
|
||||
* @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField
|
||||
* @compile WriteAddedField.java
|
||||
* @run main WriteAddedField
|
||||
* @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField
|
||||
* @compile ReadAddedField.java
|
||||
* @run main ReadAddedField
|
||||
|
||||
|
||||
*/
|
||||
import java.io.*;
|
||||
|
||||
class NewFieldClass implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
int k;
|
||||
|
||||
NewFieldClass(int value) {
|
||||
k = value;
|
||||
}
|
||||
};
|
||||
|
||||
class IncompatibleFieldClass implements Serializable {
|
||||
private static final long serialVersionUID = 3L;
|
||||
int x = 5;
|
||||
};
|
||||
|
||||
@SuppressWarnings("serial") /* Incorrect use is being tested */
|
||||
class NewExternFieldClass implements Externalizable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
byte l;
|
||||
|
||||
public NewExternFieldClass(int value) {
|
||||
l = (byte)value;
|
||||
}
|
||||
|
||||
public void readExternal(ObjectInput s)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
l = s.readByte();
|
||||
}
|
||||
|
||||
public void writeExternal(ObjectOutput s) throws IOException
|
||||
{
|
||||
s.writeByte(l);
|
||||
}
|
||||
}
|
||||
|
||||
class A implements Serializable {
|
||||
// Version 1.1 of class A. Added superclass NewSerializableSuper.
|
||||
private static final long serialVersionUID = 1L;
|
||||
NewFieldClass foo;
|
||||
NewFieldClass fooarray[];
|
||||
IncompatibleFieldClass boo;
|
||||
IncompatibleFieldClass booarray[];
|
||||
/* Excluded due to Bug 4089540
|
||||
NewExternFieldClass abc;
|
||||
NewExternFieldClass farray[];
|
||||
*/
|
||||
int bar;
|
||||
A() {
|
||||
foo = new NewFieldClass(23);
|
||||
fooarray = new NewFieldClass[24];
|
||||
for (int i = 0; i < fooarray.length; i++)
|
||||
fooarray[i] = new NewFieldClass(i);
|
||||
boo = new IncompatibleFieldClass();
|
||||
booarray = new IncompatibleFieldClass[24];
|
||||
for (int i = 0; i < booarray.length; i++)
|
||||
booarray[i] = new IncompatibleFieldClass();
|
||||
bar = 4;
|
||||
/* Excluded due to Bug 4089540
|
||||
abc = new NewExternFieldClass(66);
|
||||
farray = new NewExternFieldClass[10];
|
||||
for (int k = 0; k < farray.length; k++)
|
||||
farray[k] = new NewExternFieldClass(k);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
/** Test serial persistent fields w/o using Alternate API.
|
||||
*/
|
||||
class B implements Serializable {
|
||||
private static final long serialVersionUID = 2L;
|
||||
NewFieldClass foo;
|
||||
NewFieldClass[] array;
|
||||
IncompatibleFieldClass boo;
|
||||
IncompatibleFieldClass booarray[];
|
||||
transient NewExternFieldClass abc;
|
||||
transient NewExternFieldClass farray[];
|
||||
int bar;
|
||||
|
||||
B() {
|
||||
foo = new NewFieldClass(12);
|
||||
array = new NewFieldClass[12];
|
||||
for (int i = 0; i < array.length; i++)
|
||||
array[i] = new NewFieldClass(i);
|
||||
bar = 4;
|
||||
abc = new NewExternFieldClass(66);
|
||||
farray = new NewExternFieldClass[10];
|
||||
for (int k = 0; k < farray.length; k++)
|
||||
farray[k] = new NewExternFieldClass(k);
|
||||
}
|
||||
}
|
||||
|
||||
/** Test serial persistent fields using Alternate API.
|
||||
* Also make sure that optional data to non-existent classes can be skipped.
|
||||
*/
|
||||
class C implements Serializable {
|
||||
private static final long serialVersionUID = 3L;
|
||||
NewFieldClass foo;
|
||||
NewFieldClass[] array;
|
||||
int bar;
|
||||
transient NewExternFieldClass abc;
|
||||
transient NewExternFieldClass farray[];
|
||||
|
||||
C() {
|
||||
foo = new NewFieldClass(12);
|
||||
array = new NewFieldClass[12];
|
||||
for (int i = 0; i < array.length; i++)
|
||||
array[i] = new NewFieldClass(i);
|
||||
bar = 4;
|
||||
abc = new NewExternFieldClass(3);
|
||||
farray = new NewExternFieldClass[10];
|
||||
for (int k = 0; k < farray.length; k++)
|
||||
farray[k] = new NewExternFieldClass(k);
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream s)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
s.defaultReadObject();
|
||||
/* Excluded due to Bug 4089540
|
||||
abc = (NewExternFieldClass)fields.get("abc", null);
|
||||
farray = (NewExternFieldClass[])fields.get("farray", null);
|
||||
*/
|
||||
|
||||
/* read optional data */
|
||||
NewFieldClass tmpfoo = (NewFieldClass)s.readObject();
|
||||
NewFieldClass[] tmparray= (NewFieldClass[])s.readObject();
|
||||
int tmpbar = s.readInt();
|
||||
/* Excluded due to Bug 4089540
|
||||
NewExternFieldClass tmpabc = (NewExternFieldClass)s.readObject();
|
||||
NewExternFieldClass[] tmpfarray = (NewExternFieldClass[])s.readObject();
|
||||
*/
|
||||
}
|
||||
|
||||
private void writeObject(ObjectOutputStream s)
|
||||
throws IOException
|
||||
{
|
||||
s.defaultWriteObject();
|
||||
|
||||
/* write optional data */
|
||||
s.writeObject(foo);
|
||||
s.writeObject(array);
|
||||
s.writeInt(bar);
|
||||
|
||||
/* Excluded due to Bug 4089540
|
||||
s.writeObject(abc);
|
||||
s.writeObject(farray);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class WriteAddedField {
|
||||
public static void main(String args[]) throws IOException {
|
||||
A a = new A();
|
||||
B b = new B();
|
||||
C c = new C();
|
||||
File f = new File("tmp.ser");
|
||||
ObjectOutput out =
|
||||
new ObjectOutputStream(new FileOutputStream(f));
|
||||
out.writeObject(a);
|
||||
out.writeObject(b);
|
||||
out.writeObject(c);
|
||||
a = new A();
|
||||
b = new B();
|
||||
c = new C();
|
||||
out.writeObject(a);
|
||||
out.writeObject(b);
|
||||
out.writeObject(c);
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
31
test/jdk/java/io/Serializable/evolution/AddedField/run
Normal file
31
test/jdk/java/io/Serializable/evolution/AddedField/run
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#
|
||||
# Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
|
||||
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
#
|
||||
# This code is free software; you can redistribute it and/or modify it
|
||||
# under the terms of the GNU General Public License version 2 only, as
|
||||
# published by the Free Software Foundation.
|
||||
#
|
||||
# This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
# version 2 for more details (a copy is included in the LICENSE file that
|
||||
# accompanied this code).
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License version
|
||||
# 2 along with this work; if not, write to the Free Software Foundation,
|
||||
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
# or visit www.oracle.com if you need additional information or have any
|
||||
# questions.
|
||||
#
|
||||
|
||||
rm *.class tmp.ser
|
||||
javac WriteAddedField.java
|
||||
java WriteAddedField
|
||||
rm *.class
|
||||
javac ReadAddedField.java
|
||||
java ReadAddedField
|
||||
rm *.class
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#Here is the script I used to run this test. The "rm" are critical to
|
||||
#the test.
|
||||
java -version
|
||||
javac WriteAddedSuperClass.java
|
||||
java WriteAddedSuperClass
|
||||
rm A.class AddedSuperClass.class
|
||||
javac ReadAddedSuperClass.java
|
||||
java ReadAddedSuperClass
|
||||
rm A.class
|
||||
javac WriteAddedSuperClass.java
|
||||
java WriteAddedSuperClass
|
||||
rm A.class AddedSuperClass.class
|
||||
javac ReadAddedSuperClass2.java
|
||||
java ReadAddedSuperClass2
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @bug 4070080
|
||||
*
|
||||
* @build WriteAddedSuperClass ReadAddedSuperClass
|
||||
* @run main ReadAddedSuperClass
|
||||
* @summary Test reading an evolved class serialization into the original class
|
||||
* version. Class evolved by adding a superclass.
|
||||
* This is a compatible class evolution.
|
||||
*
|
||||
* Part a of test serializes an instance of an evolved class to a serialization stream.
|
||||
* Part b of test deserializes the serialization stream into an instance of
|
||||
* the original class.
|
||||
*
|
||||
* Description of failure:
|
||||
*
|
||||
* If you delete AddedSuperClass.class before running this test,
|
||||
* both JDK 1.1.x and 1.2 result in ClassNotFoundException for class
|
||||
* AddedSuperClass. If the .class file is not deleted, 1.2 does not
|
||||
* fail. However, JDK 1.1.4 core dumps dereferencing a null class handle
|
||||
* in the native method for inputClassDescriptor.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class A implements Serializable {
|
||||
// Version 1.0 of class A.
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
|
||||
public class ReadAddedSuperClass {
|
||||
public static void main(String args[]) throws IOException, ClassNotFoundException {
|
||||
File f = new File("tmp.ser");
|
||||
ObjectInput in =
|
||||
new ObjectInputStream(new FileInputStream(f));
|
||||
A a = (A)in.readObject();
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @bug 4070080
|
||||
*
|
||||
* @build WriteAddedSuperClass ReadAddedSuperClass2
|
||||
* @run main ReadAddedSuperClass2
|
||||
* @summary 2nd part of test deserializes the serialization stream into an
|
||||
* instance of the original class WHEN the AddedSuperClass exists
|
||||
* locally.
|
||||
*
|
||||
* Description of failure:
|
||||
*
|
||||
* If you delete AddedSuperClass.class before running this test,
|
||||
* both JDK 1.1.x and 1.2 result in ClassNotFoundException for class
|
||||
* AddedSuperClass. If the .class file is not deleted, 1.2 does not
|
||||
* fail. However, JDK 1.1.4 core dumps dereferencing a null class handle
|
||||
* in the native method for inputClassDescriptor.
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class AddedSuperClass implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Needed at least one field to recreate failure.
|
||||
int field;
|
||||
}
|
||||
|
||||
class A implements Serializable {
|
||||
// Version 1.0 of class A.
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
|
||||
public class ReadAddedSuperClass2 {
|
||||
public static void main(String args[]) throws IOException, ClassNotFoundException {
|
||||
File f = new File("tmp.ser");
|
||||
ObjectInput in =
|
||||
new ObjectInputStream(new FileInputStream(f));
|
||||
A a = (A)in.readObject();
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4070080
|
||||
*
|
||||
* @summary Test reading an evolved class serialization into the original class
|
||||
* version. Class evolved by adding a superclass.
|
||||
*
|
||||
* @clean A WriteAddedSuperClass ReadAddedSuperClass ReadAddedSuperClass2
|
||||
* @compile WriteAddedSuperClass.java
|
||||
* @run main WriteAddedSuperClass
|
||||
* @clean A AddedSuperClass
|
||||
* @compile ReadAddedSuperClass.java
|
||||
* @run main ReadAddedSuperClass
|
||||
* @clean A
|
||||
* @compile WriteAddedSuperClass.java
|
||||
* @run main WriteAddedSuperClass
|
||||
* @clean A AddedSuperClass
|
||||
* @compile ReadAddedSuperClass2.java
|
||||
* @run main ReadAddedSuperClass2
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Part a of test serializes an instance of an evolved class to a serialization stream.
|
||||
* Part b of test deserializes the serialization stream into an instance of
|
||||
* the original class.
|
||||
*
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
class AddedSuperClass implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// Needed at least one field to recreate failure.
|
||||
int field;
|
||||
}
|
||||
|
||||
class A extends AddedSuperClass implements Serializable {
|
||||
// Version 1.1 of class A. Added superclass NewSerializableSuper.
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
|
||||
public class WriteAddedSuperClass {
|
||||
public static void main(String args[]) throws IOException {
|
||||
A a = new A();
|
||||
File f = new File("tmp.ser");
|
||||
ObjectOutput out =
|
||||
new ObjectOutputStream(new FileOutputStream(f));
|
||||
out.writeObject(a);
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4087295 4785472
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.compiler.CompilerUtils
|
||||
* jdk.test.lib.Utils
|
||||
* jdk.test.lib.Asserts
|
||||
* jdk.test.lib.JDKToolFinder
|
||||
* jdk.test.lib.JDKToolLauncher
|
||||
* jdk.test.lib.Platform
|
||||
* jdk.test.lib.process.*
|
||||
* @build RenamePackageTest
|
||||
* @run main RenamePackageTest
|
||||
* @summary Enable resolveClass() to accommodate package renaming.
|
||||
* This fix enables one to implement a resolveClass method that maps a
|
||||
* Serialiazable class within a serialization stream to the same class
|
||||
* in a different package within the JVM runtime. See run shell script
|
||||
* for instructions on how to run this test.
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import jdk.test.lib.compiler.CompilerUtils;
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
|
||||
public class RenamePackageTest {
|
||||
public static void main(String args[]) throws Exception {
|
||||
setup();
|
||||
|
||||
runTestSerialDriver();
|
||||
runInstallSerialDriver();
|
||||
|
||||
runInstallSerialDriver();
|
||||
runTestSerialDriver();
|
||||
}
|
||||
|
||||
private static final Path SHARE = Paths.get(System.getProperty("test.classes"), "share");
|
||||
private static final Path OCLASSES = Paths.get(System.getProperty("test.classes"), "oclasses");
|
||||
private static final Path NCLASSES = Paths.get(System.getProperty("test.classes"), "nclasses");
|
||||
|
||||
private static void setup() throws Exception {
|
||||
|
||||
boolean b = CompilerUtils.compile(Paths.get(System.getProperty("test.src"), "extension"),
|
||||
SHARE);
|
||||
assertTrue(b);
|
||||
b = CompilerUtils.compile(Paths.get(System.getProperty("test.src"), "test"),
|
||||
OCLASSES,
|
||||
"-classpath",
|
||||
SHARE.toString());
|
||||
assertTrue(b);
|
||||
b = CompilerUtils.compile(Paths.get(System.getProperty("test.src"), "install"),
|
||||
NCLASSES,
|
||||
"-classpath",
|
||||
SHARE.toString());
|
||||
assertTrue(b);
|
||||
}
|
||||
|
||||
private static void runTestSerialDriver() throws Exception {
|
||||
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
|
||||
"-classpath",
|
||||
SHARE.toString()
|
||||
+ File.pathSeparator
|
||||
+ OCLASSES.toString(),
|
||||
"test.SerialDriver", "-s");
|
||||
Process p = ProcessTools.startProcess("test SerialDriver", pb);
|
||||
p.waitFor();
|
||||
assertTrue(p.exitValue() == 0);
|
||||
}
|
||||
|
||||
private static void runInstallSerialDriver() throws Exception {
|
||||
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
|
||||
"-classpath",
|
||||
SHARE.toString()
|
||||
+ File.pathSeparator
|
||||
+ NCLASSES.toString(),
|
||||
"install.SerialDriver", "-d");
|
||||
Process p = ProcessTools.startProcess("install SerialDriver", pb);
|
||||
p.waitFor();
|
||||
assertTrue(p.exitValue() == 0);
|
||||
}
|
||||
|
||||
private static void assertTrue(boolean b) {
|
||||
if (!b) {
|
||||
throw new RuntimeException("expected true, get false");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package extension;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.io.*;
|
||||
|
||||
public class ExtendedObjectInputStream extends ObjectInputStream {
|
||||
|
||||
private static Hashtable renamedClassMap;
|
||||
|
||||
public ExtendedObjectInputStream(InputStream si)
|
||||
throws IOException, StreamCorruptedException
|
||||
{
|
||||
super(si);
|
||||
}
|
||||
|
||||
protected Class resolveClass(ObjectStreamClass v)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
if (renamedClassMap != null) {
|
||||
// System.out.println("resolveClass(" + v.getName() + ")");
|
||||
Class newClass = (Class)renamedClassMap.get(v.getName());
|
||||
if (newClass != null) {
|
||||
v = ObjectStreamClass.lookup(newClass);
|
||||
}
|
||||
}
|
||||
return super.resolveClass(v);
|
||||
}
|
||||
|
||||
public static void addRenamedClassName(String oldName, String newName)
|
||||
throws ClassNotFoundException
|
||||
{
|
||||
Class cl = null;
|
||||
|
||||
if (renamedClassMap == null)
|
||||
renamedClassMap = new Hashtable(10);
|
||||
if (newName.startsWith("[L")) {
|
||||
// System.out.println("Array processing");
|
||||
Class componentType =
|
||||
Class.forName(newName.substring(2));
|
||||
//System.out.println("ComponentType=" + componentType.getName());
|
||||
Object dummy =
|
||||
java.lang.reflect.Array.newInstance(componentType, 3);
|
||||
|
||||
cl = dummy.getClass();
|
||||
// System.out.println("Class=" + cl.getName());
|
||||
}
|
||||
else
|
||||
cl = Class.forName(newName);
|
||||
//System.out.println("oldName=" + oldName +
|
||||
// " newName=" + cl.getName());
|
||||
renamedClassMap.put(oldName, cl);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* @bug 4087295
|
||||
* @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
|
||||
* @summary Enable resolveClass() to accommodate package renaming.
|
||||
* This fix enables one to implement a resolveClass method that maps a
|
||||
* Serializable class within a serialization stream to the same class
|
||||
* in a different package within the JVM runtime. See run shell script
|
||||
* for instructions on how to run this test.
|
||||
*/
|
||||
|
||||
package install;
|
||||
|
||||
import java.io.*;
|
||||
import extension.ExtendedObjectInputStream;
|
||||
|
||||
public class SerialDriver implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
String name;
|
||||
SerialDriver next;
|
||||
transient Object objarray[];
|
||||
|
||||
public SerialDriver() {
|
||||
name = "<terminator>";
|
||||
next = null;
|
||||
}
|
||||
|
||||
public SerialDriver(String name, SerialDriver next) {
|
||||
this.name = name;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
static boolean serialize;
|
||||
static boolean deserialize;
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
SerialDriver obj = new SerialDriver("SerialDriver_2",
|
||||
new SerialDriver());
|
||||
SerialDriver[] array = new SerialDriver[5];
|
||||
for (int i = 0; i < array.length; i++)
|
||||
array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());
|
||||
|
||||
/*
|
||||
* see if we are serializing or deserializing.
|
||||
* The ability to deserialize or serialize allows
|
||||
* us to see the bidirectional readability and writeability
|
||||
*/
|
||||
if (args.length == 1) {
|
||||
if (args[0].equals("-d")) {
|
||||
deserialize = true;
|
||||
} else if (args[0].equals("-s")) {
|
||||
serialize = true;
|
||||
} else {
|
||||
usage();
|
||||
throw new Exception("incorrect command line arguments");
|
||||
}
|
||||
} else {
|
||||
usage();
|
||||
throw new Exception("incorrect command line arguments");
|
||||
}
|
||||
|
||||
File f = new File("stream.ser");
|
||||
if (serialize) {
|
||||
// Serialize the subclass
|
||||
try (FileOutputStream fo = new FileOutputStream(f);
|
||||
ObjectOutputStream so = new ObjectOutputStream(fo))
|
||||
{
|
||||
so.writeObject(obj);
|
||||
/* Skip arrays since they do not work with rename yet.
|
||||
The serialVersionUID changes due to the name change
|
||||
and there is no way to set the serialVersionUID for an
|
||||
array. */
|
||||
so.writeObject(array);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
if (deserialize) {
|
||||
// Deserialize the subclass
|
||||
try (FileInputStream fi = new FileInputStream(f);
|
||||
ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
|
||||
{
|
||||
si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");
|
||||
si.addRenamedClassName("[Ltest.SerialDriver;",
|
||||
"[Linstall.SerialDriver");
|
||||
obj = (SerialDriver) si.readObject();
|
||||
array = (SerialDriver[]) si.readObject();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
throw e;
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("Printing deserialized class: ");
|
||||
System.out.println();
|
||||
System.out.println(obj.toString());
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
String nextString = next != null ? next.toString() : "<null>";
|
||||
return "name =" + name + " next = <" + nextString + ">";
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the usage
|
||||
*/
|
||||
static void usage() {
|
||||
System.out.println("Usage:");
|
||||
System.out.println(" -s (in order to serialize)");
|
||||
System.out.println(" -d (in order to deserialize)");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* @bug 4087295
|
||||
* @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
|
||||
* @summary Enable resolveClass() to accommodate package renaming.
|
||||
* This fix enables one to implement a resolveClass method that maps a
|
||||
* Serializable class within a serialization stream to the same class
|
||||
* in a different package within the JVM runtime. See run shell script
|
||||
* for instructions on how to run this test.
|
||||
*/
|
||||
|
||||
package test;
|
||||
|
||||
import java.io.*;
|
||||
import extension.ExtendedObjectInputStream;
|
||||
|
||||
public class SerialDriver implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
String name;
|
||||
SerialDriver next;
|
||||
|
||||
public SerialDriver() {
|
||||
name = "<terminator>";
|
||||
next = null;
|
||||
}
|
||||
|
||||
public SerialDriver(String name, SerialDriver next) {
|
||||
this.name = name;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
static boolean serialize = false;
|
||||
static boolean deserialize = false;
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
SerialDriver obj = new SerialDriver("SerialDriver_1", new SerialDriver());
|
||||
SerialDriver[] array = new SerialDriver[5];
|
||||
for (int i = 0; i < array.length; i++)
|
||||
array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());
|
||||
|
||||
/*
|
||||
* see if we are serializing or deserializing.
|
||||
* The ability to deserialize or serialize allows
|
||||
* us to see the bidirectional readability and writeability
|
||||
*/
|
||||
if (args.length == 1) {
|
||||
if (args[0].equals("-d")) {
|
||||
deserialize = true;
|
||||
} else if (args[0].equals("-s")) {
|
||||
serialize = true;
|
||||
} else {
|
||||
usage();
|
||||
throw new Exception("incorrect command line arguments");
|
||||
}
|
||||
} else {
|
||||
usage();
|
||||
throw new Exception("incorrect command line arguments");
|
||||
}
|
||||
|
||||
File f = new File("stream.ser");
|
||||
if (serialize) {
|
||||
// Serialize the subclass
|
||||
try (FileOutputStream fo = new FileOutputStream(f);
|
||||
ObjectOutputStream so = new ObjectOutputStream(fo))
|
||||
{
|
||||
so.writeObject(obj);
|
||||
/* Comment out since renaming arrays does not work
|
||||
since it changes the serialVersionUID. */
|
||||
so.writeObject(array);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
if (deserialize) {
|
||||
// Deserialize the subclass
|
||||
try (FileInputStream fi = new FileInputStream(f);
|
||||
ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
|
||||
{
|
||||
si.addRenamedClassName("install.SerialDriver",
|
||||
"test.SerialDriver");
|
||||
si.addRenamedClassName("[Linstall.SerialDriver;",
|
||||
"[Ltest.SerialDriver");
|
||||
obj = (SerialDriver) si.readObject();
|
||||
array = (SerialDriver[]) si.readObject();
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
throw e;
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("Printing deserialized class: ");
|
||||
System.out.println();
|
||||
System.out.println(obj.toString());
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
String nextString = next != null ? next.toString() : "<null>";
|
||||
return "name =" + name + " next = <" + nextString + ">";
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the usage
|
||||
*/
|
||||
static void usage() {
|
||||
System.out.println("Usage:");
|
||||
System.out.println(" -s (in order to serialize)");
|
||||
System.out.println(" -d (in order to deserialize)");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue