gr.uom.java.xmi.diff.SplitConditionalRefactoring Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of refactoring-miner Show documentation
Show all versions of refactoring-miner Show documentation
RefactoringMiner is a library/API written in Java that can detect refactorings applied in the history of a Java project.
package gr.uom.java.xmi.diff;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.refactoringminer.api.Refactoring;
import org.refactoringminer.api.RefactoringType;
import gr.uom.java.xmi.VariableDeclarationContainer;
import gr.uom.java.xmi.decomposition.AbstractCodeFragment;
import gr.uom.java.xmi.decomposition.LeafMapping;
public class SplitConditionalRefactoring implements Refactoring, LeafMappingProvider {
private AbstractCodeFragment originalConditional;
private Set splitConditionals;
private VariableDeclarationContainer operationBefore;
private VariableDeclarationContainer operationAfter;
private List subExpressionMappings;
public SplitConditionalRefactoring(AbstractCodeFragment originalConditional,
Set splitConditionals, VariableDeclarationContainer operationBefore,
VariableDeclarationContainer operationAfter) {
this.originalConditional = originalConditional;
this.splitConditionals = splitConditionals;
this.operationBefore = operationBefore;
this.operationAfter = operationAfter;
this.subExpressionMappings = new ArrayList();
}
public void addSubExpressionMapping(LeafMapping newLeafMapping) {
boolean alreadyPresent = false;
for(LeafMapping oldLeafMapping : subExpressionMappings) {
if(oldLeafMapping.getFragment1().getLocationInfo().equals(newLeafMapping.getFragment1().getLocationInfo()) &&
oldLeafMapping.getFragment2().getLocationInfo().equals(newLeafMapping.getFragment2().getLocationInfo())) {
alreadyPresent = true;
break;
}
}
if(!alreadyPresent) {
subExpressionMappings.add(newLeafMapping);
}
}
public AbstractCodeFragment getOriginalConditional() {
return originalConditional;
}
public Set getSplitConditionals() {
return splitConditionals;
}
public VariableDeclarationContainer getOperationBefore() {
return operationBefore;
}
public VariableDeclarationContainer getOperationAfter() {
return operationAfter;
}
public List getSubExpressionMappings() {
return subExpressionMappings;
}
@Override
public List leftSide() {
List ranges = new ArrayList();
ranges.add(originalConditional.codeRange()
.setDescription("original conditional")
.setCodeElement(originalConditional.toString()));
String elementType = operationBefore.getElementType();
ranges.add(operationBefore.codeRange()
.setDescription("original " + elementType + " declaration")
.setCodeElement(operationBefore.toString()));
return ranges;
}
@Override
public List rightSide() {
List ranges = new ArrayList();
for(AbstractCodeFragment splitConditional : splitConditionals) {
ranges.add(splitConditional.codeRange()
.setDescription("split conditional")
.setCodeElement(splitConditional.toString()));
}
String elementType = operationAfter.getElementType();
ranges.add(operationAfter.codeRange()
.setDescription(elementType + " declaration with split conditional")
.setCodeElement(operationAfter.toString()));
return ranges;
}
@Override
public RefactoringType getRefactoringType() {
return RefactoringType.SPLIT_CONDITIONAL;
}
@Override
public String getName() {
return this.getRefactoringType().getDisplayName();
}
@Override
public Set> getInvolvedClassesBeforeRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getOperationBefore().getLocationInfo().getFilePath(), getOperationBefore().getClassName()));
return pairs;
}
@Override
public Set> getInvolvedClassesAfterRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getOperationAfter().getLocationInfo().getFilePath(), getOperationAfter().getClassName()));
return pairs;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName()).append("\t");
String conditionalString = originalConditional.getString();
String oldConditional = (conditionalString.contains("\n") ? conditionalString.substring(0, conditionalString.indexOf("\n")) : conditionalString);
sb.append(oldConditional);
sb.append(" to ");
sb.append("[");
int i = 0;
for(AbstractCodeFragment splitConditional : splitConditionals) {
conditionalString = splitConditional.getString();
String newConditional = (conditionalString.contains("\n") ? conditionalString.substring(0, conditionalString.indexOf("\n")) : conditionalString);
sb.append(newConditional);
if(i < splitConditionals.size()-1) {
sb.append(", ");
}
i++;
}
sb.append("]");
String elementType = operationAfter.getElementType();
sb.append(" in " + elementType + " ");
sb.append(operationAfter);
sb.append(" from class ").append(operationAfter.getClassName());
return sb.toString();
}
@Override
public int hashCode() {
return Objects.hash(operationAfter, operationBefore, originalConditional, splitConditionals);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SplitConditionalRefactoring other = (SplitConditionalRefactoring) obj;
return Objects.equals(operationAfter, other.operationAfter)
&& Objects.equals(operationBefore, other.operationBefore)
&& Objects.equals(originalConditional, other.originalConditional)
&& Objects.equals(splitConditionals, other.splitConditionals);
}
}