反序列化后瞬态字段变为空?
Transient fields become null after deserialization?
我目前正在使用 JAVA 的内置序列化程序 java.io.Serializable
,但似乎无法将其反序列化并将我的字段设置为默认值。我什至尝试使用 readObject
来初始化字段,但它不起作用。
这是我的代码:
public abstract class BossQuest implements Quest,
Listenable { //Quest(interface) extends Serializable
private transient Status status = Status.IDLE; //This does not work
private String name;
public BossQuest(String name) {
this.name = name;
initialize();
}
//This also does not work
public BossQuest() {
this.status = Status.IDLE;
}
//This was my hacky attempt to forcefully set default values (Does not work)
public void initialize() {
status = Status.IDLE;
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
initialize();
}
public Status getStatus() {
System.out.print("Requesting status!");
if (status == null) {
System.out.print("Status is.. null..?");
}
System.out.print(status);
return status;
}
}
在我上面的代码中,Quest 扩展了 Serializable
唯一一次状态为空是在我反序列化文件之后。它不能通过任何修改器设置为 null。
只需使用 readResolve() 将状态初始化为 IDLE。
这是一个完整的例子:
public class BossQuest implements Serializable {
private String name;
private transient int status = 42;
public BossQuest(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
private Object readResolve() {
this.status = 42;
return this;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
BossQuest in = new BossQuest("test");
in.setStatus(987);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(in);
oos.close();
byte[] b = baos.toByteArray();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b));
BossQuest out = (BossQuest) ois.readObject();
System.out.println(out.getName()); // prints test
System.out.println(out.getStatus()); // prints 42
}
}
我目前正在使用 JAVA 的内置序列化程序 java.io.Serializable
,但似乎无法将其反序列化并将我的字段设置为默认值。我什至尝试使用 readObject
来初始化字段,但它不起作用。
这是我的代码:
public abstract class BossQuest implements Quest,
Listenable { //Quest(interface) extends Serializable
private transient Status status = Status.IDLE; //This does not work
private String name;
public BossQuest(String name) {
this.name = name;
initialize();
}
//This also does not work
public BossQuest() {
this.status = Status.IDLE;
}
//This was my hacky attempt to forcefully set default values (Does not work)
public void initialize() {
status = Status.IDLE;
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
initialize();
}
public Status getStatus() {
System.out.print("Requesting status!");
if (status == null) {
System.out.print("Status is.. null..?");
}
System.out.print(status);
return status;
}
}
在我上面的代码中,Quest 扩展了 Serializable
唯一一次状态为空是在我反序列化文件之后。它不能通过任何修改器设置为 null。
只需使用 readResolve() 将状态初始化为 IDLE。
这是一个完整的例子:
public class BossQuest implements Serializable {
private String name;
private transient int status = 42;
public BossQuest(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
private Object readResolve() {
this.status = 42;
return this;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
BossQuest in = new BossQuest("test");
in.setStatus(987);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(in);
oos.close();
byte[] b = baos.toByteArray();
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b));
BossQuest out = (BossQuest) ois.readObject();
System.out.println(out.getName()); // prints test
System.out.println(out.getStatus()); // prints 42
}
}