I have attached a test and patch that seems to fix this bug.

Henri Yandell wrote:
Forwarding over from the user list. Any opinions Stephen?

---------- Forwarded message ----------
From: Adam Paster <[EMAIL PROTECTED]>
Date: Feb 27, 2007 3:55 AM
Subject: Re: [Lang] ClassCastException in Enum.compareTo()
To: Henri Yandell <[EMAIL PROTECTED]>
Cc: Jakarta Commons Users List <commons-user@jakarta.apache.org>


In my example below, I was referring to the OperationEnum class defined
at
http://svn.apache.org/repos/asf/jakarta/commons/proper/lang/tags/LANG_2_3/src/test/org/apache/commons/lang/enums/OperationEnum.java

So, even though the two enums have different classnames, the
Enum.compareTo() method should take the getEnumClass() into account to
decide whether a ClassCastException should be thrown....

Regards
 Adam

Henri Yandell wrote:
Looking at the svn log history, I presume it's due either to LANG-259
or LANG-127:

https://issues.apache.org/jira/browse/LANG-259
https://issues.apache.org/jira/browse/LANG-127

Both were in 2.2, so 2.2 will be equally problematic for you.

LANG-127 took the code that was already in equals() to use reflection
when classes are in different classloaders, LANG-259 adding the
ClassCastException when the classes are in different classloaders and
the two enums do not have the same classname.

Does that fit with your MINUS/PLUS enums? Do they have different
classnames?

Hen

On 2/27/07, Adam Paster <[EMAIL PROTECTED]> wrote:
Hi

In commons lang 2.1 the following code used to work:
OperationEnum.MINUS.compareTo(OperationEnum.PLUS)
But when I upgraded to version 2.3 it throws a ClassCastException. Is
this a regression?

Many thanks
  Adam






___________________________________________________________
Inbox full of unwanted email? Get leading protection and 1GB storage
with All New Yahoo! Mail. http://uk.docs.yahoo.com/nowyoucan.html

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


</div>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Index: src/test/org/apache/commons/lang/enums/EnumTest.java
===================================================================
--- src/test/org/apache/commons/lang/enums/EnumTest.java        (revision 
509950)
+++ src/test/org/apache/commons/lang/enums/EnumTest.java        (working copy)
@@ -76,6 +76,10 @@
             fail();
         } catch (ClassCastException ex) {}
     }
+    
+    public void testOperationEnumCompareTo() {
+       assertTrue(OperationEnum.MINUS.compareTo(OperationEnum.PLUS) < 0);
+    }
 
     public void testEquals() {
         assertSame(ColorEnum.RED, ColorEnum.RED);
Index: src/java/org/apache/commons/lang/enums/Enum.java
===================================================================
--- src/java/org/apache/commons/lang/enums/Enum.java    (revision 509950)
+++ src/java/org/apache/commons/lang/enums/Enum.java    (working copy)
@@ -631,14 +631,18 @@
         if (other == this) {
             return 0;
         }
+        if (other instanceof Enum) {
+           if (((Enum)other).getEnumClass() == this.getEnumClass()) {
+              return iName.compareTo(((Enum) other).iName);
+           }
+        }
         if (other.getClass() != this.getClass()) {
             if (other.getClass().getName().equals(this.getClass().getName())) {
                 return iName.compareTo( getNameInOtherClassLoader(other) );
             }
-            throw new ClassCastException(
-                    "Different enum class '" + 
ClassUtils.getShortClassName(other.getClass()) + "'");
         }
-        return iName.compareTo(((Enum) other).iName);
+        throw new ClassCastException(
+              "Different enum class '" + 
ClassUtils.getShortClassName(other.getClass()) + "'");
     }
 
     /**

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to