From de4f7d6e1f54cab69562050bfd2b008c901dc918 Mon Sep 17 00:00:00 2001 From: Steve Bohlen Date: Sun, 13 Jan 2013 18:53:41 -0500 Subject: [PATCH] SPRNET-1536 replace Predicate with Func and replace custsom LINQ extensions with actual LINQ now that we're on .NET 3.5 or later --- .../AssemblyObjectDefinitionScanner.cs | 34 +++--- .../Context/Attributes/AssemblyTypeScanner.cs | 49 ++++---- .../Attributes/IAssemblyTypeScanner.cs | 6 +- .../Attributes/LinqExtensionMethods.cs | 110 ------------------ .../Context/Attributes/ReflectionOnlyUtils.cs | 25 +--- .../TypeFilters/AssignableTypeFilter.cs | 1 + .../GenericApplicationContextExtensions.cs | 14 +-- .../Spring.Core/Spring.Core.2010.csproj | 1 - .../AssemblyObjectDefinitionScannerTests.cs | 30 ----- .../Attributes/AssemblyTypeScannerTests.cs | 8 +- .../Spring.Core.Tests.2010.csproj | 1 - 11 files changed, 56 insertions(+), 223 deletions(-) delete mode 100644 src/Spring/Spring.Core/Context/Attributes/LinqExtensionMethods.cs delete mode 100644 test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyObjectDefinitionScannerTests.cs diff --git a/src/Spring/Spring.Core/Context/Attributes/AssemblyObjectDefinitionScanner.cs b/src/Spring/Spring.Core/Context/Attributes/AssemblyObjectDefinitionScanner.cs index 5618aa93..cb86ef5a 100644 --- a/src/Spring/Spring.Core/Context/Attributes/AssemblyObjectDefinitionScanner.cs +++ b/src/Spring/Spring.Core/Context/Attributes/AssemblyObjectDefinitionScanner.cs @@ -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> _assemblyExclusionPredicates = new List>(); + private readonly List> _assemblyExclusionPredicates = new List>(); private readonly IList _springAssemblies = new List() { @@ -92,8 +93,7 @@ namespace Spring.Context.Attributes /// protected override IEnumerable ApplyAssemblyFiltersTo(IEnumerable assemblyCandidates) { - return assemblyCandidates.Where( - delegate(Assembly candidate) { return IsIncludedAssembly(candidate) && !IsExcludedAssembly(candidate); }); + return assemblyCandidates.Where(candidate => IsIncludedAssembly(candidate) && !IsExcludedAssembly(candidate)); } /// @@ -105,7 +105,7 @@ namespace Spring.Context.Attributes /// protected virtual bool IsExcludedAssembly(Assembly candidate) { - return _assemblyExclusionPredicates.Any(delegate(Predicate exclude) { return exclude(candidate); }); + return _assemblyExclusionPredicates.Any(exclude => exclude(candidate)); } /// @@ -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"); } /// @@ -178,11 +178,11 @@ namespace Spring.Context.Attributes /// 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"); } } diff --git a/src/Spring/Spring.Core/Context/Attributes/AssemblyTypeScanner.cs b/src/Spring/Spring.Core/Context/Attributes/AssemblyTypeScanner.cs index d41bedab..e7818624 100644 --- a/src/Spring/Spring.Core/Context/Attributes/AssemblyTypeScanner.cs +++ b/src/Spring/Spring.Core/Context/Attributes/AssemblyTypeScanner.cs @@ -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 /// /// Names of Assemblies to exclude from being loaded for scanning. /// - protected IList> AssemblyLoadExclusionPredicates = new List>(); + protected IList> AssemblyLoadExclusionPredicates = new List>(); /// /// Assembly Inclusion Predicates. /// - protected readonly List> AssemblyInclusionPredicates = new List>(); + protected readonly List> AssemblyInclusionPredicates = new List>(); /// /// Type Exclusion Predicates. /// - protected readonly List> TypeExclusionPredicates = new List>(); + protected readonly List> TypeExclusionPredicates = new List>(); /// /// Type Exclusion Predicates. @@ -63,7 +64,7 @@ namespace Spring.Context.Attributes /// /// Type Inclusion Predicates. /// - protected readonly List> TypeInclusionPredicates = new List>(); + protected readonly List> TypeInclusionPredicates = new List>(); /// /// Type Inclusion TypeFilters. @@ -105,7 +106,7 @@ namespace Spring.Context.Attributes /// public IAssemblyTypeScanner ExcludeType() { - 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 /// public IAssemblyTypeScanner IncludeType() { - 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 /// /// The assembly predicate. /// - public IAssemblyTypeScanner WithAssemblyFilter(Predicate assemblyPredicate) + public IAssemblyTypeScanner WithAssemblyFilter(Func assemblyPredicate) { AssemblyInclusionPredicates.Add(assemblyPredicate); return this; @@ -179,7 +180,7 @@ namespace Spring.Context.Attributes /// /// The predicate. /// - public IAssemblyTypeScanner WithExcludeFilter(Predicate predicate) + public IAssemblyTypeScanner WithExcludeFilter(Func predicate) { TypeExclusionPredicates.Add(predicate); return this; @@ -203,7 +204,7 @@ namespace Spring.Context.Attributes /// /// The predicate. /// - public IAssemblyTypeScanner WithIncludeFilter(Predicate predicate) + public IAssemblyTypeScanner WithIncludeFilter(Func predicate) { TypeInclusionPredicates.Add(predicate); return this; @@ -314,15 +315,10 @@ namespace Spring.Context.Attributes /// protected virtual bool IsExcludedType(Type type) { - if (TypeExclusionPredicates.Count > 0 && TypeExclusionPredicates.Any(delegate(Predicate 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)); } /// @@ -334,7 +330,7 @@ namespace Spring.Context.Attributes /// protected virtual bool IsIncludedAssembly(Assembly assembly) { - return AssemblyInclusionPredicates.Any(delegate(Predicate include) { return include(assembly); }); + return AssemblyInclusionPredicates.Any(include => include(assembly)); } /// @@ -346,15 +342,10 @@ namespace Spring.Context.Attributes /// protected virtual bool IsIncludedType(Type type) { - if (TypeInclusionPredicates.Count > 0 && TypeInclusionPredicates.Any(delegate(Predicate 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)); } /// @@ -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); } /// @@ -387,7 +378,7 @@ namespace Spring.Context.Attributes { string name = Path.GetFileNameWithoutExtension(file); - if (!AssemblyLoadExclusionPredicates.Any(delegate(Predicate exclude) { return exclude(name); })) + if (!AssemblyLoadExclusionPredicates.Any(exclude => exclude(name))) { assemblies.Add(file); } diff --git a/src/Spring/Spring.Core/Context/Attributes/IAssemblyTypeScanner.cs b/src/Spring/Spring.Core/Context/Attributes/IAssemblyTypeScanner.cs index ff75cb14..09684f5a 100644 --- a/src/Spring/Spring.Core/Context/Attributes/IAssemblyTypeScanner.cs +++ b/src/Spring/Spring.Core/Context/Attributes/IAssemblyTypeScanner.cs @@ -42,21 +42,21 @@ namespace Spring.Context.Attributes /// /// The assembly predicate. /// - IAssemblyTypeScanner WithAssemblyFilter(Predicate assemblyPredicate); + IAssemblyTypeScanner WithAssemblyFilter(Func assemblyPredicate); /// /// Adds the predicte to the include filter for . /// /// The predicate. /// - IAssemblyTypeScanner WithIncludeFilter(Predicate predicate); + IAssemblyTypeScanner WithIncludeFilter(Func predicate); /// /// Adds the predicte to the exclude filter for . /// /// The predicate. /// - IAssemblyTypeScanner WithExcludeFilter(Predicate predicate); + IAssemblyTypeScanner WithExcludeFilter(Func predicate); /// /// Includes the specific types. diff --git a/src/Spring/Spring.Core/Context/Attributes/LinqExtensionMethods.cs b/src/Spring/Spring.Core/Context/Attributes/LinqExtensionMethods.cs deleted file mode 100644 index d30b0123..00000000 --- a/src/Spring/Spring.Core/Context/Attributes/LinqExtensionMethods.cs +++ /dev/null @@ -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 -{ - /// - /// 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. - /// - internal static class LinqExtensionMethods - { - public static int Count(this IEnumerable source) - { - if (source == null) throw new ArgumentNullException("source"); - - int counter = 0; - foreach (TSource obj in source) - { - counter++; - } - - return counter; - } - - internal static bool Contains(this IEnumerable 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 AsEnumerable(this IEnumerable source) - { - if (source == null) throw new ArgumentNullException("source"); - - IList results = new List(); - - foreach (TSource obj in source) - { - results.Add(obj); - } - - return results; - } - - - internal static IEnumerable Where(this IEnumerable source, - Predicate predicate) - { - if (source == null) throw new ArgumentNullException("source"); - if (predicate == null) throw new ArgumentNullException("predicate"); - - IList matching = new List(); - - foreach (TSource obj in source) - { - if (predicate(obj)) - { - matching.Add(obj); - } - } - - return matching; - } - - - internal static bool Any(this IEnumerable source, Predicate 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; - } - } -} \ No newline at end of file diff --git a/src/Spring/Spring.Core/Context/Attributes/ReflectionOnlyUtils.cs b/src/Spring/Spring.Core/Context/Attributes/ReflectionOnlyUtils.cs index 815bf61b..7fef34bd 100644 --- a/src/Spring/Spring.Core/Context/Attributes/ReflectionOnlyUtils.cs +++ b/src/Spring/Spring.Core/Context/Attributes/ReflectionOnlyUtils.cs @@ -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 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(); } } } \ No newline at end of file diff --git a/src/Spring/Spring.Core/Context/Attributes/TypeFilters/AssignableTypeFilter.cs b/src/Spring/Spring.Core/Context/Attributes/TypeFilters/AssignableTypeFilter.cs index 26919208..2c073144 100644 --- a/src/Spring/Spring.Core/Context/Attributes/TypeFilters/AssignableTypeFilter.cs +++ b/src/Spring/Spring.Core/Context/Attributes/TypeFilters/AssignableTypeFilter.cs @@ -19,6 +19,7 @@ #endregion using System; +using System.Linq; namespace Spring.Context.Attributes.TypeFilters { diff --git a/src/Spring/Spring.Core/Context/Extension/GenericApplicationContextExtensions.cs b/src/Spring/Spring.Core/Context/Extension/GenericApplicationContextExtensions.cs index 99c5789e..0cfd2f58 100644 --- a/src/Spring/Spring.Core/Context/Extension/GenericApplicationContextExtensions.cs +++ b/src/Spring/Spring.Core/Context/Extension/GenericApplicationContextExtensions.cs @@ -51,8 +51,8 @@ namespace Spring.Context.Support /// The assembly scan path. /// The assembly predicate. /// The type predicate. - public static void Scan(this GenericApplicationContext context, string assemblyScanPath, Predicate assemblyPredicate, - Predicate typePredicate) + public static void Scan(this GenericApplicationContext context, string assemblyScanPath, Func assemblyPredicate, + Func typePredicate) { //create a scanner instance using the scan path var scanner = new AssemblyObjectDefinitionScanner(); @@ -70,7 +70,7 @@ namespace Spring.Context.Support /// The context. /// The assembly predicate. /// The type predicate. - public static void Scan(this GenericApplicationContext context, Predicate assemblyPredicate, Predicate typePredicate) + public static void Scan(this GenericApplicationContext context, Func assemblyPredicate, Func typePredicate) { Scan(context, null, assemblyPredicate, typePredicate); } @@ -90,9 +90,9 @@ namespace Spring.Context.Support /// /// The context. /// The assembly predicate. - public static void ScanWithAssemblyFilter(this GenericApplicationContext context, Predicate assemblyPredicate) + public static void ScanWithAssemblyFilter(this GenericApplicationContext context, Func assemblyPredicate) { - Scan(context, null, assemblyPredicate, delegate { return true; }); + Scan(context, null, assemblyPredicate, obj => true); } /// @@ -100,9 +100,9 @@ namespace Spring.Context.Support /// /// The context. /// The type predicate. - public static void ScanWithTypeFilter(this GenericApplicationContext context, Predicate typePredicate) + public static void ScanWithTypeFilter(this GenericApplicationContext context, Func typePredicate) { - Scan(context, null, delegate { return true; }, typePredicate); + Scan(context, null, obj => true, typePredicate); } diff --git a/src/Spring/Spring.Core/Spring.Core.2010.csproj b/src/Spring/Spring.Core/Spring.Core.2010.csproj index 2e67ede7..47e1506a 100644 --- a/src/Spring/Spring.Core/Spring.Core.2010.csproj +++ b/src/Spring/Spring.Core/Spring.Core.2010.csproj @@ -195,7 +195,6 @@ - diff --git a/test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyObjectDefinitionScannerTests.cs b/test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyObjectDefinitionScannerTests.cs deleted file mode 100644 index 06fc43ce..00000000 --- a/test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyObjectDefinitionScannerTests.cs +++ /dev/null @@ -1,30 +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 NUnit.Framework; - -namespace Spring.Context.Attributes -{ - [TestFixture] - public class AssemblyObjectDefinitionScannerTests - { - - } -} diff --git a/test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyTypeScannerTests.cs b/test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyTypeScannerTests.cs index 69bedb7b..f7e2bab7 100644 --- a/test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyTypeScannerTests.cs +++ b/test/Spring/Spring.Core.Tests/Context/Attributes/AssemblyTypeScannerTests.cs @@ -108,25 +108,25 @@ namespace Spring.Context.Attributes private AssemblyObjectDefinitionScanner _scanner; - private List> ExcludePredicates + private List> 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>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeExclusionPredicates")); + (List>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeExclusionPredicates")); } } - private List> IncludePredicates + private List> 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>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeInclusionPredicates")); + (List>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeInclusionPredicates")); } } diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj index 04a141ef..990c40ae 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.2010.csproj @@ -153,7 +153,6 @@ Code -