Exemplo n.º 1
0
        private static IDictionary<SyntaxNode, SyntaxNode> GetRetroMethods(Compilation compilation, INamedTypeSymbol objectType)
        {
            var syntaxNodes = new Dictionary<SyntaxNode, SyntaxNode>();

            // FIND
            // 1. Generic properties symbols
            // =============================

            // NOTE -
            var genericPropertiesList = new List<IPropertySymbol>();
            var genericMethodsList = new List<IMethodSymbol>();

            var semanticModels = compilation.SyntaxTrees.Select(x => compilation.GetSemanticModel(x));

            foreach (var semanticModel in semanticModels)
            {
                var tree = semanticModel.SyntaxTree;

                // Find generic classes
                var genericClassFinder = new GenericClassFinder(tree, semanticModel);
                var classDeclarations = genericClassFinder.Get().ToArray();

                SyntaxNode newTree = tree.GetRoot();
                // Find reference to it's members

                // methods symbols
                var methodsToCast = classDeclarations
                    .SelectMany(x => x.Members)
                    .OfType<BaseMethodDeclarationSyntax>()
                    .Select(x => (IMethodSymbol)semanticModel.GetDeclaredSymbol(x));

                genericMethodsList.AddRange(methodsToCast);

                // properties symbols
                var propertiesToCast = classDeclarations
                    .SelectMany(x => x.Members)
                    .OfType<BasePropertyDeclarationSyntax>()
                    .Select(x => (IPropertySymbol)semanticModel.GetDeclaredSymbol(x));

                genericPropertiesList.AddRange(propertiesToCast);
            }
            
            var genRefs = new GenRef(genericMethodsList, genericPropertiesList);

             //CHANGE
             //1. Generic properties references with cast
             //2. Generic properties to Object properties
             //======

            foreach (var semanticModel in semanticModels)
            {
                var root = semanticModel.SyntaxTree.GetRoot();

                // get cast changes
                var castReferences = new Dictionary<SyntaxNode, SyntaxNode>();
                CastResursiveMethod(root, semanticModel, genRefs, castReferences);
                
                // get generic properties to object properties
                var retroProperties = GenericToObject(root, semanticModel, objectType);

                var allChanges = castReferences.Concat(retroProperties);

                foreach (var ch in allChanges)
                    syntaxNodes.Add(ch.Key, ch.Value);
            }

            return syntaxNodes;
        }
Exemplo n.º 2
0
        private static IDictionary <SyntaxNode, SyntaxNode> GetRetroMethods(Compilation compilation, INamedTypeSymbol objectType)
        {
            var syntaxNodes = new Dictionary <SyntaxNode, SyntaxNode>();

            // FIND
            // 1. Generic properties symbols
            // =============================

            // NOTE -
            var genericPropertiesList = new List <IPropertySymbol>();
            var genericMethodsList    = new List <IMethodSymbol>();

            var semanticModels = compilation.SyntaxTrees.Select(x => compilation.GetSemanticModel(x));

            foreach (var semanticModel in semanticModels)
            {
                var tree = semanticModel.SyntaxTree;

                // Find generic classes
                var genericClassFinder = new GenericClassFinder(tree, semanticModel);
                var classDeclarations  = genericClassFinder.Get().ToArray();

                SyntaxNode newTree = tree.GetRoot();
                // Find reference to it's members

                // methods symbols
                var methodsToCast = classDeclarations
                                    .SelectMany(x => x.Members)
                                    .OfType <BaseMethodDeclarationSyntax>()
                                    .Select(x => (IMethodSymbol)semanticModel.GetDeclaredSymbol(x));

                genericMethodsList.AddRange(methodsToCast);

                // properties symbols
                var propertiesToCast = classDeclarations
                                       .SelectMany(x => x.Members)
                                       .OfType <BasePropertyDeclarationSyntax>()
                                       .Select(x => (IPropertySymbol)semanticModel.GetDeclaredSymbol(x));

                genericPropertiesList.AddRange(propertiesToCast);
            }

            var genRefs = new GenRef(genericMethodsList, genericPropertiesList);

            //CHANGE
            //1. Generic properties references with cast
            //2. Generic properties to Object properties
            //======

            foreach (var semanticModel in semanticModels)
            {
                var root = semanticModel.SyntaxTree.GetRoot();

                // get cast changes
                var castReferences = new Dictionary <SyntaxNode, SyntaxNode>();
                CastResursiveMethod(root, semanticModel, genRefs, castReferences);

                // get generic properties to object properties
                var retroProperties = GenericToObject(root, semanticModel, objectType);

                var allChanges = castReferences.Concat(retroProperties);

                foreach (var ch in allChanges)
                {
                    syntaxNodes.Add(ch.Key, ch.Value);
                }
            }

            return(syntaxNodes);
        }
Exemplo n.º 3
0
        private static SyntaxNode CastResursiveMethod(SyntaxNode tree, SemanticModel semanticModel, GenRef genRef, Dictionary<SyntaxNode, SyntaxNode> castChanges)
        {
            var change = new Dictionary<SyntaxNode, SyntaxNode>();

            foreach (var node in tree.ChildNodes())
            {
                ITypeSymbol ts = null;
                
                // if invocation -> ITypeSymbol
                // -------------------------
                if (node is InvocationExpressionSyntax)
                {
                    ISymbol invokedSymbol = semanticModel.GetSymbolInfo(node).Symbol;

                    // if is generic method
                    if (genRef.Methods.Contains(invokedSymbol.OriginalDefinition))
                    {
                        ts = ((IMethodSymbol)invokedSymbol).ReturnType;                        
                    }
                }
                else if ((node is MemberAccessExpressionSyntax) && !(node.Parent is AssignmentExpressionSyntax))
                {
                    ISymbol invokedSymbol = semanticModel.GetSymbolInfo(node).Symbol;

                    // if is generic property
                    if (genRef.Properties.Contains(invokedSymbol.OriginalDefinition))
                    {
                        ts = ((IPropertySymbol)invokedSymbol).Type;                        
                    }
                }

                // recurse for changed node
                var casted = CastResursiveMethod(node, semanticModel, genRef, castChanges);

                if (ts != null)
                {
                    // do cast
                    casted = Helpers.CastTo((ExpressionSyntax)casted, ts);

                    if (node.Parent is MemberAccessExpressionSyntax)
                        casted = ((ExpressionSyntax)casted).Parenthesize();

                    castChanges.Add(node, casted);
                }

                // add for replace
                if (node != casted)
                    change.Add(node, casted);
            }

            if (change.Any())
                tree = tree.ReplaceNodes(change.Keys, (x, y) => change[x]);

            return tree;
        }
Exemplo n.º 4
0
        private static SyntaxNode CastResursiveMethod(SyntaxNode tree, SemanticModel semanticModel, GenRef genRef, Dictionary <SyntaxNode, SyntaxNode> castChanges)
        {
            var change = new Dictionary <SyntaxNode, SyntaxNode>();

            foreach (var node in tree.ChildNodes())
            {
                ITypeSymbol ts = null;

                // if invocation -> ITypeSymbol
                // -------------------------
                if (node is InvocationExpressionSyntax)
                {
                    ISymbol invokedSymbol = semanticModel.GetSymbolInfo(node).Symbol;

                    // if is generic method
                    if (genRef.Methods.Contains(invokedSymbol.OriginalDefinition))
                    {
                        ts = ((IMethodSymbol)invokedSymbol).ReturnType;
                    }
                }
                else if ((node is MemberAccessExpressionSyntax) && !(node.Parent is AssignmentExpressionSyntax))
                {
                    ISymbol invokedSymbol = semanticModel.GetSymbolInfo(node).Symbol;

                    // if is generic property
                    if (genRef.Properties.Contains(invokedSymbol.OriginalDefinition))
                    {
                        ts = ((IPropertySymbol)invokedSymbol).Type;
                    }
                }

                // recurse for changed node
                var casted = CastResursiveMethod(node, semanticModel, genRef, castChanges);

                if (ts != null)
                {
                    // do cast
                    casted = Helpers.CastTo((ExpressionSyntax)casted, ts);

                    if (node.Parent is MemberAccessExpressionSyntax)
                    {
                        casted = ((ExpressionSyntax)casted).Parenthesize();
                    }

                    castChanges.Add(node, casted);
                }

                // add for replace
                if (node != casted)
                {
                    change.Add(node, casted);
                }
            }

            if (change.Any())
            {
                tree = tree.ReplaceNodes(change.Keys, (x, y) => change[x]);
            }

            return(tree);
        }