Class HeapDump.InstanceBuilder

java.lang.Object
org.graalvm.tools.insight.heap.HeapDump.InstanceBuilder
Enclosing class:
HeapDump

public final class HeapDump.InstanceBuilder extends Object
Fills data for new object instance to put into the HeapDump.

Builder builder = HeapDump.newHeapBuilder(new FileOutputStream(hprof));
builder.dumpHeap((heap) -> {
    final ClassInstance classActor = heap.newClass("cartoons.Actor").
        field("name", String.class).
        field("friend", Object.class).
        field("age", int.class).
        dumpClass();
    final ObjectInstance jerry = heap.newInstance(classActor).
        put("name", heap.dumpString("Jerry")).
        putInt("age", 47).
        // field 'friend' remains null
        dumpInstance();
    final ObjectInstance tom = heap.newInstance(classActor).
        put("name", heap.dumpString("Tom")).
        put("friend", jerry).
        putInt("age", 32).
        dumpInstance();
    final ClassInstance classMain = heap.newClass("cartoons.Main").
        field("tom", classActor).
        field("jerry", classActor).
        field("thread", java.lang.Thread.class).
        dumpClass();

    HeapDump.InstanceBuilder mainBuilder = heap.newInstance(classMain);
    final ObjectInstance main = mainBuilder.id();
    mainBuilder.put("tom", tom).put("jerry", jerry);

    ObjectInstance cathingThread = heap.newThread("Catching Jerry").
        addStackFrame(classActor, "tom", "Actor.java", -1, jerry, tom, main).
        addStackFrame(classMain, "main", "Main.java", -1, main).
        dumpThread();

    mainBuilder.put("thread", cathingThread).dumpInstance();
});
Since:
21.1
See Also:
  • Method Details

    • put

      Puts reference to another object as a value of the field.
      Parameters:
      name - the name of the field
      value - reference to object in the HeapDump
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putByte

      public HeapDump.InstanceBuilder putByte(String name, byte value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putShort

      public HeapDump.InstanceBuilder putShort(String name, short value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putInt

      public HeapDump.InstanceBuilder putInt(String name, int value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putLong

      public HeapDump.InstanceBuilder putLong(String name, long value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putFloat

      public HeapDump.InstanceBuilder putFloat(String name, float value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putDouble

      public HeapDump.InstanceBuilder putDouble(String name, double value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putBoolean

      public HeapDump.InstanceBuilder putBoolean(String name, boolean value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • putChar

      public HeapDump.InstanceBuilder putChar(String name, char value)
      Puts value into a field.
      Parameters:
      name - the name of the field
      value - primitive value to assign to the field
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the field doesn't exist or its type isn't correct
      Since:
      21.1
    • dumpInstance

      public HeapDump.ObjectInstance dumpInstance() throws UncheckedIOException
      Dumps the gathered field values into the HeapDump.
      Returns:
      object representing the written instance
      Throws:
      UncheckedIOException - when an I/O error occurs
      Since:
      21.1
      See Also:
    • id

      The ID assigned to the instance. Allows one to obtain ID of an instance before it is dumped into the HeapDump.
      HeapDump.InstanceBuilder mainBuilder = heap.newInstance(classMain);
      final ObjectInstance main = mainBuilder.id();
      mainBuilder.put("tom", tom).put("jerry", jerry);
      
      ObjectInstance cathingThread = heap.newThread("Catching Jerry").
          addStackFrame(classActor, "tom", "Actor.java", -1, jerry, tom, main).
          addStackFrame(classMain, "main", "Main.java", -1, main).
          dumpThread();
      
      mainBuilder.put("thread", cathingThread).dumpInstance();
      
      Returns:
      object reference for the instance that's going to be built when dumpInstance() method is invoked
      Since:
      21.1