* @param expectedReturnVal
*/
private void renameAllSymbolsInModule(final ModuleName moduleName, final String workspaceFile, final String targetMethod, boolean renameModule, final Object expectedReturnVal) {
try {
String renamedTargetMethod = targetMethod;
BasicCALServices privateCopyLeccServices = CALServicesTestUtilities.makeUnsharedUnitTestCALServices(
MachineType.LECC,
"org.openquark.cal.test.workspace.CALRenaming_DeepTest.renameAllSymbolsInModule",
workspaceFile,
null, "CALRenaming_DeepTest.renameAllSymbolsInModule", false);
try {
privateCopyLeccServices.compileWorkspace(null, new MessageLogger());
ModuleSourceDefinition sourceDef = makeSourceDefFromBuiltIn(moduleName, privateCopyLeccServices.getCALWorkspace());
String moduleString = getSourceString(sourceDef);
List <Renaming> renamings = new ArrayList<Renaming>();
// Convert the source into a source model representation, then use this model to obtain a list of symbols to rename.
SourceModel.ModuleDefn moduleDefn = SourceModelUtilities.TextParsing.parseModuleDefnIntoSourceModel(moduleString);
SourceModel.TopLevelSourceElement[] sourceElements = moduleDefn.getTopLevelDefns();
for (int i = 0, n = sourceElements.length; i < n; i++) {
if (sourceElements[i] instanceof SourceModel.TypeConstructorDefn) {
SourceModel.TypeConstructorDefn typeConstructorDefn = (SourceModel.TypeConstructorDefn)sourceElements[i];
renamings.add(new Renaming(typeConstructorDefn.getTypeConsName(), "RenamedTypeCons" + i, SourceIdentifier.Category.TYPE_CONSTRUCTOR));
if (typeConstructorDefn instanceof SourceModel.TypeConstructorDefn.AlgebraicType) {
SourceModel.TypeConstructorDefn.AlgebraicType algebraicType = (SourceModel.TypeConstructorDefn.AlgebraicType) typeConstructorDefn;
SourceModel.TypeConstructorDefn.AlgebraicType.DataConsDefn[] dataConsDefns = algebraicType.getDataConstructors();
for (int j = 0, m = dataConsDefns.length; j < m; j++) {
renamings.add(new Renaming(dataConsDefns[j].getDataConsName(), ("RenamedDataCons" + i + "_" + j), SourceIdentifier.Category.DATA_CONSTRUCTOR));
}
}
} else if (sourceElements[i] instanceof SourceModel.TypeClassDefn) {
SourceModel.TypeClassDefn typeClassDefn = (SourceModel.TypeClassDefn)sourceElements[i];
renamings.add(new Renaming(typeClassDefn.getTypeClassName(), "RenamedTypeClass" + i, SourceIdentifier.Category.TYPE_CLASS));
SourceModel.TypeClassDefn.ClassMethodDefn[] classMethodDefns = typeClassDefn.getClassMethodDefns();
for (int j = 0, m = classMethodDefns.length; j < m; j++) {
renamings.add(new Renaming(classMethodDefns[j].getMethodName(), ("renamedClassMethod" + i + "_" + j), SourceIdentifier.Category.TOP_LEVEL_FUNCTION_OR_CLASS_METHOD));
}
} else if (sourceElements[i] instanceof SourceModel.FunctionDefn) {
SourceModel.FunctionDefn functionDefn = (SourceModel.FunctionDefn)sourceElements[i];
if (functionDefn.getName().equals(targetMethod)) {
renamedTargetMethod = "renamedFunction" + i;
}
renamings.add(new Renaming(functionDefn.getName(), "renamedFunction" + i, SourceIdentifier.Category.TOP_LEVEL_FUNCTION_OR_CLASS_METHOD));
} else if (sourceElements[i] instanceof SourceModel.InstanceDefn) {
} else if (sourceElements[i] instanceof SourceModel.FunctionTypeDeclaration) {
} else {
fail("Invalid top level source element type.");
}
}
if(renameModule) {
renamings.add(new Renaming(moduleName.toSourceText(), ("ModuleMangledByRenaming_" + moduleName), SourceIdentifier.Category.MODULE_NAME));
}
ModuleTypeInfo moduleTypeInfo = privateCopyLeccServices.getWorkspaceManager().getModuleTypeInfo(moduleName);
// Now run through the list and perform all the renamings.
ModuleName recompiledModuleName = moduleName;
for (int n = renamings.size(), i = 0; i < n; i++) {
Renaming renaming = renamings.get(i);
QualifiedName oldQualifiedName;
QualifiedName newQualifiedName;
if (renaming.category == SourceIdentifier.Category.MODULE_NAME) {
oldQualifiedName = QualifiedName.make(ModuleName.make(renaming.oldName), Refactorer.Rename.UNQUALIFIED_NAME_FOR_MODULE_RENAMING);
newQualifiedName = QualifiedName.make(ModuleName.make(renaming.newName), Refactorer.Rename.UNQUALIFIED_NAME_FOR_MODULE_RENAMING);
} else {
oldQualifiedName = QualifiedName.make(moduleName, renaming.oldName);
newQualifiedName = QualifiedName.make(moduleName, renaming.newName);
}
CompilerMessageLogger logger = new MessageLogger();
SourceModifier renamer = IdentifierRenamer.getSourceModifier(moduleTypeInfo, moduleString, oldQualifiedName, newQualifiedName, renaming.category, logger);
assertTrue(logger.getMaxSeverity().compareTo(Severity.ERROR) < 0);
moduleString = renamer.apply(moduleString);
if (renaming.category == SourceIdentifier.Category.MODULE_NAME && recompiledModuleName.equals(ModuleName.make(renaming.oldName))) {
recompiledModuleName = ModuleName.make(renaming.newName);
}
}
sourceDef = new StringModuleSourceDefinition(recompiledModuleName, moduleString);
// Remove the old module from the workspace and add the new one. This will also force a recompilation of the relevant modules.
CompilerMessageLogger logger = new MessageLogger();
privateCopyLeccServices.getWorkspaceManager().removeModule(moduleName, new Status(""));
privateCopyLeccServices.getWorkspaceManager().makeModule(sourceDef, logger);
if (logger.getMaxSeverity().compareTo(CompilerMessage.Severity.ERROR) >= 0) {
List<CompilerMessage> errors = logger.getCompilerMessages(CompilerMessage.Severity.ERROR);
for (int i = 0, n = errors.size(); i < n; i++) {
System.out.println(errors.get(i).toString());
}