This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
MethodRocks.cs
321 lines (278 loc) · 10.9 KB
/
MethodRocks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// Gendarme.Framework.Rocks.MethodRocks
//
// Authors:
// Sebastien Pouliot <sebastien@ximian.com>
// Adrian Tsai <adrian_tsai@hotmail.com>
// Daniel Abramov <ex@vingrad.ru>
// Andreas Noever <andreas.noever@gmail.com>
// Cedric Vivier <cedricv@neonux.com>
//
// Copyright (C) 2007-2008 Novell, Inc (http://www.novell.com)
// Copyright (c) 2007 Adrian Tsai
// Copyright (C) 2008 Daniel Abramov
// (C) 2008 Andreas Noever
// Copyright (C) 2008 Cedric Vivier
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using Mono.Cecil;
namespace Gendarme.Framework.Rocks {
// add Method[Reference|Definition][Collection] extensions methods here
// only if:
// * you supply minimal documentation for them (xml)
// * you supply unit tests for them
// * they are required somewhere to simplify, even indirectly, the rules
// (i.e. don't bloat the framework in case of x, y or z in the future)
/// <summary>
/// MethodRocks contains extensions methods for Method[Definition|Reference]
/// and the related collection classes.
///
/// Note: whenever possible try to use MethodReference since it's extend the
/// reach/usability of the code.
/// </summary>
public static class MethodRocks {
public static bool IsNamed (this MemberReference self, string nameSpace, string typeName, string methodName)
{
if (methodName == null)
throw new ArgumentNullException ("methodName");
if (self == null)
return false;
return ((self.Name == methodName) && self.DeclaringType.IsNamed (nameSpace, typeName));
}
/// <summary>
/// Check if the MethodReference is defined as the entry point of it's assembly.
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the method is defined as the entry point of it's assembly, False otherwise</returns>
public static bool IsEntryPoint (this MethodReference self)
{
return ((self != null) && (self == self.Module.Assembly.EntryPoint));
}
/// <summary>
/// Check if the MethodReference is a finalizer.
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the method is a finalizer, False otherwise.</returns>
public static bool IsFinalizer (this MethodReference self)
{
if (self == null)
return false;
return (self.HasThis && !self.HasParameters && (self.Name == "Finalize") &&
self.ReturnType.IsNamed ("System", "Void"));
}
/// <summary>
/// Check if the method, or it's declaring type, was generated by the compiler or a tool (i.e. not by the developer).
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the code is not generated directly by the developer,
/// False otherwise (e.g. compiler or tool generated)</returns>
public static bool IsGeneratedCode (this MethodReference self)
{
if (self == null)
return false;
MethodDefinition method = self.Resolve ();
if (method.HasAnyGeneratedCodeAttribute ())
return true;
return self.DeclaringType.IsGeneratedCode ();
}
/// <summary>
/// Check if the signature of a method is consitent for it's use as a Main method.
/// Note: it doesn't check that the method is the EntryPoint of it's assembly.
/// <code>
/// static [void|int] Main ()
/// static [void|int] Main (string[] args)
/// </code>
/// </summary>gre
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the method is a valid Main, False otherwise.</returns>
public static bool IsMain (this MethodReference self)
{
if (self == null)
return false;
MethodDefinition method = self.Resolve ();
// Main must be static
if (!method.IsStatic)
return false;
if (method.Name != "Main")
return false;
// Main must return void or int
switch (method.ReturnType.Name) {
case "Void":
case "Int32":
// ok, continue checks
break;
default:
return false;
}
// Main (void)
if (!method.HasParameters)
return true;
IList<ParameterDefinition> pdc = method.Parameters;
if (pdc.Count != 1)
return false;
// Main (string[] args)
return (pdc [0].ParameterType.Name == "String[]");
}
/// <summary>
/// Check if a method is an override to a virtual method of a base type.
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the method is an override to a virtual method, False otherwise</returns>
public static bool IsOverride (this MethodReference self)
{
if (self == null)
return false;
MethodDefinition method = self.Resolve ();
if ((method == null) || !method.IsVirtual)
return false;
TypeDefinition declaring = method.DeclaringType;
TypeDefinition parent = declaring.BaseType != null ? declaring.BaseType.Resolve () : null;
while (parent != null) {
string name = method.Name;
foreach (MethodDefinition md in parent.Methods) {
if (name != md.Name)
continue;
if (!method.CompareSignature (md))
continue;
return md.IsVirtual;
}
parent = parent.BaseType != null ? parent.BaseType.Resolve () : null;
}
return false;
}
/// <summary>
/// Check if the method corresponds to the get or set operation on a property.
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the method is a getter or a setter, False otherwise</returns>
public static bool IsProperty (this MethodReference self)
{
if (self == null)
return false;
MethodDefinition method = self.Resolve ();
if (method == null)
return false;
return ((method.SemanticsAttributes & (MethodSemanticsAttributes.Getter | MethodSemanticsAttributes.Setter)) != 0);
}
/// <summary>
/// Check if the method is visible outside of the assembly.
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the method can be used from outside of the assembly, false otherwise.</returns>
public static bool IsVisible (this MethodReference self)
{
if (self == null)
return false;
MethodDefinition method = self.Resolve ();
if ((method == null) || method.IsPrivate || method.IsAssembly)
return false;
return self.DeclaringType.Resolve ().IsVisible ();
}
/// <summary>
/// Check if the method has the signature of an Event callback.
/// They are usually of the form: void Method (object sender, EventArgs ea), where
/// the second parameters is either EventArgs or a subclass of it
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>True if the method has the signature of an event callback.</returns>
public static bool IsEventCallback (this MethodReference self)
{
if (self == null)
return false;
MethodDefinition method = self.Resolve ();
if ((method == null) || !method.HasParameters)
return false;
IList<ParameterDefinition> parameters = method.Parameters;
if (parameters.Count != 2)
return false;
TypeReference type = parameters [1].ParameterType;
GenericParameter gp = (type as GenericParameter);
if (gp == null)
return type.Inherits ("System", "EventArgs");
if (gp.HasConstraints) {
IList<TypeReference> cc = (IList < TypeReference > )gp.Constraints;
return ((cc.Count == 1) && cc [0].IsNamed ("System", "EventArgs"));
}
return false;
}
/// <summary>
/// Returns a property using supplied MethodReference of
/// a property accessor method (getter or setter).
/// </summary>
/// <param name="self">The MethodReference on which the extension method can be called.</param>
/// <returns>PropertyDefinition which corresponds to the supplied MethodReference.</returns>
public static PropertyDefinition GetPropertyByAccessor (this MethodReference self)
{
if (self == null)
return null;
MethodDefinition method = self.Resolve ();
if (method == null || !method.DeclaringType.HasProperties || !method.IsProperty ())
return null;
string mname = method.Name;
foreach (PropertyDefinition property in method.DeclaringType.Properties) {
// set_ and get_ both have a length equal to 4
string pname = property.Name;
if (String.CompareOrdinal (pname, 0, mname, 4, pname.Length) == 0)
return property;
}
return null;
}
private static bool AreSameElementTypes (TypeReference a, TypeReference b)
{
return a.IsGenericParameter || b.IsGenericParameter || b.IsNamed (a.Namespace, a.Name);
}
/// <summary>
/// Compare the IMethodSignature members with the one being specified.
/// </summary>
/// <param name="self">>The IMethodSignature on which the extension method can be called.</param>
/// <param name="signature">The IMethodSignature which is being compared.</param>
/// <returns>True if the IMethodSignature members are identical, false otherwise</returns>
public static bool CompareSignature (this IMethodSignature self, IMethodSignature signature)
{
if (self == null)
return (signature == null);
if (self.HasThis != signature.HasThis)
return false;
if (self.ExplicitThis != signature.ExplicitThis)
return false;
if (self.CallingConvention != signature.CallingConvention)
return false;
if (!AreSameElementTypes (self.ReturnType, signature.ReturnType))
return false;
bool h1 = self.HasParameters;
bool h2 = signature.HasParameters;
if (h1 != h2)
return false;
if (!h1 && !h2)
return true;
IList<ParameterDefinition> pdc1 = self.Parameters;
IList<ParameterDefinition> pdc2 = signature.Parameters;
int count = pdc1.Count;
if (count != pdc2.Count)
return false;
for (int i = 0; i < count; ++i) {
if (!AreSameElementTypes (pdc1 [i].ParameterType, pdc2 [i].ParameterType))
return false;
}
return true;
}
}
}