Java Enums:从文件中去除序列化任意枚举(Java Enums: de-serializing an arbitrary enum from a file)
今天一位同事遇到了一个有趣的问题,虽然我认为实际的,全景图的答案是“我们遇到这个问题的事实意味着我们做错了什么”,但我想我仍然会问这个问题。
鉴于以下情况:
public class CrazyEnumTest {
public class EnumeratedThing> {
public T myValue;
public EnumeratedThing(T value) {
myValue = value;
}
}
public static void main (String[] args) {
String className = args[0];
String enumValue = args[1];
Enum> value1 = Enum.valueOf(Class.forName(className), enumValue);
EnumeratedThing> thing1 = new EnumeratedThing(value1);
}
}
在对Enum.valueOf的调用中出现以下编译错误:
Bound mismatch: The generic method valueOf(Class, String) of type Enum is not applicable for the arguments (Class, String). The inferred type capture#1-of ? is not a valid substitute for the bounded parameter >
所以,我的问题是:是否有可能,只给予枚举类型名称的字符串表示以及其值之一的.name(),获取对应的枚举类型对象的引用作为枚举?
A co-worker ran into an interesting issue today, and while I think the actual, big-picture answer is "the fact that we're having this problem means we're doing something wrong", I figured I'd ask this anyway.
Given the following:
public class CrazyEnumTest {
public class EnumeratedThing> {
public T myValue;
public EnumeratedThing(T value) {
myValue = value;
}
}
public static void main (String[] args) {
String className = args[0];
String enumValue = args[1];
Enum> value1 = Enum.valueOf(Class.forName(className), enumValue);
EnumeratedThing> thing1 = new EnumeratedThing(value1);
}
}
I get the following compile error on the call to Enum.valueOf:
Bound mismatch: The generic method valueOf(Class, String) of type Enum is not applicable for the arguments (Class, String). The inferred type capture#1-of ? is not a valid substitute for the bounded parameter >
So, my question is: is it possible to, given only the String representation of a enumerated type name as well as the .name() of one of its values, get a reference to the corresponding enumerated type object as an Enum?
原文:https://stackoverflow.com/questions/1209184
更新时间:2020-01-26 14:34
最满意答案
编译错误告诉你Class>与Class extends Enum>不一样 Class extends Enum> 。 尝试:
Enum> value1 =
Enum.valueOf((Class extends Enum>) Class.forName(className), enumValue);
请注意,您将得到一个未经检查的强制转换警告,因此请确保className确实代表枚举。 您可以通过调用Class对象的isEnum()方法来进行检查,如下所示:
Class> enumClass = Class.forName(className);
if (enumClass.isEnum()) {
@SuppressWarnings("unchecked") // we just checked it, so it had better work
Enum> value1 = Enum.valueOf((Class extends Enum>) enumClass, enumValue);
EnumeratedThing> thing1 = new EnumeratedThing(value1);
}
当然,无论如何,你会在“ new EnumeratedThing(value1) ”上得到一个原始类型警告。
The compile error is telling you that Class> is not the same as Class extends Enum>. Try:
Enum> value1 =
Enum.valueOf((Class extends Enum>) Class.forName(className), enumValue);
Note that you'll get an unchecked cast warning, so make sure that className actually does represent an enum. You could check by calling the isEnum() method on the Class object, like this:
Class> enumClass = Class.forName(className);
if (enumClass.isEnum()) {
@SuppressWarnings("unchecked") // we just checked it, so it had better work
Enum> value1 = Enum.valueOf((Class extends Enum>) enumClass, enumValue);
EnumeratedThing> thing1 = new EnumeratedThing(value1);
}
Of course, you'll get a raw type warning on "new EnumeratedThing(value1)" anyway.
2012-04-23
相关问答
这里有一个简单的例子: enum RoundingMode {
UP {
public double round(double d) {
return Math.ceil(d);
}
},
DOWN {
public double round(double d) {
return Math.floor(d);
}
};
public abstra
...
这花了一段时间,但我找到了一个干净的解决方案来解决这个问题。 我生成了一个自定义对象(BitmapDataObject),它实现了Serializable并具有一个byte []来存储来自原始位图的PNG数据。 使用这种方式,数据被正确地存储在ObjectOutputStream / ObjectInputStream中 - 这有效地允许将一个Bitmap对象序列化和反序列化,方法是将其作为PNG存储在自定义对象的byte []中。 下面的代码解决了我的查询。 private String tit
...
最后我找到了解决方案。 我必须用@JsonSerialize(using = OrderTypeSerializer.class)和@JsonSerialize(using = OrderTypeSerializer.class)自定义序列化程序来注释枚举: public class OrderTypeSerializer extends JsonSerializer {
@Override
public void serialize(OrderType value
...
由于类型安全限制,我认为没有一个清晰的解决方案。 不过,如果你依靠Java使用类型擦除这个事实来解决这个问题,那么下面是一个似乎可行的解决方案: class EnumSerializer() extends Serializer[Enum[_]] {
override def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Enum[_]] = {
// using Json4sFak
...
我能找到解决这个问题的唯一方法是为枚举创建一个AbstractSingleValueConverter,然后用xstream注册它。 public class SingleValueEnumConverter extends AbstractSingleValueConverter
{
private final Class enumType;
public SingleValueEnumConverter(Class type)
{
this.enumT
...
编译错误告诉你Class>与Class extends Enum>不一样 Class extends Enum> 。 尝试: Enum> value1 =
Enum.valueOf((Class extends Enum>) Class.forName(className), enumValue);
请注意,您将得到一个未经检查的强制转换警告,因此请确保className确实代表枚举。 您可以通过调用Class对象的isEnum()方法来进行检查,如下所示:
...
尝试以下操作: List results = new ArrayList();
FileInputStream fis = new FileInputStream("cool_file.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
try {
while (true) {
results.add(ois.readObjec
...
无法正确理解您的要求。 引入一个拦截器的方法如下所示,该拦截器使用包下的任何注释扫描类。 public class Interceptor implements HandlerInterceptor{
public boolean preHandle(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
ClassPathScanningCandidateCompone
...