}
} finally {
if (extendedDebugInfo) {
debugInfoStack.pop();
}
}
}
然后我们先看看writeClassDesc(desc, false)的实现:
[java]
private void writeClassDesc(ObjectStreamClass desc, boolean unshared)
throws IOException
{
int handle;
if (desc == null) {
writeNull();
} else if (!unshared && (handle = handles.lookup(desc)) != -1) {
writeHandle(handle);
} else if (desc.isProxy()) { //如果是proxy对象,则调用该序列化机制 ,isProxy的判断
//isProxy = Proxy.isProxyClass(cl); Returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.
writeProxyDesc(desc, unshared);
} else {
writeNonProxyDesc(desc, unshared);
}
}
/**
* Writes class descriptor representing a dynamic proxy class to stream.
*/
private void writeProxyDesc(ObjectStreamClass desc, boolean unshared)
throws IOException
{
bout.writeByte(TC_PROXYCLASSDESC); //写入代理对象的标示符
handles.assign(unshared null : desc);
Class cl = desc.forClass();
Class[] ifaces = cl.getInterfaces(); //如果是proxy对象,则写入对象的interfaces的名称
bout.writeInt(ifaces.length); //先写入interface个数
for (int i = 0; i < ifaces.length; i++) {
bout.writeUTF(ifaces[i].getName()); //再写入每个interface的名称
}
bout.setBlockDataMode(true);
annotateProxyClass(cl);
bout.setBlockDataMode(false);
bout.writeByte(TC_ENDBLOCKDATA); //结束标签
writeClassDesc(desc.getSuperDesc(), false);//递归写入父类的序列化信息,因为java是单继承,
}
/**
* Writes class descriptor representing a standard (i.e., not a dynamic
* proxy) class to stream.
*/
private void writeNonProxyDesc(ObjectStreamClass desc, boolean unshared)
throws IOException
{
bout.writeByte(TC_CLASSDESC);//写入class对象的标示符
handles.assign(unshared null : desc);
if (protocol == PROTOCOL_VERSION_1) { //如果非代理类对象的具体class信息,查看下面的方法
// do not invoke class descriptor write hook with old protocol
desc.writeNonProxy(this);
} else {
writeClassDescriptor(desc);
}
Class cl = desc.forClass();
bout.setBlockDataMode(true);
annotateClass(cl);
bout.setBlockDataMode(false);
bout.writeByte(TC_ENDBLOCKDATA);
writeClassDesc(desc.getSuperDesc(), false);//递归写入父类的序列化信息,因为java是单继承,
}
writeClassDescriptor(desc)方法:
[java]
throws IOException
{
desc.writeNonProxy(this);
}
void writeNonProxy(ObjectOutputStream out) throws IOException {
out.writeUTF(name);//写入类的名称
out.writeLong(getSerialVersionUID());//写入类的SerialVersionUID
byte flags = 0;
if (externalizable) {//是否实现externalizable接口
flags |= ObjectStreamConstants.SC_EXTERNALIZABLE;
int protocol = out.getProtocolVersion();
if (protocol != ObjectStreamConstants.PROTOCOL_VERSION_1) {
flags |= ObjectStreamConstants.SC_BLOCK_DATA;
}
} else if (serializable) {//是否实现serializable接口
flags |= ObjectStreamConstants.SC_SERIALIZABLE;
}
if (hasWriteOb