Author: niallp Date: Sun Feb 21 02:52:56 2010 New Revision: 912292 URL: http://svn.apache.org/viewvc?rev=912292&view=rev Log: LANG-76 EnumUtils.getEnum() doesn't work in 1.5+ - force initialization of the Enum class using Class.forName() thanks to Marcus Schulte for the solution (Note: for JDK 1.5+ the Ant build compiles EnumUtilsLang76Test with source/target 1.5 which causes this problem to occur)
Added: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsLang76Test.java (with props) Modified: commons/proper/lang/branches/LANG_2_X/build.xml commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enum/Enum.java commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enums/Enum.java commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsTest.java Modified: commons/proper/lang/branches/LANG_2_X/build.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/build.xml?rev=912292&r1=912291&r2=912292&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/build.xml (original) +++ commons/proper/lang/branches/LANG_2_X/build.xml Sun Feb 21 02:52:56 2010 @@ -53,6 +53,7 @@ <filter token="compile.source" value="${compile.source}"/> <filter token="compile.target" value="${compile.target}"/> <mkdir dir="${build.home}"/> + <available property="jdk.1.5.present" classname="java.lang.StringBuilder"/> </target> <!-- ========== Compile Targets ========================================= --> @@ -66,9 +67,21 @@ </copy> </target> - <target name="compile.tests" depends="compile" description="Compile unit test cases"> + <!-- Compile EnumUtilsLang76Test using source/target 1.5 if JDK 1.5 present (see LANG-76) --> + <target name="test.compile.jdk15" depends="compile" description="Run test for LANG-76" if="jdk.1.5.present"> + <echo message="Compiling EnumUtilsLang76Test using source/target 1.5"/> + <mkdir dir="${build.home}/tests"/> + <javac srcdir="${test.home}" destdir="${build.home}/tests" + target="1.5" source="1.5" + includes="org/apache/commons/lang/enums/EnumUtilsLang76Test.java"> + <classpath refid="test.classpath"/> + </javac> + </target> + + <target name="compile.tests" depends="compile,test.compile.jdk15" description="Compile unit test cases"> <mkdir dir="${build.home}/tests"/> <javac srcdir="${test.home}" destdir="${build.home}/tests" debug="${compile.debug}" deprecation="off" target="${compile.target}" source="${compile.source}" optimize="${compile.optimize}"> + <exclude name="org/apache/commons/lang/enums/EnumUtilsLang76Test.java" if="jdk.1.5.present"/> <classpath refid="test.classpath"/> </javac> <copy todir="${build.home}/tests" filtering="on"> Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enum/Enum.java URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enum/Enum.java?rev=912292&r1=912291&r2=912292&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enum/Enum.java (original) +++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enum/Enum.java Sun Feb 21 02:52:56 2010 @@ -484,6 +484,17 @@ throw new IllegalArgumentException("The Class must be a subclass of Enum"); } Entry entry = (Entry) cEnumClasses.get(enumClass); + + if (entry == null) { + try { + // LANG-76 - try to force class initialization for JDK 1.5+ + Class.forName(enumClass.getName(), true, enumClass.getClassLoader()); + entry = (Entry) cEnumClasses.get(enumClass); + } catch (Throwable t) { + // Ignore + } + } + return entry; } Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enums/Enum.java URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enums/Enum.java?rev=912292&r1=912291&r2=912292&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enums/Enum.java (original) +++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/enums/Enum.java Sun Feb 21 02:52:56 2010 @@ -530,6 +530,17 @@ throw new IllegalArgumentException("The Class must be a subclass of Enum"); } Entry entry = (Entry) cEnumClasses.get(enumClass); + + if (entry == null) { + try { + // LANG-76 - try to force class initialization for JDK 1.5+ + Class.forName(enumClass.getName(), true, enumClass.getClassLoader()); + entry = (Entry) cEnumClasses.get(enumClass); + } catch (Throwable t) { + // Ignore + } + } + return entry; } Added: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsLang76Test.java URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsLang76Test.java?rev=912292&view=auto ============================================================================== --- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsLang76Test.java (added) +++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsLang76Test.java Sun Feb 21 02:52:56 2010 @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.TestCase; + +/** + * Test cases for the LANG-76 issue with {...@link EnumUtils}. + * + * NOTE: this needs to be compiled with source/target versions set to 1.5 + * in order to replicate/test the issue properly + */ +public class EnumUtilsLang76Test extends TestCase { + + public EnumUtilsLang76Test(String name) { + super(name); + } + + /** + * Test LANG-76 + */ + public void test_EnumUtils_getEnum_LANG76() { + Object obj = EnumUtils.getEnum(TestEnum.class, "1"); + assertNotNull("Enum is not intialized", obj); + assertEquals("EnumUtilsLang76Test.TestEnum[1]", obj.toString()); + } + + /** Test Enum for LANG-76 **/ + public static final class TestEnum extends Enum { + private static final long serialVersionUID = 1L; + public static final TestEnum ONE = new TestEnum("1"); + public static final TestEnum TWO = new TestEnum("2"); + public static final TestEnum THREE = new TestEnum("3"); + + private TestEnum(String value) { + super(value); + } + } +} Propchange: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsLang76Test.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsLang76Test.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsTest.java?rev=912292&r1=912291&r2=912292&view=diff ============================================================================== --- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsTest.java (original) +++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/enums/EnumUtilsTest.java Sun Feb 21 02:52:56 2010 @@ -156,22 +156,4 @@ fail(); } catch (IllegalArgumentException ex) {} } - - /** Test for LANG-76 **/ - public void testGetEnum_LANG76() { - Object obj = EnumUtils.getEnum(Lang76Enum.class, "1"); - assertNotNull(obj); - assertEquals("EnumUtilsTest.Lang76Enum[1]", obj.toString()); - } - - /** Test Enum for LANG-76 **/ - public static final class Lang76Enum extends Enum { - public static final Lang76Enum ONE = new Lang76Enum("1"); - public static final Lang76Enum TWO = new Lang76Enum("2"); - public static final Lang76Enum THREE = new Lang76Enum("3"); - - private Lang76Enum(String suit) { - super(suit); - } - } }