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

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

    In case you are using findbugs-maven-plugin and have to persist a field, and that field is a class not implementing Serializable interface, for example, a field that has a class defined in a 3rd party. You can manually configure exclude file for findbugs,

    If this is the only case, add it in an exclude file: pom:

    
        org.codehaus.mojo
        findbugs-maven-plugin
        3.0.3
        
              true
              target/findbugs/
              findbugs-exclude.xml
              findbugs-include.xml
              true
        
    ...
    

    exclude.xml:

    
    
        
             
            
        
    

    Entity:

    @Entity
    public class Foo extends Boo {
        StateMachineContext stateMachineContext;
    

    Although I don't understand why adding would not work. Besides, I don't agree with the solution of adding annotation on the field like @edu.umd.cs.findbugs.annotations.SuppressWarnings(justification="No bug", values="SE_BAD_FIELD"), because building tools better not penetrate business code.maven plugin usage & findbugs filters both include and exclude

    About SE_BAD_FIELD: Non-transient non-serializable instance field in serializable class, I think it should not check on entities. Because, javax.persistence.AttributeConverter offers methods to serialize a field out side (implements Serializable is an inside method to serialize).

提交回复
热议问题