~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ParameterCanBeDemotedIssue/ParameterCanBeIEnumerableIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ParameterCanBeIEnumerableIssue.cs
 
3
//
 
4
// Author:
 
5
//       Ciprian Khlud <ciprian.mustiata@yahoo.com>
 
6
//
 
7
// Copyright (c) 2013 Ciprian Khlud
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System.Collections.Generic;
 
28
using ICSharpCode.NRefactory.TypeSystem;
 
29
using ICSharpCode.NRefactory.Semantics;
 
30
using System.Linq;
 
31
 
 
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
33
{
 
34
 
 
35
        [IssueDescription("A parameter can IEnumerable/ICollection/IList<T>",
 
36
                          Description = "Finds parameters that can be demoted to a generic list.",
 
37
                          Category = IssueCategories.Opportunities,
 
38
                          Severity = Severity.Suggestion
 
39
                          )]
 
40
        public class ParameterCanBeIEnumerableIssue : ICodeIssueProvider
 
41
        {
 
42
            readonly bool tryResolve;
 
43
                
 
44
                public ParameterCanBeIEnumerableIssue() : this (true)
 
45
                {
 
46
                }
 
47
                
 
48
                public ParameterCanBeIEnumerableIssue(bool tryResolve)
 
49
                {
 
50
                        this.tryResolve = tryResolve;
 
51
                }
 
52
                
 
53
                #region ICodeIssueProvider implementation
 
54
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 
55
                {
 
56
                        var gatherer = new GatherVisitor(context, tryResolve);
 
57
                        var issues = gatherer.GetIssues();
 
58
                        return issues;
 
59
                }
 
60
                #endregion
 
61
                
 
62
                class GatherVisitor : GatherVisitorBase<ParameterCanBeDemotedIssue>
 
63
                {
 
64
                        bool tryResolve;
 
65
                        
 
66
                        public GatherVisitor(BaseRefactoringContext context, bool tryResolve) : base (context)
 
67
                        {
 
68
                                this.tryResolve = tryResolve;
 
69
                        }
 
70
                        
 
71
                        public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
 
72
                        {
 
73
                                methodDeclaration.Attributes.AcceptVisitor(this);
 
74
                                if (HasEntryPointSignature(methodDeclaration))
 
75
                                        return;
 
76
                                var eligibleParameters = methodDeclaration.Parameters
 
77
                                        .Where(p => p.ParameterModifier != ParameterModifier.Out && p.ParameterModifier != ParameterModifier.Ref)
 
78
                                                .ToList();
 
79
                                if (eligibleParameters.Count == 0)
 
80
                                        return;
 
81
                                var declarationResolveResult = ctx.Resolve(methodDeclaration) as MemberResolveResult;
 
82
                                if (declarationResolveResult == null)
 
83
                                        return;
 
84
                                var member = declarationResolveResult.Member;
 
85
                                if (member.IsOverride || member.IsOverridable || member.ImplementedInterfaceMembers.Any())
 
86
                                        return;
 
87
                                
 
88
                                var collector = new TypeCriteriaCollector(ctx);
 
89
                                methodDeclaration.AcceptVisitor(collector);
 
90
                                
 
91
                                foreach (var parameter in eligibleParameters) {
 
92
                                        ProcessParameter(parameter, methodDeclaration.Body, collector);
 
93
                                }
 
94
                        }
 
95
                        
 
96
                        bool HasEntryPointSignature(MethodDeclaration methodDeclaration)
 
97
                        {
 
98
                                if (!methodDeclaration.Modifiers.HasFlag(Modifiers.Static))
 
99
                                        return false;
 
100
                                var returnType = ctx.Resolve(methodDeclaration.ReturnType).Type;
 
101
                                if (!returnType.IsKnownType(KnownTypeCode.Int32) && !returnType.IsKnownType(KnownTypeCode.Void))
 
102
                                        return false;
 
103
                                var parameterCount = methodDeclaration.Parameters.Count;
 
104
                                if (parameterCount == 0)
 
105
                                        return true;
 
106
                                if (parameterCount != 1)
 
107
                                        return false;
 
108
                                var parameterType = ctx.Resolve(methodDeclaration.Parameters.First()).Type as ArrayType;
 
109
                                return parameterType != null && parameterType.ElementType.IsKnownType(KnownTypeCode.String);
 
110
                        }
 
111
 
 
112
            void ProcessParameter(ParameterDeclaration parameter, AstNode rootResolutionNode, TypeCriteriaCollector collector)
 
113
            {
 
114
                var localResolveResult = ctx.Resolve(parameter) as LocalResolveResult;
 
115
                if (localResolveResult == null)
 
116
                    return;
 
117
                var variable = localResolveResult.Variable;
 
118
                var typeKind = variable.Type.Kind;
 
119
                if (!(typeKind == TypeKind.Class ||
 
120
                      typeKind == TypeKind.Struct ||
 
121
                      typeKind == TypeKind.Interface ||
 
122
                      typeKind == TypeKind.Array) ||
 
123
                    !collector.UsedVariables.Contains(variable))
 
124
                {
 
125
                    return;
 
126
                }
 
127
 
 
128
                var candidateTypes = localResolveResult.Type.GetAllBaseTypes()
 
129
                    .Where(t => t.IsParameterized)
 
130
                    .ToList();
 
131
 
 
132
                var validTypes =
 
133
                    (from type in candidateTypes
 
134
                     where !tryResolve || ParameterCanBeDemotedIssue.TypeChangeResolvesCorrectly(ctx, parameter, rootResolutionNode, type)
 
135
                     select type).ToList();
 
136
                if (!validTypes.Any()) return;
 
137
 
 
138
                var foundType = validTypes.FirstOrDefault();
 
139
                if (foundType == null)
 
140
                    return;
 
141
 
 
142
                AddIssue(parameter.NameToken, string.Format(ctx.TranslateString("Parameter can be {0}"),
 
143
                                                            foundType.Name),
 
144
                         script => script.Replace(parameter.Type, CreateShortType(ctx, foundType, parameter)));
 
145
            }
 
146
 
 
147
                    public static AstType CreateShortType(BaseRefactoringContext refactoringContext, IType expressionType, AstNode node)
 
148
            {
 
149
 
 
150
                var csResolver = refactoringContext.Resolver.GetResolverStateBefore(node);
 
151
                var builder = new TypeSystemAstBuilder(csResolver);
 
152
                return builder.ConvertType(expressionType);
 
153
            }
 
154
        }
 
155
        }
 
156
}