/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. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.Threading;
  22. using ICSharpCode.Decompiler;
  23. using ICSharpCode.Decompiler.Ast;
  24. using Mono.Cecil;
  25. namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
  26. {
  27. /// <summary>
  28. /// Searches for overrides of the analyzed method.
  29. /// </summary>
  30. internal sealed class AnalyzedMethodOverridesTreeNode : AnalyzerSearchTreeNode
  31. {
  32. private readonly MethodDefinition analyzedMethod;
  33. public AnalyzedMethodOverridesTreeNode(MethodDefinition analyzedMethod)
  34. {
  35. if (analyzedMethod == null)
  36. throw new ArgumentNullException("analyzedMethod");
  37. this.analyzedMethod = analyzedMethod;
  38. }
  39. public override object Text
  40. {
  41. get { return "Overridden By"; }
  42. }
  43. protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
  44. {
  45. var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedMethod, FindReferencesInType);
  46. return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
  47. }
  48. private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
  49. {
  50. AnalyzerTreeNode newNode = null;
  51. try {
  52. if (!TypesHierarchyHelpers.IsBaseType(analyzedMethod.DeclaringType, type, resolveTypeArguments: false))
  53. yield break;
  54. foreach (MethodDefinition method in type.Methods) {
  55. if (TypesHierarchyHelpers.IsBaseMethod(analyzedMethod, method)) {
  56. bool hidesParent = !method.IsVirtual ^ method.IsNewSlot;
  57. newNode = new AnalyzedMethodTreeNode(method, hidesParent ? "(hides) " : "");
  58. }
  59. }
  60. }
  61. catch (ReferenceResolvingException) {
  62. // ignore this type definition. maybe add a notification about such cases.
  63. }
  64. if (newNode != null) {
  65. newNode.Language = this.Language;
  66. yield return newNode;
  67. }
  68. }
  69. public static bool CanShow(MethodDefinition method)
  70. {
  71. return method.IsVirtual &&
  72. !method.IsFinal &&
  73. !method.DeclaringType.IsSealed &&
  74. !method.DeclaringType.IsInterface; // interface methods are definitions not implementations - cannot be overridden
  75. }
  76. }
  77. }