11/*******************************************************************************
2- * Copyright 2011, 2017 Data Access Technologies, Inc. (Model Driven Solutions)
2+ * Copyright 2011, 2018 Data Access Technologies, Inc. (Model Driven Solutions)
33 * All rights reserved worldwide. This program and the accompanying materials
44 * are made available for use under the terms of the GNU General Public License
55 * (GPL) version 3 that accompanies this distribution and is available at
@@ -385,15 +385,17 @@ public static ElementReference commonAncestor(Collection<ElementReference> class
385385 if (classifier == null || classifier .getImpl ().isAny ()) {
386386 return classifier ;
387387 }
388+ // Note: allParents may be cached, so it should not be mutated.
388389 Collection <ElementReference > ancestors = classifier .getImpl ().allParents ();
389- ancestors .add (classifier );
390390 if (isFirst ) {
391+ commonAncestors .add (classifier );
391392 commonAncestors .addAll (ancestors );
392393 isFirst = false ;
393394 } else {
394- for (Object commonAncestor : commonAncestors .toArray ()) {
395- if (!(((ElementReference )commonAncestor ).getImpl ().
396- isContainedIn (ancestors ))) {
395+ for (Object object : commonAncestors .toArray ()) {
396+ ElementReference commonAncestor = (ElementReference )object ;
397+ if (!commonAncestor .getImpl ().equals (classifier ) &&
398+ !commonAncestor .getImpl ().isContainedIn (ancestors )) {
397399 commonAncestors .remove (commonAncestor );
398400 }
399401 }
@@ -406,7 +408,8 @@ public static ElementReference commonAncestor(Collection<ElementReference> class
406408 // Remove any common ancestors that are parents of other common
407409 // ancestors.
408410 for (Object ancestor : commonAncestors .toArray ()) {
409- Collection <ElementReference > parents = ((ElementReference )ancestor ).getImpl ().parents ();
411+ Collection <ElementReference > parents =
412+ ((ElementReference )ancestor ).getImpl ().parents ();
410413 for (ElementReference parent : parents ) {
411414 commonAncestors .remove (parent );
412415 }
0 commit comments