Browse Source

Ignore duplicate body method issue.

I take that one out for now - tests are failing and that's a very
unlikely issue to happen.
pull/32/merge
Mike Krüger 12 years ago
parent
commit
5f867c5321
  1. 288
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateBodyMethodIssue.cs
  2. 49
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DuplicateBodyMethodIssueTests.cs

288
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/DuplicateBodyMethodIssue.cs

@ -23,7 +23,6 @@ @@ -23,7 +23,6 @@
// 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.Collections.Generic;
using System.Text;
using ICSharpCode.NRefactory.PatternMatching;
@ -32,157 +31,146 @@ using System.Linq; @@ -32,157 +31,146 @@ using System.Linq;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[IssueDescription("Methods have duplicate body",
Description = "One method has the same body as other method",
Severity = Severity.Hint,
IssueMarker = IssueMarker.Underline)]
public class DuplicateBodyMethodIssue : ICodeIssueProvider
{
#region ICodeIssueProvider implementation
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
{
var visitor = new GatherVisitor(context);
visitor.GetMethods();
visitor.ComputeConflicts();
return visitor.GetIssues();
}
#endregion
private class GatherVisitor : GatherVisitorBase<DuplicateBodyMethodIssue>
{
public List<MethodDeclaration> DeclaredMethods;
public GatherVisitor(BaseRefactoringContext context)
: base(context)
{
DeclaredMethods = new List<MethodDeclaration>();
}
static string GetMethodDescriptor(MethodDeclaration methodDeclaration) {
var sb = new StringBuilder();
sb.Append(methodDeclaration.ReturnType);
sb.Append(";");
foreach (var parameter in methodDeclaration.Parameters) {
sb.AppendFormat("{0}:{1};", parameter.Name, parameter.Type);
}
sb.Append(methodDeclaration.Modifiers);
return sb.ToString();
}
public void GetMethods()
{
ctx.RootNode.AcceptVisitor(this);
}
internal void ComputeConflicts()
{
var dict = new Dictionary<string, List<MethodDeclaration>>();
foreach (var declaredMethod in DeclaredMethods)
{
var methodDescriptor = GetMethodDescriptor(declaredMethod);
List<MethodDeclaration> listMethods;
if (!dict.TryGetValue(methodDescriptor, out listMethods))
{
listMethods = new List<MethodDeclaration>();
dict[methodDescriptor] = listMethods;
}
listMethods.Add(declaredMethod);
}
DeclaredMethods.Clear();
foreach (var list in dict.Values.Where(list => list.Count >= 2))
{
for (var i = 0; i < list.Count - 1; i++)
{
var firstMethod = list[i];
for (var j = i + 1; j < list.Count; j++)
{
var secondMethod = list[j];
if (firstMethod.Body.IsMatch(secondMethod.Body))
{
AddIssue(secondMethod.NameToken,
string.Format("Method '{0}' has the same with '{1}' ", secondMethod.Name,
firstMethod.Name),
script => { InvokeMethod(script, firstMethod, secondMethod); }
);
}
}
}
}
}
readonly InsertParenthesesVisitor _insertParenthesesVisitor = new InsertParenthesesVisitor();
private TypeDeclaration _parentType;
private void InvokeMethod(Script script, MethodDeclaration firstMethod, MethodDeclaration secondMethod)
{
var statement =
// [IssueDescription("Methods have duplicate body",
// Description = "One method has the same body as other method",
// Severity = Severity.Hint,
// IssueMarker = IssueMarker.Underline)]
public class DuplicateBodyMethodIssue : ICodeIssueProvider
{
#region ICodeIssueProvider implementation
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
{
var visitor = new GatherVisitor(context);
visitor.GetMethods();
visitor.ComputeConflicts();
return visitor.GetIssues();
}
#endregion
private class GatherVisitor : GatherVisitorBase<DuplicateBodyMethodIssue>
{
public List<MethodDeclaration> DeclaredMethods;
public GatherVisitor(BaseRefactoringContext context)
: base(context)
{
DeclaredMethods = new List<MethodDeclaration>();
}
static string GetMethodDescriptor(MethodDeclaration methodDeclaration)
{
var sb = new StringBuilder();
sb.Append(methodDeclaration.ReturnType);
sb.Append(";");
foreach (var parameter in methodDeclaration.Parameters) {
sb.AppendFormat("{0}:{1};", parameter.Name, parameter.Type);
}
sb.Append(methodDeclaration.Modifiers);
return sb.ToString();
}
public void GetMethods()
{
ctx.RootNode.AcceptVisitor(this);
}
internal void ComputeConflicts()
{
var dict = new Dictionary<string, List<MethodDeclaration>>();
foreach (var declaredMethod in DeclaredMethods) {
var methodDescriptor = GetMethodDescriptor(declaredMethod);
List<MethodDeclaration> listMethods;
if (!dict.TryGetValue(methodDescriptor, out listMethods)) {
listMethods = new List<MethodDeclaration>();
dict [methodDescriptor] = listMethods;
}
listMethods.Add(declaredMethod);
}
DeclaredMethods.Clear();
foreach (var list in dict.Values.Where(list => list.Count >= 2)) {
for (var i = 0; i < list.Count - 1; i++) {
var firstMethod = list [i];
for (var j = i + 1; j < list.Count; j++) {
var secondMethod = list [j];
if (firstMethod.Body.IsMatch(secondMethod.Body)) {
AddIssue(secondMethod.NameToken,
string.Format("Method '{0}' has the same with '{1}' ", secondMethod.Name,
firstMethod.Name),
script => {
InvokeMethod(script, firstMethod, secondMethod); }
);
}
}
}
}
}
readonly InsertParenthesesVisitor _insertParenthesesVisitor = new InsertParenthesesVisitor();
private TypeDeclaration _parentType;
private void InvokeMethod(Script script, MethodDeclaration firstMethod, MethodDeclaration secondMethod)
{
var statement =
new ExpressionStatement(new InvocationExpression(new IdentifierExpression(firstMethod.Name),
firstMethod.Parameters.Select(
declaration =>
GetArgumentExpression(declaration).Clone())));
statement.AcceptVisitor(_insertParenthesesVisitor);
if(firstMethod.ReturnType.ToString()!="System.Void"){
var returnStatement = new ReturnStatement(statement.Expression.Clone());
script.Replace(secondMethod.Body, new BlockStatement { returnStatement });
}
else {
script.Replace(secondMethod.Body, new BlockStatement { statement });
}
}
static Expression GetArgumentExpression(ParameterDeclaration parameter)
{
var identifierExpr = new IdentifierExpression(parameter.Name);
switch (parameter.ParameterModifier)
{
case ParameterModifier.Out:
return new DirectionExpression(FieldDirection.Out, identifierExpr);
case ParameterModifier.Ref:
return new DirectionExpression(FieldDirection.Ref, identifierExpr);
}
return identifierExpr;
}
public override void VisitMethodDeclaration(MethodDeclaration declaration)
{
var context = ctx;
var methodDeclaration = declaration;
var resolved = context.Resolve(methodDeclaration) as MemberResolveResult;
if (resolved == null)
return;
var isImplementingInterface = resolved.Member.ImplementedInterfaceMembers.Any();
if (isImplementingInterface)
return;
if (declaration.Body.IsNull)
return;
var parentType = declaration.Parent as TypeDeclaration;
if (parentType == null)
return;
if (_parentType == null)
_parentType = parentType;
else
{
//if we are here, it means that we switched from one class to another, so it means that we should compute
//the duplicates up-to now, then reset the list of methods
if (parentType != _parentType)
{
ComputeConflicts();
DeclaredMethods.Add(declaration);
_parentType = parentType;
return;
}
}
DeclaredMethods.Add(declaration);
}
}
}
statement.AcceptVisitor(_insertParenthesesVisitor);
if (firstMethod.ReturnType.ToString() != "System.Void") {
var returnStatement = new ReturnStatement(statement.Expression.Clone());
script.Replace(secondMethod.Body, new BlockStatement { returnStatement });
} else {
script.Replace(secondMethod.Body, new BlockStatement { statement });
}
}
static Expression GetArgumentExpression(ParameterDeclaration parameter)
{
var identifierExpr = new IdentifierExpression(parameter.Name);
switch (parameter.ParameterModifier) {
case ParameterModifier.Out:
return new DirectionExpression(FieldDirection.Out, identifierExpr);
case ParameterModifier.Ref:
return new DirectionExpression(FieldDirection.Ref, identifierExpr);
}
return identifierExpr;
}
public override void VisitMethodDeclaration(MethodDeclaration declaration)
{
var context = ctx;
var methodDeclaration = declaration;
var resolved = context.Resolve(methodDeclaration) as MemberResolveResult;
if (resolved == null)
return;
var isImplementingInterface = resolved.Member.ImplementedInterfaceMembers.Any();
if (isImplementingInterface)
return;
if (declaration.Body.IsNull)
return;
var parentType = declaration.Parent as TypeDeclaration;
if (parentType == null)
return;
if (_parentType == null)
_parentType = parentType;
else {
//if we are here, it means that we switched from one class to another, so it means that we should compute
//the duplicates up-to now, then reset the list of methods
if (parentType != _parentType) {
ComputeConflicts();
DeclaredMethods.Add(declaration);
_parentType = parentType;
return;
}
}
DeclaredMethods.Add(declaration);
}
}
}
}

49
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/DuplicateBodyMethodIssueTests.cs

@ -23,13 +23,12 @@ @@ -23,13 +23,12 @@
// 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 NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
{
[Ignore]
[TestFixture]
public class DuplicateBodyMethodIssueTests : InspectionActionTestBase
{
@ -43,50 +42,54 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues @@ -43,50 +42,54 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
{
int a = 2;
}
void $Test2 () {
int a = 2;
void $Test2 ()
{
int a = 2;
}
}",
@"class TestClass {
@"class TestClass {
void Test ()
{
int a = 2;
}
void Test2 () {
Test ();
void Test2 ()
{
Test ();
}
}
"
);
);
}
[Test]
[Test]
public void TestNonVoid()
{
Test<DuplicateBodyMethodIssue>(
@"class TestClass {
{
Test<DuplicateBodyMethodIssue>(
@"class TestClass {
int Test ()
{
int a = 2;
return a;
return a;
}
int $Test2 () {
int a = 2;
return a;
int $Test2 ()
{
int a = 2;
return a;
}
}",
@"class TestClass {
@"class TestClass {
int Test ()
{
int a = 2;
return a;
return a;
}
int Test2 () {
return Test ();
int Test2 ()
{
return Test ();
}
}
"
);
}
);
}
}
}

Loading…
Cancel
Save