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

gr.uom.java.xmi.decomposition.VariableReferenceExtractor Maven / Gradle / Ivy

Go to download

RefactoringMiner is a library/API written in Java that can detect refactorings applied in the history of a Java project.

There is a newer version: 3.0.9
Show newest version
package gr.uom.java.xmi.decomposition;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import gr.uom.java.xmi.LocationInfo.CodeElementType;

public class VariableReferenceExtractor {

	public static Set findReferences(VariableDeclaration declaration1, VariableDeclaration declaration2, Set mappings) {
		Set references = new LinkedHashSet();
		VariableScope scope1 = declaration1.getScope();
		VariableScope scope2 = declaration2.getScope();
		for(AbstractCodeMapping mapping : mappings) {
			AbstractCodeFragment fragment1 = mapping.getFragment1();
			AbstractCodeFragment fragment2 = mapping.getFragment2();
			if(scope1.subsumes(fragment1.getLocationInfo()) && scope2.subsumes(fragment2.getLocationInfo()) &&
					usesVariable(fragment1, declaration1) && usesVariable(fragment2, declaration2)) {
				references.add(mapping);
			}
		}
		return references;
	}

	private static boolean usesVariable(AbstractCodeFragment fragment, VariableDeclaration declaration) {
		List variables = fragment.getVariables();
		return variables.contains(declaration.getVariableName()) ||
				(declaration.isAttribute() && variables.contains("this." + declaration.getVariableName()));
	}

	public static Set findReturnReferences(Set mappings) {
		Set references = new LinkedHashSet();
		for(AbstractCodeMapping mapping : mappings) {
			if(mapping.getFragment1().getLocationInfo().getCodeElementType().equals(CodeElementType.RETURN_STATEMENT) &&
					mapping.getFragment2().getLocationInfo().getCodeElementType().equals(CodeElementType.RETURN_STATEMENT)) {
				references.add(mapping);
			}
		}
		return references;
	}

	public static Set findReferences(VariableDeclaration declaration1, VariableDeclaration declaration2, List operationBodyMapperList) {
		Set references = new LinkedHashSet();
		for(UMLOperationBodyMapper mapper : operationBodyMapperList) {
			references.addAll(findReferences(declaration1, declaration2, mapper.getMappings()));
		}
		return references;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy