jaxb
  1. jaxb
  2. JAXB-476

PATCH : Exceptions thrown by unmarshal callback methods are currently ignored

    Details

    • Type: Task Task
    • Status: Resolved
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 2.1.6
    • Fix Version/s: 2.1.10
    • Component/s: runtime
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      476

      Description

      The JAXB spec says, regarding unmarshal event callbacks (section 4.4.1) : "An
      event callback method throwing an exception terminates the current unmarshal
      process."

      However, currently exceptions thrown by the [before|after]Unmarshal() methods
      are ignored because JaxBeanInfo.invokeUnmarshallCallback() reports them using
      UnmarshallingContext.handleError(Exception). This ultimately calls the
      handleEvent() method with a severity of 'ERROR' (not FATAL_ERROR) and
      'canRecover' set to true - this is silently swallowed by the default
      ValidationEventHandler, rather than terminating the unmarshal process. This goes
      against the spec, and I guess would go against the behaviour the developer would
      expect (at least in my case)!

      A proposed patch is attached, which simply changes the
      JaxBeanInfo.invokeUnmarshallCallback() method to report exceptions with
      'canRecover' set to false, which will ensure that an exception is thrown and the
      unmarshal process terminated, no matter what.

      The following test case re-creates this bug:


      public class UnmarshalCallbackMethodsTest {

      public static void main(String[] args) throws JAXBException

      { JAXBContext context = JAXBContext.newInstance(Cabbage.class); String xml = "<cabbage type='red' />"; Cabbage cabbage = (Cabbage) context.createUnmarshaller().unmarshal(new StringReader(xml)); System.out.println("Unmarshalled "+cabbage.type+" cabbage without incident"); }

      }

      @XmlRootElement
      class Cabbage {

      @XmlAttribute
      public String type;

      void beforeUnmarshal(Unmarshaller unmarshaller, Object parent)

      { System.out.println("beforeUnmarshal called, about to throw an exception..."); throw new RuntimeException("This exception is ignored..."); }

      void afterUnmarshal(Unmarshaller unmarshaller, Object parent)

      { System.out.println("afterUnmarshal called, about to throw an exception..."); throw new RuntimeException("This exception is also ignored..."); }

      }


      The output generated by the test case is:


      beforeUnmarshal called, about to throw an exception...
      afterUnmarshal called, about to throw an exception...
      Unmarshalled red cabbage without incident


      Credit to Philippa Tyley for her help in defining this bug!

        Activity

        Hide
        robertotyley added a comment -

        Created an attachment (id=235)
        Proposed patch to ensure exceptions in callbacks are not ignored

        Show
        robertotyley added a comment - Created an attachment (id=235) Proposed patch to ensure exceptions in callbacks are not ignored
        Hide
        robertotyley added a comment -

        I've completed a Sun Contributor Agreement (SCA) and submitted it to
        sun_ca@sun.com as of 12th January 2008. Any positive/negative feedback on the
        patch (whether it's misguided or a just a bit pointless) is welcome!

        Show
        robertotyley added a comment - I've completed a Sun Contributor Agreement (SCA) and submitted it to sun_ca@sun.com as of 12th January 2008. Any positive/negative feedback on the patch (whether it's misguided or a just a bit pointless) is welcome!
        Hide
        Pavel Bucek added a comment -

        Hi,

        thanks for contributing. Committed to 2.1.10.

        Show
        Pavel Bucek added a comment - Hi, thanks for contributing. Committed to 2.1.10.
        Hide
        robertotyley added a comment -

        The "Notable Changes between 2.1.9 to 2.1.10" report
        (https://jaxb.dev.java.net/nonav/2.1.10/docs/changelog2.html) is based off the
        'Target milestone' property, which was unset for this bug so it didn't show up
        in the list!

        Show
        robertotyley added a comment - The "Notable Changes between 2.1.9 to 2.1.10" report ( https://jaxb.dev.java.net/nonav/2.1.10/docs/changelog2.html ) is based off the 'Target milestone' property, which was unset for this bug so it didn't show up in the list!
        Hide
        realsonic3 added a comment -

        Hello. Regarding to the previous comment (robertotyley added a comment - 29/Mar/09 03:24 AM)... is this issue is really solved?

        Show
        realsonic3 added a comment - Hello. Regarding to the previous comment (robertotyley added a comment - 29/Mar/09 03:24 AM)... is this issue is really solved?
        Hide
        Iaroslav Savytskyi added a comment -

        I see patch changes in the code.

        Show
        Iaroslav Savytskyi added a comment - I see patch changes in the code.

          People

          • Assignee:
            jaxb-issues
            Reporter:
            robertotyley
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: