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

(enum) Add a function to query if enum name is a valid enum constant name

XMLWordPrintable

      ###@###.### 2004-04-20

      J2SE Version (please include all output from java -version flag):
        java version "1.5.0-beta2"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b44)
        Java HotSpot(TM) Client VM (build 1.5.0-beta2-b46, mixed mode, sharing)

      Does this problem occur on J2SE 1.3.x or 1.4.x? Yes / No (pick one)
        N/A

      Operating System Configuration Information (be specific):
        WinXP

      Hardware Configuration Information (be specific):
        Dell Optiplex

      Bug Description:
        Like the new enum support. It's great. However, just had to write
        some ugly code...

        try {
         PrimitiveType.Kind kind = PrimitiveType.Kind.valueOf(typeName);
         // typeName must be the name of a primitive - we will process from here
         ...
       } catch (IllegalArgumentException e) {
         // The case where typeName is not a primitive - controlo-flow by
         exception handling is evil
         ...
       }

        Each enum has a static valueOf(String) method. Enum itself defines
        static <T extends Enum<T>> T valueOf(Class<T>, String), which I presume
        the generated code for valueOf(String) in each enum class chains on to.

        Could we have an interogation function static boolean hasValue(String)
        and static <T extends Enum<T>> boolean hasValue(Class<T>, String) to go
        with this so that we can query the enum without resorting to exceptions?

      Workarround:

        Instead of resorting to try/catch, use a loop over
        PrimitiveType.Kind.values() searching for a matching name:

        boolean hasValue(String name) {
          for(Primitive.Kind kind: Primitive.Kind.values())
            if(kind.name().equals(name)) return true;
          return false;
        }

        This looks like utility code though.
      ###@###.### 10/28/04 00:53 GMT

            darcy Joe Darcy
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: