📄 serializablechecker.java
字号:
{ throw new WicketNotSerializableException( toPrettyPrintedStack(obj.getClass().getName()), exception); } ObjectStreamClass desc; for (;;) { try { desc = (ObjectStreamClass)LOOKUP_METHOD.invoke(null, new Object[] { cls, Boolean.TRUE }); Class repCl; if (!((Boolean)HAS_WRITE_REPLACE_METHOD_METHOD.invoke(desc, null)).booleanValue() || (obj = INVOKE_WRITE_REPLACE_METHOD.invoke(desc, new Object[] { obj })) == null || (repCl = obj.getClass()) == cls) { break; } cls = repCl; } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } if (cls.isPrimitive()) { // skip } else if (cls.isArray()) { checked.put(obj, null); Class ccl = cls.getComponentType(); if (!(ccl.isPrimitive())) { Object[] objs = (Object[])obj; for (int i = 0; i < objs.length; i++) { String arrayPos = "[" + i + "]"; simpleName = arrayPos; fieldDescription += arrayPos; check(objs[i]); } } } else if (obj instanceof Externalizable && (!Proxy.isProxyClass(cls))) { Externalizable extObj = (Externalizable)obj; try { extObj.writeExternal(new ObjectOutputAdaptor() { private int count = 0; public void writeObject(Object streamObj) throws IOException { // Check for circular reference. if (checked.containsKey(streamObj)) { return; } checked.put(streamObj, null); String arrayPos = "[write:" + count++ + "]"; simpleName = arrayPos; fieldDescription += arrayPos; check(streamObj); } }); } catch (Exception e) { if (e instanceof WicketNotSerializableException) { throw (WicketNotSerializableException)e; } log.warn("error delegating to Externalizable : " + e.getMessage() + ", path: " + currentPath()); } } else { Method writeObjectMethod = null; Object o = writeObjectMethodCache.get(cls); if (o != null) { if (o instanceof Method) { writeObjectMethod = (Method)o; } } else { try { writeObjectMethod = cls.getDeclaredMethod("writeObject", new Class[] { java.io.ObjectOutputStream.class }); } catch (SecurityException e) { // we can't access/ set accessible to true writeObjectMethodCache.put(cls, Boolean.FALSE); } catch (NoSuchMethodException e) { // cls doesn't have that method writeObjectMethodCache.put(cls, Boolean.FALSE); } } final Object original = obj; if (writeObjectMethod != null) { class InterceptingObjectOutputStream extends ObjectOutputStream { private int counter; InterceptingObjectOutputStream() throws IOException { super(DUMMY_OUTPUT_STREAM); enableReplaceObject(true); } protected Object replaceObject(Object streamObj) throws IOException { if (streamObj == original) { return streamObj; } counter++; // Check for circular reference. if (checked.containsKey(streamObj)) { return null; } checked.put(original, null); String arrayPos = "[write:" + counter + "]"; simpleName = arrayPos; fieldDescription += arrayPos; check(streamObj); return streamObj; } } try { InterceptingObjectOutputStream ioos = new InterceptingObjectOutputStream(); ioos.writeObject(obj); } catch (Exception e) { if (e instanceof WicketNotSerializableException) { throw (WicketNotSerializableException)e; } log.warn("error delegating to writeObject : " + e.getMessage() + ", path: " + currentPath()); } } else { Object[] slots; try { slots = (Object[])GET_CLASS_DATA_LAYOUT_METHOD.invoke(desc, null); } catch (Exception e) { throw new RuntimeException(e); } for (int i = 0; i < slots.length; i++) { ObjectStreamClass slotDesc; try { Field descField = slots[i].getClass().getDeclaredField("desc"); descField.setAccessible(true); slotDesc = (ObjectStreamClass)descField.get(slots[i]); } catch (Exception e) { throw new RuntimeException(e); } checked.put(obj, null); checkFields(obj, slotDesc); } } } traceStack.removeLast(); nameStack.removeLast(); } private void checkFields(Object obj, ObjectStreamClass desc) { int numFields; try { numFields = ((Integer)GET_NUM_OBJ_FIELDS_METHOD.invoke(desc, null)).intValue(); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } if (numFields > 0) { int numPrimFields; ObjectStreamField[] fields = desc.getFields(); Object[] objVals = new Object[numFields]; numPrimFields = fields.length - objVals.length; try { GET_OBJ_FIELD_VALUES_METHOD.invoke(desc, new Object[] { obj, objVals }); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } for (int i = 0; i < objVals.length; i++) { if (objVals[i] instanceof String || objVals[i] instanceof Number || objVals[i] instanceof Date || objVals[i] instanceof Boolean || objVals[i] instanceof Class) { // filter out common cases continue; } // Check for circular reference. if (checked.containsKey(objVals[i])) { continue; } ObjectStreamField fieldDesc = fields[numPrimFields + i]; Field field; try { field = (Field)GET_FIELD_METHOD.invoke(fieldDesc, null); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } String fieldName = field.getName(); simpleName = field.getName(); fieldDescription = field.toString(); check(objVals[i]); } } } /** * @return name from root to current node concatenated with slashes */ private StringBuffer currentPath() { StringBuffer b = new StringBuffer(); for (Iterator it = nameStack.iterator(); it.hasNext();) { b.append(it.next()); if (it.hasNext()) { b.append('/'); } } return b; } /** * Dump with indentation. * * @param type * the type that couldn't be serialized * @return A very pretty dump */ private final String toPrettyPrintedStack(String type) { StringBuffer result = new StringBuffer(); StringBuffer spaces = new StringBuffer(); result.append("Unable to serialize class: "); result.append(type); result.append("\nField hierarchy is:"); for (Iterator i = traceStack.listIterator(); i.hasNext();) { spaces.append(" "); TraceSlot slot = (TraceSlot)i.next(); result.append("\n").append(spaces).append(slot.fieldDescription); result.append(" [class=").append(slot.object.getClass().getName()); if (slot.object instanceof Component) { Component component = (Component)slot.object; result.append(", path=").append(component.getPath()); } result.append("]"); } result.append(" <----- field that is not serializable"); return result.toString(); } /** * @see java.io.ObjectOutputStream#writeObjectOverride(java.lang.Object) */ protected final void writeObjectOverride(Object obj) throws IOException { if (!available) { return; } root = obj; if (fieldDescription == null) { fieldDescription = (root instanceof Component) ? ((Component)root).getPath() : ""; } check(root); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -