Browse Source

Added a support for Events. Some refactoring and fixes with declaring types.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6029 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Tomáš Linhart 16 years ago
parent
commit
4db20ab738
  1. 1
      src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj
  2. 31
      src/AddIns/Analysis/CodeQuality/Src/Event.cs
  3. 13
      src/AddIns/Analysis/CodeQuality/Src/Field.cs
  4. 12
      src/AddIns/Analysis/CodeQuality/Src/Method.cs
  5. 120
      src/AddIns/Analysis/CodeQuality/Src/MetricsReader.cs
  6. 13
      src/AddIns/Analysis/CodeQuality/Src/Type.cs

1
src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj

@ -94,6 +94,7 @@ @@ -94,6 +94,7 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Src\DependencyGraphCommand.cs" />
<Compile Include="Src\Event.cs" />
<Compile Include="Src\Field.cs" />
<Compile Include="Src\IDependency.cs" />
<Compile Include="Src\MetricsReader.cs" />

31
src/AddIns/Analysis/CodeQuality/Src/Event.cs

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuickGraph;
namespace ICSharpCode.CodeQualityAnalysis
{
public class Event : IDependency
{
/// <summary>
/// Name of event
/// </summary>
public string Name { get; set; }
/// <summary>
/// Type of event
/// </summary>
public Type EventType { get; set; }
/// <summary>
/// Type which owns this event
/// </summary>
public Type Owner { get; set; }
public BidirectionalGraph<object, IEdge<object>> BuildDependencyGraph()
{
return null;
}
}
}

13
src/AddIns/Analysis/CodeQuality/Src/Field.cs

@ -16,8 +16,18 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -16,8 +16,18 @@ namespace ICSharpCode.CodeQualityAnalysis
/// <summary>
/// Type of field
/// </summary>
public Type Type { get; set; }
public Type FieldType { get; set; }
/// <summary>
/// Type which owns this field
/// </summary>
public Type Owner { get; set; }
/// <summary>
/// Whether this field is event
/// </summary>
public bool IsEvent { get; set; }
public BidirectionalGraph<object, IEdge<object>> BuildDependencyGraph()
{
return null;
@ -27,5 +37,6 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -27,5 +37,6 @@ namespace ICSharpCode.CodeQualityAnalysis
{
return Name;
}
}
}

12
src/AddIns/Analysis/CodeQuality/Src/Method.cs

@ -31,7 +31,17 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -31,7 +31,17 @@ namespace ICSharpCode.CodeQualityAnalysis
/// <summary>
/// A return type of method
/// </summary>
public Type Type { get; set; }
public Type MethodType { get; set; }
/// <summary>
/// Type which owns this method
/// </summary>
public Type Owner { get; set; }
/// <summary>
/// Whether a method is constructor or not
/// </summary>
public bool IsConstructor { get; set; }
public Method()
{

120
src/AddIns/Analysis/CodeQuality/Src/MetricsReader.cs

@ -1,5 +1,4 @@ @@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
@ -101,6 +100,7 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -101,6 +100,7 @@ namespace ICSharpCode.CodeQualityAnalysis
{
foreach (TypeDefinition typeDefinition in types)
{
if (typeDefinition.Name != "<Module>")
{
var type =
@ -113,15 +113,12 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -113,15 +113,12 @@ namespace ICSharpCode.CodeQualityAnalysis
if (typeDefinition.HasFields)
ReadFields(type, typeDefinition.Fields);
/*if (typeDefinition.HasEvents)
ReadEvents(type, typeDefinition.Events);*/
if (typeDefinition.HasEvents)
ReadEvents(type, typeDefinition.Events);
if (typeDefinition.HasMethods)
ReadMethods(type, typeDefinition.Methods);
/*if (typeDefinition.HasConstructors)
ReadConstructors(type, typeDefinition.Constructors);*/
if (typeDefinition.HasNestedTypes)
ReadFromTypes(module, typeDefinition.NestedTypes);
}
@ -130,7 +127,37 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -130,7 +127,37 @@ namespace ICSharpCode.CodeQualityAnalysis
private void ReadEvents(Type type, Collection<EventDefinition> events)
{
throw new NotImplementedException();
foreach (var eventDefinition in events)
{
var e = new Event()
{
Name = eventDefinition.Name,
Owner = type
};
type.Events.Add(e);
var declaringType =
(from n in type.Namespace.Module.Namespaces
from t in n.Types
where t.Name == e.Name
select t).SingleOrDefault();
e.EventType = declaringType;
// Mono.Cecil threats Events as regular fields
// so I have to find a field and set IsEvent to true
var field =
(from n in type.Namespace.Module.Namespaces
from t in n.Types
from f in t.Fields
where f.Name == e.Name
select f).SingleOrDefault();
if (field != null)
field.IsEvent = true;
}
}
/// <summary>
@ -144,7 +171,9 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -144,7 +171,9 @@ namespace ICSharpCode.CodeQualityAnalysis
{
var field = new Field()
{
Name = fieldDefinition.Name
Name = fieldDefinition.Name,
IsEvent = false,
Owner = type
};
type.Fields.Add(field);
@ -152,10 +181,10 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -152,10 +181,10 @@ namespace ICSharpCode.CodeQualityAnalysis
var declaringType =
(from n in type.Namespace.Module.Namespaces
from t in n.Types
where t.Name == field.Name
where t.Name == FormatTypeName(fieldDefinition.DeclaringType)
select t).SingleOrDefault();
field.Type = declaringType;
field.FieldType = declaringType;
}
}
@ -171,9 +200,18 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -171,9 +200,18 @@ namespace ICSharpCode.CodeQualityAnalysis
var method = new Method
{
Name = FormatMethodName(methodDefinition),
Type = type
Owner = type,
IsConstructor = methodDefinition.IsConstructor
};
var declaringType =
(from n in type.Namespace.Module.Namespaces
from t in n.Types
where t.Name == FormatTypeName(methodDefinition.DeclaringType)
select t).SingleOrDefault();
method.MethodType = declaringType;
type.Methods.Add(method);
}
@ -190,37 +228,6 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -190,37 +228,6 @@ namespace ICSharpCode.CodeQualityAnalysis
}
}
/// <summary>
/// Reads constructors and add them to method list for type
/// </summary>
/// <param name="type"></param>
/// <param name="constructors"></param>
private void ReadConstructors(Type type, Collection<MethodDefinition> constructors)
{
foreach (MethodDefinition constructor in constructors)
{
var method = new Method
{
Name = FormatMethodName(constructor),
Type = type
};
type.Methods.Add(method);
}
foreach (MethodDefinition constructor in constructors)
{
var method = (from m in type.Methods
where m.Name == FormatMethodName(constructor)
select m).SingleOrDefault();
if (constructor.Body != null)
{
ReadInstructions(method, constructor, constructor.Body.Instructions);
}
}
}
/// <summary>
/// Reads method calls by extracting instrunctions
/// </summary>
@ -237,7 +244,7 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -237,7 +244,7 @@ namespace ICSharpCode.CodeQualityAnalysis
if (instr is MethodDefinition)
{
var md = instr as MethodDefinition;
var type = (from n in method.Type.Namespace.Module.Namespaces
var type = (from n in method.MethodType.Namespace.Module.Namespaces
from t in n.Types
where t.Name == FormatTypeName(md.DeclaringType) &&
n.Name == t.Namespace.Name
@ -249,20 +256,23 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -249,20 +256,23 @@ namespace ICSharpCode.CodeQualityAnalysis
where m.Name == FormatMethodName(md)
select m).SingleOrDefault();
if (findTargetMethod != null && type == method.Type)
if (findTargetMethod != null && type == method.MethodType)
method.MethodUses.Add(findTargetMethod);
}
if (instr is FieldDefinition)
{
var fd = instr as FieldDefinition;
var field = (from f in method.Type.Fields
var field = (from f in method.MethodType.Fields
where f.Name == fd.Name
select f).SingleOrDefault();
if (field != null)
method.FieldUses.Add(field);
}
if (instr != null)
Console.WriteLine(instr.GetType().ToString());
}
}
@ -300,13 +310,13 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -300,13 +310,13 @@ namespace ICSharpCode.CodeQualityAnalysis
bool hasNext = enumerator.MoveNext();
while (hasNext)
{
builder.Append(((ParameterDefinition) enumerator.Current).ParameterType.FullName);
builder.Append((enumerator.Current).ParameterType.FullName);
hasNext = enumerator.MoveNext();
if (hasNext)
builder.Append(", ");
}
return methodDefinition.Name + "(" + builder.ToString() + ")";
return methodDefinition.Name + "(" + builder + ")";
}
else
{
@ -333,13 +343,13 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -333,13 +343,13 @@ namespace ICSharpCode.CodeQualityAnalysis
bool hasNext = enumerator.MoveNext();
while (hasNext)
{
builder.Append(((GenericParameter)enumerator.Current).Name);
builder.Append((enumerator.Current).Name);
hasNext = enumerator.MoveNext();
if (hasNext)
builder.Append(",");
}
return StripGenericName(typeDefinition.Name) + "<" + builder.ToString() + ">";
return StripGenericName(typeDefinition.Name) + "<" + builder + ">";
}
return typeDefinition.Name;
@ -364,13 +374,11 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -364,13 +374,11 @@ namespace ICSharpCode.CodeQualityAnalysis
{
if (type.IsNested && type.DeclaringType != null)
return GetNamespaceName(type.DeclaringType);
else
{
if (!String.IsNullOrEmpty(type.Namespace))
return type.Namespace;
else
return "-";
}
if (!String.IsNullOrEmpty(type.Namespace))
return type.Namespace;
return "-";
}
}
}

13
src/AddIns/Analysis/CodeQuality/Src/Type.cs

@ -11,7 +11,7 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -11,7 +11,7 @@ namespace ICSharpCode.CodeQualityAnalysis
/// <summary>
/// Nested types like inner classes, interfaces and so on.
/// </summary>
public ISet<Type> InnerTypes { get; set; }
public ISet<Type> NestedTypes { get; set; }
/// <summary>
/// Methods within type
@ -23,6 +23,11 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -23,6 +23,11 @@ namespace ICSharpCode.CodeQualityAnalysis
/// </summary>
public ISet<Field> Fields { get; set; }
/// <summary>
/// Events within type
/// </summary>
public ISet<Event> Events { get; set; }
/// <summary>
/// Name of type with a name of namespace.
/// </summary>
@ -48,6 +53,8 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -48,6 +53,8 @@ namespace ICSharpCode.CodeQualityAnalysis
{
Methods = new HashSet<Method>();
Fields = new HashSet<Field>();
Events = new HashSet<Event>();
NestedTypes = new HashSet<Type>();
}
/// <summary>
@ -65,8 +72,8 @@ namespace ICSharpCode.CodeQualityAnalysis @@ -65,8 +72,8 @@ namespace ICSharpCode.CodeQualityAnalysis
foreach (var field in Fields)
{
if (field.Type != null) // if it is null so it is type from outside of this assembly
set.Add(field.Type); // TODO: find solution to handle them
if (field.FieldType != null) // if it is null so it is type from outside of this assembly
set.Add(field.FieldType); // TODO: find solution to handle them
}
return set;

Loading…
Cancel
Save