Browse Source

[CodeIssue]PublicConstructorInAbstractClass

Convert modifiers of constructors in Abstract class from "public" to
"protected".
pull/32/merge
leoowen19 12 years ago
parent
commit
579d2aca48
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 78
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ConstructorIssues/PublicConstructorInAbstractClass.cs
  3. 140
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/PublicConstructorInAbstractClassIssueTest.cs
  4. 4
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -533,6 +533,7 @@ @@ -533,6 +533,7 @@
<Compile Include="Refactoring\CodeIssues\ConstructorIssues\StaticConstructorAccessModifierIssue.cs" />
<Compile Include="Refactoring\CodeIssues\RedundantBaseConstructorIssue.cs" />
<Compile Include="Refactoring\CodeIssues\ConstructorIssues\RedudantConstructorIssue.cs" />
<Compile Include="Refactoring\CodeIssues\ConstructorIssues\PublicConstructorInAbstractClass.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

78
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/ConstructorIssues/PublicConstructorInAbstractClass.cs

@ -0,0 +1,78 @@ @@ -0,0 +1,78 @@
// RedundantConstructorIssue.cs
//
// Author:
// Ji Kun <jikun.nus@gmail.com>
//
// Copyright (c) 2013 Ji Kun
//
// 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 SaHALL 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 System.Linq;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[IssueDescription("Constructor in Abstract Class should not be public",
Description = "Constructor in Abstract Class should not be public",
Category = IssueCategories.CompilerErrors,
Severity = Severity.Suggestion,
ResharperDisableKeyword = "PublicConstructorInAbstractClass",
IssueMarker = IssueMarker.Underline)]
public class PublicConstructorInAbstractClassIssue : ICodeIssueProvider
{
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
{
var unit = context.RootNode as SyntaxTree;
if (unit == null)
return Enumerable.Empty<CodeIssue>();
return new GatherVisitor(context).GetIssues();
}
class GatherVisitor : GatherVisitorBase<StaticConstructorAccessModifierIssue>
{
public GatherVisitor(BaseRefactoringContext ctx)
: base(ctx)
{
}
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
{
if (!typeDeclaration.HasModifier(Modifiers.Abstract)) {
return;
}
foreach (var child in typeDeclaration.Children.OfType<ConstructorDeclaration>()) {
foreach (var token_ in child.ModifierTokens) {
var token = token_;
if (token.Modifier.Equals(Modifiers.Public)) {
AddIssue(token, ctx.TranslateString("Convert public to protected"), script => {
script.Replace(token, new CSharpModifierToken(TextLocation.Empty, Modifiers.Protected));
});
}
}
}
}
}
}
}

140
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/PublicConstructorInAbstractClassIssueTest.cs

@ -0,0 +1,140 @@ @@ -0,0 +1,140 @@
//
// RedundantAssignmentIssueTests.cs
//
// Author:
// Ji Kun <jikun.nus@gmail.com>
//
// Copyright (c) 2012 Ji Kun
//
// 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 ICSharpCode.NRefactory.CSharp.Refactoring;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.CodeActions;
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
{
[TestFixture]
public class PublicConstructorInAbstractClassIssueTests : InspectionActionTestBase
{
[Test]
public void TestInspectorCase1()
{
var input = @"
abstract class TestClass
{
public TestClass ()
{
}
}";
var output = @"
abstract class TestClass
{
protected TestClass ()
{
}
}";
TestRefactoringContext context;
var issues = GetIssues(new PublicConstructorInAbstractClassIssue(), input, out context);
Assert.AreEqual(1, issues.Count);
CheckFix(context, issues, output);
}
[Test]
public void TestInspectorCase2()
{
var input = @"
abstract class TestClass
{
static TestClass ()
{
}
void TestMethod ()
{
var i = 1;
}
}";
TestRefactoringContext context;
var issues = GetIssues(new PublicConstructorInAbstractClassIssue(), input, out context);
Assert.AreEqual(0, issues.Count);
}
[Test]
public void TestInspectorCase3()
{
var input = @"
abstract class TestClass
{
public TestClass ()
{
}
private TestClass ()
{
}
public TestClass (string str)
{
Console.WriteLine(str);
}
}";
var output = @"
abstract class TestClass
{
protected TestClass ()
{
}
private TestClass ()
{
}
protected TestClass (string str)
{
Console.WriteLine(str);
}
}";
TestRefactoringContext context;
var issues = GetIssues(new PublicConstructorInAbstractClassIssue(), input, out context);
Assert.AreEqual(2, issues.Count);
CheckFix(context, issues, output);
}
[Ignore]
[Test]
public void TestResharperDisable()
{
var input = @"
abstract class TestClass
{
//Resharper disable PublicConstructorInAbstractClass
public TestClass ()
{
}
//Resharper restore PublicConstructorInAbstractClass
}";
TestRefactoringContext context;
var issues = GetIssues(new PublicConstructorInAbstractClassIssue(), input, out context);
Assert.AreEqual(0, issues.Count);
}
}
}

4
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -406,9 +406,9 @@ @@ -406,9 +406,9 @@
<Compile Include="CSharp\CodeIssues\RedundantNullCheckTests.cs">
</Compile>
<Compile Include="CSharp\CodeIssues\StaticConstructorAccessModifierTest.cs" />
<Compile Include="CSharp\CodeIssues\RedundantBaseConstructorIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\RedundantBaseConstructorIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\RedundantConstructorTests.cs" />
<Compile Include="CSharp\CodeIssues\PublicConstructorInAbstractClassIssueTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save