Author: spouliot
Date: 2008-02-14 15:26:33 -0500 (Thu, 14 Feb 2008)
New Revision: 95691
Modified:
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/FieldRocks.cs
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/MethodRocks.cs
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs
Log:
2008-02-14 Sebastien Pouliot <[EMAIL PROTECTED]>
* FieldRocks.cs: Add Resolve (FieldReference to FieldDefinition) and
change other rocks to accept FieldReference (and Resolve themselves).
* MethodRocks.cs: Add Resolve (MethodReference to MethodDefinition)
and change other rocks to accept MethodReference (and Resolve
themselves).
* TypeRocks.cs: Add Resolve (TypeReference to TypeDefinition) and
change other rocks to accept TypeReference (and Resolve themselves).
Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog
2008-02-14 20:24:12 UTC (rev 95690)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/ChangeLog
2008-02-14 20:26:33 UTC (rev 95691)
@@ -1,3 +1,13 @@
+2008-02-14 Sebastien Pouliot <[EMAIL PROTECTED]>
+
+ * FieldRocks.cs: Add Resolve (FieldReference to FieldDefinition) and
+ change other rocks to accept FieldReference (and Resolve themselves).
+ * MethodRocks.cs: Add Resolve (MethodReference to MethodDefinition)
+ and change other rocks to accept MethodReference (and Resolve
+ themselves).
+ * TypeRocks.cs: Add Resolve (TypeReference to TypeDefinition) and
+ change other rocks to accept TypeReference (and Resolve themselves).
+
2008-02-14 Sebastien Pouliot <[EMAIL PROTECTED]>
* CecilRocks.cs: New. Shared rocks on other Cecil types
Modified:
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/FieldRocks.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/FieldRocks.cs
2008-02-14 20:24:12 UTC (rev 95690)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/FieldRocks.cs
2008-02-14 20:26:33 UTC (rev 95691)
@@ -37,36 +37,50 @@
/// <summary>
/// Check if the field contains an attribute of a specified
type.
/// </summary>
- /// <param name="self">The FieldDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The FieldReference on which the
extension method can be called.</param>
/// <param name="attributeName">Full name of the attribute
class</param>
/// <returns>True if the field contains an attribute of the
same name,
/// False otherwise.</returns>
- public static bool HasAttribute (this FieldDefinition self,
string attributeName)
+ public static bool HasAttribute (this FieldReference self,
string attributeName)
{
- return self.CustomAttributes.ContainsType
(attributeName);
+ return self.Resolve ().CustomAttributes.ContainsType
(attributeName);
}
/// <summary>
/// Check if the field was generated by the compiler or a tool
(i.e. not by the developer).
/// This can occurs (compiler) with auto-implemented properties
(C# 3)
/// </summary>
- /// <param name="self">The FieldDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The FieldReference on which the
extension method can be called.</param>
/// <returns>True if the field was not added directly by the
developer, False otherwise</returns>
- public static bool IsGeneratedCode (this FieldDefinition self)
+ public static bool IsGeneratedCode (this FieldReference self)
{
- return self.CustomAttributes.ContainsAnyType
(CustomAttributeRocks.GeneratedCodeAttributes);
+ return self.Resolve ().CustomAttributes.ContainsAnyType
(CustomAttributeRocks.GeneratedCodeAttributes);
}
/// <summary>
/// Check if the field is visible outside of the assembly.
/// </summary>
- /// <param name="self">The FieldDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The FieldReference on which the
extension method can be called.</param>
/// <returns>True if the field can be used from outside of the
assembly, false otherwise.</returns>
- public static bool IsVisible (this FieldDefinition self)
+ public static bool IsVisible (this FieldReference self)
{
- if (self.IsPrivate || self.IsAssembly)
+ FieldDefinition field = self.Resolve ();
+ if (field.IsPrivate || field.IsAssembly)
return false;
- return ((TypeDefinition) self.DeclaringType).IsVisible
();
+ return field.DeclaringType.Resolve ().IsVisible ();
}
+
+ /// <summary>
+ /// Resolve a FieldReference into a FieldDefinition.
+ /// </summary>
+ /// <param name="self">The FieldReference on which the
extension method can be called.</param>
+ /// <returns>A FieldDefinition if resolved, null
otherwise.</returns>
+ public static FieldDefinition Resolve (this FieldReference self)
+ {
+ FieldDefinition field = (self as FieldDefinition);
+ if (field == null)
+ field = AssemblyResolver.Resolver.Resolve
(self);
+ return field;
+ }
}
}
Modified:
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/MethodRocks.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/MethodRocks.cs
2008-02-14 20:24:12 UTC (rev 95690)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/MethodRocks.cs
2008-02-14 20:26:33 UTC (rev 95691)
@@ -55,13 +55,13 @@
/// <summary>
/// Check if the method contains an attribute of a specified
type.
/// </summary>
- /// <param name="self">The MethodDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The MethodReference on which the
extension method can be called.</param>
/// <param name="attributeName">Full name of the attribute
class</param>
/// <returns>True if the method contains an attribute of the
same name,
/// False otherwise.</returns>
- public static bool HasAttribute (this MethodDefinition self,
string attributeName)
+ public static bool HasAttribute (this MethodReference self,
string attributeName)
{
- return self.CustomAttributes.ContainsType
(attributeName);
+ return self.Resolve ().CustomAttributes.ContainsType
(attributeName);
}
/// <summary>
@@ -88,15 +88,16 @@
/// <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 MethodDefinition on which the
extension method can be called.</param>
+ /// <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 MethodDefinition self)
+ public static bool IsGeneratedCode (this MethodReference self)
{
- if (self.CustomAttributes.ContainsAnyType
(CustomAttributeRocks.GeneratedCodeAttributes))
+ MethodDefinition method = self.Resolve ();
+ if (method.CustomAttributes.ContainsAnyType
(CustomAttributeRocks.GeneratedCodeAttributes))
return true;
- return self.DeclaringType.IsGeneratedCode ();
+ return method.DeclaringType.IsGeneratedCode ();
}
/// <summary>
@@ -107,19 +108,20 @@
/// static [void|int] Main (string[] args)
/// </code>
/// </summary>gre
- /// <param name="self">The MethodDefinition on which the
extension method can be called.</param>
+ /// <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 MethodDefinition self)
+ public static bool IsMain (this MethodReference self)
{
+ MethodDefinition method = self.Resolve ();
// Main must be static
- if (!self.IsStatic)
+ if (!method.IsStatic)
return false;
- if (self.Name != "Main")
+ if (method.Name != "Main")
return false;
// Main must return void or int
- switch (self.ReturnType.ReturnType.Name) {
+ switch (method.ReturnType.ReturnType.Name) {
case "Void":
case "Int32":
// ok, continue checks
@@ -128,13 +130,13 @@
return false;
}
- switch (self.Parameters.Count) {
+ switch (method.Parameters.Count) {
case 0:
// Main (void)
return true;
case 1:
// Main (string[] args)
- return (self.Parameters [0].ParameterType.Name
== "String[]");
+ return (method.Parameters
[0].ParameterType.Name == "String[]");
default:
return false;
}
@@ -143,23 +145,37 @@
/// <summary>
/// Check if the method corresponds to the get or set operation
on a property.
/// </summary>
- /// <param name="self">The MethodDefinition on which the
extension method can be called.</param>
+ /// <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 MethodDefinition self)
+ public static bool IsProperty (this MethodReference self)
{
- return ((self.SemanticsAttributes &
(MethodSemanticsAttributes.Getter | MethodSemanticsAttributes.Setter)) != 0);
+ return ((self.Resolve ().SemanticsAttributes &
(MethodSemanticsAttributes.Getter | MethodSemanticsAttributes.Setter)) != 0);
}
/// <summary>
/// Check if the method is visible outside of the assembly.
/// </summary>
- /// <param name="self">The MethodDefinition on which the
extension method can be called.</param>
+ /// <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 MethodDefinition self)
+ public static bool IsVisible (this MethodReference self)
{
- if (self.IsPrivate || self.IsAssembly)
+ MethodDefinition method = self.Resolve ();
+ if (method.IsPrivate || method.IsAssembly)
return false;
- return ((TypeDefinition) self.DeclaringType).IsVisible
();
+ return self.DeclaringType.Resolve ().IsVisible ();
}
+
+ /// <summary>
+ /// Resolve a MethodReference into a MethodDefinition.
+ /// </summary>
+ /// <param name="self">The MethodReference on which the
extension method can be called.</param>
+ /// <returns>A MethodDefinition if resolved, null
otherwise.</returns>
+ public static MethodDefinition Resolve (this MethodReference
self)
+ {
+ MethodDefinition method = (self as MethodDefinition);
+ if (method == null)
+ method = AssemblyResolver.Resolver.Resolve
(self);
+ return method;
+ }
}
}
Modified:
trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs
2008-02-14 20:24:12 UTC (rev 95690)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework.Rocks/TypeRocks.cs
2008-02-14 20:26:33 UTC (rev 95691)
@@ -60,26 +60,27 @@
/// traverse all MethodDefinition in the type. That includes
the Constructors
/// and Methods collections.
/// </summary>
- /// <param name="self"></param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <returns>An IEnumerable to traverse all constructors and
methods</returns>
- public static IEnumerable<MethodDefinition> AllMethods (this
TypeDefinition self)
+ public static IEnumerable<MethodDefinition> AllMethods (this
TypeReference self)
{
- foreach (MethodDefinition ctor in self.Constructors)
+ TypeDefinition type = self.Resolve ();
+ foreach (MethodDefinition ctor in type.Constructors)
yield return ctor;
- foreach (MethodDefinition method in self.Methods)
+ foreach (MethodDefinition method in type.Methods)
yield return method;
}
/// <summary>
/// Returns the first MethodDefinition that satisfies a given
MethodSignature.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="signature">The MethodSignature to
match.</param>
/// <returns>The first MethodDefinition for wich
signature.Matches returns true.</returns>
/// <remarks>
/// Do not allocate a MethodSignature for only one call. Use
one of the other GetMethod overloads instead.
/// </remarks>
- public static MethodDefinition GetMethod (this TypeDefinition
self, MethodSignature signature)
+ public static MethodDefinition GetMethod (this TypeReference
self, MethodSignature signature)
{
foreach (MethodDefinition method in self.AllMethods ())
{
if (signature.Matches (method))
@@ -91,14 +92,14 @@
/// <summary>
/// Searches for a method.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="attributes">An attribute mask matched against
the attributes of the method.</param>
/// <param name="name">The name of the method to match. Ignored
if null.</param>
/// <param name="returnType">The full name (Namespace.Type) of
the return type. Ignored if null.</param>
/// <param name="parameters">An array of full names
(Namespace.Type) of parameter types. Ignored if null. Null entries act as
wildcards.</param>
/// <param name="customCondition">A custom condition that is
called for each MethodDefinition that satisfies all other conditions. Ignored
if null.</param>
/// <returns>The first MethodDefinition that satisfies all
conditions.</returns>
- public static MethodDefinition GetMethod (this TypeDefinition
self, MethodAttributes attributes, string name, string returnType, string []
parameters, Func<MethodDefinition, bool> customCondition)
+ public static MethodDefinition GetMethod (this TypeReference
self, MethodAttributes attributes, string name, string returnType, string []
parameters, Func<MethodDefinition, bool> customCondition)
{
foreach (MethodDefinition method in self.AllMethods ())
{
if (name != null && method.Name != name)
@@ -132,13 +133,13 @@
/// <summary>
/// Searches for a method by name, returnType, parameters and
attributes.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="attributes">An attribute mask matched against
the attributes of the method.</param>
/// <param name="name">The name of the method to match. Ignored
if null.</param>
/// <param name="returnType">The full name (Namespace.Type) of
the return type. Ignored if null.</param>
/// <param name="parameters">An array of full names
(Namespace.Type) of parameter types. Ignored if null. Null entries act as
wildcard.</param>
/// <returns>The first MethodDefinition that satisfies all
conditions.</returns>
- public static MethodDefinition GetMethod (this TypeDefinition
self, MethodAttributes attributes, string name, string returnType, string []
parameters)
+ public static MethodDefinition GetMethod (this TypeReference
self, MethodAttributes attributes, string name, string returnType, string []
parameters)
{
return self.GetMethod (attributes, name, returnType,
parameters, null);
}
@@ -146,11 +147,11 @@
/// <summary>
/// Searches for a method by attributes and by name.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="attributes">An attribute mask matched against
the attributes of the method.</param>
/// <param name="name">The name of the method to match. Ignored
if null.</param>
/// <returns>The first MethodDefinition that satisfies all
conditions.</returns>
- public static MethodDefinition GetMethod (this TypeDefinition
self, MethodAttributes attributes, string name)
+ public static MethodDefinition GetMethod (this TypeReference
self, MethodAttributes attributes, string name)
{
return self.GetMethod (attributes, name, null, null,
null);
}
@@ -158,12 +159,12 @@
/// <summary>
/// Searches for a method by name, returnType and parameters.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="name">The name of the method to match. Ignored
if null.</param>
/// <param name="returnType">The full name (Namespace.Type) of
the return type. Ignored if null.</param>
/// <param name="parameters">An array of full names
(Namespace.Type) of parameter types. Ignored if null. Null entries act as
wildcards.</param>
/// <returns>The first MethodDefinition that satisfies all
conditions.</returns>
- public static MethodDefinition GetMethod (this TypeDefinition
self, string name, string returnType, string [] parameters)
+ public static MethodDefinition GetMethod (this TypeReference
self, string name, string returnType, string [] parameters)
{
return self.GetMethod (0, name, returnType, parameters,
null);
}
@@ -171,10 +172,10 @@
/// <summary>
/// Searches for a method with a specific name.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="name">The name of the method to match.</param>
/// <returns>The first MethodDefinition with a specifiy
name.</returns>
- public static MethodDefinition GetMethod (this TypeDefinition
self, string name)
+ public static MethodDefinition GetMethod (this TypeReference
self, string name)
{
return self.GetMethod (0, name, null, null, null);
}
@@ -182,10 +183,10 @@
/// <summary>
/// Searches for a method using a custom condition.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="customCondition">A custom condition that is
called for each MethodDefinition.</param>
/// <returns>The first MethodDefinition that satisfies the
customCondition.</returns>
- public static MethodDefinition GetMethod (this TypeDefinition
self, Func<MethodDefinition, bool> customCondition)
+ public static MethodDefinition GetMethod (this TypeReference
self, Func<MethodDefinition, bool> customCondition)
{
return self.GetMethod (0, null, null, null,
customCondition);
}
@@ -205,10 +206,10 @@
/// <summary>
/// Checks if at least one Method satisfies a given
MethodSignature.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <param name="signature">The MethodSignature to
match.</param>
/// <returns>True if at least one method matches the signature.
Otherwise false.</returns>
- public static bool HasMethod (this TypeDefinition self,
MethodSignature signature)
+ public static bool HasMethod (this TypeReference self,
MethodSignature signature)
{
return (self.GetMethod (signature) != null);
}
@@ -221,24 +222,24 @@
/// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
/// <param name="interfaceName">Full name of the
interface</param>
/// <returns>True if the type implements the interface, False
otherwise.</returns>
- public static bool Implements (this TypeDefinition self, string
interfaceName)
+ public static bool Implements (this TypeReference self, string
interfaceName)
{
if (interfaceName == null)
throw new ArgumentNullException
("interfaceName");
+ TypeDefinition type = self.Resolve ();
// special case, check if we implement ourselves
- if (self.IsInterface && (self.FullName ==
interfaceName))
+ if (type.IsInterface && (type.FullName ==
interfaceName))
return true;
// does the type implements it itself
- foreach (TypeReference iface in self.Interfaces) {
+ foreach (TypeReference iface in type.Interfaces) {
if (iface.FullName == interfaceName)
return true;
}
// if not, then maybe it's parent does
- // FIXME: right now we "ignore" case were a
TypeReference is given
- TypeDefinition parent = (self.BaseType as
TypeDefinition);
+ TypeReference parent = type.BaseType;
if (parent != null)
return parent.Implements (interfaceName);
@@ -258,14 +259,12 @@
if (className == null)
throw new ArgumentNullException ("className");
- TypeReference current = self;
+ TypeReference current = self.Resolve ();
while ((current != null) && (current.FullName !=
"System.Object")) {
if (current.FullName == className)
return true;
- // FIXME: plugin AssemblyResolver when ready
- TypeDefinition type = (current as
TypeDefinition);
- current = (type == null) ? null : type.BaseType;
+ current = current.Resolve ().BaseType;
}
return false;
}
@@ -296,15 +295,16 @@
/// <summary>
/// Check if the type is a delegate.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <returns>True if the type is a delegate, False
otherwise.</returns>
- public static bool IsDelegate (this TypeDefinition self)
+ public static bool IsDelegate (this TypeReference self)
{
+ TypeDefinition type = self.Resolve ();
// e.g. this occurs for <Module>
- if (self.BaseType == null)
+ if (type.BaseType == null)
return false;
- switch (self.BaseType.FullName) {
+ switch (type.BaseType.FullName) {
case "System.Delegate":
case "System.MulticastDelegate":
return true;
@@ -316,14 +316,15 @@
/// <summary>
/// Check if the type is a enumeration flags.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <returns>True if the type as the [Flags] attribute, false
otherwise.</returns>
- public static bool IsFlags (this TypeDefinition self)
+ public static bool IsFlags (this TypeReference self)
{
- if (!self.IsEnum)
+ TypeDefinition type = self.Resolve ();
+ if (!type.IsEnum)
return false;
- return self.HasAttribute ("System.FlagsAttribute");
+ return type.HasAttribute ("System.FlagsAttribute");
}
/// <summary>
@@ -384,17 +385,31 @@
/// <summary>
/// Check if the type is visible outside of the assembly.
/// </summary>
- /// <param name="self">The TypeDefinition on which the
extension method can be called.</param>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
/// <returns>True if the type can be used from outside of the
assembly, false otherwise.</returns>
- public static bool IsVisible (this TypeDefinition self)
+ public static bool IsVisible (this TypeReference self)
{
- while (self.IsNested) {
- if (self.IsNestedPrivate ||
self.IsNestedAssembly)
+ TypeDefinition type = (self as TypeDefinition);
+ while (type.IsNested) {
+ if (type.IsNestedPrivate ||
type.IsNestedAssembly)
return false;
// Nested classes are always inside the same
assembly, so the cast is ok
- self = (TypeDefinition) self.DeclaringType;
+ type = type.DeclaringType.Resolve ();
}
- return self.IsPublic;
+ return type.IsPublic;
}
+
+ /// <summary>
+ /// Resolve a TypeReference into a TypeDefinition.
+ /// </summary>
+ /// <param name="self">The TypeReference on which the extension
method can be called.</param>
+ /// <returns>A TypeDefinition if resolved, null
otherwise.</returns>
+ public static TypeDefinition Resolve (this TypeReference self)
+ {
+ TypeDefinition type = (self as TypeDefinition);
+ if (type == null)
+ type = AssemblyResolver.Resolver.Resolve (self);
+ return type;
+ }
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches