diff --git a/src/main/java/org/jpeek/calculus/java/Ccm.java b/src/main/java/org/jpeek/calculus/java/Ccm.java index 1ac780cb..59b3dd3e 100644 --- a/src/main/java/org/jpeek/calculus/java/Ccm.java +++ b/src/main/java/org/jpeek/calculus/java/Ccm.java @@ -159,7 +159,7 @@ private static Integer calculateComponents(final XML clazz, final Map splits = Arrays.asList(classpath.split("\\.")); if (parents.keySet().contains(splits.get(splits.size() - 1))) { - parents.put(name, splits.get(splits.size() - 1)); + UnionFind.unite(name, splits.get(splits.size() - 1), parents); } } else { final String var = operation.node().getTextContent(); @@ -239,9 +239,14 @@ private static Integer runUnionFind(final Map parents, */ private static String getParent(final String node, final Map parents) { String ancestor = node; - if (!parents.get(ancestor).equals(ancestor)) { - ancestor = getParent(parents.get(ancestor), parents); - parents.put(node, ancestor); + while (!parents.get(ancestor).equals(ancestor)) { + ancestor = parents.get(ancestor); + } + String current = node; + while (!parents.get(current).equals(current)) { + final String temp = parents.get(current); + parents.put(current, ancestor); + current = temp; } return ancestor; }