这9个单例被破坏的事故现场,你遇到过几个? 评论区见 (6)

file

大家一定会想:这是什么原因呢?为什么要这样写?看上去很神奇的样子,也让人有些费解。不如一起来看JDK的源码实现以了解清楚。进入ObjectInputStream类的readObject()方法,代码如下。

public final Object readObject() throws IOException, ClassNotFoundException { if (enableOverride) { return readObjectOverride(); } int outerHandle = passHandle; try { Object obj = readObject0(false); handles.markDependency(outerHandle, passHandle); ClassNotFoundException ex = handles.lookupException(passHandle); if (ex != null) { throw ex; } if (depth == 0) { vlist.doCallbacks(); } return obj; } finally { passHandle = outerHandle; if (closed && depth == 0) { clear(); } } }

可以看到,在readObject()方法中又调用了重写的readObject0()方法。进入readObject0()方法,源码如下。

private Object readObject0(boolean unshared) throws IOException { ... case TC_OBJECT: return checkResolve(readOrdinaryObject(unshared)); ... }

我们看到TC_OBJECT中调用了ObjectInputStream的readOrdinaryObject()方法,源码如下。

private Object readOrdinaryObject(boolean unshared) throws IOException { if (bin.readByte() != TC_OBJECT) { throw new InternalError(); } ObjectStreamClass desc = readClassDesc(false); desc.checkDeserialize(); Class<?> cl = desc.forClass(); if (cl == String.class || cl == Class.class || cl == ObjectStreamClass.class) { throw new InvalidClassException("invalid class descriptor"); } Object obj; try { obj = desc.isInstantiable() ? desc.newInstance() : null; } catch (Exception ex) { throw (IOException) new InvalidClassException( desc.forClass().getName(), "unable to create instance").initCause(ex); } ... return obj; }

我们发现调用了ObjectStreamClass的isInstantiable()方法,而isInstantiable()方法的源码如下。

boolean isInstantiable() { requireInitialized(); return (cons != null); }

上述代码非常简单,就是判断一下构造方法是否为空。如果构造方法不为空,则返回true。这意味着只要有无参构造方法就会实例化。
这时候其实还没有找到加上readResolve()方法就可以避免单例模式被破坏的真正原因。再回到ObjectInputStream的readOrdinaryObject()方法,继续往下看源码。

private Object readOrdinaryObject(boolean unshared) throws IOException { if (bin.readByte() != TC_OBJECT) { throw new InternalError(); } ObjectStreamClass desc = readClassDesc(false); desc.checkDeserialize(); Class<?> cl = desc.forClass(); if (cl == String.class || cl == Class.class || cl == ObjectStreamClass.class) { throw new InvalidClassException("invalid class descriptor"); } Object obj; try { obj = desc.isInstantiable() ? desc.newInstance() : null; } catch (Exception ex) { throw (IOException) new InvalidClassException( desc.forClass().getName(), "unable to create instance").initCause(ex); } ... if (obj != null && handles.lookupException(passHandle) == null && desc.hasReadResolveMethod()) { Object rep = desc.invokeReadResolve(obj); if (unshared && rep.getClass().isArray()) { rep = cloneArray(rep); } if (rep != obj) { if (rep != null) { if (rep.getClass().isArray()) { filterCheck(rep.getClass(), Array.getLength(rep)); } else { filterCheck(rep.getClass(), -1); } } handles.setObject(passHandle, obj = rep); } } return obj; }

在判断无参构造方法是否存在之后,又调用了hasReadResolveMethod()方法,源码如下。

boolean hasReadResolveMethod() { requireInitialized(); return (readResolveMethod != null); }

上述代码的逻辑非常简单,就是判断readResolveMethod是否为空,如果不为空,则返回true。那么readResolveMethod是在哪里被赋值的呢?通过全局查找知道,在私有方法ObjectStreamClass()中对readResolveMethod进行了赋值,源码如下。

readResolveMethod = getInheritableMethod( cl, "readResolve", null, Object.class);

上面的逻辑其实就是通过反射找到一个无参的readResolve()方法,并且保存下来。再回到ObjectInputStream的readOrdinaryObject()方法,继续往下看,如果readResolve()方法存在,则调用invokeReadResolve()方法,代码如下。

Object invokeReadResolve(Object obj) throws IOException, UnsupportedOperationException { requireInitialized(); if (readResolveMethod != null) { try { return readResolveMethod.invoke(obj, (Object[]) null); } catch (InvocationTargetException ex) { Throwable th = ex.getTargetException(); if (th instanceof ObjectStreamException) { throw (ObjectStreamException) th; } else { throwMiscException(th); throw new InternalError(th); } } catch (IllegalAccessException ex) { throw new InternalError(ex); } } else { throw new UnsupportedOperationException(); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjzzw.html