Community
Participate
Working Groups
a declared enum type public enum RuleType{ AVAILABLE, MANDATORY } has the generated method RuleType.valueOf(String) this call chokes with an ArrayIndexOutOfBoundsException when called with valid values: RuleType.valueOf(RuleType.AVAILABLE.name())
The exception happens at runtime. public class X { public enum RuleType{ AVAILABLE, MANDATORY } public static void main(String[] args) { System.out.println(RuleType.valueOf(RuleType.AVAILABLE.name())); } } Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at X$RuleType.valueOf(X.java:1) at X.main(X.java:5)
Created attachment 15457 [details] Apply on HEAD With this patch the code works. Three errors found: The code is doing a reverse loop on the values array, but it doesn't take: - length - 1 for the first index - doesn't get the name() of the current entry - stop one two earlier (gt instead of ge)
I get: C:\tests_sources>java X AVAILABLE
We could also use the method: public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) defined on java.lang.Enum.
Pls also update the comment in front: // static X valueOf(String name) { // X[] values; // for (int i = (values = $VALUES).length; --i >= 0;) { // X value; // if (name.equals(value = values[i].name())) return value; // } // throw new IllegalArgumentException(name); // }
Fixed and released in HEAD. Regression test added in EnumTest.
Verified for 3.1M3 with build I20041102