Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6349921

(enum) Include links from java.lang.Enum to EnumSet and EnumMap

    Details

    • Subcomponent:
    • Resolved In Build:
      b75
    • CPU:
      x86
    • OS:
      linux

      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 object, 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 );
          }
      }

        Attachments

          Activity

            People

            • Assignee:
              darcy Joe Darcy
              Reporter:
              gmanwanisunw Girish Manwani (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: