java.lang.IllegalArgumentException: No enum const class 即使迭代 values() 工作正常,原因是什么? [英] What is the reason for java.lang.IllegalArgumentException: No enum const class even though iterating through values() works just fine?
问题描述
这个问题基本上是我上一个问题的延伸.我问了上一个问题,以确保在类加载时填充 Enum 常量.这是我的类,添加了一个简单的方法 getByName
:
This question is basically an extension of my previous question . I asked the previous question to be sure that the Enum constants are populated when the class loads . Here's is my class again with the addition of a simple method getByName
:
public enum PropName {
CONTENTS("contents"),
USE_QUOTES("useQuotes"),
ONKEYDOWN("onkeydown"),
BROWSER_ENTIRE_TABLE("browseEntireTable"),
COLUMN_HEADINGS("columnHeadings"),
PAGE_SIZE("pageSize"),
POPUP_TITLE("popupTitle"),
FILTER_COL("filterCol"),
SQL_SELECT("sqlSelect"),
;
private String name;
private PropName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static PropName getByName(String name){
return PropName.valueOf(name);
}
}
对方法 getByName("columnHeadings")
的调用抛出 java.lang.IllegalArgumentException: No enum const class labware.web.component.limsgrid.PropName.columnHeadings
但是如果我用下面的代码替换这个方法,它就可以工作.
A call to the method getByName("columnHeadings")
is throwing java.lang.IllegalArgumentException: No enum const class labware.web.component.limsgrid.PropName.columnHeadings
but if I replace this method with the following code it just works .
public static PropName getByName(String name){
for(PropName prop : values()){
if(prop.getName().equals(name)){
return prop;
}
}
throw new IllegalArgumentException(name + " is not a valid PropName");
}
对我在这里做错了什么有任何想法吗?
Any ideas as to what I am doing wrong here ?
推荐答案
Enum.valueOf()
只检查常量名,所以需要传入"COLUMN_HEADINGS"
而不是列标题".您的 name
属性与 Enum 内部结构无关.
Enum.valueOf()
only checks the constant name, so you need to pass it "COLUMN_HEADINGS"
instead of "columnHeadings". Your name
property has nothing to do with Enum internals.
解决评论中的问题/疑虑:
To address the questions/concerns in the comments:
枚举的内置"(隐式声明)valueOf(String name)
方法将查找具有该确切名称的枚举常量.如果您的输入是columnHeadings",则您(至少)有三个选择:
The enum's "builtin" (implicitly declared) valueOf(String name)
method will look up an enum constant with that exact name. If your input is "columnHeadings", you have (at least) three choices:
- 暂时忘记命名约定,只为您的常量命名,因为它最有意义:
enum PropName { contents, columnHeadings, ...}
.这显然是最方便的. - 在调用
valueOf
之前,将您的驼峰式输入转换为 UPPER_SNAKE_CASE,如果您真的很喜欢命名约定. - 实现您自己的查找方法而不是内置的
valueOf
来查找输入的相应常量.如果同一组常量有多个可能的映射,这将最有意义.
- Forget about the naming conventions for a bit and just name your constants as it makes most sense:
enum PropName { contents, columnHeadings, ...}
. This is obviously the most convenient. - Convert your camelCase input into UPPER_SNAKE_CASE before calling
valueOf
, if you're really fond of naming conventions. - Implement your own lookup method instead of the builtin
valueOf
to find the corresponding constant for an input. This makes most sense if there are multiple possible mappings for the same set of constants.
这篇关于java.lang.IllegalArgumentException: No enum const class 即使迭代 values() 工作正常,原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!