typeConsName = baseName + index;
index++;
}
// Only one class with this base name.
QualifiedName qualifiedTypeConsName = QualifiedName.make(currentModuleName, baseName);
classToTypeConsNameMap.put(conflictingClass, qualifiedTypeConsName);
} else {
// More than one class with the same base name.
// Iterate over the classes in the set, figuring out how many qualifications are necessary to disambiguate.
// Names are qualified in reverse order.
// eg. for org.openquark.gems.client.jfit.ForeignImportGenerator,
// base name : ForeignImportGenerator
// 1 qualification : jfit.ForeignImportGenerator
// 2 qualifications: client.jfit.ForeignImportGenerator
// etc.
int numQualifications = 1;
while (numQualifications > 0) {
// (Set of String) names of classes, with the given number of qualifications.
Set<String> namesWithQualification = new HashSet<String>();
boolean foundConflict = false;
for (final Class<?> conflictingClass : classSet) {
String nameWithQualification = makeTypeConsName(conflictingClass, numQualifications);
if (nameWithQualification == null) {
// Not enough qualifications exist in the name.
// foundConflict is false, so we will break out of the while.. loop.
numQualifications = -1;
break;
}
if (!namesWithQualification.add(nameWithQualification)) {
// This number of qualifications is not enough to disambiguate with respect to the other classes.
foundConflict = true;
break;
} else {
// Didn't find a conflict. Try the next name.
}
}
if (foundConflict) {
// Try more qualifications.
numQualifications++;
} else {
// Found a sufficient number of qualifications.
break;
}
}
// Now populate the classToTypeConsNameMap
for (final Class<?> conflictingClass : classSet) {
String nameWithQualification = makeTypeConsName(conflictingClass, numQualifications);
// Disambiguate.
int index = 2;
String typeConsName = nameWithQualification;
while (!existingUnqualifiedTypeConsNamesSet.add(typeConsName)) {
typeConsName = baseName + index;
index++;
}
// Add a mapping for the generated name.
QualifiedName qualifiedTypeConsName = QualifiedName.make(currentModuleName, typeConsName);
classToTypeConsNameMap.put(conflictingClass, qualifiedTypeConsName);
}
}
}
}