gr.uom.java.xmi.diff.InlineVariableRefactoring Maven / Gradle / Ivy
package gr.uom.java.xmi.diff;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
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.AbstractCodeMapping;
import gr.uom.java.xmi.decomposition.LeafMapping;
import gr.uom.java.xmi.decomposition.VariableDeclaration;
public class InlineVariableRefactoring implements Refactoring, ReferenceBasedRefactoring, LeafMappingProvider {
private VariableDeclaration variableDeclaration;
private VariableDeclarationContainer operationBefore;
private VariableDeclarationContainer operationAfter;
private Set references;
private Set unmatchedStatementReferences;
private List subExpressionMappings;
private boolean insideExtractedOrInlinedMethod;
public InlineVariableRefactoring(VariableDeclaration variableDeclaration, VariableDeclarationContainer operationBefore, VariableDeclarationContainer operationAfter,
boolean insideExtractedOrInlinedMethod) {
this.variableDeclaration = variableDeclaration;
this.operationBefore = operationBefore;
this.operationAfter = operationAfter;
this.references = new LinkedHashSet();
this.unmatchedStatementReferences = new LinkedHashSet();
this.subExpressionMappings = new ArrayList();
this.insideExtractedOrInlinedMethod = insideExtractedOrInlinedMethod;
}
public void addReference(AbstractCodeMapping mapping) {
references.add(mapping);
}
public void addUnmatchedStatementReference(AbstractCodeFragment fragmentT2) {
unmatchedStatementReferences.add(fragmentT2);
}
public void addReferences(Set mappings) {
references.addAll(mappings);
}
public void addUnmatchedStatementReferences(Set fragmentsT2) {
unmatchedStatementReferences.addAll(fragmentsT2);
}
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 RefactoringType getRefactoringType() {
return RefactoringType.INLINE_VARIABLE;
}
public String getName() {
return this.getRefactoringType().getDisplayName();
}
public VariableDeclaration getVariableDeclaration() {
return variableDeclaration;
}
public VariableDeclarationContainer getOperationBefore() {
return operationBefore;
}
public VariableDeclarationContainer getOperationAfter() {
return operationAfter;
}
public Set getReferences() {
return references;
}
public Set getUnmatchedStatementReferences() {
return unmatchedStatementReferences;
}
public List getSubExpressionMappings() {
return subExpressionMappings;
}
public boolean isInsideExtractedOrInlinedMethod() {
return insideExtractedOrInlinedMethod;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName()).append("\t");
sb.append(variableDeclaration);
String elementType = operationBefore.getElementType();
sb.append(" in " + elementType + " ");
sb.append(operationBefore);
sb.append(" from class ");
sb.append(operationBefore.getClassName());
return sb.toString();
}
/**
* @return the code range of the inlined variable declaration in the parent commit
*/
public CodeRange getInlinedVariableDeclarationCodeRange() {
return variableDeclaration.codeRange();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((operationBefore == null) ? 0 : operationBefore.hashCode());
result = prime * result + ((variableDeclaration == null) ? 0 : variableDeclaration.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
InlineVariableRefactoring other = (InlineVariableRefactoring) obj;
if (operationBefore == null) {
if (other.operationBefore != null)
return false;
} else if (!operationBefore.equals(other.operationBefore))
return false;
if (variableDeclaration == null) {
if (other.variableDeclaration != null)
return false;
} else if (!variableDeclaration.equals(other.variableDeclaration))
return false;
return true;
}
public Set> getInvolvedClassesBeforeRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getOperationBefore().getLocationInfo().getFilePath(), getOperationBefore().getClassName()));
return pairs;
}
public Set> getInvolvedClassesAfterRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getOperationAfter().getLocationInfo().getFilePath(), getOperationAfter().getClassName()));
return pairs;
}
@Override
public List leftSide() {
List ranges = new ArrayList();
ranges.add(variableDeclaration.codeRange()
.setDescription("inlined variable declaration")
.setCodeElement(variableDeclaration.toString()));
for(AbstractCodeMapping mapping : references) {
ranges.add(mapping.getFragment1().codeRange().setDescription("statement with the name of the inlined variable"));
}
/*
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(AbstractCodeMapping mapping : references) {
ranges.add(mapping.getFragment2().codeRange().setDescription("statement with the initializer of the inlined variable"));
}
for(AbstractCodeFragment fragment : unmatchedStatementReferences) {
ranges.add(fragment.codeRange().setDescription("statement with the initializer of the inlined variable"));
}
/*
String elementType = operationAfter.getElementType();
ranges.add(operationAfter.codeRange()
.setDescription(elementType + " declaration with inlined variable")
.setCodeElement(operationAfter.toString()));
*/
return ranges;
}
}