Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Util
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv31005/src/Spring/Spring.Core/Util
Modified Files:
AttributeUtils.cs SafeAttributeUtils.cs
Log Message:
misc cleanup
updates AttibuteUtils
Index: AttributeUtils.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Util/AttributeUtils.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** AttributeUtils.cs 11 Oct 2007 05:57:25 -0000 1.1
--- AttributeUtils.cs 16 Oct 2007 00:28:49 -0000 1.2
***************
*** 37,41 ****
/// <author>Stan Dvoychenko</author>
/// <version>$Id$</version>
! internal static class AttributeUtils
{
//TODO: (SD) change back to private when SafeReflectionUtils2 is not required.
--- 37,41 ----
/// <author>Stan Dvoychenko</author>
/// <version>$Id$</version>
! public static class AttributeUtils
{
//TODO: (SD) change back to private when SafeReflectionUtils2 is not required.
***************
*** 47,50 ****
--- 47,57 ----
ParameterInfo parameterInfo);
+ /// <summary>
+ /// Provides array of <see cref="CustomAttributeBuilder"/> based onto the
+ /// attributes applied to the target type.
+ /// </summary>
+ public delegate CustomAttributeBuilder[] CustomAttributeBuildersProvider
+ (MemberInfo target, IList attributeTypesToExclude);
+
//TODO: (SD) change back to private when SafeReflectionUtils2 is not required.
internal static ParameterBuilder CreateMethodParameter(MethodBuilder methodBuilder,
***************
*** 137,145 ****
if (attributeTypesToExclude != null)
{
! // Welcome to the stone age!!!(SD)
! attributeTypesToExcludeList = new List<Type>();
! foreach (Type type in attributeTypesToExclude)
{
! attributeTypesToExcludeList.Add(type);
}
}
--- 144,155 ----
if (attributeTypesToExclude != null)
{
! lock (attributeTypesToExclude.SyncRoot) // reader lock would be enough (SD)
{
! // Welcome to the stone age!!!(SD)
! attributeTypesToExcludeList = new List<Type>();
! foreach (Type type in attributeTypesToExclude)
! {
! attributeTypesToExcludeList.Add(type);
! }
}
}
***************
*** 215,233 ****
}
- //public static Attribute GetCustomAttributeBuilderAttribute(
- // CustomAttributeBuilder attrBuilder)
- //{
- // AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(
- // Guid.NewGuid().ToString()), AssemblyBuilderAccess.Run);
- // ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("module");
- // TypeBuilder typeBuilder = moduleBuilder.DefineType("temptype");
- // typeBuilder.SetCustomAttribute(attrBuilder);
-
- // Type type = typeBuilder.CreateType();
-
- // Attribute attribute = type.GetCustomAttributes(false)[0];
-
- //}
-
private static CustomAttributeBuilder CreateCustomAttributeBuilder(CustomAttributeData attributeData)
{
--- 225,228 ----
***************
*** 270,274 ****
propertiesToSet.Add(attributeProperties[j]);
namedParameterValues[k++] = namedArgument.TypedValue.Value;
! //TODO: Check for the type can be applied (SD)
break;
}
--- 265,275 ----
propertiesToSet.Add(attributeProperties[j]);
namedParameterValues[k++] = namedArgument.TypedValue.Value;
!
! // The bellow few lines are only for testing purposes
! //if (namedArgument.TypedValue.ArgumentType == typeof(String))
! //{
! // namedParameterValues[k - 1] = "";
! //}
!
break;
}
Index: SafeAttributeUtils.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Util/SafeAttributeUtils.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** SafeAttributeUtils.cs 11 Oct 2007 05:57:25 -0000 1.1
--- SafeAttributeUtils.cs 16 Oct 2007 00:28:49 -0000 1.2
***************
*** 22,28 ****
--- 22,30 ----
using System;
+ using System.Globalization;
using System.Reflection.Emit;
using System.Reflection;
using System.Collections;
+ using Common.Logging;
namespace Spring.Util
***************
*** 31,38 ****
/// Utility class to discover and apply attributes to types, methods and parameters
/// </summary>
/// <author>Stan Dvoychenko</author>
/// <version>$Id$</version>
! internal static class SafeAttributeUtils
{
/// <summary>
--- 33,54 ----
/// Utility class to discover and apply attributes to types, methods and parameters
/// </summary>
+ /// <remarks>SafeAttributeUtils is only a wrapper class for the
+ /// AttributeUtils for catching and swallowing any exceptions coming from it, in
+ /// which case it prevents exception from bubling transforming it
+ /// either into the boolean success flag for void methods or to the
+ /// null for collection/arrays return type of methods. This gives an
+ /// opportunity to the caller then to apply the old approach.</remarks>
/// <author>Stan Dvoychenko</author>
/// <version>$Id$</version>
! public static class SafeAttributeUtils
{
+ #region Log Setup
+ /// <summary>
+ /// The shared <see cref="Common.Logging.ILog"/> instance for this class (and derived classes).
+ /// </summary>
+ static readonly ILog log = LogManager.GetLogger(typeof(SafeAttributeUtils));
+ #endregion
+
+ #region Wrapper methods
/// <summary>
***************
*** 57,67 ****
return true;
}
! catch (InvalidOperationException)
{
! //TODO: (SD) Log but continue
}
return false;
}
/// <summary>
/// Gets the assembly attributes.
--- 73,84 ----
return true;
}
! catch (ArgumentException ex)
{
! HandleExceptionFromAttributeUtils(targetType, ex);
}
return false;
}
+
/// <summary>
/// Gets the assembly attributes.
***************
*** 79,85 ****
});
}
! catch (InvalidOperationException)
{
! //TODO: (SD) Log but continue
}
return null;
--- 96,102 ----
});
}
! catch (ArgumentException ex)
{
! HandleExceptionFromAttributeUtils(target, ex);
}
return null;
***************
*** 102,108 ****
});
}
! catch (InvalidOperationException)
{
! //TODO: (SD) Log but continue
}
return null;
--- 119,125 ----
});
}
! catch (ArgumentException ex)
{
! HandleExceptionFromAttributeUtils(target, ex);
}
return null;
***************
*** 133,139 ****
return AttributeUtils.GetMemberAttributes(target, attributeTypesToExclude);
}
! catch (InvalidOperationException)
{
! //TODO: (SD) Log but continue
}
return null;
--- 150,156 ----
return AttributeUtils.GetMemberAttributes(target, attributeTypesToExclude);
}
! catch (ArgumentException ex)
{
! HandleExceptionFromAttributeUtils(target, ex);
}
return null;
***************
*** 155,161 ****
});
}
! catch (InvalidOperationException)
{
! //TODO: (SD) Log but continue
}
return null;
--- 172,178 ----
});
}
! catch (ArgumentException ex)
{
! HandleExceptionFromAttributeUtils(target, ex);
}
return null;
***************
*** 163,168 ****
- #region Second circle of functional encapsulation
-
/// <summary>
/// Applies the method parameter attributes.
--- 180,183 ----
***************
*** 210,216 ****
return true;
}
! catch (Exception) //TODO: Identify the specific excepion (SD)
{
! // TODO: (SD) Log about it, but continue
}
return false;
--- 225,231 ----
return true;
}
! catch (ArgumentException ex)
{
! HandleExceptionFromAttributeUtils(parameter, ex);
}
return false;
***************
*** 218,221 ****
--- 233,289 ----
#endregion
+
+ #region Utility Methods
+
+ //TODO: (SD) For all those methods add more details about the target!
+
+ private static void HandleExceptionFromAttributeUtils(MemberInfo target, Exception ex)
+ {
+ HandleExceptionFromAttributeUtils(
+ ((target != null) ? target.Name : "null"), ex);
+ }
+ private static void HandleExceptionFromAttributeUtils(ParameterInfo target, Exception ex)
+ {
+ HandleExceptionFromAttributeUtils(
+ ((target != null) ? target.Name : "null"), ex);
+ }
+ private static void HandleExceptionFromAttributeUtils(Assembly target, Exception ex)
+ {
+ HandleExceptionFromAttributeUtils(
+ ((target != null) ? target.FullName : "null"), ex);
+ }
+ private static void HandleExceptionFromAttributeUtils(Module target, Exception ex)
+ {
+ HandleExceptionFromAttributeUtils(
+ ((target != null) ? target.FullyQualifiedName : "null"), ex);
+ }
+ private static void HandleExceptionFromAttributeUtils(MethodInfo target, Exception ex)
+ {
+ HandleExceptionFromAttributeUtils(
+ ((target != null) ? target.Name : "null"), ex);
+ }
+
+ /// <summary>
+ /// Handles the exception from <see cref="AttributeUtils"/>
+ /// </summary>
+ /// <param name="targetDetails">The target details.</param>
+ /// <param name="ex">The ex.</param>
+ private static void HandleExceptionFromAttributeUtils(string targetDetails, Exception ex)
+ {
+ // Unfortunately, .NET is throwing the ArgumentException for
+ // current issues with System.Enum and WebService attributes,
+ // ArgumentNull, ArgumentOutOfRange exceptions inherit from it
+ // which requires then a bit of extra code to rethrow those
+ // and any other that inherit from ArgumentException here.
+ if (ex.GetType() != typeof(ArgumentException))
+ throw ex;
+
+ log.Warn(String.Format(CultureInfo.InvariantCulture,
+ "Exception during the executing operation via CustomAttributeData for target {0}." +
+ " Falling back to the old method.",
+ targetDetails, ex));
+ }
+
+ #endregion
}
}
|