Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11276 > unrolled thread

FindBugs complaining about non-serializable field although everything looks Serializable

Started by"laredotornado@zipmail.com" <laredotornado@gmail.com>
First post2012-01-12 08:40 -0800
Last post2012-01-15 10:18 -0800
Articles 10 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  FindBugs complaining about non-serializable field although everything looks Serializable "laredotornado@zipmail.com" <laredotornado@gmail.com> - 2012-01-12 08:40 -0800
    Re: FindBugs complaining about non-serializable field although everything looks Serializable Jeff Higgins <jeff@invalid.invalid> - 2012-01-12 12:04 -0500
      Re: FindBugs complaining about non-serializable field although everything looks Serializable "laredotornado@zipmail.com" <laredotornado@gmail.com> - 2012-01-12 09:48 -0800
        Re: FindBugs complaining about non-serializable field although everything looks Serializable Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-12 09:52 -0800
    Re: FindBugs complaining about non-serializable field although everything looks Serializable Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-12 09:52 -0800
    Re: FindBugs complaining about non-serializable field although everything looks Serializable Ian Shef <invalid@avoiding.spam> - 2012-01-12 19:29 +0000
      Re: FindBugs complaining about non-serializable field although everything looks Serializable Lew <noone@lewscanon.com> - 2012-01-12 21:10 -0800
        Re: FindBugs complaining about non-serializable field although everything looks Serializable Lew <noone@lewscanon.com> - 2012-01-12 21:11 -0800
        Re: FindBugs complaining about non-serializable field although everything looks Serializable Arne Vajhøj <arne@vajhoej.dk> - 2012-01-14 23:23 -0500
          Re: FindBugs complaining about non-serializable field although everything looks Serializable Lew <noone@lewscanon.com> - 2012-01-15 10:18 -0800

#11276 — FindBugs complaining about non-serializable field although everything looks Serializable

From"laredotornado@zipmail.com" <laredotornado@gmail.com>
Date2012-01-12 08:40 -0800
SubjectFindBugs complaining about non-serializable field although everything looks Serializable
Message-ID<a3ed0747-5eb5-4da1-9f7f-c6ebdc8cbafd@v13g2000yqc.googlegroups.com>
Hi,

I'm using Java 1.6.  My FindBugs tool is giving me this error ...


    Non-transient non-serializable instance field in serializable
class
    Class com.myco.clearing.common.xml.Node defines non-transient non-
serializable instance field children


The class and its private fields that Findbugs is complaining about
are below ...


    public class Node implements Serializable, Comparable<Node>,
Cloneable {
	/**
	 * For serializable classes.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Unique id
	 */
	private long id;
	/**
	 * Node Name
	 */
	private String name;
	/**
	 * Node value
	 */
	private String value = "";
	/**
	 * Child nodes
	 */
	private List<Node> children;
	/**
	 * Parent node
	 */
	private Node parent;
	/**
	 * Node attributes
	 */
	private List<Attribute> attributes;


I have a public, no-argument constructor and getter/setter methods for
all the fields you see (except serialVersionUID).  Any ideas why
FindBugs is complaining about the field "children" or how I can
troubleshoot this further?

The above references a class, "Attribute".  The relevant parts are
below.  Same thing -- a public, no-argument constructor and getter/
setter methods present.


    public class Attribute implements Serializable, Cloneable {

	/**
	 * For serializable classes.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Attribute Name
	 */
	private String name;
	/**
	 * Attribute value, can be local or inherited
	 */
	private String value;
	/**
	 * Node having this attribute
	 */
	private Node node;


Thanks, - Dave

[toc] | [next] | [standalone]


#11279

FromJeff Higgins <jeff@invalid.invalid>
Date2012-01-12 12:04 -0500
Message-ID<jen3e5$vms$1@dont-email.me>
In reply to#11276
On 01/12/2012 11:40 AM, laredotornado@zipmail.com wrote:
> Hi,
>
> I'm using Java 1.6.  My FindBugs tool is giving me this error ...
>
>
>      Non-transient non-serializable instance field in serializable

?<http://findbugs.sourceforge.net/bugDescriptions.html#SE_BAD_FIELD>

> class
>      Class com.myco.clearing.common.xml.Node defines non-transient non-
> serializable instance field children
>
>
> The class and its private fields that Findbugs is complaining about
> are below ...
>
>
>      public class Node implements Serializable, Comparable<Node>,
> Cloneable {
> 	/**
> 	 * For serializable classes.
> 	 */
> 	private static final long serialVersionUID = 1L;
>
> 	/**
> 	 * Unique id
> 	 */
> 	private long id;
> 	/**
> 	 * Node Name
> 	 */
> 	private String name;
> 	/**
> 	 * Node value
> 	 */
> 	private String value = "";
> 	/**
> 	 * Child nodes
> 	 */
> 	private List<Node>  children;
> 	/**
> 	 * Parent node
> 	 */
> 	private Node parent;
> 	/**
> 	 * Node attributes
> 	 */
> 	private List<Attribute>  attributes;
>
>
> I have a public, no-argument constructor and getter/setter methods for
> all the fields you see (except serialVersionUID).  Any ideas why
> FindBugs is complaining about the field "children" or how I can
> troubleshoot this further?
>
> The above references a class, "Attribute".  The relevant parts are
> below.  Same thing -- a public, no-argument constructor and getter/
> setter methods present.
>
>
>      public class Attribute implements Serializable, Cloneable {
>
> 	/**
> 	 * For serializable classes.
> 	 */
> 	private static final long serialVersionUID = 1L;
>
> 	/**
> 	 * Attribute Name
> 	 */
> 	private String name;
> 	/**
> 	 * Attribute value, can be local or inherited
> 	 */
> 	private String value;
> 	/**
> 	 * Node having this attribute
> 	 */
> 	private Node node;
>
>
> Thanks, - Dave

[toc] | [prev] | [next] | [standalone]


#11282

From"laredotornado@zipmail.com" <laredotornado@gmail.com>
Date2012-01-12 09:48 -0800
Message-ID<6f8d9f50-2f73-4844-b887-520892e3f93a@u20g2000yqb.googlegroups.com>
In reply to#11279
On Jan 12, 11:04 am, Jeff Higgins <j...@invalid.invalid> wrote:
> On 01/12/2012 11:40 AM, laredotorn...@zipmail.com wrote:
>
> > Hi,
>
> > I'm using Java 1.6.  My FindBugs tool is giving me this error ...
>
> >      Non-transient non-serializable instance field in serializable
>
> ?<http://findbugs.sourceforge.net/bugDescriptions.html#SE_BAD_FIELD>
>
>
>
>
>
>
>
> > class
> >      Class com.myco.clearing.common.xml.Node defines non-transient non-
> > serializable instance field children
>
> > The class and its private fields that Findbugs is complaining about
> > are below ...
>
> >      public class Node implements Serializable, Comparable<Node>,
> > Cloneable {
> >    /**
> >     * For serializable classes.
> >     */
> >    private static final long serialVersionUID = 1L;
>
> >    /**
> >     * Unique id
> >     */
> >    private long id;
> >    /**
> >     * Node Name
> >     */
> >    private String name;
> >    /**
> >     * Node value
> >     */
> >    private String value = "";
> >    /**
> >     * Child nodes
> >     */
> >    private List<Node>  children;
> >    /**
> >     * Parent node
> >     */
> >    private Node parent;
> >    /**
> >     * Node attributes
> >     */
> >    private List<Attribute>  attributes;
>
> > I have a public, no-argument constructor and getter/setter methods for
> > all the fields you see (except serialVersionUID).  Any ideas why
> > FindBugs is complaining about the field "children" or how I can
> > troubleshoot this further?
>
> > The above references a class, "Attribute".  The relevant parts are
> > below.  Same thing -- a public, no-argument constructor and getter/
> > setter methods present.
>
> >      public class Attribute implements Serializable, Cloneable {
>
> >    /**
> >     * For serializable classes.
> >     */
> >    private static final long serialVersionUID = 1L;
>
> >    /**
> >     * Attribute Name
> >     */
> >    private String name;
> >    /**
> >     * Attribute value, can be local or inherited
> >     */
> >    private String value;
> >    /**
> >     * Node having this attribute
> >     */
> >    private Node node;
>
> > Thanks, - Dave

Hi, I read that, but all fields are serializable, including
java.util.List.  So, I'm not seeing what is throwing it off, do you? -
Dave

[toc] | [prev] | [next] | [standalone]


#11284

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-01-12 09:52 -0800
Message-ID<K5FPq.74067$ed2.70254@newsfe05.iad>
In reply to#11282
On 1/12/12 9:48 AM, laredotornado@zipmail.com wrote:
> On Jan 12, 11:04 am, Jeff Higgins<j...@invalid.invalid>  wrote:
>> On 01/12/2012 11:40 AM, laredotorn...@zipmail.com wrote:
>>
>>> Hi,
>>
>>> I'm using Java 1.6.  My FindBugs tool is giving me this error ...
>>
>>>       Non-transient non-serializable instance field in serializable
>>
>> ?<http://findbugs.sourceforge.net/bugDescriptions.html#SE_BAD_FIELD>
>>
>>
>>
>>
>>
>>
>>
>>> class
>>>       Class com.myco.clearing.common.xml.Node defines non-transient non-
>>> serializable instance field children
>>
>>> The class and its private fields that Findbugs is complaining about
>>> are below ...
>>
>>>       public class Node implements Serializable, Comparable<Node>,
>>> Cloneable {
>>>     /**
>>>      * For serializable classes.
>>>      */
>>>     private static final long serialVersionUID = 1L;
>>
>>>     /**
>>>      * Unique id
>>>      */
>>>     private long id;
>>>     /**
>>>      * Node Name
>>>      */
>>>     private String name;
>>>     /**
>>>      * Node value
>>>      */
>>>     private String value = "";
>>>     /**
>>>      * Child nodes
>>>      */
>>>     private List<Node>    children;
>>>     /**
>>>      * Parent node
>>>      */
>>>     private Node parent;
>>>     /**
>>>      * Node attributes
>>>      */
>>>     private List<Attribute>    attributes;
>>
>>> I have a public, no-argument constructor and getter/setter methods for
>>> all the fields you see (except serialVersionUID).  Any ideas why
>>> FindBugs is complaining about the field "children" or how I can
>>> troubleshoot this further?
>>
>>> The above references a class, "Attribute".  The relevant parts are
>>> below.  Same thing -- a public, no-argument constructor and getter/
>>> setter methods present.
>>
>>>       public class Attribute implements Serializable, Cloneable {
>>
>>>     /**
>>>      * For serializable classes.
>>>      */
>>>     private static final long serialVersionUID = 1L;
>>
>>>     /**
>>>      * Attribute Name
>>>      */
>>>     private String name;
>>>     /**
>>>      * Attribute value, can be local or inherited
>>>      */
>>>     private String value;
>>>     /**
>>>      * Node having this attribute
>>>      */
>>>     private Node node;
>>
>>> Thanks, - Dave
>
> Hi, I read that, but all fields are serializable, including
> java.util.List.  So, I'm not seeing what is throwing it off, do you? -
> Dave
Check again. java.util.List is not Serializable.

[toc] | [prev] | [next] | [standalone]


#11283

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-01-12 09:52 -0800
Message-ID<e5FPq.74066$ed2.40342@newsfe05.iad>
In reply to#11276
On 1/12/12 8:40 AM, laredotornado@zipmail.com wrote:
> Hi,
>
> I'm using Java 1.6.  My FindBugs tool is giving me this error ...
>
>
>      Non-transient non-serializable instance field in serializable
> class
>      Class com.myco.clearing.common.xml.Node defines non-transient non-
> serializable instance field children
>
>
> The class and its private fields that Findbugs is complaining about
> are below ...
>
>
>      public class Node implements Serializable, Comparable<Node>,
> Cloneable {
<snip>
  	private List<Node>  children;
> 	/**
> 	 * Parent node
> 	 */
> 	private Node parent;
> 	/**
> 	 * Node attributes
> 	 */
> 	private List<Attribute>  attributes;
>
I'm assuming that "List" is the java.util.List interface, which does not 
extend Serializable.  Many implementations *are* serializable, but the 
interface itself is not.  FindBugs is warning you that it is possible to 
set the "children" and "attributes" fields to List implementations that 
are not serializable.

[toc] | [prev] | [next] | [standalone]


#11285

FromIan Shef <invalid@avoiding.spam>
Date2012-01-12 19:29 +0000
Message-ID<Xns9FD87F16AD841vaj4088ianshef@138.125.254.103>
In reply to#11276
"laredotornado@zipmail.com" <laredotornado@gmail.com> wrote in 
news:a3ed0747-5eb5-4da1-9f7f-c6ebdc8cbafd@v13g2000yqc.googlegroups.com:

> Hi,
> 
> I'm using Java 1.6.  My FindBugs tool is giving me this error ...
> 
> 
>     Non-transient non-serializable instance field in serializable
> class
>     Class com.myco.clearing.common.xml.Node defines non-transient non-
> serializable instance field children
> 
> 
> The class and its private fields that Findbugs is complaining about
> are below ...
> 
> 
>     public class Node implements Serializable, Comparable<Node>,
> Cloneable {
>      /**
>       * For serializable classes.
>       */
>      private static final long serialVersionUID = 1L;
> 
>      /**
>       * Unique id
>       */
>      private long id;
>      /**
>       * Node Name
>       */
>      private String name;
>      /**
>       * Node value
>       */
>      private String value = "";
>      /**
>       * Child nodes
>       */
>      private List<Node> children;
>      /**
>       * Parent node
>       */
>      private Node parent;
>      /**
>       * Node attributes
>       */
>      private List<Attribute> attributes;
> 
> 
> I have a public, no-argument constructor 
Not relevant.
> and getter/setter methods for
> all the fields you see (except serialVersionUID).  
Not relevant, I think.
> Any ideas why
> FindBugs is complaining about the field "children" or how I can
> troubleshoot this further?

If I recall correctly, a class cannot be serialized unless all of its 
fields are either Serializable or marked transient (or unless special 
methods are written).

Primitives (long, in your casse) are always Serializable.
String is Serializable.
List is NOT Serializable, and you have not marked children and attributes 
as transient.  This will lead to a NotSerializableException at the time of 
serialization.

Node cannot be serialized unless its nontransient fields can be serialized.
(Obviously, transient fields don't get serialized and don't get restored by 
deserialization.)

<class Attribute snipped as not relevant>

The choices would seem to be:

Don't make Node Serializable, or
Mark children and attributes as transient, or
Use something Serializable in place of List (such as ArrayList), or
Provide writeObject and readObject methods (or writeReplace and
  readResolve, or...) to appropriately serialize Node objects.

[toc] | [prev] | [next] | [standalone]


#11296

FromLew <noone@lewscanon.com>
Date2012-01-12 21:10 -0800
Message-ID<jeoec5$q62$1@news.albasani.net>
In reply to#11285
Ian Shef wrote:
> List is NOT Serializable,

Isn't this something determined at runtime?

If the implementation type is, say, ArrayList, won't it just magically work?

Okay, okay, I'll run up NetBeans and see for myself.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [next] | [standalone]


#11297

FromLew <noone@lewscanon.com>
Date2012-01-12 21:11 -0800
Message-ID<jeoedi$q62$2@news.albasani.net>
In reply to#11296
On 01/12/2012 09:10 PM, Lew wrote:
> Ian Shef wrote:
>> List is NOT Serializable,
>
> Isn't this something determined at runtime?
>
> If the implementation type is, say, ArrayList, won't it just magically work?
>
> Okay, okay, I'll run up NetBeans and see for myself.
>
Oh, wait, I see why I'm wrong. (facepalm)

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [next] | [standalone]


#11331

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-01-14 23:23 -0500
Message-ID<4f1254c3$0$283$14726298@news.sunsite.dk>
In reply to#11296
On 1/13/2012 12:10 AM, Lew wrote:
> Ian Shef wrote:
>> List is NOT Serializable,
>
> Isn't this something determined at runtime?

For serialization: yes.

For findbugs utility: no.

Arne

[toc] | [prev] | [next] | [standalone]


#11354

FromLew <noone@lewscanon.com>
Date2012-01-15 10:18 -0800
Message-ID<jev58s$bum$1@news.albasani.net>
In reply to#11331
Arne Vajhøj wrote:
> Lew wrote:
>> Ian Shef wrote:
>>> List is NOT Serializable,
>>
>> Isn't this something determined at runtime?
>
> For serialization: yes.
>
> For findbugs utility: no.

Hence the facepalm.

-- 
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web