How to handle a Findbugs “Non-transient non-serializable instance field in serializable class”?

前端 未结 8 1382
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 02:03

Consider the class below. If I run Findbugs against it it will give me an error (\"Non-transient non-serializable instance field in serializable class\") on line 5 but not o

8条回答
  •  一生所求
    2020-12-24 02:21

    You could use a capture helper to ensure that a passed in Set supports two interfaces:

    private static class SerializableTestClass & Serializable> implements Serializable
    {
        private static final long serialVersionUID = 1L;
        private final T serializableSet;
    
        private SerializableTestClass(T serializableSet)
        {
            this.serializableSet = serializableSet;
        }
    }
    
    public static class PublicApiTestClass
    {
        public static  & Serializable> Serializable forSerializableSet(T set)
        {
            return new SerializableTestClass(set);
        }
    }
    

    In this way you can have a public API that enforces Serializable without checking/requiring specific implementation details.

提交回复
热议问题