如何生成源$ C ​​$ C创建一个对象我调试? [英] How do I generate the source code to create an object I'm debugging?

查看:159
本文介绍了如何生成源$ C ​​$ C创建一个对象我调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我典型场景:


  1. 遗留code,我的工作只有在生产客户端具有一个bug

  2. 我附加一个调试器,并找出如何重现上的献出的系统的及其的输入问题。但是,我不知道为什么错误发生呢。

  3. 现在,我想要写我的本地系统上的自动化测试,试图重现然后修正错误

  1. The legacy code I work on has a bug that only a client in production is having
  2. I attach a debugger and figure out how to reproduce the issue on their system given their input. But, I don't know why the error is happening yet.
  3. Now I want to write an automated test on my local system to try and reproduce then fix the bug

这最后一步是真的很难。该输入可以是非常复杂的,有大量的数据给它的。手工创建(例如输入:点P =新的P(); p.setX(X); p.setY(X); 想象这样做1000次创建对象)是非常繁琐和容易出错。事实上,你可能会注意到有一个在我刚给的例子一个错字。

That last step is really hard. The input can be very complex and have a lot of data to it. Creating the input by hand (eg: P p = new P(); p.setX("x"); p.setY("x"); imagine doing this 1000 times to create the object) is very tedious and error prone. In fact you may notice there's a typo in the example I just gave.

有没有从我的调试器一个破发点采取现场和生成源$ C ​​$ C,将创建一个对象,填充以同样的方式自动化的方式?

我想出的唯一事情是序列化此输入(使用西河,例如)。我可以在保存到一个文件,并在一个自动化测试读回。这有一个主要问题:如果在某些方面类的变化(例如:现场/的getter / setter名称重命名),我将无法再反序列化对象。换句话说,测试是极其脆弱的。

The only thing I've come up with is to serialize this input (using Xstream, for example). I can save that to a file and read it back in in an automated test. This has a major problem: If the class changes in certain ways (eg: a field/getter/setter name is renamed), I won't be able to deserialize the object anymore. In other words, the tests are extremely fragile.

推荐答案

Java标准序列化以及知道是不是非常有用的对象时改变自己的版本(内容领域的命名)。其优良的快速演示项目。结果

Java standard serialisation is well know to be not very usefull when objects change their version ( content, naming of fields). Its fine for quick demo projects.

更适合您的需求,是objetcs支持你自己的(二进制)自定义序列化的办法:结果
这并不困难,使用 DataOutputStream类来写出对象的所有领域。但现在引进versiong,先写了 VERSIONID 。有只有一个版本的对象,写出VERSIONID 1.这样,你可以在以后,当你介绍你的objetcs的改变,删除字段,添加字段,提高版本号。结果

More suitable for your needs, is the approach that objetcs support your own (binary) custom serialisation:
This is not difficult, use DataOutputStream to write out all fields of an object. But now introduce versiong, by first writing out a versionId. Objects that have only one version, write out versionId 1. That way you can later, when you have to introduce a change in your objetcs, remove fields, add fields, raise the version number.

这样的 ICustomSerializable 然后将首先读出从输入流的版本号,在readObject()方法,并根据不同的版本ID叫readVersionV1()或例如: readVersionV2()

Such a ICustomSerializable will then first read out the version number from the input stream, in a readObject() method, and depending on the version Id call readVersionV1() or e.g readVersionV2().

public Interface ICustomSerializable {
  void writeObject(DataOutputStream dos);
  Object readObject(DataInputStream dis);
}

public Class Foo {
  public static final VERSION_V1 = 1;
  public static final VERSION_V2 = 2;

  public static final CURRENT_VERSION = VERSION_V2;
  private int version;

  private int fooNumber;
  private double fooDouble;

  public void writeObject(DataOutputStream dos) {
     dos.writeInt(this.version);
      if (version == VERSION_V1) {
          writeVersionV1(dos);
      } else (version == VERSION_V2) {
          writeVersionV2(dos);
      } else {
         throw new IllegalFormatException("unkown version: " + this.version);
      }
  }
  public void writeVersionV1(DataOutputStream dos) {
        writeInt(this.fooNumber);
        writeDouble(this.fooValue);
  }
}

另外getter和setter,并初始化需要的版本CURRENT_VERSION构造。

Further getter and setter, and a constructor with initialised the version to CURRENT_VERSION is needed.

这种serialisazion是安全的,如果您更改或添加也相应的读写版本重构。用于使用来自外部库类不UND的控制研究复杂的对象,也可以是更多的工作,但字符串,列表很容易序列

This kind of serialisazion is safe to refactoring if you change or add also the appropriate read and write version. For complex objects using classes from external libs not und your controll, it can be more work, but strings, lists are easily serialized.

这篇关于如何生成源$ C ​​$ C创建一个对象我调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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