gr.uom.java.xmi.decomposition.replacement.ConsistentReplacementDetector Maven / Gradle / Ivy
package gr.uom.java.xmi.decomposition.replacement;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import gr.uom.java.xmi.diff.CandidateAttributeRefactoring;
public class ConsistentReplacementDetector {
private static Set inconsistentRenames(
Set currentRenames, T newRename) {
Set inconsistentRenames = new LinkedHashSet();
if(!newRename.getBefore().toUpperCase().equals(newRename.getAfter().toUpperCase())) {
for(T rename : currentRenames) {
if(rename.getBefore().equals(newRename.getBefore()) && !rename.getAfter().equals(newRename.getAfter())) {
inconsistentRenames.add(rename);
}
else if(!rename.getBefore().equals(newRename.getBefore()) && rename.getAfter().equals(newRename.getAfter())) {
inconsistentRenames.add(rename);
}
}
}
return inconsistentRenames;
}
public static void updateRenames(
Set allConsistentRenames,
Set allInconsistentRenames,
Map> aliasedVariablesInOriginalMethod,
Map> aliasedVariablesInNextMethod,
Set renames) {
boolean allRenamesHaveIdenticalTypeAndInitializer = allRenamesHaveIdenticalTypeAndInitializer(renames);
for(T newRename : renames) {
Set inconsistentRenames = inconsistentRenames(allConsistentRenames, newRename);
filter(inconsistentRenames, aliasedVariablesInOriginalMethod, aliasedVariablesInNextMethod);
if(inconsistentRenames.isEmpty() || (identicalTypeAndInitializer(newRename) && !allRenamesHaveIdenticalTypeAndInitializer)) {
allConsistentRenames.add(newRename);
}
else {
if(!allRenamesHaveIdenticalTypeAndInitializer) {
for(T rename : inconsistentRenames) {
if(!identicalTypeAndInitializer(rename)) {
allInconsistentRenames.add(rename);
}
}
}
else {
allInconsistentRenames.addAll(inconsistentRenames);
}
allInconsistentRenames.add(newRename);
}
}
}
public static void updateRenames(
Set allConsistentRenames,
Set allInconsistentRenames,
Set renames) {
boolean allRenamesHaveIdenticalTypeAndInitializer = allRenamesHaveIdenticalTypeAndInitializer(renames);
for(T newRename : renames) {
Set inconsistentRenames = inconsistentRenames(allConsistentRenames, newRename);
if(inconsistentRenames.isEmpty() || (identicalTypeAndInitializer(newRename) && !allRenamesHaveIdenticalTypeAndInitializer)) {
allConsistentRenames.add(newRename);
}
else {
if(!allRenamesHaveIdenticalTypeAndInitializer) {
for(T rename : inconsistentRenames) {
if(!identicalTypeAndInitializer(rename)) {
allInconsistentRenames.add(rename);
}
}
}
else {
allInconsistentRenames.addAll(inconsistentRenames);
}
allInconsistentRenames.add(newRename);
}
}
}
private static boolean allRenamesHaveIdenticalTypeAndInitializer(Set renames) {
if(renames.size() > 1) {
for(T rename : renames) {
if(!identicalTypeAndInitializer(rename)) {
return false;
}
}
return true;
}
return false;
}
private static boolean identicalTypeAndInitializer(T newRename) {
if(newRename instanceof VariableDeclarationReplacement) {
VariableDeclarationReplacement replacement = (VariableDeclarationReplacement)newRename;
if(replacement.identicalTypeAndInitializer()) {
return true;
}
}
return false;
}
public static void updateRenames(
Set allConsistentRenames,
Set allInconsistentRenames,
Set renames,
Map> aliasedAttributesInOriginalClass,
Map> aliasedAttributesInNextClass,
Map> renameMap) {
for(T newRename : renames) {
Set inconsistentRenames = inconsistentRenames(allConsistentRenames, newRename);
if(inconsistentRenames.size() > 0) {
validInconsistencies(inconsistentRenames, newRename, renameMap);
}
filter(inconsistentRenames, aliasedAttributesInOriginalClass, aliasedAttributesInNextClass);
if(inconsistentRenames.isEmpty()) {
allConsistentRenames.add(newRename);
}
else {
allInconsistentRenames.addAll(inconsistentRenames);
allInconsistentRenames.add(newRename);
}
}
}
private static void validInconsistencies(Set inconsistentRenames, T newRename, Map> renameMap) {
if(involvesOnlyAttributes(newRename, renameMap)) {
Set toBeRemoved = new LinkedHashSet();
for(T rename : inconsistentRenames) {
if(!involvesOnlyAttributes(rename, renameMap)) {
toBeRemoved.add(rename);
}
}
inconsistentRenames.removeAll(toBeRemoved);
}
else {
inconsistentRenames.clear();
}
}
private static boolean involvesOnlyAttributes(T rename, Map> renameMap) {
Set newCandidates = renameMap.get(rename);
int candidatesWithBothAttributes = 0;
for(CandidateAttributeRefactoring newCandidate : newCandidates) {
if(newCandidate.getOriginalVariableDeclaration() == null && newCandidate.getRenamedVariableDeclaration() == null) {
candidatesWithBothAttributes++;
}
}
return candidatesWithBothAttributes == newCandidates.size();
}
private static Set filter(Set inconsistentRenames,
Map> aliasedAttributesInOriginalClass,
Map> aliasedAttributesInNextClass) {
Set renamesToBeRemoved = new LinkedHashSet();
for(String key : aliasedAttributesInOriginalClass.keySet()) {
Set aliasedAttributes = aliasedAttributesInOriginalClass.get(key);
for(T r : inconsistentRenames) {
if(r instanceof VariableDeclarationReplacement) {
Replacement rename = ((VariableDeclarationReplacement)r).getVariableNameReplacement();
if(aliasedAttributes.contains(rename.getBefore())) {
renamesToBeRemoved.add(r);
}
}
else if(aliasedAttributes.contains(r.getBefore())) {
renamesToBeRemoved.add(r);
}
}
}
for(String key : aliasedAttributesInNextClass.keySet()) {
Set aliasedAttributes = aliasedAttributesInNextClass.get(key);
for(T r : inconsistentRenames) {
if(r instanceof VariableDeclarationReplacement) {
Replacement rename = ((VariableDeclarationReplacement)r).getVariableNameReplacement();
if(aliasedAttributes.contains(rename.getAfter())) {
renamesToBeRemoved.add(r);
}
}
else if(aliasedAttributes.contains(r.getAfter())) {
renamesToBeRemoved.add(r);
}
}
}
inconsistentRenames.removeAll(renamesToBeRemoved);
return inconsistentRenames;
}
}