Serializer { public void writeObject(Serializable serializable) { if (serializable == null) { writeByte(SerializationConstants.TC_NULL); return; } else if (serializable instanceof Long) { writeByte(SerializationConstants.TC_LONG); writeLong((Long)serializable); return; } else if (serializable instanceof String) { writeByte(SerializationConstants.TC_STRING); writeString((String)serializable); return; } else if (serializable instanceof Integer) { writeByte(SerializationConstants.TC_INTEGER); writeInt((Integer)serializable); return; } else if (serializable instanceof Boolean) { writeByte(SerializationConstants.TC_BOOLEAN); writeBoolean((Boolean)serializable); return; } else if (serializable instanceof Class) { Class<?> clazz = (Class<?>)serializable; String contextName = ClassLoaderPool.getContextName( clazz.getClassLoader()); writeByte(SerializationConstants.TC_CLASS); writeString(contextName); writeString(clazz.getName()); return; } else if (serializable instanceof Short) { writeByte(SerializationConstants.TC_SHORT); writeShort((Short)serializable); return; } else if (serializable instanceof Character) { writeByte(SerializationConstants.TC_CHARACTER); writeChar((Character)serializable); return; } else if (serializable instanceof Byte) { writeByte(SerializationConstants.TC_BYTE); writeByte((Byte)serializable); return; } else if (serializable instanceof Double) { writeByte(SerializationConstants.TC_DOUBLE); writeDouble((Double)serializable); return; } else if (serializable instanceof Float) { writeByte(SerializationConstants.TC_FLOAT); writeFloat((Float)serializable); return; } else { writeByte(SerializationConstants.TC_OBJECT); } try { ObjectOutputStream objectOutputStream = new AnnotatedObjectOutputStream(new BufferOutputStream()); objectOutputStream.writeObject(serializable); objectOutputStream.flush(); } catch (IOException ioException) { throw new RuntimeException( "Unable to write ordinary serializable object " + serializable, ioException); } } Serializer(); ByteBuffer toByteBuffer(); void writeBoolean(boolean b); void writeByte(byte b); void writeChar(char c); void writeDouble(double d); void writeFloat(float f); void writeInt(int i); void writeLong(long l); void writeObject(Serializable serializable); void writeShort(short s); void writeString(String s); void writeTo(OutputStream outputStream);  }
@Test public void testWriteObjectOrdinaryNPE() throws Exception { Serializer serializer = new Serializer(); Serializable serializable = new Serializable() { private void writeObject(ObjectOutputStream objectOutputStream) throws IOException { throw new IOException("Forced IOException"); } }; try { serializer.writeObject(serializable); Assert.fail(); } catch (RuntimeException runtimeException) { String message = runtimeException.getMessage(); Assert.assertTrue( message.startsWith( "Unable to write ordinary serializable object ")); Throwable throwable = runtimeException.getCause(); Assert.assertTrue(throwable instanceof IOException); Assert.assertEquals("Forced IOException", throwable.getMessage()); } }