This solution only works if your classes contain a property that acts as an ID (i.e. annotated with @XmlID).
One of the classes has to implement the interface CycleRecoverable which provides the sole method Object onCycleDetected(Context ctx).
When overriding this method return an object that only contains the ID of that class e.g.:class A implements CycleRecoverable {
public Object onCycleDetected(Context ctx) { return new A(this.id); }
}