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

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

    I use a findbugs-exclude Filter for collection-Fields:

    <Match>
        <Field type="java.util.Map" />
        <Bug pattern="SE_BAD_FIELD" />
    </Match>
    <Match>
        <Field type="java.util.Set" />
        <Bug pattern="SE_BAD_FIELD" />
    </Match>
    <Match>
        <Field type="java.util.List" />
        <Bug pattern="SE_BAD_FIELD" />
    </Match>
    

    See http://findbugs.sourceforge.net/manual/filter.html

    0 讨论(0)
  • 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<Integer> mySet;
    
        public TestClass(Set<Integer> s) {
            super();
            setMySet(s);
        }
    
        public void setMySet(Set<Integer> s) {
            mySet = (s == null) ? new HashSet<>() : new HashSet<>(s);
        }
    }
    
    0 讨论(0)
提交回复
热议问题