gr.uom.java.xmi.diff.ExtractVariableRefactoring 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.Set;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.refactoringminer.api.Refactoring;
import org.refactoringminer.api.RefactoringType;
import gr.uom.java.xmi.UMLOperation;
import gr.uom.java.xmi.decomposition.AbstractCodeMapping;
import gr.uom.java.xmi.decomposition.VariableDeclaration;
public class ExtractVariableRefactoring implements Refactoring {
private VariableDeclaration variableDeclaration;
private UMLOperation operationBefore;
private UMLOperation operationAfter;
private Set references;
public ExtractVariableRefactoring(VariableDeclaration variableDeclaration, UMLOperation operationBefore, UMLOperation operationAfter) {
this.variableDeclaration = variableDeclaration;
this.operationBefore = operationBefore;
this.operationAfter = operationAfter;
this.references = new LinkedHashSet();
}
public void addReference(AbstractCodeMapping mapping) {
references.add(mapping);
}
public RefactoringType getRefactoringType() {
return RefactoringType.EXTRACT_VARIABLE;
}
public String getName() {
return this.getRefactoringType().getDisplayName();
}
public VariableDeclaration getVariableDeclaration() {
return variableDeclaration;
}
public UMLOperation getOperationBefore() {
return operationBefore;
}
public UMLOperation getOperationAfter() {
return operationAfter;
}
public Set getReferences() {
return references;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName()).append("\t");
sb.append(variableDeclaration);
sb.append(" in method ");
sb.append(operationAfter);
sb.append(" from class ");
sb.append(operationAfter.getClassName());
return sb.toString();
}
/**
* @return the code range of the extracted variable declaration in the child commit
*/
public CodeRange getExtractedVariableDeclarationCodeRange() {
return variableDeclaration.codeRange();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((operationAfter == null) ? 0 : operationAfter.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;
ExtractVariableRefactoring other = (ExtractVariableRefactoring) obj;
if (operationAfter == null) {
if (other.operationAfter != null)
return false;
} else if (!operationAfter.equals(other.operationAfter))
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();
for(AbstractCodeMapping mapping : references) {
ranges.add(mapping.getFragment1().codeRange().setDescription("statement with the initializer of the extracted variable"));
}
return ranges;
}
@Override
public List rightSide() {
List ranges = new ArrayList();
ranges.add(variableDeclaration.codeRange()
.setDescription("extracted variable declaration")
.setCodeElement(variableDeclaration.toString()));
for(AbstractCodeMapping mapping : references) {
ranges.add(mapping.getFragment2().codeRange().setDescription("statement with the name of the extracted variable"));
}
return ranges;
}
}