1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind;
  27 
  28 import javax.xml.bind.Marshaller;
  29 
  30 /**
  31  * Optional interface that can be implemented by JAXB-bound objects
  32  * to handle cycles in the object graph.
  33  *
  34  * <p>
  35  * As discussed in <a href="https://javaee.github.io/jaxb-v2/doc/user-guide/ch03.html#annotating-your-classes-mapping-cyclic-references-to-xml">
  36  * the users' guide</a>, normally a cycle in the object graph causes the marshaller to report an error,
  37  * and when an error is found, the JAXB RI recovers by cutting the cycle arbitrarily.
  38  * This is not always a desired behavior.
  39  *
  40  * <p>
  41  * Implementing this interface allows user application to change this behavior.
  42  *
  43  * @since JAXB 2.1 EA2
  44  * @author Kohsuke Kawaguchi
  45  */
  46 public interface CycleRecoverable {
  47     /**
  48      * Called when a cycle is detected by the JAXB RI marshaller
  49      * to nominate a new object to be marshalled instead.
  50      *
  51      * @param context
  52      *      This object is provided by the JAXB RI to inform
  53      *      the object about the marshalling process that's going on.
  54      *
  55      *
  56      * @return
  57      *      the object to be marshalled instead of {@code this} object.
  58      *      Or return null to indicate that the JAXB RI should behave
  59      *      just like when your object does not implement {@link CycleRecoverable}
  60      *      (IOW, cut the cycle arbitrarily and try to go on.)
  61      */
  62     Object onCycleDetected(Context context);
  63 
  64     /**
  65      * This interface is implemented by the JAXB RI to provide
  66      * information about the on-going marshalling process.
  67      *
  68      * <p>
  69      * We may add more methods in the future, so please do not
  70      * implement this interface in your application.
  71      */
  72     public interface Context {
  73         /**
  74          * Returns the marshaller object that's doing the marshalling.
  75          *
  76          * @return
  77          *      always non-null.
  78          */
  79         Marshaller getMarshaller();
  80     }
  81 }