Skip to content

Commit

Permalink
SPRNET-1536 replace Predicate<T> with Func<T, bool> and replace custs…
Browse files Browse the repository at this point in the history
…om LINQ extensions with actual LINQ now that we're on .NET 3.5 or later
  • Loading branch information
sbohlen committed Jan 13, 2013
1 parent e4a59c6 commit de4f7d6
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 223 deletions.
Expand Up @@ -20,6 +20,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Spring.Objects.Factory.Support;
using Spring.Stereotype;
Expand All @@ -32,7 +33,7 @@ namespace Spring.Context.Attributes
[Serializable]
public class AssemblyObjectDefinitionScanner : RequiredConstraintAssemblyTypeScanner
{
private readonly List<Predicate<Assembly>> _assemblyExclusionPredicates = new List<Predicate<Assembly>>();
private readonly List<Func<Assembly, bool>> _assemblyExclusionPredicates = new List<Func<Assembly, bool>>();

private readonly IList<string> _springAssemblies = new List<string>()
{
Expand Down Expand Up @@ -92,8 +93,7 @@ private void RegisterDefinitionsForTypes(IObjectDefinitionRegistry registry, IEn
/// <returns></returns>
protected override IEnumerable<Assembly> ApplyAssemblyFiltersTo(IEnumerable<Assembly> assemblyCandidates)
{
return assemblyCandidates.Where(
delegate(Assembly candidate) { return IsIncludedAssembly(candidate) && !IsExcludedAssembly(candidate); });
return assemblyCandidates.Where(candidate => IsIncludedAssembly(candidate) && !IsExcludedAssembly(candidate));
}

/// <summary>
Expand All @@ -105,7 +105,7 @@ protected override IEnumerable<Assembly> ApplyAssemblyFiltersTo(IEnumerable<Asse
/// </returns>
protected virtual bool IsExcludedAssembly(Assembly candidate)
{
return _assemblyExclusionPredicates.Any(delegate(Predicate<Assembly> exclude) { return exclude(candidate); });
return _assemblyExclusionPredicates.Any(exclude => exclude(candidate));
}

/// <summary>
Expand Down Expand Up @@ -135,8 +135,9 @@ protected override bool IsRequiredConstraintSatisfiedBy(Type type)

foreach (CustomAttributeData customAttributeData in CustomAttributeData.GetCustomAttributes(type))
{
if (customAttributeData.Constructor.DeclaringType.FullName == typeof(ComponentAttribute).FullName &&
!type.IsAbstract)
if (customAttributeData.Constructor.DeclaringType != null &&
(customAttributeData.Constructor.DeclaringType.FullName == typeof(ComponentAttribute).FullName &&
!type.IsAbstract))
{
satisfied = true;
break;
Expand All @@ -155,12 +156,11 @@ protected override void SetDefaultFilters()
base.SetDefaultFilters();

//add the desired assembly exclusions to the list
_assemblyExclusionPredicates.Add(
delegate(Assembly a) { return _springAssemblies.Contains(a.GetName().Name); });
_assemblyExclusionPredicates.Add(delegate(Assembly a) { return a.GetName().Name.StartsWith("System."); });
_assemblyExclusionPredicates.Add(delegate(Assembly a) { return a.GetName().Name.StartsWith("Microsoft."); });
_assemblyExclusionPredicates.Add(delegate(Assembly a) { return a.GetName().Name == "mscorlib"; });
_assemblyExclusionPredicates.Add(delegate(Assembly a) { return a.GetName().Name == "System"; });
_assemblyExclusionPredicates.Add(a => _springAssemblies.Contains(a.GetName().Name));
_assemblyExclusionPredicates.Add(a => a.GetName().Name.StartsWith("System."));
_assemblyExclusionPredicates.Add(a => a.GetName().Name.StartsWith("Microsoft."));
_assemblyExclusionPredicates.Add(a => a.GetName().Name == "mscorlib");
_assemblyExclusionPredicates.Add(a => a.GetName().Name == "System");
}

/// <summary>
Expand All @@ -178,11 +178,11 @@ public virtual void ScanAndRegisterTypes(IObjectDefinitionRegistry registry)
/// </summary>
public AssemblyObjectDefinitionScanner()
{
AssemblyLoadExclusionPredicates.Add(delegate(string name) { return _springAssemblies.Contains(name); });
AssemblyLoadExclusionPredicates.Add(delegate(string name) { return name.StartsWith("System."); });
AssemblyLoadExclusionPredicates.Add(delegate(string name) { return name.StartsWith("Microsoft."); });
AssemblyLoadExclusionPredicates.Add(delegate(string name) { return name == "mscorlib"; });
AssemblyLoadExclusionPredicates.Add(delegate(string name) { return name == "System"; });
AssemblyLoadExclusionPredicates.Add(name => _springAssemblies.Contains(name));
AssemblyLoadExclusionPredicates.Add(name => name.StartsWith("System."));
AssemblyLoadExclusionPredicates.Add(name => name.StartsWith("Microsoft."));
AssemblyLoadExclusionPredicates.Add(name => name == "mscorlib");
AssemblyLoadExclusionPredicates.Add(name => name == "System");
}

}
Expand Down
49 changes: 20 additions & 29 deletions src/Spring/Spring.Core/Context/Attributes/AssemblyTypeScanner.cs
Expand Up @@ -21,6 +21,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Common.Logging;
using Spring.Context.Attributes.TypeFilters;
Expand All @@ -43,17 +44,17 @@ public abstract class AssemblyTypeScanner : IAssemblyTypeScanner
/// <summary>
/// Names of Assemblies to exclude from being loaded for scanning.
/// </summary>
protected IList<Predicate<string>> AssemblyLoadExclusionPredicates = new List<Predicate<string>>();
protected IList<Func<string, bool>> AssemblyLoadExclusionPredicates = new List<Func<string, bool>>();

/// <summary>
/// Assembly Inclusion Predicates.
/// </summary>
protected readonly List<Predicate<Assembly>> AssemblyInclusionPredicates = new List<Predicate<Assembly>>();
protected readonly List<Func<Assembly, bool>> AssemblyInclusionPredicates = new List<Func<Assembly, bool>>();

/// <summary>
/// Type Exclusion Predicates.
/// </summary>
protected readonly List<Predicate<Type>> TypeExclusionPredicates = new List<Predicate<Type>>();
protected readonly List<Func<Type, bool>> TypeExclusionPredicates = new List<Func<Type, bool>>();

/// <summary>
/// Type Exclusion Predicates.
Expand All @@ -63,7 +64,7 @@ public abstract class AssemblyTypeScanner : IAssemblyTypeScanner
/// <summary>
/// Type Inclusion Predicates.
/// </summary>
protected readonly List<Predicate<Type>> TypeInclusionPredicates = new List<Predicate<Type>>();
protected readonly List<Func<Type, bool>> TypeInclusionPredicates = new List<Func<Type, bool>>();

/// <summary>
/// Type Inclusion TypeFilters.
Expand Down Expand Up @@ -105,7 +106,7 @@ public IAssemblyTypeScanner AssemblyHavingType<T>()
/// <returns></returns>
public IAssemblyTypeScanner ExcludeType<T>()
{
TypeExclusionPredicates.Add(delegate(Type t) { return t.FullName == typeof(T).FullName; });
TypeExclusionPredicates.Add(t => t.FullName == typeof (T).FullName);
return this;
}

Expand All @@ -116,7 +117,7 @@ public IAssemblyTypeScanner ExcludeType<T>()
/// <returns></returns>
public IAssemblyTypeScanner IncludeType<T>()
{
TypeInclusionPredicates.Add(delegate(Type t) { return t.FullName == typeof(T).FullName; });
TypeInclusionPredicates.Add(t => t.FullName == typeof (T).FullName);
return this;
}

Expand All @@ -130,7 +131,7 @@ public IAssemblyTypeScanner IncludeTypes(IEnumerable<Type> typeSource)
AssertUtils.ArgumentNotNull(typeSource, "typeSource");
TypeSources.Add(typeSource);
TypeInclusionPredicates.Add(
delegate(Type t) { return typeSource.Any(delegate(Type t1) { return t1.FullName == t.FullName; }); });
t => typeSource.Any(t1 => t1.FullName == t.FullName));
return this;
}

Expand Down Expand Up @@ -168,7 +169,7 @@ public virtual IEnumerable<Type> Scan()
/// </summary>
/// <param name="assemblyPredicate">The assembly predicate.</param>
/// <returns></returns>
public IAssemblyTypeScanner WithAssemblyFilter(Predicate<Assembly> assemblyPredicate)
public IAssemblyTypeScanner WithAssemblyFilter(Func<Assembly, bool> assemblyPredicate)
{
AssemblyInclusionPredicates.Add(assemblyPredicate);
return this;
Expand All @@ -179,7 +180,7 @@ public IAssemblyTypeScanner WithAssemblyFilter(Predicate<Assembly> assemblyPredi
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
public IAssemblyTypeScanner WithExcludeFilter(Predicate<Type> predicate)
public IAssemblyTypeScanner WithExcludeFilter(Func<Type, bool> predicate)
{
TypeExclusionPredicates.Add(predicate);
return this;
Expand All @@ -203,7 +204,7 @@ public IAssemblyTypeScanner WithExcludeFilter(ITypeFilter filter)
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
public IAssemblyTypeScanner WithIncludeFilter(Predicate<Type> predicate)
public IAssemblyTypeScanner WithIncludeFilter(Func<Type, bool> predicate)
{
TypeInclusionPredicates.Add(predicate);
return this;
Expand Down Expand Up @@ -314,15 +315,10 @@ protected virtual IEnumerable<Assembly> ApplyAssemblyFiltersTo(IEnumerable<Assem
/// </returns>
protected virtual bool IsExcludedType(Type type)
{
if (TypeExclusionPredicates.Count > 0 && TypeExclusionPredicates.Any(delegate(Predicate<Type> exclude) { return exclude(type); }))
if (TypeExclusionPredicates.Count > 0 && TypeExclusionPredicates.Any(exclude => exclude(type)))
return true;

foreach(var filter in TypeExclusionTypeFilters)
{
if (filter.Match(type))
return true;
}
return false;
return Enumerable.Any(TypeExclusionTypeFilters, filter => filter.Match(type));
}

/// <summary>
Expand All @@ -334,7 +330,7 @@ protected virtual bool IsExcludedType(Type type)
/// </returns>
protected virtual bool IsIncludedAssembly(Assembly assembly)
{
return AssemblyInclusionPredicates.Any(delegate(Predicate<Assembly> include) { return include(assembly); });
return AssemblyInclusionPredicates.Any(include => include(assembly));
}

/// <summary>
Expand All @@ -346,15 +342,10 @@ protected virtual bool IsIncludedAssembly(Assembly assembly)
/// </returns>
protected virtual bool IsIncludedType(Type type)
{
if (TypeInclusionPredicates.Count > 0 && TypeInclusionPredicates.Any(delegate(Predicate<Type> include) { return include(type); }))
if (TypeInclusionPredicates.Count > 0 && TypeInclusionPredicates.Any(include => include(type)))
return true;

foreach(var filter in TypeInclusionTypeFilter)
{
if (filter.Match(type))
return true;
}
return false;
return Enumerable.Any(TypeInclusionTypeFilter, filter => filter.Match(type));
}

/// <summary>
Expand All @@ -363,13 +354,13 @@ protected virtual bool IsIncludedType(Type type)
protected virtual void SetDefaultFilters()
{
if (TypeInclusionPredicates.Count == 0 && TypeInclusionTypeFilter.Count == 0)
TypeInclusionPredicates.Add(delegate { return true; });
TypeInclusionPredicates.Add(obj => true);

if (TypeExclusionPredicates.Count == 0 && TypeExclusionTypeFilters.Count == 0)
TypeExclusionPredicates.Add(delegate { return false; });
TypeExclusionPredicates.Add(obj => false);

if (AssemblyInclusionPredicates.Count == 0)
AssemblyInclusionPredicates.Add(delegate { return true; });
AssemblyInclusionPredicates.Add(obj => true);
}

/// <summary>
Expand All @@ -387,7 +378,7 @@ private IList<string> DiscoverAssemblies(string folderPath, string extension)
{
string name = Path.GetFileNameWithoutExtension(file);

if (!AssemblyLoadExclusionPredicates.Any(delegate(Predicate<string> exclude) { return exclude(name); }))
if (!AssemblyLoadExclusionPredicates.Any(exclude => exclude(name)))
{
assemblies.Add(file);
}
Expand Down
Expand Up @@ -42,21 +42,21 @@ public interface IAssemblyTypeScanner
/// </summary>
/// <param name="assemblyPredicate">The assembly predicate.</param>
/// <returns></returns>
IAssemblyTypeScanner WithAssemblyFilter(Predicate<Assembly> assemblyPredicate);
IAssemblyTypeScanner WithAssemblyFilter(Func<Assembly, bool> assemblyPredicate);

/// <summary>
/// Adds the predicte to the include filter for <see cref="Type"/>.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
IAssemblyTypeScanner WithIncludeFilter(Predicate<Type> predicate);
IAssemblyTypeScanner WithIncludeFilter(Func<Type, bool> predicate);

/// <summary>
/// Adds the predicte to the exclude filter for <see cref="Type"/>.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns></returns>
IAssemblyTypeScanner WithExcludeFilter(Predicate<Type> predicate);
IAssemblyTypeScanner WithExcludeFilter(Func<Type, bool> predicate);

/// <summary>
/// Includes the specific types.
Expand Down
110 changes: 0 additions & 110 deletions src/Spring/Spring.Core/Context/Attributes/LinqExtensionMethods.cs

This file was deleted.

0 comments on commit de4f7d6

Please sign in to comment.