将Java对象序列化为Java代码?

And*_*tin 10 java serialization code-generation

是否有将Java对象序列化为Java代码的实现?例如,如果我有对象

Map<String,Integer> m = new Map<String,Integer>();
m.put("foo",new Integer(21));
Run Code Online (Sandbox Code Playgroud)

我可以使用它来序列化

ObjectOutputStream out = new ObjectOutputStream( ... );
out.writeObject( m );
out.flush();
Run Code Online (Sandbox Code Playgroud)

例如,输出就是

java.util.Map<String,Integer> m = new java.util.Map<String,Integer>(); 
m.put("foo",new Integer(21));
Run Code Online (Sandbox Code Playgroud)

你为什么要这个?有时,以编程方式部分创建复杂对象更容易,然后在代码中手动完成创建.然后可以将此代码包含在源代码中,并使用其他所有内容控制版本.请注意,使用外部序列化对象不是例外.

谢谢你提供的所有帮助.

Swa*_*rma 1

可以实现对象的自定义序列化。您必须在类中使用确切的签名实现两个方法:

private void writeObject(ObjectOutputStream oos)
{
    //write your serialization code here
}


private void readObject(ObjectInputStream ois)
{
    //write your de-serialization code here
}
Run Code Online (Sandbox Code Playgroud)

然而,您所寻求的灵活性是非常值得怀疑的。