Permalink
Browse files

SPRNET-1536 replace Predicate<T> with Func<T, bool> and replace custs…

…om LINQ extensions with actual LINQ now that we're on .NET 3.5 or later
  • Loading branch information...
1 parent e4a59c6 commit de4f7d6e1f54cab69562050bfd2b008c901dc918 @sbohlen sbohlen committed Jan 13, 2013
@@ -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 @@ 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>
@@ -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>
@@ -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;
@@ -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>
@@ -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");
}
}
@@ -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 @@ 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.
@@ -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.
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
@@ -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;
@@ -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;
@@ -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>
@@ -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>
@@ -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>
@@ -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>
@@ -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);
}
@@ -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.
@@ -1,110 +0,0 @@
-#region License
-
-/*
- * Copyright © 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;
- }
- }
-}
Oops, something went wrong. Retry.

0 comments on commit de4f7d6

Please sign in to comment.