复制java中类似类之间的字段 [英] Copy fields between similar classes in java

查看:289
本文介绍了复制java中类似类之间的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对类,其中一个字段是另一个字段的子集,超集类的getter都是可预测命名的( getFoo()) 。有没有办法有效地将所有公共字段从超集类复制到子集类,或者至少自动生成代码来执行此操作。

I have pairs of classes where the fields of one is a subset of the fields of another and the getters of the superset classes are all predictably named (getFoo()). Is there some way to efficiently copy all the common fields over from the superset class to the subset class, or at least auto-generate the code to do so.

我应该注意:


  • 出于各种原因,我不能编辑超集类,我也不能一直使用它们以避免必须进行数据复制。

  • 我可以在子集类中创建新方法,但我无法更改它们字段。

  • 我们有很多这样的对,有些类有很多字段,所以手工做这件事至少可以说是笨拙的。

  • 一位同事提出了一种创建泛型复制方法的方法,该方法使用java反射来获取任意两个类,以字符串形式迭代,执行字符串操作以确定getter名称,然后执行它以自动设置子集类中的字段。这很糟糕,但似乎有效。我真的希望有更好的方法。

  • For various reasons, I can't edit the superset classes, nor can I just use them throughout to avoid having to do the data copy.
  • I can potentially create new methods in the subset classes, but I can't change their fields.
  • We have dozens of these pairs, and some of the classes have many many fields so doing this by hand is unwieldy to say the least.
  • A colleague has come up with a method to create a generic copy method that uses java reflection to take any two classes, iterate through the fields as Strings, do string manipulation to determine the getter name, and then execute it to automatically set the field in the subset class. It's awful, but it appears to work. I'm really hoping there's a better way.

编辑:一些简单的代码按要求

public class SuperClass {
  private int foo;
  private int bar;
  private float bat;
  public int getFoo() { return foo; }
  public int getBar() { return bar; }
  public float getBat() { return bat; }
}

public class SubClass {
  private int foo;
  private float bat;
}

//wanted
public static copySuperFieldsToSubMethod(Object super, Object sub) { ??? }

// also acceptable would be some way to autogenerate all the assignment 
// functions needed


推荐答案

您可以使用Spring Framework中的 BeanUtils 类来执行此操作。它可能不一定比基于反射的技术更有效,但编码肯定很简单。我希望你需要做的就是:

You could use the BeanUtils class in the Spring Framework to do this. It may not necessarily be any more efficient than your reflection-based technique, but it's certainly simple to code. I expect that all you would need to do is:

BeanUtils.copyProperties(source, target);

此方法的Javadoc位于 http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/BeanUtils.html#copyProperties(java.lang.Object中,%20java.lang.Object )

Javadoc for this method is available at http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/BeanUtils.html#copyProperties(java.lang.Object,%20java.lang.Object)

如果不合适,您还可以考虑使用 BeanWrapper Spring框架中的/ BeanWrapperImpl ,用于遍历类的属性。这比使用低级反射API更简单。

If that doesn't suit, you could also consider using BeanWrapper / BeanWrapperImpl in the Spring Framework to iterate through the properties of your classes. That would be simpler than using low-level reflection APIs.

这篇关于复制java中类似类之间的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆