5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using ICSharpCode.NRefactory.Semantics;
30
using ICSharpCode.NRefactory.CSharp.Resolver;
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
34
class StaticVisitor : DepthFirstAstVisitor
36
readonly RefactoringContext context;
37
public bool UsesNonStaticMember = false;
39
StaticVisitor(RefactoringContext context)
41
this.context = context;
45
public static bool UsesNotStaticMember(RefactoringContext context, AstNode node)
47
var visitor = new StaticVisitor(context);
48
node.AcceptVisitor(visitor);
49
return visitor.UsesNonStaticMember;
52
protected override void VisitChildren(AstNode node)
54
if (UsesNonStaticMember)
56
base.VisitChildren(node);
59
public override void VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression)
61
UsesNonStaticMember = true;
62
base.VisitThisReferenceExpression(thisReferenceExpression);
65
public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression)
67
UsesNonStaticMember = true;
68
base.VisitBaseReferenceExpression(baseReferenceExpression);
71
public override void VisitIdentifierExpression(IdentifierExpression identifierExpression)
73
var resolveResult = context.Resolve(identifierExpression);
74
if (resolveResult is MemberResolveResult) {
75
var memberResult = (MemberResolveResult)resolveResult;
76
if (!memberResult.Member.IsStatic)
77
UsesNonStaticMember = true;
78
} else if (resolveResult is MethodGroupResolveResult) {
79
var methodGroupResolveResult = (MethodGroupResolveResult)resolveResult;
80
if (methodGroupResolveResult.Methods.Any(m => !m.IsStatic))
81
UsesNonStaticMember = true;