39

Does anyone know whether it is possible to save the state of an object while debugging?

I would like to compare the state of an object in two different execution cycles.

Bonus Question: Anyone with experience in writing IntelliJ plugins? Does the IntelliJ SDK allow to access debug values in debug mode? Read them out of IntelliJ cache and write them to disk?

Moritz Schmitz v. Hülst
  • 2,260
  • 3
  • 24
  • 43
  • 1
    I came across question, I'm developing plugin (I'm a rookie) that accesses the selected node in debugger, just to let you know, yes you can access debug data from the debug node. All you need is to write your own `DebugAction` and register it in plugin.xml – Nikola Despotoski Aug 26 '16 at 12:16

6 Answers6

29

As a very simple solution you can use the Fully Expand Tree Node action for objects in Variables or Watches views. This action is bound to Numpad * key (*) by default and opens the whole object tree. Then you select all the elements of opened object tree with shift and copy them to clipboard.

Andrew Terekhine
  • 1,341
  • 18
  • 17
13

To expand on Josep's answer, your best bet is to import Google's Gson library into your project and run:

Gson gson = new Gson();
gson.toJson(yourObject);

Then copy the value from the result.

Here's an example on IntelliJ IDEA:

example

maaw
  • 978
  • 8
  • 11
7

My best workaround to save an object state is to use the Evaluate tool when I have the object in the desired state and using Gson library convert it to a JSON, then at the test setup I copy the JSON as a String and convert it to a Java object again. May be a bit rude but for really large and complex objects it can save you a lot of time.

  • 3
    Definitely better than copying to plain text. I wish this was an option within the debugger. – maaw Dec 14 '19 at 12:02
4

In IntelliJ 2016, you have an option to "View Text" when you right click on a variable in "Variables" window while debugging

Anton Skovorodko
  • 567
  • 1
  • 8
  • 19
1

If you are OK with saving each and every object (you cannot select which objects are saved) then there is a plugin for this: https://plugins.jetbrains.com/plugin/14974-breakpoint-exporter-importer-with-variable-information

Csa77
  • 449
  • 9
  • 17
0

Add below sample code and it will show the data in json form:

ObjectMapper mapper = new ObjectMapper();
  try {
    String sample = mapper.writeValueAsString(Your object);
    System.out.println(sample);
  } catch (JsonProcessingException e) {
    e.printStackTrace();
  }