// Get the arguments for the anonymous root:
List<CompositionArgument> freeArguments = subtreeNodeInfo.getFreeUnburntArguments();
// Iterate over the collectors, finding the parent of the deepest one (ie. the scope which encloses the smallest scope).
// This will be the maximal scope at which the root can be validly defined.
Collector deepestCollectorParent = rootNodeTarget;
// Ensure that the deepest collector parent is an original collector..
// Note: iterates over the whole excludedCollectorReplacementMap, so potentially slow.
if (deepestCollectorParent instanceof SyntheticCollector) {
for (final Map.Entry<Collector, SyntheticCollector> entry : excludedCollectorReplacementMap.entrySet()) {
final Collector excludedCollector = entry.getKey();
if (deepestCollectorParent == entry.getValue()) {
deepestCollectorParent = excludedCollector;
break;
}
}
}
for (final Collector collector : subtreeNodeInfo.getCollectorsUsed()) {
if (enclosesCollector(deepestCollectorParent, collector)) {
if (collector != deepestCollectorParent && collector.getTargetCollector() != deepestCollectorParent) {
deepestCollectorParent = collector.getTargetCollector();
}
} else if (!enclosesCollector(collector.getTargetCollector(), deepestCollectorParent)) {
CALCompiler.COMPILER_LOGGER.log(Level.SEVERE, "Inconsistent hierarchy detected for anonymous gem tree.");
return null;
}
}
// Replace with replacement if necessary.
if (excludedCollectorReplacementMap.containsKey(deepestCollectorParent)) {
deepestCollectorParent = excludedCollectorReplacementMap.get(deepestCollectorParent);
}
// Get an anonymous name which doesn't conflict with any other names.
String candidateCollectorName = "anonymousCollector";
while (collectorNames.contains(candidateCollectorName)) {
candidateCollectorName = "anonymousCollector" + nextAnonymousNameIndex;
nextAnonymousNameIndex++;
}
collectorNames.add(candidateCollectorName);
// Create the collector with that name.
Collector collectorForRoot = new SyntheticCollector(candidateCollectorName, deepestCollectorParent, freeArguments, uncollectedRoot);
// Add to the map.
collectorToUncollectedRootMap.put(collectorForRoot, uncollectedRoot);
}