PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/ILSpy/TreeNodes/Analyzer/AnalyzedMethodOverridesTreeNode.cs

http://github.com/icsharpcode/ILSpy
C# | 87 lines | 56 code | 10 blank | 21 comment | 6 complexity | d48fe3e33107d975df938e0649488bae MD5 | raw file
 1// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
 2// 
 3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
 4// software and associated documentation files (the "Software"), to deal in the Software
 5// without restriction, including without limitation the rights to use, copy, modify, merge,
 6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
 7// to whom the Software is furnished to do so, subject to the following conditions:
 8// 
 9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11// 
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Threading;
23using ICSharpCode.Decompiler;
24using ICSharpCode.Decompiler.Ast;
25using Mono.Cecil;
26
27namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
28{
29	/// <summary>
30	/// Searches for overrides of the analyzed method.
31	/// </summary>
32	internal sealed class AnalyzedMethodOverridesTreeNode : AnalyzerSearchTreeNode
33	{
34		private readonly MethodDefinition analyzedMethod;
35
36		public AnalyzedMethodOverridesTreeNode(MethodDefinition analyzedMethod)
37		{
38			if (analyzedMethod == null)
39				throw new ArgumentNullException("analyzedMethod");
40
41			this.analyzedMethod = analyzedMethod;
42		}
43
44		public override object Text
45		{
46			get { return "Overridden By"; }
47		}
48
49		protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
50		{
51			var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedMethod, FindReferencesInType);
52			return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
53		}
54
55		private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
56		{
57			AnalyzerTreeNode newNode = null;
58			try {
59				if (!TypesHierarchyHelpers.IsBaseType(analyzedMethod.DeclaringType, type, resolveTypeArguments: false))
60					yield break;
61
62				foreach (MethodDefinition method in type.Methods) {
63					if (TypesHierarchyHelpers.IsBaseMethod(analyzedMethod, method)) {
64						bool hidesParent = !method.IsVirtual ^ method.IsNewSlot;
65						newNode = new AnalyzedMethodTreeNode(method, hidesParent ? "(hides) " : "");
66					}
67				}
68			}
69			catch (ReferenceResolvingException) {
70				// ignore this type definition. maybe add a notification about such cases.
71			}
72
73			if (newNode != null) {
74				newNode.Language = this.Language;
75				yield return newNode;
76			}
77		}
78
79		public static bool CanShow(MethodDefinition method)
80		{
81			return method.IsVirtual &&
82				!method.IsFinal &&
83				!method.DeclaringType.IsSealed &&
84				!method.DeclaringType.IsInterface;	// interface methods are definitions not implementations - cannot be overridden
85		}
86	}
87}