从 HashMap 设置对象字段

发布于 2024-12-27 08:43:03 字数 526 浏览 3 评论 0原文

是否有一个库可以执行以下操作?:

给定一个对象和一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扛起拖把扫天下 2025-01-03 08:43:04

查看 Apache Commons BeanUtils

org.apache.commons.beanutils.BeanUtils.populate(Object bean, Map properties)

Javadoc:
根据指定的名称/值对填充指定 bean 的 JavaBeans 属性。该方法使用 Java 反射 API 来识别相应的“属性 setter”方法名称,并处理 String、boolean、int、long、float 和 double 类型的 setter 参数。

Look at Apache Commons BeanUtils

org.apache.commons.beanutils.BeanUtils.populate(Object bean, Map properties)

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.

疾风者 2025-01-03 08:43:04

更好地使用 BeanUtils 类:

public Object setData(Object object, HashMap<String, Object> fields) {
   for(Entry<String, Object> entry : fields.entrySet()) {
      BeanUtils.setProperty(object, entry.getKey(), entry.getValue());
   }
   return object;
}

Better use BeanUtils class:

public Object setData(Object object, HashMap<String, Object> fields) {
   for(Entry<String, Object> entry : fields.entrySet()) {
      BeanUtils.setProperty(object, entry.getKey(), entry.getValue());
   }
   return object;
}
去了角落 2025-01-03 08:43:04

我有一个很久以前编写的 BeanAsMap 类。 asMap 方法返回一个 Map,它是 Java bean (POJO) 上的视图。您可以在该 Map 上调用 putAll,将其传递给您要从中复制数据的 Map

请随意使用我上面提到的代码。

例子:

MyClass bean = ...;
Map<String, Object> inputData = ...;

Map<String, Object> view = BeanAsMap.asMap(bean);
view.putAll(inputData);

I have a BeanAsMap class that I wrote a long time ago. The method asMap returns a Map that is a view on a Java bean (POJO). You can call putAll on that Map, passing it the Map that you want to copy data from.

Feel free to use my code mentioned above.

Example:

MyClass bean = ...;
Map<String, Object> inputData = ...;

Map<String, Object> view = BeanAsMap.asMap(bean);
view.putAll(inputData);
残花月 2025-01-03 08:43:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文