XStream
  1. XStream
  2. XSTR-74

Converter for Throwable that retains stack trace

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.0.1
    • Component/s: Converters
    • Labels:
      None

      Description

      Looking at the source to Throwable, it appears that the JVM does some special 'magic' to set the stack trace.

      — from Brian Oxley —

      > I had trouble getting a simple unit test to pass for serializing exceptions in XStream 1.0. The stack trace didn't make it.
      >
      > I checked the issue tracker and did not see anything about this, and my Google searches of lists.codehaus.org and searching the full archive weren't fruitful.
      >
      > Looking into the Throwable from JDK 1.4.2_02, I see (code at bottom) which makes me expect it to "just work". However the XML for serializing a Throwable is:
      >
      > <java.lang.Throwable>
      > <cause>
      > <cause reference=".."/>
      > <detailMessage>some message</detailMessage>
      > </cause>
      > <detailMessage>some other message</detailMessage>
      > </java.lang.Throwable>
      >
      > For whatever reason, the JDK uses a cause of "this" when a Throwable has no cause, and XStream is serializing the field's reference rather than what getCause() returns (which is "null" when cause is "this").
      >
      > What's the best way for me to maintainably code serialization of exceptions with XStream?
      >
      >
      > Cheers,
      > --binkley
      >
      >
      > Code:
      >
      > /**
      > * Native code saves some indication of the stack backtrace in this slot.
      > */
      > private transient Object backtrace;
      >
      > /**
      > * Specific details about the Throwable. For example, for
      > * <tt>FileNotFoundException</tt>, this contains the name of
      > * the file that could not be found.
      > *
      > * @serial
      > */
      > private String detailMessage;
      >
      > /**
      > * The throwable that caused this throwable to get thrown, or null if this
      > * throwable was not caused by another throwable, or if the causative
      > * throwable is unknown. If this field is equal to this throwable itself,
      > * it indicates that the cause of this throwable has not yet been
      > * initialized.
      > *
      > * @serial
      > * @since 1.4
      > */
      > private Throwable cause = this;
      >
      > /**
      > * The stack trace, as returned by

      {@link #getStackTrace()}

      .
      > *
      > * @serial
      > * @since 1.4
      > */
      > private StackTraceElement[] stackTrace;
      > /*
      > * This field is lazily initialized on first use or serialization and
      > * nulled out when fillInStackTrace is called.
      > */

        People

        • Assignee:
          Unassigned
          Reporter:
          Joe Walnes
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: