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: 5034509
Votes 2
Synopsis (enum) Add a function to query enum name is a valid enum constant name
Category java:classes_lang
Reported Against tiger-beta2
Release Fixed
State 5-Cause Known, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 20-APR-2004
Description
  xxxxx@xxxxx   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):
   customer  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.
  xxxxx@xxxxx   10/28/04 00:53 GMT
Work Around
N/A
Evaluation
Will consider such a convenience method for a future release.

  xxxxx@xxxxx   2004-04-28
Contribution forum : https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=18752
Posted Date : 2007-02-26 17:58:26.0
Comments
  
  Include a link with my name & email   

Submitted On 17-JAN-2007
grandinj
As a different solution to the problem, how about making the try/catch block cleaner by  making Enum.valueOf() throw an exception that is a subclass of IllegalArgumentException, for example "UnknownEnumNameException".
This will preserve backwards compatibility and make the resulting code cleaner.


Submitted On 26-FEB-2007
MiguelM
When I've done this kind of thing, I've done it like this:
Enum Dir { NORTH, SOUTH, EAST, WEST;
  static Set<String> allDirs = new HashSet<String>();
  static {
    for ( Dir dir: Dir.values() )
      allDirs.add(dir.toString());
  }
  public boolean hasValue(String dirName) {
    return allDirs.contains(dirName);
  }
}

This is a faster workaround.


Submitted On 28-FEB-2007
MiguelM
Here's a simpler workaround:

public enum Dir {
  NORTH, SOUTH, EAST, WEST;
  public static boolean isValue(String valueName) {
    try {
      valueOf(Dir.class, valueName);
        return true;
    } catch (IllegalArgumentException e) {
      return false;
    }
  }
}

Some people would consider catching the illegal argument exception unsafe, but when you look at the code that I'm calling, the only way it  would get thrown is if the string is not a valid value.



PLEASE NOTE: JDK6 is formerly known as Project Mustang