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

前端 未结 8 1377
没有蜡笔的小新
没有蜡笔的小新 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:36

    Use a concrete Serializable set for your internal representation, but make any public interfaces use the Set interface.

    public class TestClass implements Serializable {
        private static final long serialVersionUID = 1905162041950251407L;
    
        private HashSet mySet;
    
        public TestClass(Set s) {
            super();
            setMySet(s);
        }
    
        public void setMySet(Set s) {
            mySet = (s == null) ? new HashSet<>() : new HashSet<>(s);
        }
    }
    

提交回复
热议问题