Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6349921
Votes 0
Synopsis (enum) Include links from java.lang.Enum to EnumSet and EnumMap
Category java:classes_lang
Reported Against
Release Fixed 7(b75)
State 10-Fix Delivered, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 14-NOV-2005
Description
Add an of EnumSet.allOf( Class ) to make it easier to see this is the means of accessing the "secret static values()".

(Original bug request)
A DESCRIPTION OF THE REQUEST :
For every enum class, the compiler secretly creates a static values() method that takes no params and returns all the values of the enum. This is cool, but it requires reflection to access this method. We'd be much better off if we could access these without reflection. This could be easily accomplished through a static Enum method that returns all values in an enum given the class of the enum. I'll implement the method for Sun, they just have to add it to the Enum class:


public static <T extends Enum<T>> Collection<T> values( Class<T> enumClass ) {
    try {
        Method valuesMethod = enumClass.getMethod( "values", new Class[0] );
        T[] values = (T[]) valuesMethod.invoke( null, new Object[0] );
        return Arrays.asList( values );
    }
    catch ( Exception ex ) {
        throw new RuntimeException( "Exceptions here should be impossible", ex );
    }
}



JUSTIFICATION :
 Instances come up all the time where you're writing a class that works for any type of Enum. So your class has a generic type <T extends Enum<T>> and a reference to the enum class  customer , Class<T> theEnumClass. Now how do you get at the values?




CUSTOMER SUBMITTED WORKAROUND :
Create your own EnumUtil class and add this method:


public static <T extends Enum<T>> Collection<T> values( Class<T> enumClass ) {
    try {
        Method valuesMethod = enumClass.getMethod( "values", new Class[0] );
        T[] values = (T[]) valuesMethod.invoke( null, new Object[0] );
        return Arrays.asList( values );
    }
    catch ( Exception ex ) {
        throw new RuntimeException( "Exceptions here should be impossible", ex );
    }
}
Posted Date : 2006-06-07 14:32:02.0
Work Around
Use EnumSet.allOf( Class )
Evaluation
As the customer has indicated after the bug was file, the method EnumSet.allOf(Class<E>) already provides the requested functionality.
Posted Date : 2005-11-15 01:29:00.0

Would it be worth updating the documentation with some examples?
Posted Date : 2006-06-02 18:34:59.0

I vote yes, but don't know who the right person is to create the examples. The enum area doesn't have distinct ownership (correct me if I'm wrong).
Posted Date : 2006-06-07 14:32:03.0

The existence of use of EnumSet.allOf should be quite clear from the documenation.

However, it would be worthwhile to add @see links between java.lang.Enum and EnumSet and EnumMap.
Posted Date : 2009-10-09 17:57:47.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang