SPRNETCODECONFIG-3 change Spring-Assembly exclusion approach to prefix vs absolute match to ease in long-term maintenance issues

This commit is contained in:
Steve Bohlen
2013-01-13 19:31:46 -05:00
parent de4f7d6e1f
commit 5a0e1c483f
2 changed files with 38 additions and 51 deletions

View File

@@ -35,28 +35,20 @@ namespace Spring.Context.Attributes
{
private readonly List<Func<Assembly, bool>> _assemblyExclusionPredicates = new List<Func<Assembly, bool>>();
private readonly IList<string> _springAssemblies = new List<string>()
{
"Spring.Core",
"Spring.Core.Configuration",
"Spring.Aop",
"Spring.Data",
"Spring.Services",
"Spring.Messaging",
"Spring.Messaging.Ems",
"Spring.Messaging.Nms",
"Spring.Template.Velocity",
"Spring.Messaging.Quartz",
"Spring.Testing.Microsoft",
"Spring.Testing.Nunit",
"Spring.Data.NHibernate12",
"Spring.Data.NHibernate21",
"Spring.Data.NHibernate20",
"Spring.Data.NHibernate30",
"Spring.Web",
"Spring.Web.Extensions",
"Spring.Web.Mvc",
};
private readonly IList<string> _springAssemblyExcludePrefixes = new List<string>()
{
"Spring.",
"NHibernate.",
"Common.Logging.",
"log4net",
"Quartz"
};
//TODO: HACK -- required to permit testing since testing assy also starts with excluded name "Spring."
private readonly IList<string> _springAssemblyIncludeNames = new List<string>()
{
"Spring.Core.Tests"
};
private IObjectNameGenerator _objectNameGenerator = new AttributeObjectNameGenerator();
@@ -122,7 +114,7 @@ namespace Spring.Context.Attributes
try
{
return Attribute.GetCustomAttribute(type, typeof(ComponentAttribute), true) != null &&
!type.IsAbstract;
!type.IsAbstract;
}
catch (AmbiguousMatchException)
{
@@ -135,7 +127,7 @@ namespace Spring.Context.Attributes
foreach (CustomAttributeData customAttributeData in CustomAttributeData.GetCustomAttributes(type))
{
if (customAttributeData.Constructor.DeclaringType != null &&
if (customAttributeData.Constructor.DeclaringType != null &&
(customAttributeData.Constructor.DeclaringType.FullName == typeof(ComponentAttribute).FullName &&
!type.IsAbstract))
{
@@ -156,7 +148,9 @@ namespace Spring.Context.Attributes
base.SetDefaultFilters();
//add the desired assembly exclusions to the list
_assemblyExclusionPredicates.Add(a => _springAssemblies.Contains(a.GetName().Name));
_assemblyExclusionPredicates.Add(a => _springAssemblyExcludePrefixes.Any(n => n.StartsWith(a.GetName().Name)));
//_assemblyExclusionPredicates.Add(a => a.GetName().Name.StartsWith(_springAssemblyPrefix));
//_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");
@@ -178,7 +172,9 @@ namespace Spring.Context.Attributes
/// </summary>
public AssemblyObjectDefinitionScanner()
{
AssemblyLoadExclusionPredicates.Add(name => _springAssemblies.Contains(name));
//AssemblyLoadExclusionPredicates.Add(name => _springAssemblyExcludePrefixes.Any(n => name.StartsWith(n) && !name.Contains(".Tests")));
AssemblyLoadExclusionPredicates.Add(candidate => _springAssemblyExcludePrefixes.Any(excludeName => candidate.StartsWith(excludeName) && _springAssemblyIncludeNames.All(includeName => includeName != candidate)));
//AssemblyLoadExclusionPredicates.Add(name => _springAssemblies.Contains(name));
AssemblyLoadExclusionPredicates.Add(name => name.StartsWith("System."));
AssemblyLoadExclusionPredicates.Add(name => name.StartsWith("Microsoft."));
AssemblyLoadExclusionPredicates.Add(name => name == "mscorlib");

View File

@@ -45,7 +45,7 @@ namespace Spring.Context.Attributes
public void AssemblyHavingType_T_Adds_Assembly()
{
_scanner.AssemblyHavingType<IOrdered>();
Assert.That(TypeSources.Any(t => t.Contains(typeof (IOrdered))));
Assert.That(TypeSources.Any(t => t.Contains(typeof(IOrdered))));
}
[Test]
@@ -54,8 +54,8 @@ namespace Spring.Context.Attributes
_scanner.IncludeType<IOrdered>();
_scanner.IncludeType<IPriorityOrdered>();
IncludePredicates.Any(p => p(typeof (IOrdered)));
IncludePredicates.Any(p => p(typeof (IPriorityOrdered)));
IncludePredicates.Any(p => p(typeof(IOrdered)));
IncludePredicates.Any(p => p(typeof(IPriorityOrdered)));
}
[Test]
@@ -66,23 +66,14 @@ namespace Spring.Context.Attributes
_scanner.IncludeType<TheConfigurationClass>();
_scanner.IncludeType<TheImportedConfigurationClass>();
_scanner.WithExcludeFilter(t => t.Name.StartsWith("TheImported"));
try
{
IEnumerable<Type> types = _scanner.Scan();
IEnumerable<Type> types = _scanner.Scan();
//Assert.That(types.Any(t => t.Name == "TheConfigurationClass"));
//Assert.False(types.Any(t => t.Name == "TheImportedConfigurationClass"));
//Assert.That(types.Any(t => t.Name == "TheConfigurationClass"));
//Assert.False(types.Any(t => t.Name == "TheImportedConfigurationClass"));
Assert.That(types, Contains.Item((typeof (TheConfigurationClass))));
Assert.False(types.Contains(typeof (TheImportedConfigurationClass)));
} catch (ReflectionTypeLoadException e)
{
//Console.WriteLine(e);
foreach (var ee in e.LoaderExceptions)
{
//Console.WriteLine(ee);
}
}
Assert.That(types, Contains.Item((typeof(TheConfigurationClass))));
Assert.False(types.Contains(typeof(TheImportedConfigurationClass)));
}
[Test]
@@ -91,10 +82,10 @@ namespace Spring.Context.Attributes
_scanner.WithIncludeFilter(t => t.Name.Contains("ConfigurationClass"));
IEnumerable<Type> types = _scanner.Scan();
Assert.That(types, Contains.Item((typeof (TheConfigurationClass))));
Assert.That(types, Contains.Item((typeof (TheImportedConfigurationClass))));
Assert.That(types.Count(),Is.EqualTo(2));
Assert.That(types, Contains.Item((typeof(TheConfigurationClass))));
Assert.That(types, Contains.Item((typeof(TheImportedConfigurationClass))));
Assert.That(types.Count(), Is.EqualTo(2));
}
[Serializable]
@@ -115,7 +106,7 @@ namespace Spring.Context.Attributes
//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<Func<Type, bool>>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeExclusionPredicates"));
(List<Func<Type, bool>>)(ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeExclusionPredicates"));
}
}
@@ -126,7 +117,7 @@ namespace Spring.Context.Attributes
//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<Func<Type, bool>>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeInclusionPredicates"));
(List<Func<Type, bool>>)(ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeInclusionPredicates"));
}
}
@@ -136,7 +127,7 @@ namespace Spring.Context.Attributes
{
//get at the collection of typeSources from the private field
//(yuck!-- test smell, but at least its wrapped up in a neat private property getter!)
return (List<IEnumerable<Type>>) (ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeSources"));
return (List<IEnumerable<Type>>)(ReflectionUtils.GetInstanceFieldValue(_scanner, "TypeSources"));
}
}
}