JDK-5034509 : (enum) Add a function to query if enum name is a valid enum constant name
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2004-04-20
  • Updated: 2017-05-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Related Reports
Duplicate :  
Relates :  
###@###.### 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)

Operating System Configuration Information (be specific):

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?


  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

An alternative might be to have a non-throwing variant of valueOf(). For example, in an enum type of E, the following method might be added: public static Optional<E> getValueOf(String name) One can do this today (Java 8) using the array returned by values(): Optional<E> match = Arrays.stream(E.values()).filter(e -> name.equals(e.name())).findFirst(); Of course, this does a linear search of the Enum values, which might be slow, though we generally expect there to be relatively few enum values, don't we? This could be slightly optimized by JDK-8073381, which proposes to add a method to get the enum values as a list or a stream. In this case the Arrays.stream(E.values()) would be replaced by an API call, which would make the code shorter, and it would avoid creating a copy of the values array every time.

EVALUATION Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=18752

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang

EVALUATION Will consider such a convenience method for a future release. ###@###.### 2004-04-28