Skip to content

Commit

Permalink
Make internal & private classes sealed where possible. Make private m…
Browse files Browse the repository at this point in the history
…ethods static where possible
  • Loading branch information
Henr1k80 committed Oct 16, 2024
1 parent f372c2c commit 235e04d
Show file tree
Hide file tree
Showing 81 changed files with 126 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/Internal/InterfaceAttributeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private InterfaceAttributeUtil(Type derivedType, Type[] baseTypes)
results = new List<object>();
}

private Aged<Type>[] CollectTypes(Type derivedType, Type[] baseTypes)
private static Aged<Type>[] CollectTypes(Type derivedType, Type[] baseTypes)
{
var ages = new Dictionary<Type, int>();
int age;
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/Internal/WeakKeyComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Castle.Core.Internal
using System;
using System.Collections.Generic;

internal class WeakKeyComparer<TKey> : IEqualityComparer<object>
internal sealed class WeakKeyComparer<TKey> : IEqualityComparer<object>
where TKey : class
{
public static readonly WeakKeyComparer<TKey>
Expand Down
4 changes: 2 additions & 2 deletions src/Castle.Core/Core/Internal/WeakKeyDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Castle.Core.Internal
using System.Collections;
using System.Collections.Generic;

internal class WeakKeyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
internal sealed class WeakKeyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
where TKey : class
{
private readonly Dictionary<object, TValue> dictionary;
Expand Down Expand Up @@ -177,7 +177,7 @@ public void TrimDeadObjects()
dictionary.Remove(key);
}

private class KeyCollection : ICollection<TKey>
private sealed class KeyCollection : ICollection<TKey>
{
private readonly ICollection<object> keys;

Expand Down
6 changes: 3 additions & 3 deletions src/Castle.Core/Core/Logging/NullLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public void WarnFormat(Exception exception, IFormatProvider formatProvider, stri
{
}

private class NullContextProperties : IContextProperties
private sealed class NullContextProperties : IContextProperties
{
public static readonly NullContextProperties Instance = new NullContextProperties();

Expand All @@ -490,7 +490,7 @@ public object this[string key]
}
}

private class NullContextStack : IContextStack, IDisposable
private sealed class NullContextStack : IContextStack, IDisposable
{
public static readonly NullContextStack Instance = new NullContextStack();

Expand Down Expand Up @@ -519,7 +519,7 @@ public void Dispose()
}
}

private class NullContextStacks : IContextStacks
private sealed class NullContextStacks : IContextStacks
{
public static readonly NullContextStacks Instance = new NullContextStacks();

Expand Down
4 changes: 2 additions & 2 deletions src/Castle.Core/Core/Logging/TraceLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override ILogger Create(string name)
return InternalCreate(name);
}

private ILogger InternalCreate(string name)
private static ILogger InternalCreate(string name)
{
return new TraceLogger(name);
}
Expand All @@ -49,7 +49,7 @@ public override ILogger Create(string name, LoggerLevel level)
return InternalCreate(name, level);
}

private ILogger InternalCreate(string name, LoggerLevel level)
private static ILogger InternalCreate(string name, LoggerLevel level)
{
return new TraceLogger(name, level);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/ReflectionBasedDictionaryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static bool IsReadable(PropertyInfo property)
return property.CanRead && property.GetIndexParameters().Length == 0;
}

private class DictionaryEntryEnumeratorAdapter : IDictionaryEnumerator
private sealed class DictionaryEntryEnumeratorAdapter : IDictionaryEnumerator
{
private readonly IDictionaryEnumerator enumerator;
private KeyValuePair<string, object> current;
Expand Down
6 changes: 3 additions & 3 deletions src/Castle.Core/Core/Resource/AssemblyResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ private string GetNameFound(string[] names)
return nameFound;
}

private string ConvertToResourceName(string assembly, string resource)
private static string ConvertToResourceName(string assembly, string resource)
{
assembly = GetSimpleName(assembly);
// TODO: use path for relative name construction
return string.Format(CultureInfo.CurrentCulture, "{0}{1}", assembly, resource.Replace('/', '.'));
}

private string GetSimpleName(string assembly)
private static string GetSimpleName(string assembly)
{
int indexOfComma = assembly.IndexOf(',');
if(indexOfComma<0)
Expand All @@ -130,7 +130,7 @@ private string GetSimpleName(string assembly)
return assembly.Substring(0, indexOfComma);
}

private string ConvertToPath(string resource)
private static string ConvertToPath(string resource)
{
string path = resource.Replace('.', '/');
if (path[0] != '/')
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/StringObjectDictionaryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public IEnumerator GetEnumerator()
return ((IEnumerable) dictionary).GetEnumerator();
}

internal class EnumeratorAdapter : IEnumerator<KeyValuePair<string, object>>
internal sealed class EnumeratorAdapter : IEnumerator<KeyValuePair<string, object>>
{
private readonly StringObjectDictionaryAdapter adapter;
private readonly IEnumerator<string> keyEnumerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Castle.DynamicProxy.Contributors
using Castle.DynamicProxy.Internal;
using Castle.DynamicProxy.Tokens;

internal class ClassProxyTargetContributor : CompositeTypeContributor
internal sealed class ClassProxyTargetContributor : CompositeTypeContributor
{
private readonly Type targetType;

Expand Down Expand Up @@ -129,7 +129,7 @@ private MethodBuilder CreateCallbackMethod(ClassEmitter emitter, MethodInfo meth
return callBackMethod.MethodBuilder;
}

private bool ExplicitlyImplementedInterfaceMethod(MetaMethod method)
private static bool ExplicitlyImplementedInterfaceMethod(MetaMethod method)
{
return method.MethodOnTarget.IsPrivate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Castle.DynamicProxy.Contributors
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Tokens;

internal class ClassProxyWithTargetTargetContributor : CompositeTypeContributor
internal sealed class ClassProxyWithTargetTargetContributor : CompositeTypeContributor
{
private readonly Type targetType;

Expand Down Expand Up @@ -156,7 +156,7 @@ private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, Class
contributor);
}

private bool IsDirectlyAccessible(MetaMethod method)
private static bool IsDirectlyAccessible(MetaMethod method)
{
return method.MethodOnTarget.IsPublic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Castle.DynamicProxy.Contributors
using System;
using System.Collections.Generic;

internal class FieldReferenceComparer : IComparer<Type>
internal sealed class FieldReferenceComparer : IComparer<Type>
{
public int Compare(Type x, Type y)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Castle.DynamicProxy.Contributors

using Castle.DynamicProxy.Generators;

internal class InterfaceMembersCollector : MembersCollector
internal sealed class InterfaceMembersCollector : MembersCollector
{
public InterfaceMembersCollector(Type @interface)
: base(@interface)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Castle.DynamicProxy.Contributors

using Castle.DynamicProxy.Generators;

internal class InterfaceMembersOnClassCollector : MembersCollector
internal sealed class InterfaceMembersOnClassCollector : MembersCollector
{
private readonly InterfaceMapping map;
private readonly bool onlyProxyVirtual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Castle.DynamicProxy.Contributors
using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Generators.Emitters;

internal class InterfaceProxyWithOptionalTargetContributor : InterfaceProxyWithoutTargetContributor
internal sealed class InterfaceProxyWithOptionalTargetContributor : InterfaceProxyWithoutTargetContributor
{
private readonly GetTargetReferenceDelegate getTargetReference;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Castle.DynamicProxy.Contributors

using Castle.DynamicProxy.Generators;

internal class InterfaceProxyWithTargetInterfaceTargetContributor : InterfaceProxyTargetContributor
internal sealed class InterfaceProxyWithTargetInterfaceTargetContributor : InterfaceProxyTargetContributor
{
public InterfaceProxyWithTargetInterfaceTargetContributor(Type proxyTargetType, bool allowChangeTarget,
INamingScope namingScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Castle.DynamicProxy.Contributors
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Tokens;

internal class InvocationWithDelegateContributor : IInvocationCreationContributor
internal sealed class InvocationWithDelegateContributor : IInvocationCreationContributor
{
private readonly Type delegateType;
private readonly MetaMethod method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace Castle.DynamicProxy.Contributors
{
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

using Castle.DynamicProxy.Generators;
Expand All @@ -25,7 +24,7 @@ namespace Castle.DynamicProxy.Contributors
using Castle.DynamicProxy.Internal;
using Castle.DynamicProxy.Tokens;

internal class InvocationWithGenericDelegateContributor : IInvocationCreationContributor
internal sealed class InvocationWithGenericDelegateContributor : IInvocationCreationContributor
{
private readonly Type delegateType;
private readonly MetaMethod method;
Expand Down Expand Up @@ -73,7 +72,7 @@ private Reference GetDelegate(AbstractTypeEmitter invocation, MethodEmitter invo
return localReference;
}

private AssignStatement SetDelegate(LocalReference localDelegate, Reference localTarget,
private static AssignStatement SetDelegate(LocalReference localDelegate, Reference localTarget,
Type closedDelegateType, MethodInfo closedMethodOnTarget)
{
var delegateCreateDelegate = new MethodInvocationExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ namespace Castle.DynamicProxy.Contributors
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Generators.Emitters;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Internal;

internal class MixinContributor : CompositeTypeContributor
internal sealed class MixinContributor : CompositeTypeContributor
{
private readonly bool canChangeTarget;
private readonly IList<Type> empty = new List<Type>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ namespace Castle.DynamicProxy.Contributors
using System.Runtime.CompilerServices;

using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Generators.Emitters;
using Castle.DynamicProxy.Internal;

internal class WrappedClassMembersCollector : ClassMembersCollector
internal sealed class WrappedClassMembersCollector : ClassMembersCollector
{
public WrappedClassMembersCollector(Type type) : base(type)
{
Expand All @@ -49,13 +48,13 @@ protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGener
return new MetaMethod(method, method, isStandalone, accepted, hasTarget: true);
}

protected bool IsGeneratedByTheCompiler(FieldInfo field)
private static bool IsGeneratedByTheCompiler(FieldInfo field)
{
// for example fields backing autoproperties
return field.IsDefined(typeof(CompilerGeneratedAttribute));
}

protected virtual bool IsOKToBeOnProxy(FieldInfo field)
private static bool IsOKToBeOnProxy(FieldInfo field)
{
return IsGeneratedByTheCompiler(field);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private static int CombineMemberHashCodes(IDictionary<string, object?> dict)
}
}

private IDictionary<string, object?> MakeNameValueDictionary<T>(T[] members, object?[] values)
private static IDictionary<string, object?> MakeNameValueDictionary<T>(T[] members, object?[] values)
where T : MemberInfo
{
var dict = new Dictionary<string, object?>();
Expand All @@ -306,7 +306,7 @@ private static int CombineMemberHashCodes(IDictionary<string, object?> dict)
return dict;
}

private class AttributeArgumentValueEqualityComparer : IEqualityComparer<object?>
private sealed class AttributeArgumentValueEqualityComparer : IEqualityComparer<object?>
{
new public bool Equals(object? x, object? y)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Type CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[]?
return generator.GetProxyType();
}

private void AssertValidMixins(ProxyGenerationOptions options, string paramName)
private static void AssertValidMixins(ProxyGenerationOptions options, string paramName)
{
try
{
Expand All @@ -129,12 +129,12 @@ private void AssertValidMixins(ProxyGenerationOptions options, string paramName)
}
}

private void AssertValidType(Type target, string paramName)
private static void AssertValidType(Type target, string paramName)
{
AssertValidTypeForTarget(target, target, paramName);
}

private void AssertValidTypeForTarget(Type type, Type target, string paramName)
private static void AssertValidTypeForTarget(Type type, Type target, string paramName)
{
if (type.IsGenericTypeDefinition)
{
Expand All @@ -152,7 +152,7 @@ private void AssertValidTypeForTarget(Type type, Type target, string paramName)
}
}

private void AssertValidTypes(IEnumerable<Type>? targetTypes, string paramName)
private static void AssertValidTypes(IEnumerable<Type>? targetTypes, string paramName)
{
if (targetTypes != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private IEnumerable<Type> GetTypeImplementerMapping(out IEnumerable<ITypeContrib
return typeImplementerMapping.Keys;
}

private void EnsureDoesNotImplementIProxyTargetAccessor(Type type, string name)
private static void EnsureDoesNotImplementIProxyTargetAccessor(Type type, string name)
{
if (!typeof(IProxyTargetAccessor).IsAssignableFrom(type))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void CreateFields(ClassEmitter emitter, Type proxyTargetType)
#endif
}

private void EnsureValidBaseType(Type type)
private static void EnsureValidBaseType(Type type)
{
if (type == null)
{
Expand All @@ -286,7 +286,7 @@ private void EnsureValidBaseType(Type type)
}
}

private void ThrowInvalidBaseType(Type type, string doesNotHaveAccessibleParameterlessConstructor)
private static void ThrowInvalidBaseType(Type type, string doesNotHaveAccessibleParameterlessConstructor)
{
var format =
"Type {0} is not valid base type for interface proxy, because {1}. Only a non-sealed class with non-private default constructor can be used as base type for interface proxy. Please use some other valid type.";
Expand Down
Loading

0 comments on commit 235e04d

Please sign in to comment.