gr.uom.java.xmi.diff.ChangeAttributeAccessModifierRefactoring 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.UMLAttribute;
import gr.uom.java.xmi.UMLModifier;
import gr.uom.java.xmi.Visibility;
public class ChangeAttributeAccessModifierRefactoring implements Refactoring {
private Visibility originalAccessModifier;
private Visibility changedAccessModifier;
private UMLAttribute attributeBefore;
private UMLAttribute attributeAfter;
public ChangeAttributeAccessModifierRefactoring(Visibility originalAccessModifier, Visibility changedAccessModifier,
UMLAttribute attributeBefore, UMLAttribute attributeAfter) {
this.originalAccessModifier = originalAccessModifier;
this.changedAccessModifier = changedAccessModifier;
this.attributeBefore = attributeBefore;
this.attributeAfter = attributeAfter;
}
public UMLModifier getOldModifier() {
for(UMLModifier m : attributeBefore.getVariableDeclaration().getModifiers()) {
if(m.getKeyword().equals(originalAccessModifier.toString())) {
return m;
}
}
return null;
}
public UMLModifier getNewModifier() {
for(UMLModifier m : attributeAfter.getVariableDeclaration().getModifiers()) {
if(m.getKeyword().equals(changedAccessModifier.toString())) {
return m;
}
}
return null;
}
public Visibility getOriginalAccessModifier() {
return originalAccessModifier;
}
public Visibility getChangedAccessModifier() {
return changedAccessModifier;
}
public UMLAttribute getAttributeBefore() {
return attributeBefore;
}
public UMLAttribute getAttributeAfter() {
return attributeAfter;
}
@Override
public List leftSide() {
List ranges = new ArrayList();
ranges.add(attributeBefore.codeRange()
.setDescription("original attribute declaration")
.setCodeElement(attributeBefore.toString()));
return ranges;
}
@Override
public List rightSide() {
List ranges = new ArrayList();
ranges.add(attributeAfter.codeRange()
.setDescription("attribute declaration with changed access modifier")
.setCodeElement(attributeAfter.toString()));
return ranges;
}
@Override
public RefactoringType getRefactoringType() {
return RefactoringType.CHANGE_ATTRIBUTE_ACCESS_MODIFIER;
}
@Override
public String getName() {
return this.getRefactoringType().getDisplayName();
}
@Override
public Set> getInvolvedClassesBeforeRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getAttributeBefore().getLocationInfo().getFilePath(), getAttributeBefore().getClassName()));
return pairs;
}
@Override
public Set> getInvolvedClassesAfterRefactoring() {
Set> pairs = new LinkedHashSet>();
pairs.add(new ImmutablePair(getAttributeAfter().getLocationInfo().getFilePath(), getAttributeAfter().getClassName()));
return pairs;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getName()).append("\t");
sb.append(originalAccessModifier);
sb.append(" to ");
sb.append(changedAccessModifier);
sb.append(" in attribute ");
sb.append(attributeAfter);
sb.append(" from class ");
sb.append(attributeAfter.getClassName());
return sb.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((attributeAfter == null || attributeAfter.getVariableDeclaration() == null) ? 0 : attributeAfter.getVariableDeclaration().hashCode());
result = prime * result + ((attributeBefore == null || attributeBefore.getVariableDeclaration() == null) ? 0 : attributeBefore.getVariableDeclaration().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;
ChangeAttributeAccessModifierRefactoring other = (ChangeAttributeAccessModifierRefactoring) obj;
if (attributeBefore == null) {
if (other.attributeBefore != null)
return false;
} else if(attributeBefore.getVariableDeclaration() == null) {
if(other.attributeBefore.getVariableDeclaration() != null)
return false;
} else if (!attributeBefore.getVariableDeclaration().equals(other.attributeBefore.getVariableDeclaration()))
return false;
if (attributeAfter == null) {
if (other.attributeAfter != null)
return false;
} else if(attributeAfter.getVariableDeclaration() == null) {
if(other.attributeAfter.getVariableDeclaration() != null)
return false;
} else if (!attributeAfter.getVariableDeclaration().equals(other.attributeAfter.getVariableDeclaration()))
return false;
return true;
}
}