Coding a refactoring

Example of Process Name Refactoring

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.

  1. Create a CleanUpProcessNames.java class that performs the process name cleaning. This class implements the DSLGapwalkRefactoring interface and provides the logic for 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));
}
}
}
  1. Open the Refactor.java class and add the created CleanUpProcessNames.java class.
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;
}
}
  1. Save and zip the folder containing the CleanUpProcessNames.java and Refactor.java classes.
  2. Go to BluInsights and upload the zipped folder.
  3. Activate the Refactor step and do a Run.

Checking Refactor result

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.
 

Checking Refactor result.png


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 a Refactor Project

Debugging is facilitated through tools such as Eclipse and file comparison utilities like WinMerge.
To debug refactorings:

  1. Download and unzip the Outputs of the Transform action.
  2. In Eclipse, run Refactor.java as a Java Application with:
    1. Path to the input folder containing files to refactor
    2. Path to the output folder containing refactored files
  3. Use breakpoints and logs in Eclipse then compare input and output folders.