Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ordering methods and serializing classes

Status
Not open for further replies.

Diancecht

Programmer
Jan 8, 2004
4,042
ES
This is noy exactly a Java question, it's more that I need a second opinion.

From Javadoc, when you invoke a getMethods() on a Class, methods are returned in no particular order, randomly ordered in fact.

Not good, but tolerable. Now imagine we have a "bean" with a property of type String with its get and set method and in additon a set method that accepts a char for some dark reasons.

Let's serialize this. In my opinion, the property should be serialized as a String, discarfind the char setter. I'd even understand that the propery were ignored, as is not fully standard.

To my surprise, I found that Introspector, when looking for the property, sometimes shows is, sometimes not.

The reason: it get the methods and iterate with them. If it first finds the char one, nice, it gets overwritten by the String one and the property is serialized.

If the String is first, it gets overwritten, there's an incompatibility between get and set methods and the property just fades.

Is just me or do you think this is almost a bug from Java?

Cheers,

Dian
 
I guess the question comes down to whether or not Sun considers that a properly formed "Bean" can have more than one setter per property.

It's obviously assuming that it will not. However, I searched and could not find a reference that actually said that.

I overload property setters all the time. If I'm pulling a number from the screen, it's a String. If I pull the same number from the database, it's a long. I've been creating a setter for each. Perhaps I should rethink that...
 
Well, I'm not actually discussing if it's should be a bean or not. I think it should, but anyway, I feel that a random behaviour on such type of beans is not good.

If they want to define beans with just a setter metohd by property, it's ok for me, but maybe the Introspector should throw an Exception instead of providing an undeterministic behaviour.

Looking at he source coe for IBM's JVM you can find comments like // This is rare or // This is very rare.

Just wondering it it was just me that tries to do strange things or anyone more outside there feels the same.

Cheers,

Dian
 
You have a good point that it should throw an exception, rather than just overwrite the setter.

However, I think if you presented it to Sun as a bug, they would respond that it's your code that's buggy because you have two setters.

A request for an enhancement might be more appropriate.

And yes, it's just you doing these strange things.
 
Thanks for your comments.

I realize I spent my programming life trying to do strange things, but I'm paid for that :p

I managed to make it work, so I will just sit and wait. I don't want to have zillions of beans developers looking for the stupid guy that asked to change the specification. :)

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top