In the following example of Model-Based Refactoring, we will develop a refactoring code aimed at cleaning up process names by removing underscores and any subsequent numbers.
package com.netfective.bluage;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import com.netfective.bluage.reverse.gapwalk.refactoring.GapWalk.STM.Process;
import com.netfective.bluage.reverse.gapwalk.refactoring.java.api.refactorings.DSLGapwalkRefactoring;
import com.netfective.bluage.reverse.gapwalk.refactoring.java.api.refactorings.beans.DSLBean;
import com.netfective.bluage.reverse.gapwalk.refactoring.java.api.tools.LoggerUtils;
import com.netfective.bluage.reverse.gapwalk.refactoring.java.api.tools.ResultBuilder;
import com.netfective.bluage.reverse.gapwalk.refactoring.java.process.result.RefactoringResult;
import com.netfective.bluage.reverse.gapwalk.refactoring.tools.EMFTools;
public class CleanUpProcessNames implements DSLGapwalkRefactoring {
private boolean modified = false;
@Override
public String getDisplayName() {
return "Naming";
}
@Override
public RefactoringResult refactor(DSLBean bean) {
removeProcessNamePrefixes(bean);
return ResultBuilder.success(modified);
}
private void removeProcessNamePrefixes(DSLBean bean) {
EMFTools.childrenOfType(bean.getStmFile(), Process.class)
.filter(p -> p.getName().startsWith("_"))
.forEach(p -> renameProcess(bean, p));
}
private void renameProcess (DSLBean bean, Process p) {
String cleanProcessName = p.getName().substring(1);
while (CharUtils.isAsciiNumeric(cleanProcessName.charAt(0))) {
cleanProcessName = cleanProcessName.substring(1);
}
if(!p.getName().equals(cleanProcessName)) {
cleanProcessName = StringUtils.uncapitalize(cleanProcessName);
modified = true;
LoggerUtils.info(bean.getStmFile(),
String.format("Renamed process from %s to %s.",
p.getName(), cleanProcessName));
p.setName(StringUtils.uncapitalize(cleanProcessName));
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import com.netfective.bluage.reverse.gapwalk.refactoring.java.api.refactorings.IGapwalkRefactoring;
import com.netfective.bluage.reverse.gapwalk.refactoring.java.api.runner.CommandLineRefactoringRunner;
public class Refactor {
public static void main(String[] args) {
new CommandLineRefactoringRunner(args, getRefactorings()).run();
}
public static List<Supplier<? extends IGapwalkRefactoring>> getRefactorings() {
List<Supplier<? extends IGapwalkRefactoring>>
refactorings = new ArrayList<>();
refactorings.add(() -> new MQConstantsRefactoring());
return refactorings;
}
}
Refactor results can be validated by comparing output files with their corresponding input files. This process ensures that all transformations have been applied as intended.
File comparison tools, such as WinMerge, are recommended for this purpose, as they provide a clear visualization of changes. In the example screenshot below, the original STM file is displayed on the left, while the refactored version appears on the right, highlighting the modifications applied during the Refactor action.
The comparison above illustrates the execution of a process name cleanup function. Any process name beginning with an underscore followed by numbers has been simplified by removing both the underscore and the numerical sequence. For instance, if a process was originally named "_0000AcctfileOpen()", it would be transformed to "acctfileOpen()".
Debugging is facilitated through tools such as Eclipse and file comparison utilities like WinMerge.
To debug refactorings: