Debian Codegraph @

external/api-doc-tools/mdoc/Mono.Documentation/Util/AttachedEntitiesHelper.cs

raw
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Mono.Cecil;
5 using Mono.Collections.Generic;
6 using Mono.Documentation.Updater;
7
8 namespace Mono.Documentation.Util
9 {
10 public static class AttachedEntitiesHelper
11 {
12 private const string PropertyConst = "Property";
13 private const string EventConst = "Event";
14
15 private static readonly int EventLength = EventConst.Length;
16 private static readonly int PropertyLength = PropertyConst.Length;
17
18 public static string GetEventName(string fieldDefinitionName)
19 {
20 return fieldDefinitionName.Substring(0, fieldDefinitionName.Length - EventLength);
21 }
22
23 public static string GetPropertyName(string fieldDefinitionName)
24 {
25 return fieldDefinitionName.Substring(0, fieldDefinitionName.Length - PropertyLength);
26 }
27
28 public static IEnumerable<MemberReference> GetAttachedEntities(TypeDefinition type)
29 {
30 var methodsLookUpTable = GetMethodsLookUpTable(type);
31 foreach (var attachedEventReference in GetAttachedEvents(type, methodsLookUpTable))
32 {
33 yield return attachedEventReference;
34 }
35 foreach (var attachedEventProperty in GetAttachedProperties(type, methodsLookUpTable))
36 {
37 yield return attachedEventProperty;
38 }
39 }
40
41 private static Dictionary<string, IEnumerable<MethodDefinition>> GetMethodsLookUpTable(TypeDefinition type)
42 {
43 return type.Methods.GroupBy(i => i.Name, i => i).ToDictionary(i => i.Key, i => i.AsEnumerable());
44 }
45
46 #region Attached Events
47 private static IEnumerable<AttachedEventReference> GetAttachedEvents(TypeDefinition type, Dictionary<string, IEnumerable<MethodDefinition>> methods)
48 {
49 foreach (var field in type.Fields)
50 {
51 if (IsAttachedEvent(field, methods))
52 yield return new AttachedEventReference(field);
53 }
54 }
55
56 private static bool IsAttachedEvent(FieldDefinition field, Dictionary<string, IEnumerable<MethodDefinition>> methods)
57 {
58 // https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/attached-events-overview
59 if (!field.Name.EndsWith(EventConst))
60 return false;
61 var addMethodName = $"Add{GetEventName(field.Name)}Handler";
62 var removeMethodName = $"Remove{GetEventName(field.Name)}Handler";
63 return
64 // WPF implements attached events as routed events; the identifier to use for an event (RoutedEvent) is already defined by the WPF event system
65 IsAssignableTo(field.FieldType, "System.Windows.RoutedEvent")
66 && field.IsPublic
67 && field.IsStatic
68 && field.IsInitOnly
69
70 // Has a method Add*Handler with two parameters.
71 // Has a method Remove*Handler with two parameters.
72 && methods.ContainsKey(addMethodName)
73 && methods.ContainsKey(removeMethodName)
74 && methods[addMethodName].Any(IsAttachedEventMethod)
75 && methods[removeMethodName].Any(IsAttachedEventMethod);
76 }
77
78 private static bool IsAttachedEventMethod(MethodDefinition method)
79 {
80 // The method must be public and static, with no return value.
81 return method.IsPublic
82 && method.IsStatic
83 && method.ReturnType.FullName == Consts.VoidFullName
84 && AreAttachedEventMethodParameters(method.Parameters);
85 }
86
87 private static bool AreAttachedEventMethodParameters(Collection<ParameterDefinition> parameters)
88 {
89 if (parameters.Count != 2)
90 return false;
91 return
92 // The first parameter is DependencyObject
93 IsAssignableTo(parameters[0].ParameterType, "System.Windows.DependencyObject")
94
95 // The second parameter is the handler to add/remove
96 && IsAttachedEventHandler(parameters[1].ParameterType);
97 }
98
99 private static bool IsAttachedEventHandler(TypeReference typeReference)
100 {
101 var type = typeReference.Resolve();
102 if (!DocUtils.IsDelegate(type))
103 return false;
104 MethodDefinition invoke = type.GetMethod("Invoke");
105 return invoke.Parameters.Count == 2;
106 }
107 #endregion
108
109 #region Attached Properties
110 private static IEnumerable<AttachedPropertyReference> GetAttachedProperties(TypeDefinition type, Dictionary<string, IEnumerable<MethodDefinition>> methods)
111 {
112 foreach (var field in type.Fields)
113 {
114 if (IsAttachedProperty(field, methods))
115 yield return new AttachedPropertyReference(field);
116 }
117 }
118
119 private static bool IsAttachedProperty(FieldDefinition field, Dictionary<string, IEnumerable<MethodDefinition>> methods)
120 {
121 // https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/attached-properties-overview
122 // https://github.com/mono/api-doc-tools/issues/63#issuecomment-328995418
123 if (!field.Name.EndsWith(PropertyConst, StringComparison.Ordinal))
124 return false;
125 var propertyName = GetPropertyName(field.Name);
126 var getMethodName = $"Get{propertyName}";
127 var setMethodName = $"Set{propertyName}";
128
129 var hasExistingProperty = field?.DeclaringType?.Properties.Any (p => p.Name.Equals (propertyName, System.StringComparison.Ordinal));
130 var hasExistingField = field?.DeclaringType?.Fields.Any (f => f.Name.Equals (propertyName, System.StringComparison.Ordinal));
131
132 return !hasExistingProperty.IsTrue () && !hasExistingField.IsTrue () &&
133 // Class X has a static field of type DependencyProperty [Name]Property
134 (field.FieldType.FullName == Consts.DependencyPropertyFullName || field.FieldType.FullName == Consts.DependencyPropertyFullNameXaml)
135 && field.IsPublic
136 && field.IsStatic
137 && field.IsInitOnly
138
139 // Class X also has static methods with the following names: Get[Name] and Set[Name]
140 && ((methods.ContainsKey(getMethodName) && methods[getMethodName].Any(IsAttachedPropertyGetMethod))
141 || (methods.ContainsKey(setMethodName) && methods[setMethodName].Any(IsAttachedPropertySetMethod)));
142
143 }
144
145 private static bool IsAttachedPropertyGetMethod(MethodDefinition method)
146 {
147 return method.Parameters.Count == 1
148
149 // returns a value of type dp.PropertyType (or IsAssignableTo…), where dp is the value of the static field.
150 // && IsAssignableTo(method.ReturnType, "");
151
152 // The Get method takes one argument of type DependencyObject(or something IsAssignableTo(DependencyObject),
153 && (IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullName) || IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullNameXaml));
154 }
155
156 private static bool IsAttachedPropertySetMethod(MethodDefinition method)
157 {
158 return method.Parameters.Count == 2// The Set method takes two arguments.
159
160 // The first has type DependencyObject(or IsAssignableTo…),
161 && (IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullName) || IsAssignableTo(method.Parameters[0].ParameterType, Consts.DependencyObjectFullNameXaml))
162
163 // the second has type dp.PropertyType (or IsAssignableTo…).
164 // && IsAssignableTo(method.Parameters[1].ParameterType, "")
165
166 // It returns void.
167 && method.ReturnType.FullName == Consts.VoidFullName;
168 }
169 #endregion
170
171 private static bool IsAssignableTo(TypeReference type, string targetTypeName)
172 {
173 if (type == null)
174 return false;
175 var typeDefenition = type.Resolve();
176 if (typeDefenition == null || typeDefenition.IsSealed)
177 return type.FullName == targetTypeName;
178
179 return type.FullName == targetTypeName || IsAssignableTo(typeDefenition.BaseType, targetTypeName);
180 }
181
182
183 }
184 internal static class NBoolExtensions
185 {
186 public static bool IsTrue (this Nullable<bool> value) =>
187 value.HasValue && value.Value;
188 }
189 }