/ILSpy/TreeNodes/Analyzer/AnalyzedTypeInstantiationsTreeNode.cs

http://github.com/icsharpcode/ILSpy · C# · 93 lines · 61 code · 13 blank · 19 comment · 18 complexity · d374213dc45f8b6db6d8d828cd94a220 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.Ast;
  23. using Mono.Cecil;
  24. using Mono.Cecil.Cil;
  25. namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
  26. {
  27. internal sealed class AnalyzedTypeInstantiationsTreeNode : AnalyzerSearchTreeNode
  28. {
  29. private readonly TypeDefinition analyzedType;
  30. private readonly bool isSystemObject;
  31. public AnalyzedTypeInstantiationsTreeNode(TypeDefinition analyzedType)
  32. {
  33. if (analyzedType == null)
  34. throw new ArgumentNullException("analyzedType");
  35. this.analyzedType = analyzedType;
  36. this.isSystemObject = (analyzedType.FullName == "System.Object");
  37. }
  38. public override object Text
  39. {
  40. get { return "Instantiated By"; }
  41. }
  42. protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
  43. {
  44. var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedType, FindReferencesInType);
  45. return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
  46. }
  47. private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
  48. {
  49. foreach (MethodDefinition method in type.Methods) {
  50. bool found = false;
  51. if (!method.HasBody)
  52. continue;
  53. // ignore chained constructors
  54. // (since object is the root of everything, we can short circuit the test in this case)
  55. if (method.Name == ".ctor" &&
  56. (isSystemObject || analyzedType == type || TypesHierarchyHelpers.IsBaseType(analyzedType, type, false)))
  57. continue;
  58. foreach (Instruction instr in method.Body.Instructions) {
  59. MethodReference mr = instr.Operand as MethodReference;
  60. if (mr != null && mr.Name == ".ctor") {
  61. if (Helpers.IsReferencedBy(analyzedType, mr.DeclaringType)) {
  62. found = true;
  63. break;
  64. }
  65. }
  66. }
  67. method.Body = null;
  68. if (found) {
  69. var node = new AnalyzedMethodTreeNode(method);
  70. node.Language = this.Language;
  71. yield return node;
  72. }
  73. }
  74. }
  75. public static bool CanShow(TypeDefinition type)
  76. {
  77. return (type.IsClass && !(type.IsAbstract && type.IsSealed) && !type.IsEnum);
  78. }
  79. }
  80. }