public bool HasRefactoring()
        {
            // Hosting all the detected refactorings.
            var detectedRefactorings = new List<IManualRefactoring>();

            // Convert string to IDocument instance.
            var converter = new String2IDocumentConverter();
            var documentBefore = (IDocument)converter.Convert(beforeSource, null, null, null);
            var documentAfter = (IDocument)converter.Convert(afterSource, null, null, null);

            // Group mehthos by scopes, a scope is defined by namespace name + class name.
            var analyzer = AnalyzerFactory.GetDocumentAnalyzer();
            analyzer.SetDocument(documentBefore);
            var methodsGroupsBefore = GetMethodInSameScope(analyzer);
            analyzer.SetDocument(documentAfter);
            var methodsGroupsAfter = GetMethodInSameScope(analyzer);

            // Get the copes that are common in both before and after document.
            var commonKeys = methodsGroupsAfter.Keys.Intersect(methodsGroupsBefore.Keys);

            // For each common scope
            foreach (var key in commonKeys)
            {
                IEnumerable<SyntaxNode> methodsBefore, methodsAfter;

                // Get methods in the before document within this scope.
                methodsGroupsBefore.TryGetValue(key, out methodsBefore);

                // Get methods in the after documetn within this scope
                methodsGroupsAfter.TryGetValue(key, out methodsAfter);

                foreach (MethodDeclarationSyntax methodbefore in methodsBefore)
                {
                    foreach (MethodDeclarationSyntax methodAfter in methodsAfter)
                    {
                        // Consider two methods are before and after version if they are in the same scope
                        // and also have the same identifier (method name).
                        if(methodbefore.Identifier.ValueText.Equals(methodAfter.Identifier.ValueText))
                        {
                            // Get an in-method detector.
                            var detector = new InMethodChangeSignatureDetector(methodbefore, methodAfter);
                            if(detector.HasRefactoring())
                            {
                                // Add the detected refactorings
                                detectedRefactorings.AddRange(detector.GetRefactorings());
                            }
                        }
                    }
                }
            }
            if(detectedRefactorings.Any())
            {
                this.refactorings = detectedRefactorings.AsEnumerable();
                return true;
            }

            return false;
        }
 public TypeHierarchyAnalyzerTests()
 {
     var source = FileUtil.ReadAllText(TestUtil.GetFakeSourceFolder() + "/TypeHierarchyFakeSource.cs");
     var converter = new String2IDocumentConverter();
     document = (IDocument)converter.Convert(source, null, null, null);
     analyzer = AnalyzerFactory.GetTypeHierarchyAnalyzer();
     analyzer.SetSemanticModel(document.GetSemanticModel());
     root = (SyntaxNode) document.GetSyntaxRoot();
 }
 public MethodInvocationAnalyzerTests()
 {
     this.analyzer = AnalyzerFactory.GetMethodInvocationAnalyzer();
     var converter = new String2IDocumentConverter();
     this.document = (IDocument) converter.Convert(FileUtil.ReadAllText(
         TestUtil.GetFakeSourceFolder() + "ChangeMethodSignatureAfter.txt"), null, null, null);
     this.invocations = ((SyntaxNode)document.GetSyntaxRoot()).
         DescendantNodes().Where(i => i.Kind == SyntaxKind.InvocationExpression);
     this.logger = NLoggerUtil.GetNLogger(typeof(MethodInvocationAnalyzerTests));
 }
Beispiel #4
0
 public void TestMethod5()
 {
     string path = TestUtil.GetTestProjectPath() + "UtilityTest.cs";
     string code = FileUtil.ReadAllText(path);
     Assert.IsNotNull(code);
     Assert.IsFalse(code.Equals(""));
     var converter = new String2IDocumentConverter();
     IDocument document = (IDocument) converter.Convert(code, typeof (IDocument), null, null);
     Assert.IsNotNull(document);
     logger.Info(document.GetText());
 }
 public TypableRetrieverTests()
 {
     this.retriever = RetrieverFactory.GetTypablesRetriever();
     var code = TestUtil.GetFakeSourceFolder() + "MethodAnalyzerExample.cs";
     var converter = new String2IDocumentConverter();
     this.document = (IDocument)converter.Convert(FileUtil.ReadAllText(code), null, null, null);
     this.logger = NLoggerUtil.GetNLogger(typeof (TypableRetrieverTests));
     this.analyzer = AnalyzerFactory.GetDocumentAnalyzer();
     retriever.SetDocument(document);
     analyzer.SetDocument(document);
 }
        public MethodAnalyzerTests()
        {
            var code = TestUtil.GetFakeSourceFolder() + "MethodAnalyzerExample.cs";
            var converter = new String2IDocumentConverter();
            document = (IDocument) converter.Convert(FileUtil.ReadAllText(code), null, null, null);
            logger = NLoggerUtil.GetNLogger(typeof (MethodAnalyzerTests));

            documentAnalyzer.SetDocument(document);
            var namespaceDec = documentAnalyzer.GetNamespaceDecalarations().First();
            var classDec = documentAnalyzer.GetClassDeclarations((NamespaceDeclarationSyntax)namespaceDec).First();
            methods = documentAnalyzer.GetMethodDeclarations((ClassDeclarationSyntax)classDec);
        }
        public ExtractMethodConditionCheckTests()
        {
            string fileBefore = TestUtil.GetFakeSourceFolder() + "EMDetectorBefore.cs";
            string fileAfter = TestUtil.GetFakeSourceFolder() + "EMDetectorAfter.cs";
            var converter = new String2IDocumentConverter();

            // Get before and after document.
            before = (IDocument) converter.Convert(FileUtil.ReadAllText(fileBefore), typeof(IDocument), null, null);
            after = (IDocument)converter.Convert(FileUtil.ReadAllText(fileAfter), typeof(IDocument), null, null);

            // Get all the methods in before and after.
            beforeMethods = GetAllMethod(before);
            afterMethods = GetAllMethod(after);
        }
        public DataFlowAnalyzerTests()
        {
            var code = TestUtil.GetFakeSourceFolder() + "DataFlowExample.cs";
            var converter = new String2IDocumentConverter();
            this.document = (IDocument)converter.Convert(FileUtil.ReadAllText(code), null, null, null);
            logger = NLoggerUtil.GetNLogger(typeof(DataFlowAnalyzerTests));

            var analyzer = AnalyzerFactory.GetDocumentAnalyzer();
            analyzer.SetDocument(document);
            var namespaceDec = analyzer.GetNamespaceDecalarations().First();
            var classDec = analyzer.GetClassDeclarations((NamespaceDeclarationSyntax) namespaceDec).First();
            this.methods = analyzer.GetMethodDeclarations((ClassDeclarationSyntax) classDec);

            _statementsDataFlowanalyzer.SetDocument(document);
        }
        public InlineMethodRefactoringTests()
        {
            var convertor = new String2IDocumentConverter();
            this.detector = RefactoringDetectorFactory.CreateInlineMethodDetector();
            this.checkersList = ConditionCheckingFactory.GetInlineMethodConditionsList();
            this.codeBefore = FileUtil.ReadAllText(TestUtil.GetFakeSourceFolder() + "InlineMethodBefore.txt");
            this.codeAfter = FileUtil.ReadAllText(TestUtil.GetFakeSourceFolder() + "InlineMethodAfter.txt");
            this.documentBefore = (IDocument)convertor.Convert(codeBefore, null, null, null);
            this.documentAfter = (IDocument)convertor.Convert(codeAfter, null, null, null);

            detector.SetSourceBefore(codeBefore);
            detector.SetSourceAfter(codeAfter);

            this.dummyDetector = RefactoringDetectorFactory.CreateDummyInlineMethodDetector();
            dummyDetector.SetSourceBefore(codeBefore);
            dummyDetector.SetSourceAfter(codeAfter);
        }
Beispiel #10
0
        public SyntaxNode GetDeclarationSyntaxNode()
        {
            // Get the default declaration location of the symbol
            var definition = symbol.OriginalDefinition.Locations.FirstOrDefault();

            // Get the source code and span of the definition
            var source = definition.SourceTree.GetRoot().GetText();
            var span = definition.SourceSpan;

            // Convert the source code to IDocument to use document analyzer.
            var converter = new String2IDocumentConverter();
            document = (IDocument) converter.Convert(source, null, null, null);
            var analyzer = AnalyzerFactory.GetDocumentAnalyzer();
            analyzer.SetDocument(document);
            return analyzer.
                // First get all the declarations in the document.
                GetAllDeclarations().
                    // Select the declaration that contains the symbol declaration.
                    Where(d => d.Span.Contains(span)).
                        // Order them by the length, the shortest shall be the declaration for the symbol.
                        OrderBy(d => d.Span.Length).First();
        }
 /// <summary>
 /// Convert a string to an instance of document.
 /// </summary>
 /// <param name="code"></param>
 /// <returns></returns>
 public static IDocument Convert2IDocument(string code)
 {
     var converter = new String2IDocumentConverter();
     return (IDocument) converter.Convert(code, null, null, null);
 }
 public IDocument Convert2Document()
 {
     var converter = new String2IDocumentConverter();
     return (IDocument) converter.Convert(GetSource(), null, null, null);
 }
Beispiel #13
0
 public static IDocument GetDocumentForFakeSource(string name)
 {
     var source = FileUtil.ReadAllText(GetFakeSourceFolder() + name);
     var converter = new String2IDocumentConverter();
     return (IDocument)converter.Convert(source, null, null, null);
 }