JAL-2405 check for circular reference in SequenceGroup.context
[jalview.git] / src / jalview / datamodel / SequenceGroup.java
index a1579c5..4f9d530 100755 (executable)
@@ -1347,13 +1347,25 @@ public class SequenceGroup implements AnnotatedCollectionI
   private AnnotatedCollectionI context;
 
   /**
-   * set the alignment or group context for this group
+   * Sets the alignment or group context for this group
    * 
-   * @param context
+   * @param ctx
+   * @throws IllegalArgumentException
+   *           if setting the context would result in a circular reference chain
    */
-  public void setContext(AnnotatedCollectionI context)
+  public void setContext(AnnotatedCollectionI ctx)
   {
-    this.context = context;
+    AnnotatedCollectionI ref = ctx;
+    while (ref != null)
+    {
+      if (ref == this)
+      {
+        throw new IllegalArgumentException(
+                "Circular reference in SequenceGroup.context");
+      }
+      ref = ref.getContext();
+    }
+    this.context = ctx;
   }
 
   /*