Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XMLEncoder - Stackoverflowerror - overriding equals

 
Jonathan Janisch
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to write out a class to XML using XMLEncoder.

There's a few unique things about this class.

1) It has no default constructor
2) I'm overriding the equals method.
3) The class has a static int count which defaults to 0. Each object is assigned an ID based on the current count. The ID is used to compare two objects.

Since it has no default constructor, I'm using the defaultpersistencedelegate. Here's the code:



If you run this, you will get a stackoverflowerror (atleast under jdk1.6.0_01 on Windows). I'm not an XMLEncoder expert, I've narrowed the problem down and I came up with this simple Person.java example.

The combination of the static int ID and override equals seems to be the problem. If you change the equals to:



It will still give the same error. However, if you remove the equals method completely, it will work fine and the xml output is as I expected. But unfortunately, I need to override the equals method.

1) Does anyone know how I can fix this?
2) Why would "return super.equals(obj)" give different behavior than having no equals method at all?

Note: I realize if my class were really a Person class, the equals method should compare SSN, first and last name, etc. and not some made up int ID - this is just a simple example to demonstrate the problem.

Thank you!
 
Jonathan Janisch
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it using my own persistence delegate to create a new Person. I don't understand why I had to do this, since I thought that was the purpose of the defaultpersistencedelegate. I did see some stuff in the source for defaultpersistencedelegate that involved checks for overriding equals - but I won't pretend like I understand how it works.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic