Browse Source
https://bugzilla.xamarin.com/show_bug.cgi?id=11866 The test fails because EOLN issues for now, and it works just for methods that return void for now. But it does look for methods inside the classes that have same: arguments, return type and modifiers (so are duplicates)pull/32/merge
4 changed files with 252 additions and 0 deletions
@ -0,0 +1,186 @@
@@ -0,0 +1,186 @@
|
||||
//
|
||||
// DuplicateBodyMethodIssue.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// 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.Collections.Generic; |
||||
using System.Text; |
||||
using ICSharpCode.NRefactory.PatternMatching; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
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 |
||||
{ |
||||
private Dictionary<string, List<MethodDeclaration>> _dict; |
||||
|
||||
#region ICodeIssueProvider implementation
|
||||
|
||||
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context) |
||||
{ |
||||
var visitor = new GatherVisitor(context); |
||||
visitor.GetMethods(); |
||||
return visitor.GetIssues(); |
||||
} |
||||
|
||||
#endregion
|
||||
|
||||
private class GatherVisitor : GatherVisitorBase<DuplicateBodyMethodIssue> |
||||
{ |
||||
public List<MethodDeclaration> DeclaredMethods; |
||||
|
||||
public GatherVisitor(BaseRefactoringContext context) |
||||
: base(context) |
||||
{ |
||||
DeclaredMethods = new List<MethodDeclaration>(); |
||||
} |
||||
|
||||
|
||||
string GetMethodDescriptor(MethodDeclaration methodDeclaration) |
||||
{ |
||||
var sb = new StringBuilder(); |
||||
sb.Append(methodDeclaration.ReturnType); |
||||
sb.Append(";"); |
||||
sb.Append(methodDeclaration.ReturnType.ToString()); |
||||
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); |
||||
} |
||||
|
||||
private 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); |
||||
} |
||||
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); |
||||
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.Clear(); |
||||
|
||||
DeclaredMethods.Add(declaration); |
||||
_parentType = parentType; |
||||
return; |
||||
} |
||||
} |
||||
|
||||
DeclaredMethods.Add(declaration); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// DuplicateBodyMethodIssueTests.cs
|
||||
//
|
||||
// Author:
|
||||
// Ciprian Khlud <ciprian.mustiata@yahoo.com>
|
||||
//
|
||||
// Copyright (c) 2013 Ciprian Khlud
|
||||
//
|
||||
// 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 NUnit.Framework; |
||||
using ICSharpCode.NRefactory.CSharp.Refactoring; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.CodeIssues |
||||
{ |
||||
|
||||
[TestFixture] |
||||
public class DuplicateBodyMethodIssueTests : InspectionActionTestBase |
||||
{ |
||||
|
||||
[Test] |
||||
public void Test() |
||||
{ |
||||
Test<DuplicateBodyMethodIssue>( |
||||
@"class TestClass {
|
||||
void Test () |
||||
{ |
||||
int a = 2; |
||||
} |
||||
void $Test2 () { |
||||
int a = 2; |
||||
} |
||||
}",
|
||||
@"class TestClass {
|
||||
void Test () |
||||
{ |
||||
int a = 2; |
||||
} |
||||
void Test2 () { |
||||
Test (); |
||||
} |
||||
} |
||||
"
|
||||
); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue