import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class HelperClass implements InstancePattern
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int i = 666;
    
    interface InstancePattern extends Serializable
    {
        default Object readResolve()
        {
            // return ReflectionUtils.getStaticInstance(this);
            System.err.println("** read resolve got called: NO BUG!");
            return this; // for testing the bug - this does NOT get called
        }
    }
    
    public static void main(String[] args)
    {
        serializeAndDeserialize(new HelperClass());
        System.err.println(
                "If there is no message above saying 'NO BUG' then you have seen the bug.");
    }
    
    private static Serializable serializeAndDeserialize(Serializable object)
    {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream;
        
        try
        {
            objectOutputStream = new ObjectOutputStream(bytes);
            objectOutputStream.writeObject(object);
            objectOutputStream.close();
            
            byte[] actualBytes = bytes.toByteArray();
            ByteArrayInputStream byteStream = new ByteArrayInputStream(actualBytes);
            
            ObjectInputStream objectInputStream = new ObjectInputStream(byteStream);
            return (Serializable) objectInputStream.readObject();
        }
        catch (IOException | ClassNotFoundException ex)
        {
            throw new AssertionError("Something exceptional", ex);
        }
    }
}
