public ICodeIssueComputer CheckCondition(IDocument before, IDocument after, IManualRefactoring input) { return CheckCondition(before, after, (IManualExtractMethodRefactoring)input); }
public bool HasRefactoring() { // Get the first invocation of callee in the caller method body. var invocation = ASTUtil.GetAllInvocationsInMethod(callerAfter, calleeAfter, treeAfter).First(); // Precondition Contract.Requires(invocation != null); /* Flatten the caller after by replacing callee invocation with the code in the calle method body. */ String callerAfterFlattenned = ASTUtil.FlattenMethodInvocation(callerAfter, calleeAfter, invocation); // logger.Info("Caller Before:\n" + callerBefore.GetText()); // logger.Info("Caller After:\n" + callerAfter.GetText()); // logger.Info("Callee after:\n" + calleeAfter.GetText()); // logger.Info("Flattened Caller:\n" + callerAfterFlattenned); var beforeWithoutSpace = callerBefore.GetFullText().Replace(" ", ""); // The distance between flattened caller after and the caller before. int dis1 = StringUtil.GetStringDistance(callerAfterFlattenned.Replace(" ", ""), beforeWithoutSpace); // The distance between caller after and the caller before. int dis2 = StringUtil.GetStringDistance(callerAfter.GetFullText().Replace(" ", ""), beforeWithoutSpace); logger.Info("Distance Gain by Flattening:" + (dis2 - dis1)); // Check whether the distance is shortened by flatten. if (dis2 > dis1) { // If similar enough, a manual refactoring instance is likely to be detected and created. var analyzer = RefactoringAnalyzerFactory.CreateManualExtractMethodAnalyzer(); analyzer.SetMethodDeclarationBeforeExtracting(callerBefore); analyzer.SetExtractedMethodDeclaration(calleeAfter); analyzer.SetInvocationExpression(invocation); // If the analyzer can get a refactoring from the given information, get the refactoring // and return true. if (analyzer.CanGetRefactoring()) { refactoring = analyzer.GetRefactoring(); return true; } } return false; }
public ICodeIssueComputer CheckCondition(IDocument before, IDocument after, IManualRefactoring input) { var signatureRefactoring = (IChangeMethodSignatureRefactoring) input; return new UnchangedMethodInvocationComputer(((IChangeMethodSignatureRefactoring) input).ChangedMethodDeclaration , signatureRefactoring.ParametersMap.AsEnumerable()); }
public bool HasRefactoring() { // Mapping parameters in before and after version, for example, f(int a, int b) and f(int b, int a) // 's mapper shall be <0,1><1,0>> var parametersMap = new List<Tuple<int, int>>(); // Combine the RefactoringType of parameters in before and after method declaration as a string. var typeStringBefore = GetParameterTypeCombined(beforeMethod); var typeStringAfter = GetParameterTypeCombined(afterMethod); // If the strings are not equal, compiler warnings are enough to detect unchanged method signatures. if(typeStringBefore.Equals(typeStringAfter)) { // Get indexes for parameters in the before and after version. var beforeParaUsageIndexes = GetParameterUsagesIndexes(beforeMethod); var afterParaUsageIndexes = GetParameterUsagesIndexes(afterMethod); // Iterate usages for each parameter in the after version for (int i = 0; i < afterParaUsageIndexes.Count(); i++ ) { // Retrieve the usage indexes for this parameter. var afterIndex = afterParaUsageIndexes.ElementAt(i); // Iterate usages for each parameter in the before version. for (int j = 0; j < beforeParaUsageIndexes.Count(); j++) { // Retrieve the usage indexes for current parameter. var beforeIndex = beforeParaUsageIndexes.ElementAt(j); // If all the indexes are equal, finding a map. if(AreAllElemenetsEqual(afterIndex, beforeIndex)) { logger.Info("Para " + j + " in before version's usage indexes:" + StringUtil.ConcatenateAll(",", beforeIndex.Select(integer => integer.ToString()))); logger.Info("Para " + i + " in after version's usage indexes:" + StringUtil.ConcatenateAll(",", afterIndex.Select(integer => integer.ToString()))); logger.Info("Parameter mapping: " + j + "=>" + i); parametersMap.Add(Tuple.Create(j, i)); } } } // Search for all the tuples in the map. foreach (var pair in parametersMap) { // If a tuple has values that are different, positions of parameters are changed. if (pair.Item1 != pair.Item2) { // Order the map by indexes in before versions. var orderedList = new List<Tuple<int,int>>(); orderedList.AddRange(parametersMap.OrderBy(t => t.Item1)); refactoring = ManualRefactoringFactory. CreateManualChangeMethodSignatureRefactoring(afterMethod, orderedList); return true; } } } return false; }
/* Handle a detected refactoring by saveing it at an independent file. */ private string HandleDetectedRefactoring(string before, string after, IManualRefactoring refactoring) { // Get the folder and the file name for this detected refactoring. string refactoringDirectory = DETECTED_REFACTORINGS_ROOT + solutionName + "/" + GetRefactoringType(refactoring); string refactoringFilePath = refactoringDirectory + "/" + fileName + refactoringsCount + ".txt"; // If the directory does not exist, create it. if(!Directory.Exists(refactoringDirectory)) { Directory.CreateDirectory(refactoringDirectory); } // Streaming all the needed information to the file. var stream = File.CreateText(refactoringFilePath); stream.WriteLine("Source Before:"); stream.WriteLine(before); stream.WriteLine("Source After:"); stream.WriteLine(after); stream.WriteLine("Detected Refactoring:"); stream.WriteLine(refactoring.ToString()); stream.Flush(); stream.Close(); // Return the saved refactoring file path. return refactoringFilePath; }
private string GetRefactoringType(IManualRefactoring refactoring) { var converter = new RefactoringType2StringConverter(); return (string)converter.Convert(refactoring.RefactoringType, null, null, null); }
public void CheckRefactoringCondition(ICodeHistoryRecord before, ICodeHistoryRecord after, IManualRefactoring refactoring) { Enqueue(new ConditionCheckWorkItem(before, after, refactoring)); }
public ConditionCheckWorkItem(ICodeHistoryRecord before, ICodeHistoryRecord after, IManualRefactoring refactoring) { this.before = before.Convert2Document(); this.after = after.Convert2Document(); this.DocumentKey = before.GetKey(); this.refactoring = refactoring; logger = NLoggerUtil.GetNLogger(typeof(ConditionCheckWorkItem)); }