All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gr.uom.java.xmi.diff.ExtractAttributeRefactoring 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.RefactoringMinerTimedOutException;
import org.refactoringminer.api.RefactoringType;

import gr.uom.java.xmi.UMLAbstractClass;
import gr.uom.java.xmi.UMLAnonymousClass;
import gr.uom.java.xmi.UMLAttribute;
import gr.uom.java.xmi.decomposition.AbstractCodeMapping;
import gr.uom.java.xmi.decomposition.AnonymousClassDeclarationObject;
import gr.uom.java.xmi.decomposition.LeafMapping;
import gr.uom.java.xmi.decomposition.UMLOperationBodyMapper;

public class ExtractAttributeRefactoring implements Refactoring, ReferenceBasedRefactoring, LeafMappingProvider {
	private UMLAttribute attributeDeclaration;
	private UMLAbstractClass originalClass;
	private UMLAbstractClass nextClass;
	private Set references;
	private List subExpressionMappings;
	private boolean insideExtractedOrInlinedMethod;
	private List anonymousClassDiffList;

	public ExtractAttributeRefactoring(UMLAttribute variableDeclaration, UMLAbstractClass originalClass, UMLAbstractClass nextClass,
			boolean insideExtractedOrInlinedMethod) {
		this.attributeDeclaration = variableDeclaration;
		this.originalClass = originalClass;
		this.nextClass = nextClass;
		this.references = new LinkedHashSet();
		this.subExpressionMappings = new ArrayList();
		this.insideExtractedOrInlinedMethod = insideExtractedOrInlinedMethod;
		this.anonymousClassDiffList = new ArrayList<>();
	}

	public List addReference(AbstractCodeMapping mapping, UMLAbstractClassDiff classDiff, UMLModelDiff modelDiff) throws RefactoringMinerTimedOutException {
		references.add(mapping);
		List allRefactorings = new ArrayList<>();
		List attributeAnonymousClassList = attributeDeclaration.getAnonymousClassList();
		List fragmentAnonymousClassDeclarations = new ArrayList<>();
		recursivelyCollectAnonymousClassDeclarations(fragmentAnonymousClassDeclarations, mapping.getFragment1().getAnonymousClassDeclarations());
		if(attributeAnonymousClassList.size() > 0 && fragmentAnonymousClassDeclarations.size() > 0 &&
				attributeAnonymousClassList.size() == fragmentAnonymousClassDeclarations.size()) {
			for(int i=0; i matchedOperationMappers = anonymousClassDiff.getOperationBodyMapperList();
					if(matchedOperationMappers.size() > 0) {
						anonymousClassDiffList.add(anonymousClassDiff);
						List anonymousClassDiffRefactorings = anonymousClassDiff.getRefactorings();
						allRefactorings.addAll(anonymousClassDiffRefactorings);
						if(classDiff != null && classDiff.getRemovedAnonymousClasses().contains(before)) {
							classDiff.getRemovedAnonymousClasses().remove(before);
						}
						if(classDiff != null && classDiff.getAddedAnonymousClasses().contains(after)) {
							classDiff.getAddedAnonymousClasses().remove(after);
						}
					}
				}
			}
		}
		return allRefactorings;
	}

	public void addReferences(Set mappings) {
		references.addAll(mappings);
	}

	private static void recursivelyCollectAnonymousClassDeclarations(List all, List current) {
		for(AnonymousClassDeclarationObject anonymous : current) {
			recursivelyCollectAnonymousClassDeclarations(all, anonymous.getAnonymousClassDeclarations());
			all.add(anonymous);
		}
	}

	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.EXTRACT_ATTRIBUTE;
	}

	public String getName() {
		return this.getRefactoringType().getDisplayName();
	}

	public UMLAttribute getVariableDeclaration() {
		return attributeDeclaration;
	}

	public Set getReferences() {
		return references;
	}

	public List getSubExpressionMappings() {
		return subExpressionMappings;
	}

	public List getAnonymousClassDiffList() {
		return anonymousClassDiffList;
	}

	public boolean isInsideExtractedOrInlinedMethod() {
		return insideExtractedOrInlinedMethod;
	}

	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 UMLAbstractClass getOriginalClass() {
		return originalClass;
	}

	public UMLAbstractClass getNextClass() {
		return nextClass;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((attributeDeclaration.getVariableDeclaration() == null) ? 0 : attributeDeclaration.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;
		ExtractAttributeRefactoring other = (ExtractAttributeRefactoring) obj;
		if (attributeDeclaration == null) {
			if (other.attributeDeclaration != null)
				return false;
		} else if (!attributeDeclaration.getVariableDeclaration().equals(other.attributeDeclaration.getVariableDeclaration()))
			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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy