Fix Enum equals to work correctly by reflection
bug 28180, from Matthias Eichel git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137837 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
aa5c638764
commit
aa1e8866bb
|
@ -228,8 +228,9 @@ import org.apache.commons.lang.StringUtils;
|
|||
* @author Stephen Colebourne
|
||||
* @author Chris Webb
|
||||
* @author Mike Bowler
|
||||
* @since 1.0
|
||||
* @version $Id: Enum.java,v 1.1 2004/02/23 04:34:20 ggregory Exp $
|
||||
* @author Matthias Eichel
|
||||
* @since 2.1 (class existed in enum package from v1.0)
|
||||
* @version $Id: Enum.java,v 1.2 2004/06/01 20:54:57 scolebourne Exp $
|
||||
*/
|
||||
public abstract class Enum implements Comparable, Serializable {
|
||||
|
||||
|
@ -537,7 +538,10 @@ public abstract class Enum implements Comparable, Serializable {
|
|||
// classes are in the same class loader.
|
||||
return iName.equals(((Enum) other).iName);
|
||||
} else {
|
||||
// This and other are in different class loaders, we must use reflection.
|
||||
// This and other are in different class loaders, we must check indirectly
|
||||
if (other.getClass().getName().equals(this.getClass().getName()) == false) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Method mth = other.getClass().getMethod("getName", null);
|
||||
String name = (String) mth.invoke(other, null);
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright 2004 The Apache Software Foundation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.lang.enums;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Test cases for the {@link Enum} class equals method.
|
||||
*
|
||||
* @author Matthias Eichel
|
||||
* @author Stephen Colebourne
|
||||
* @version $Id: EnumEqualsTest.java,v 1.1 2004/06/01 20:54:57 scolebourne Exp $
|
||||
*/
|
||||
public final class EnumEqualsTest extends TestCase {
|
||||
|
||||
public EnumEqualsTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(EnumEqualsTest.class);
|
||||
suite.setName("Enum equals Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
static final class CarColorEnum extends Enum {
|
||||
public static final CarColorEnum BLACK = new CarColorEnum("black");
|
||||
public static final CarColorEnum BROWN = new CarColorEnum("brown");
|
||||
public static final CarColorEnum YELLOW = new CarColorEnum("yellow");
|
||||
public static final CarColorEnum BLUE = new CarColorEnum("blue");
|
||||
public static final CarColorEnum RED = new CarColorEnum("red");
|
||||
|
||||
private CarColorEnum(String enumAsString) {
|
||||
super(enumAsString);
|
||||
}
|
||||
}
|
||||
|
||||
static final class TrafficlightColorEnum extends Enum {
|
||||
public static final TrafficlightColorEnum RED = new TrafficlightColorEnum("red");
|
||||
public static final TrafficlightColorEnum YELLOW = new TrafficlightColorEnum("yellow");
|
||||
public static final TrafficlightColorEnum GREEN = new TrafficlightColorEnum("green");
|
||||
|
||||
private TrafficlightColorEnum(String enumAsString) {
|
||||
super(enumAsString);
|
||||
}
|
||||
}
|
||||
|
||||
static class TotallyUnrelatedClass {
|
||||
private final String name;
|
||||
|
||||
public TotallyUnrelatedClass(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testEquals() {
|
||||
assertEquals(false, CarColorEnum.RED.equals(TrafficlightColorEnum.RED));
|
||||
assertEquals(false, CarColorEnum.YELLOW.equals(TrafficlightColorEnum.YELLOW));
|
||||
|
||||
assertEquals(false, TrafficlightColorEnum.RED.equals(new TotallyUnrelatedClass("red")));
|
||||
assertEquals(false, CarColorEnum.RED.equals(new TotallyUnrelatedClass("red")));
|
||||
|
||||
assertEquals(false, TrafficlightColorEnum.RED.equals(new TotallyUnrelatedClass("some")));
|
||||
assertEquals(false, CarColorEnum.RED.equals(new TotallyUnrelatedClass("some")));
|
||||
}
|
||||
}
|
|
@ -24,7 +24,7 @@ import junit.textui.TestRunner;
|
|||
* Test suite for the Enum package.
|
||||
*
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @version $Id: EnumTestSuite.java,v 1.1 2004/02/23 04:34:20 ggregory Exp $
|
||||
* @version $Id: EnumTestSuite.java,v 1.2 2004/06/01 20:54:57 scolebourne Exp $
|
||||
*/
|
||||
public class EnumTestSuite extends TestCase {
|
||||
|
||||
|
@ -49,6 +49,7 @@ public class EnumTestSuite extends TestCase {
|
|||
TestSuite suite = new TestSuite();
|
||||
suite.setName("Commons-Lang-Enum Tests");
|
||||
suite.addTest(EnumTest.suite());
|
||||
suite.addTest(EnumEqualsTest.suite());
|
||||
suite.addTest(EnumUtilsTest.suite());
|
||||
suite.addTest(ValuedEnumTest.suite());
|
||||
return suite;
|
||||
|
|
Loading…
Reference in New Issue