Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jul 18, 2015
1 parent 71f1cf2 commit 3ff182a
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 66 deletions.
2 changes: 1 addition & 1 deletion AssemblyToProcess/ClassWithPrivateMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public void PublicWrapperOfPrivateMethod()
}

// ReSharper disable UnusedParameter.Local
private void SomePrivateMethod(string x)
void SomePrivateMethod(string x)
// ReSharper restore UnusedParameter.Local
{
}
Expand Down
8 changes: 4 additions & 4 deletions AssemblyToProcess/SimpleClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void PublicWrapperOfPrivateMethod()
SomePrivateMethod(null);
}

private void SomePrivateMethod(string x)
void SomePrivateMethod(string x)
{
Console.WriteLine(x);
}
Expand Down Expand Up @@ -123,23 +123,23 @@ public object MethodWithOutAndReturn(out string prefix)

public void MethodWithExistingArgumentGuard(string x)
{
if (String.IsNullOrEmpty(x))
if (string.IsNullOrEmpty(x))
throw new ArgumentException("x is null or empty.", "x");

Console.WriteLine(x);
}

public void MethodWithExistingArgumentNullGuard(string x)
{
if (String.IsNullOrEmpty(x))
if (string.IsNullOrEmpty(x))
throw new ArgumentNullException("x");

Console.WriteLine(x);
}

public void MethodWithExistingArgumentNullGuardWithMessage(string x)
{
if (String.IsNullOrEmpty(x))
if (string.IsNullOrEmpty(x))
throw new ArgumentNullException("x", "x is null or empty.");

Console.WriteLine(x);
Expand Down
4 changes: 3 additions & 1 deletion AssemblyToProcess/SpecialClass.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
#if (DEBUG)
using System;
using System.Threading.Tasks;
using NullGuard;
#endif

public class SpecialClass
{
Expand Down
6 changes: 3 additions & 3 deletions Fody/CecilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

public static class CecilExtensions
{
private const string AllowNullAttributeTypeName = "AllowNullAttribute";
private const string CanBeNullAttributeTypeName = "CanBeNullAttribute";
const string AllowNullAttributeTypeName = "AllowNullAttribute";
const string CanBeNullAttributeTypeName = "CanBeNullAttribute";

public static bool HasInterface(this TypeDefinition type, string interfaceFullName)
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public static bool MayNotBeNull(this ParameterDefinition arg)
return !arg.AllowsNull() && !arg.IsOptionalArgumentWithNullDefaultValue() && arg.ParameterType.IsRefType() && !arg.IsOut;
}

private static bool IsOptionalArgumentWithNullDefaultValue(this ParameterDefinition arg)
static bool IsOptionalArgumentWithNullDefaultValue(this ParameterDefinition arg)
{
return arg.IsOptional && arg.Constant == null;
}
Expand Down
2 changes: 1 addition & 1 deletion Fody/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void Error(Exception exception, string format, params object[] args)

public bool IsErrorEnabled { get { return LoggerFactory.LogError != null; } }

private string ExceptionToString(Exception exception)
string ExceptionToString(Exception exception)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(exception.GetType().FullName + ":");
Expand Down
36 changes: 18 additions & 18 deletions Fody/MethodProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

public class MethodProcessor
{
private const string STR_OutParameterIsNull = "[NullGuard] Out parameter '{0}' is null.";
private const string STR_ReturnValueOfMethodIsNull = "[NullGuard] Return value of method '{0}' is null.";
private const string STR_IsNull = "[NullGuard] {0} is null.";
const string OutParameterIsNull = "[NullGuard] Out parameter '{0}' is null.";
const string ReturnValueOfMethodIsNull = "[NullGuard] Return value of method '{0}' is null.";
const string IsNull = "[NullGuard] {0} is null.";

private readonly bool isDebug;
private readonly ValidationFlags validationFlags;
bool isDebug;
ValidationFlags validationFlags;

public MethodProcessor(ValidationFlags validationFlags, bool isDebug)
{
Expand All @@ -40,7 +40,7 @@ public void Process(MethodDefinition method)
}
}

private void InnerProcess(MethodDefinition method)
void InnerProcess(MethodDefinition method)
{
var localValidationFlags = validationFlags;

Expand Down Expand Up @@ -82,15 +82,15 @@ private void InnerProcess(MethodDefinition method)
returnType.IsRefType() &&
returnType.FullName != typeof(void).FullName)
{
InjectMethodReturnGuardAsync(body, String.Format(CultureInfo.InvariantCulture, STR_ReturnValueOfMethodIsNull, method.FullName), method.FullName);
InjectMethodReturnGuardAsync(body, string.Format(CultureInfo.InvariantCulture, ReturnValueOfMethodIsNull, method.FullName), method.FullName);
}
}

body.InitLocals = true;
body.OptimizeMacros();
}

private void InjectMethodArgumentGuards(MethodDefinition method, MethodBody body, SequencePoint seqPoint)
void InjectMethodArgumentGuards(MethodDefinition method, MethodBody body, SequencePoint seqPoint)
{
var guardInstructions = new List<Instruction>();

Expand All @@ -106,7 +106,7 @@ private void InjectMethodArgumentGuards(MethodDefinition method, MethodBody body
continue;

var entry = body.Instructions.First();
var errorMessage = String.Format(CultureInfo.InvariantCulture, STR_IsNull, parameter.Name);
var errorMessage = string.Format(CultureInfo.InvariantCulture, IsNull, parameter.Name);

guardInstructions.Clear();

Expand All @@ -133,7 +133,7 @@ private void InjectMethodArgumentGuards(MethodDefinition method, MethodBody body
}
}

private void InjectMethodReturnGuard(ValidationFlags localValidationFlags, MethodDefinition method, MethodBody body, SequencePoint seqPoint)
void InjectMethodReturnGuard(ValidationFlags localValidationFlags, MethodDefinition method, MethodBody body, SequencePoint seqPoint)
{
var guardInstructions = new List<Instruction>();

Expand All @@ -151,7 +151,7 @@ private void InjectMethodReturnGuard(ValidationFlags localValidationFlags, Metho
method.ReturnType.FullName != typeof(void).FullName &&
!method.IsGetter)
{
var errorMessage = String.Format(CultureInfo.InvariantCulture, STR_ReturnValueOfMethodIsNull, method.FullName);
var errorMessage = string.Format(CultureInfo.InvariantCulture, ReturnValueOfMethodIsNull, method.FullName);
AddReturnNullGuard(body.Instructions, seqPoint, ret, method.ReturnType, errorMessage, Instruction.Create(OpCodes.Throw));
}

Expand All @@ -167,7 +167,7 @@ private void InjectMethodReturnGuard(ValidationFlags localValidationFlags, Metho
parameter.ParameterType.IsRefType() &&
!parameter.AllowsNull())
{
var errorMessage = String.Format(CultureInfo.InvariantCulture, STR_OutParameterIsNull, parameter.Name);
var errorMessage = string.Format(CultureInfo.InvariantCulture, OutParameterIsNull, parameter.Name);

guardInstructions.Clear();

Expand Down Expand Up @@ -197,7 +197,7 @@ private void InjectMethodReturnGuard(ValidationFlags localValidationFlags, Metho
}
}

private void InjectMethodReturnGuardAsync(MethodBody body, string errorMessage, string methodName)
void InjectMethodReturnGuardAsync(MethodBody body, string errorMessage, string methodName)
{
foreach (var local in body.Variables)
{
Expand All @@ -213,7 +213,7 @@ private void InjectMethodReturnGuardAsync(MethodBody body, string errorMessage,
}
}

private void InjectMethodReturnGuardAsyncIntoMoveNext(MethodDefinition method, string errorMessage, string methodName)
void InjectMethodReturnGuardAsyncIntoMoveNext(MethodDefinition method, string errorMessage, string methodName)
{
method.Body.SimplifyMacros();

Expand Down Expand Up @@ -244,7 +244,7 @@ private void InjectMethodReturnGuardAsyncIntoMoveNext(MethodDefinition method, s
method.Body.OptimizeMacros();
}

private void AddReturnNullGuard(Collection<Instruction> instructions, SequencePoint seqPoint, int ret, TypeReference returnType, string errorMessage, params Instruction[] finalInstructions)
void AddReturnNullGuard(Collection<Instruction> instructions, SequencePoint seqPoint, int ret, TypeReference returnType, string errorMessage, params Instruction[] finalInstructions)
{
var returnInstruction = instructions[ret];

Expand Down Expand Up @@ -274,7 +274,7 @@ private void AddReturnNullGuard(Collection<Instruction> instructions, SequencePo
instructions.Insert(ret, guardInstructions);
}

private static bool CheckForExistingGuard(Collection<Instruction> instructions, ParameterDefinition parameter)
static bool CheckForExistingGuard(Collection<Instruction> instructions, ParameterDefinition parameter)
{
for (var i = 1; i < instructions.Count - 1; i++)
{
Expand Down Expand Up @@ -303,7 +303,7 @@ private static bool CheckForExistingGuard(Collection<Instruction> instructions,
return false;
}

private static bool IsSetResultMethod(MethodReference methodReference)
static bool IsSetResultMethod(MethodReference methodReference)
{
return
methodReference != null &&
Expand All @@ -312,7 +312,7 @@ private static bool IsSetResultMethod(MethodReference methodReference)
methodReference.DeclaringType.FullName.StartsWith("System.Runtime.CompilerServices.AsyncTaskMethodBuilder");
}

private static bool IsSetExceptionMethod(MethodReference methodReference)
static bool IsSetExceptionMethod(MethodReference methodReference)
{
return
methodReference != null &&
Expand Down
16 changes: 8 additions & 8 deletions Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void Execute()
RemoveReference();
}

private List<TypeDefinition> GetTypesToProcess()
List<TypeDefinition> GetTypesToProcess()
{
var allTypes = new List<TypeDefinition>(ModuleDefinition.GetTypes());
List<TypeDefinition> types;
Expand All @@ -68,7 +68,7 @@ private List<TypeDefinition> GetTypesToProcess()
return types;
}

private void ReadConfig()
void ReadConfig()
{
if (Config == null)
{
Expand All @@ -79,7 +79,7 @@ private void ReadConfig()
ReadExcludeRegex();
}

private void ReadIncludeDebugAssert()
void ReadIncludeDebugAssert()
{
var includeDebugAssertAttribute = Config.Attribute("IncludeDebugAssert");
if (includeDebugAssertAttribute != null)
Expand All @@ -91,7 +91,7 @@ private void ReadIncludeDebugAssert()
}
}

private void ReadExcludeRegex()
void ReadExcludeRegex()
{
var attribute = Config.Attribute("ExcludeRegex");
if(attribute != null)
Expand All @@ -104,7 +104,7 @@ private void ReadExcludeRegex()
}
}

private void CheckForBadAttributes(List<TypeDefinition> types)
void CheckForBadAttributes(List<TypeDefinition> types)
{
foreach (var typeDefinition in types)
{
Expand All @@ -125,7 +125,7 @@ private void CheckForBadAttributes(List<TypeDefinition> types)
}
}

private void ProcessAssembly(List<TypeDefinition> types)
void ProcessAssembly(List<TypeDefinition> types)
{
var isDebug = IncludeDebugAssert && DefineConstants.Any(c => c == "DEBUG") && ReferenceFinder.DebugAssertMethod != null;

Expand All @@ -145,7 +145,7 @@ private void ProcessAssembly(List<TypeDefinition> types)
}
}

private void RemoveAttributes(List<TypeDefinition> types)
void RemoveAttributes(List<TypeDefinition> types)
{
ModuleDefinition.Assembly.RemoveAllNullGuardAttributes();
ModuleDefinition.RemoveAllNullGuardAttributes();
Expand All @@ -170,7 +170,7 @@ private void RemoveAttributes(List<TypeDefinition> types)
}
}

private void RemoveReference()
void RemoveReference()
{
var referenceToRemove = ModuleDefinition.AssemblyReferences.FirstOrDefault(x => x.Name == "NullGuard");
if (referenceToRemove == null)
Expand Down
18 changes: 9 additions & 9 deletions Fody/PropertyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

public class PropertyProcessor
{
private const string STR_ReturnValueOfPropertyIsNull = "[NullGuard] Return value of property '{0}' is null.";
private const string STR_CannotSetTheValueOfPropertyToNull = "[NullGuard] Cannot set the value of property '{0}' to null.";
const string ReturnValueOfPropertyIsNull = "[NullGuard] Return value of property '{0}' is null.";
const string CannotSetTheValueOfPropertyToNull = "[NullGuard] Cannot set the value of property '{0}' to null.";

private readonly bool isDebug;
private readonly ValidationFlags validationFlags;
bool isDebug;
ValidationFlags validationFlags;

public PropertyProcessor(ValidationFlags validationFlags, bool isDebug)
{
Expand All @@ -38,7 +38,7 @@ public void Process(PropertyDefinition property)
}
}

private void InnerProcess(PropertyDefinition property)
void InnerProcess(PropertyDefinition property)
{
var localValidationFlags = validationFlags;

Expand Down Expand Up @@ -93,7 +93,7 @@ private void InnerProcess(PropertyDefinition property)
}
}

private void InjectPropertyGetterGuard(MethodBody getBody, SequencePoint seqPoint, PropertyReference property)
void InjectPropertyGetterGuard(MethodBody getBody, SequencePoint seqPoint, PropertyReference property)
{
var guardInstructions = new List<Instruction>();

Expand All @@ -106,7 +106,7 @@ private void InjectPropertyGetterGuard(MethodBody getBody, SequencePoint seqPoin
foreach (var ret in returnPoints)
{
var returnInstruction = getBody.Instructions[ret];
var errorMessage = String.Format(CultureInfo.InvariantCulture, STR_ReturnValueOfPropertyIsNull, property.FullName);
var errorMessage = string.Format(CultureInfo.InvariantCulture, ReturnValueOfPropertyIsNull, property.FullName);

guardInstructions.Clear();

Expand Down Expand Up @@ -136,15 +136,15 @@ private void InjectPropertyGetterGuard(MethodBody getBody, SequencePoint seqPoin
}
}

private void InjectPropertySetterGuard(MethodBody setBody, SequencePoint seqPoint, PropertyDefinition property)
void InjectPropertySetterGuard(MethodBody setBody, SequencePoint seqPoint, PropertyDefinition property)
{
var valueParameter = property.SetMethod.GetPropertySetterValueParameter();

if (!valueParameter.MayNotBeNull())
return;

var guardInstructions = new List<Instruction>();
var errorMessage = String.Format(CultureInfo.InvariantCulture, STR_CannotSetTheValueOfPropertyToNull, property.FullName);
var errorMessage = string.Format(CultureInfo.InvariantCulture, CannotSetTheValueOfPropertyToNull, property.FullName);
var entry = setBody.Instructions.First();

if (isDebug)
Expand Down
8 changes: 4 additions & 4 deletions Tests/Helpers/AssemblyWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static AssemblyWeaver()
});
}

private static Assembly WeaveAssembly(string beforeAssemblyPath, string afterAssemblyPath, string beforePdbPath, string afterPdbPath, Action<ModuleDefinition> weaveAction)
static Assembly WeaveAssembly(string beforeAssemblyPath, string afterAssemblyPath, string beforePdbPath, string afterPdbPath, Action<ModuleDefinition> weaveAction)
{
if (File.Exists(afterAssemblyPath))
File.Delete(afterAssemblyPath);
Expand All @@ -151,17 +151,17 @@ private static Assembly WeaveAssembly(string beforeAssemblyPath, string afterAss
return Assembly.LoadFile(afterAssemblyPath);
}

private static void LogInfo(string error)
static void LogInfo(string error)
{
Infos.Add(error);
}

private static void LogWarn(string error)
static void LogWarn(string error)
{
Warns.Add(error);
}

private static void LogError(string error)
static void LogError(string error)
{
Errors.Add(error);
}
Expand Down
Loading

0 comments on commit 3ff182a

Please sign in to comment.