commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jörg Schaible <Joerg.Schai...@Elsag-Solutions.com>
Subject RE: [lang] ClassCastException in Enum.compareTo with anonymous inner classes
Date Thu, 13 Sep 2007 14:05:30 GMT
Hi Samuel,

Samuel Fleischle wrote on Thursday, September 13, 2007 3:51 PM:

> Hi,
> I updated from Commons Lang 2.1 to Commons Lang 2.3 and got
> some issues with the changed compareTo() method of Enum with
> anonymouse inner classes: 
> 
> Here is my simplified Enum:
> 
> 
> public class ItemStatus extends Enum
> {
>        // --------------------------------------------------
> Static Fields
>       public static final ItemStatus CANCEL = new
> ItemStatus("CANCEL", "Cancel");
> 
>        public static final ItemStatus SHIPPED = new
> ItemStatus("SHIPPED", "Shipped") {
>        	public String getDisplayName() {
>        		// do something special for this status
> 		}
>        };
> 
>       public static final ItemStatus MOVED = new
> ItemStatus("MOVED", "Moved") {
>        	public String getDisplayName() {
> 			// do something special for this status
> 		}
>        };
> 

[snip]

> }

[snip]
 
> com.myapp.common.model.order.ItemStatus
> com.myapp.common.model.order.ItemStatus$1
> com.myapp.common.model.order.ItemStatus$2
> 
> The compareTo-Method now tries to compare ItemStatus$1 with
> ItemStatus$2 and says to me, that my ItemStatus-Enums are
> different classes.

Well, obviously those *are* different classes.

> I saw in JIRA there are some other issues in
> ValuedEnum.compareTo(). Is there a bug in the
> Enum.compareTo() implementation which got changed in Lang 2.2?

IIRC, this was more about using different class loaders loading the EnumValue class.
 
> Thanks in advance for any help or comment on this issue.

Why don't you factor out a (private) interface with this method? Write an addition ctor that
takes such an implementation and use otherwise a default one. With this approach the enum
class is always the same and the anonymous class is of a different type. Make the an additional
field in the enum class keeping that implementation and declare it as transient for seamless
serialization.

- Jörg

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message