从 HashMap 设置对象字段
是否有一个库可以执行以下操作?:
给定一个对象和一个 HashMap,它枚举 Hashmap 的键,并在对象中查找这些键的设置器并设置关联的值。看起来像这样:
public Object setData(Object object, HashMap<String, Object> fields) {
for (Entry<String, Object> entry : fields.entrySet()) {
Method m = object.getClass().getMethod("set" + entry.getKey(), entry.getValue().getClass());
if (m != null) {
m.invoke(object, entry.getValue());
}
}
return object;
}
这个任务乍一看很简单,但我希望有人已经注意到了一些细微差别。如您所知,重新发明轮子(好轮子)是一个糟糕的方法。
Is there a library that can do the following?:
Given an Object and a HashMap, it enumerates the keys of the Hashmap and looks up the setters for these keys in the Object and sets the associated values. Something looking like that:
public Object setData(Object object, HashMap<String, Object> fields) {
for (Entry<String, Object> entry : fields.entrySet()) {
Method m = object.getClass().getMethod("set" + entry.getKey(), entry.getValue().getClass());
if (m != null) {
m.invoke(object, entry.getValue());
}
}
return object;
}
The task looks simple at the first look but there are some nuances that I hope someone has already taken care of. As you know, reinventing the wheel (the good wheel) is a bad approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 Apache Commons BeanUtils
Javadoc:
根据指定的名称/值对填充指定 bean 的 JavaBeans 属性。该方法使用 Java 反射 API 来识别相应的“属性 setter”方法名称,并处理 String、boolean、int、long、float 和 double 类型的 setter 参数。
Look at Apache Commons BeanUtils
Javadoc:
Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs. This method uses Java reflection APIs to identify corresponding "property setter" method names, and deals with setter arguments of type String, boolean, int, long, float, and double.
更好地使用
BeanUtils
类:Better use
BeanUtils
class:我有一个很久以前编写的
BeanAsMap
类。asMap
方法返回一个Map
,它是 Java bean (POJO) 上的视图。您可以在该Map
上调用putAll
,将其传递给您要从中复制数据的Map
。请随意使用我上面提到的代码。
例子:
I have a
BeanAsMap
class that I wrote a long time ago. The methodasMap
returns aMap
that is a view on a Java bean (POJO). You can callputAll
on thatMap
, passing it theMap
that you want to copy data from.Feel free to use my code mentioned above.
Example:
BeanUtils 没问题。
但是,作为良好的实践,我不会编写使用反射的代码。或者作为我的最后一个解决方案,如果没有找到其他解决方案。
无法在 Eclipse 等 IDE 中跟踪此代码(无调用层次结构),从而使开发人员认为 setter 从未被调用。他可以破坏你的代码并且仍然可以编译。
像这样太高的抽象级别会使代码难以理解。
当编写此类内容时,被混淆的代码将被混淆器本身破坏。
最好的解决方案是重新考虑使用反射来设置对象字段。
BeanUtils is fine.
But, as good practice, i would not write code that use reflection. Or as the last solution i have, if none other has been found.
This code cannot be tracked in IDE like Eclipse (no call hierarchy), making the developer think that the setters are never called. He can break your code and that will still compile.
Too high level of abstraction like this makes the code difficult to understand.
Code that is being obfuscated will be broken by the obfuscator itself when writting such things.
Best solution would be to rethink the use of reflection to set the object fields.
查看http://commons.apache.org/beanutils/,特别是
BeanUtils。 populate()
:http://commons.apache.org/beanutils/v1.8.3/apidocs/index.html
Check out http://commons.apache.org/beanutils/, in particular
BeanUtils.populate()
:http://commons.apache.org/beanutils/v1.8.3/apidocs/index.html