-
Enhancement
-
Resolution: Won't Fix
-
P4
-
5.0
###@###.### 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
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
- duplicates
-
JDK-8133234 Exception safe enum transformation.
- Closed
- relates to
-
JDK-8073381 need API to get enum's values without creating a new array
- Open