SPRNET-1536 replace Predicate<T> with Func<T, bool> and replace custsom LINQ extensions with actual LINQ now that we're on .NET 3.5 or later

This commit is contained in:
Steve Bohlen
2013-01-13 18:53:41 -05:00
parent e4a59c6dd4
commit de4f7d6e1f
11 changed files with 56 additions and 223 deletions

View File

@@ -20,6 +20,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Spring.Objects.Factory.Support;
using Spring.Stereotype;
@@ -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>()
{
@@ -92,8 +93,7 @@ namespace Spring.Context.Attributes
/// <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>
@@ -105,7 +105,7 @@ namespace Spring.Context.Attributes
/// </returns>
protected virtual bool IsExcludedAssembly(Assembly candidate)
{
return _assemblyExclusionPredicates.Any(delegate(Predicate<Assembly> exclude) { return exclude(candidate); });
return _assemblyExclusionPredicates.Any(exclude => exclude(candidate));
}
/// <summary>
@@ -135,8 +135,9 @@ namespace Spring.Context.Attributes
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;
@@ -155,12 +156,11 @@ namespace Spring.Context.Attributes
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>
@@ -178,11 +178,11 @@ namespace Spring.Context.Attributes
/// </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");
}
}

View File

@@ -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;
@@ -43,17 +44,17 @@ namespace Spring.Context.Attributes
/// <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.
@@ -63,7 +64,7 @@ namespace Spring.Context.Attributes
/// <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.
@@ -105,7 +106,7 @@ namespace Spring.Context.Attributes
/// <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;
}
@@ -116,7 +117,7 @@ namespace Spring.Context.Attributes
/// <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;
}
@@ -130,7 +131,7 @@ namespace Spring.Context.Attributes
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;
}
@@ -168,7 +169,7 @@ namespace Spring.Context.Attributes
/// </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;
@@ -179,7 +180,7 @@ namespace Spring.Context.Attributes
/// </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;
@@ -203,7 +204,7 @@ namespace Spring.Context.Attributes
/// </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;
@@ -314,15 +315,10 @@ namespace Spring.Context.Attributes
/// </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>
@@ -334,7 +330,7 @@ namespace Spring.Context.Attributes
/// </returns>
protected virtual bool IsIncludedAssembly(Assembly assembly)
{
return AssemblyInclusionPredicates.Any(delegate(Predicate<Assembly> include) { return include(assembly); });
return AssemblyInclusionPredicates.Any(include => include(assembly));
}
/// <summary>
@@ -346,15 +342,10 @@ namespace Spring.Context.Attributes
/// </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>
@@ -363,13 +354,13 @@ namespace Spring.Context.Attributes
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>
@@ -387,7 +378,7 @@ namespace Spring.Context.Attributes
{
string name = Path.GetFileNameWithoutExtension(file);
if (!AssemblyLoadExclusionPredicates.Any(delegate(Predicate<string> exclude) { return exclude(name); }))
if (!AssemblyLoadExclusionPredicates.Any(exclude => exclude(name)))
{
assemblies.Add(file);
}

View File

@@ -42,21 +42,21 @@ namespace Spring.Context.Attributes
/// </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.

View File

@@ -1,110 +0,0 @@
#region License
/*
* Copyright <20> 2010-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
namespace Spring.Context.Attributes
{
/// <summary>
/// Limited extension methods reproducing the small subset of LINQ that is needed in the code; required b/c the project targets .NET 2.0 where LINQ is not available.
/// </summary>
internal static class LinqExtensionMethods
{
public static int Count<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException("source");
int counter = 0;
foreach (TSource obj in source)
{
counter++;
}
return counter;
}
internal static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value) where TSource : class
{
if (source == null) throw new ArgumentNullException("source");
foreach (TSource obj in source)
{
if (obj == value)
{
return true;
}
}
return false;
}
internal static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw new ArgumentNullException("source");
IList<TSource> results = new List<TSource>();
foreach (TSource obj in source)
{
results.Add(obj);
}
return results;
}
internal static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
Predicate<TSource> predicate)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
IList<TSource> matching = new List<TSource>();
foreach (TSource obj in source)
{
if (predicate(obj))
{
matching.Add(obj);
}
}
return matching;
}
internal static bool Any<TSource>(this IEnumerable<TSource> source, Predicate<TSource> predicate)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
foreach (TSource obj in source)
{
if (predicate(obj))
{
return true;
}
}
return false;
}
}
}

View File

@@ -20,6 +20,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.Security.Policy;
@@ -78,30 +79,12 @@ namespace Spring.Context.Attributes
private static Assembly InternalLoad(params object[] args)
{
// Easiest to query because the StackCrawlMark type is internal
/*
* TODO: cannot do it this way under .NET 2.0 b/c .First(...) relies on LINQ which we don't have (yet)
* (plan to eventually uncomment this impl once we move to .NET 3.5 or greater)
*
return (Assembly)
typeof(Assembly).GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
.First(m => m.Name == "InternalLoad" &&
m.GetParameters()[0].ParameterType == typeof (AssemblyName))
.Invoke(null, args);
*/
IEnumerable<MethodInfo> methods =
typeof(Assembly).GetMethods(BindingFlags.NonPublic | BindingFlags.Static).Where(
delegate(MethodInfo m)
{
return m.Name == "InternalLoad" &&
m.GetParameters()[0].ParameterType == typeof(AssemblyName);
});
m => m.Name == "InternalLoad" &&
m.GetParameters()[0].ParameterType == typeof (AssemblyName));
foreach (MethodInfo methodInfo in methods)
{
return (Assembly)methodInfo.Invoke(null, args);
}
return null;
return methods.Select(methodInfo => (Assembly) methodInfo.Invoke(null, args)).FirstOrDefault();
}
}
}

View File

@@ -19,6 +19,7 @@
#endregion
using System;
using System.Linq;
namespace Spring.Context.Attributes.TypeFilters
{

View File

@@ -51,8 +51,8 @@ namespace Spring.Context.Support
/// <param name="assemblyScanPath">The assembly scan path.</param>
/// <param name="assemblyPredicate">The assembly predicate.</param>
/// <param name="typePredicate">The type predicate.</param>
public static void Scan(this GenericApplicationContext context, string assemblyScanPath, Predicate<Assembly> assemblyPredicate,
Predicate<Type> typePredicate)
public static void Scan(this GenericApplicationContext context, string assemblyScanPath, Func<Assembly, bool> assemblyPredicate,
Func<Type, bool> typePredicate)
{
//create a scanner instance using the scan path
var scanner = new AssemblyObjectDefinitionScanner();
@@ -70,7 +70,7 @@ namespace Spring.Context.Support
/// <param name="context">The context.</param>
/// <param name="assemblyPredicate">The assembly predicate.</param>
/// <param name="typePredicate">The type predicate.</param>
public static void Scan(this GenericApplicationContext context, Predicate<Assembly> assemblyPredicate, Predicate<Type> typePredicate)
public static void Scan(this GenericApplicationContext context, Func<Assembly, bool> assemblyPredicate, Func<Type, bool> typePredicate)
{
Scan(context, null, assemblyPredicate, typePredicate);
}
@@ -90,9 +90,9 @@ namespace Spring.Context.Support
/// </summary>
/// <param name="context">The context.</param>
/// <param name="assemblyPredicate">The assembly predicate.</param>
public static void ScanWithAssemblyFilter(this GenericApplicationContext context, Predicate<Assembly> assemblyPredicate)
public static void ScanWithAssemblyFilter(this GenericApplicationContext context, Func<Assembly, bool> assemblyPredicate)
{
Scan(context, null, assemblyPredicate, delegate { return true; });
Scan(context, null, assemblyPredicate, obj => true);
}
/// <summary>
@@ -100,9 +100,9 @@ namespace Spring.Context.Support
/// </summary>
/// <param name="context">The context.</param>
/// <param name="typePredicate">The type predicate.</param>
public static void ScanWithTypeFilter(this GenericApplicationContext context, Predicate<Type> typePredicate)
public static void ScanWithTypeFilter(this GenericApplicationContext context, Func<Type, bool> typePredicate)
{
Scan(context, null, delegate { return true; }, typePredicate);
Scan(context, null, obj => true, typePredicate);
}

View File

@@ -195,7 +195,6 @@
<Compile Include="Context\Attributes\ImportAttribute.cs" />
<Compile Include="Context\Attributes\ImportResourceAttribute.cs" />
<Compile Include="Context\Attributes\LazyAttribute.cs" />
<Compile Include="Context\Attributes\LinqExtensionMethods.cs" />
<Compile Include="Context\Attributes\ObjectDefAttribute.cs" />
<Compile Include="Context\Attributes\ReflectionOnlyUtils.cs" />
<Compile Include="Context\Attributes\RequiredConstraintAssemblyTypeScanner.cs" />

View File

@@ -1,30 +0,0 @@
#region License
/*
* Copyright <20> 2010-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using NUnit.Framework;
namespace Spring.Context.Attributes
{
[TestFixture]
public class AssemblyObjectDefinitionScannerTests
{
}
}

View File

@@ -108,25 +108,25 @@ namespace Spring.Context.Attributes
private AssemblyObjectDefinitionScanner _scanner;
private List<Predicate<Type>> ExcludePredicates
private List<Func<Type, bool>> ExcludePredicates
{
get
{
//get at the collection of excludePredicates from the private field
//(yuck!-- test smell, but at least its wrapped up in a neat private property getter!)
return
(List<Predicate<Type>>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeExclusionPredicates"));
(List<Func<Type, bool>>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeExclusionPredicates"));
}
}
private List<Predicate<Type>> IncludePredicates
private List<Func<Type, bool>> IncludePredicates
{
get
{
//get at the collection of includePredicates from the private field
//(yuck!-- test smell, but at least its wrapped up in a neat private property getter!)
return
(List<Predicate<Type>>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeInclusionPredicates"));
(List<Func<Type, bool>>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeInclusionPredicates"));
}
}

View File

@@ -153,7 +153,6 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Context\Attributes\AbstractConfigurationClassPostProcessorTests.cs" />
<Compile Include="Context\Attributes\AssemblyObjectDefinitionScannerTests.cs" />
<Compile Include="Context\Attributes\AssemblyTypeScannerTests.cs" />
<Compile Include="Context\Attributes\CodeConfigApplicationContextTests.cs" />
<Compile Include="Context\Attributes\ConfigurationClassObjectDefinitionReaderTests.cs" />