gr.uom.java.xmi.diff.ExtractAttributeRefactoring 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.UMLAttribute;
import gr.uom.java.xmi.UMLClass;
import gr.uom.java.xmi.decomposition.AbstractCodeMapping;
public class ExtractAttributeRefactoring implements Refactoring {
private UMLAttribute attributeDeclaration;
private UMLClass originalClass;
private UMLClass nextClass;
private Set references;
public ExtractAttributeRefactoring(UMLAttribute variableDeclaration, UMLClass originalClass, UMLClass nextClass) {
this.attributeDeclaration = variableDeclaration;
this.originalClass = originalClass;
this.nextClass = nextClass;
this.references = new LinkedHashSet();
}
public void addReference(AbstractCodeMapping mapping) {
references.add(mapping);
}
public RefactoringType getRefactoringType() {
return RefactoringType.EXTRACT_ATTRIBUTE;
}
public String getName() {
return this.getRefactoringType().getDisplayName();
}
public UMLAttribute getVariableDeclaration() {
return attributeDeclaration;
}
public Set getReferences() {
return references;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName()).append("\t");
sb.append(attributeDeclaration);
sb.append(" in class ");
sb.append(attributeDeclaration.getClassName());
return sb.toString();
}
/**
* @return the code range of the extracted variable declaration in the child commit
*/
public CodeRange getExtractedVariableDeclarationCodeRange() {
return attributeDeclaration.codeRange();
}
public UMLClass getOriginalClass() {
return originalClass;
}
public UMLClass getNextClass() {
return nextClass;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((attributeDeclaration == null) ? 0 : attributeDeclaration.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;
ExtractAttributeRefactoring other = (ExtractAttributeRefactoring) obj;
if (attributeDeclaration == null) {
if (other.attributeDeclaration != null)
return false;
} else if (!attributeDeclaration.equals(other.attributeDeclaration))
return false;
return true;
}
public Set> getInvolvedClassesBeforeRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getOriginalClass().getLocationInfo().getFilePath(), getOriginalClass().getName()));
return pairs;
}
public Set> getInvolvedClassesAfterRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getNextClass().getLocationInfo().getFilePath(), getNextClass().getName()));
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 attribute"));
}
return ranges;
}
@Override
public List rightSide() {
List ranges = new ArrayList();
ranges.add(attributeDeclaration.codeRange()
.setDescription("extracted attribute declaration")
.setCodeElement(attributeDeclaration.toString()));
for(AbstractCodeMapping mapping : references) {
ranges.add(mapping.getFragment2().codeRange().setDescription("statement with the name of the extracted attribute"));
}
return ranges;
}
}