User: xtoff
Date: 2009/11/13 10:13 AM

Modified:
 /DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/
  InterceptorSelectorTestCase.cs, ProxyGenerationOptionsTestCase.cs
 /DynamicProxy/trunk/src/Castle.DynamicProxy/
  ProxyGenerationOptions.cs, ProxyGenerator.cs
 /DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/
  BaseProxyGenerator.cs, ClassProxyGenerator.cs, InterfaceMethodGenerator.cs, 
InterfaceProxyWithTargetGenerator.cs, InterfaceProxyWithoutTargetGenerator.cs, 
MethodWithCallbackGenerator.cs

Log:
 - fixed DYNPROXY-ISSUE-122 - "interceptor selector should not affect caching"
 - some changes in naming of proxy members.
 - removed not used typeTokenCache field

File Changes:

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/
=======================================================

File [modified]: ProxyGenerationOptions.cs
Delta lines: +10 -5
===================================================================

--- DynamicProxy/trunk/src/Castle.DynamicProxy/ProxyGenerator.cs        
2009-11-13 16:46:13 UTC (rev 6321)
+++ DynamicProxy/trunk/src/Castle.DynamicProxy/ProxyGenerator.cs        
2009-11-13 17:13:55 UTC (rev 6322)
@@ -280,9 +280,11 @@
                private List<object> GetConstructorArguments(object target, 
IInterceptor[] interceptors, ProxyGenerationOptions options)
                {
                        // create constructor arguments (initialized with mixin 
implementations, interceptors and target type constructor arguments)
-                       List<object> arguments = new 
List<object>(options.MixinData.Mixins);
-                       arguments.Add(interceptors);
-                       arguments.Add(target);
+                       var arguments = new 
List<object>(options.MixinData.Mixins) { interceptors, target };
+                       if(options.Selector!=null)
+                       {
+                               arguments.Add(options.Selector);
+                       }
                        return arguments;
                }
 
@@ -940,8 +942,11 @@
 
                private List<object> 
BuildArgumentListForClassProxy(ProxyGenerationOptions options, IInterceptor[] 
interceptors)
                {
-                       List<object> arguments = new 
List<object>(options.MixinData.Mixins);
-                       arguments.Add(interceptors);
+                       var arguments = new 
List<object>(options.MixinData.Mixins) { interceptors };
+                       if (options.Selector != null)
+                       {
+                               arguments.Add(options.Selector);
+                       }
                        return arguments;
                }

File [modified]: ProxyGenerator.cs
Delta lines: +6 -4
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/InterceptorSelectorTestCase.cs 
    2009-11-13 16:46:13 UTC (rev 6321)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/InterceptorSelectorTestCase.cs 
    2009-11-13 17:13:55 UTC (rev 6322)
@@ -131,7 +131,6 @@
                }
 
                [Test]
-               [Ignore("DYNPROXY-ISSUE-122")]
                public void 
When_two_selectors_present_and_not_equal_should_cache_type_anyway()
                {
                        var options1 = new ProxyGenerationOptions { Selector = 
new AllInterceptorSelector() };
@@ -140,10 +139,13 @@
                                Selector = new 
TypeInterceptorSelector<CallCountingInterceptor>()
                        };
 
-                       var type1 = 
generator.CreateInterfaceProxyWithTargetInterface<IOne>(new One(), 
options1).GetType();
-                       var type2 = 
generator.CreateInterfaceProxyWithTargetInterface<IOne>(new One(), 
options2).GetType();
+                       var proxy1 = 
generator.CreateInterfaceProxyWithTargetInterface<IOne>(new One(), options1);
+                       var proxy2 = 
generator.CreateInterfaceProxyWithTargetInterface<IOne>(new One(), options2);
+                       proxy1.OneMethod();
+                       proxy2.OneMethod();
+                       
 
-                       Assert.AreSame(type1, type2);
+                       Assert.AreSame(proxy1.GetType(), proxy2.GetType());
                }
        }

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/
=============================================================

File [modified]: InterceptorSelectorTestCase.cs
Delta lines: +15 -0
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/ProxyGenerationOptionsTestCase.cs
  2009-11-13 16:46:13 UTC (rev 6321)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy.Tests/ProxyGenerationOptionsTestCase.cs
  2009-11-13 17:13:55 UTC (rev 6322)
@@ -173,6 +173,21 @@
                }
 
                [Test]
+               public void Equals_Compares_selectors_existence()
+               {
+                       _options1.Selector = new AllInterceptorSelector();
+                       _options2.Selector = new 
TypeInterceptorSelector<StandardInterceptor>();
+
+                       Assert.AreEqual(_options1, _options2);
+
+                       _options2.Selector = null;
+                       Assert.AreNotEqual(_options1, _options2);
+
+                       _options1.Selector = null;
+                       Assert.AreEqual(_options1, _options2);
+               }
+
+               [Test]
                public void GetHashCode_EmptyOptions()
                {

File [modified]: ProxyGenerationOptionsTestCase.cs
Delta lines: +0 -0
===================================================================

Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/
==================================================================

File [modified]: BaseProxyGenerator.cs
Delta lines: +6 -1
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/ClassProxyGenerator.cs    
    2009-11-13 16:46:13 UTC (rev 6321)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/ClassProxyGenerator.cs    
    2009-11-13 17:13:55 UTC (rev 6322)
@@ -102,6 +102,7 @@
 
                        var emitter = BuildClassEmitter(newName, targetType, 
implementedInterfaces);
                        CreateOptionsField(emitter);
+                       CreateSelectorField(emitter);
                        emitter.AddCustomAttributes(ProxyGenerationOptions);
 
 #if !SILVERLIGHT
@@ -129,8 +130,12 @@
 
                        // constructor arguments
                        constructorArguments.Add(interceptorsField);
+                       var selector = emitter.GetField("__selector");
+                       if (selector != null)
+                       {
+                               constructorArguments.Add(selector);
+                       }
 
-                       CreateInitializeCacheMethodBody(targetType, emitter, 
cctor);
                        GenerateConstructors(emitter, targetType, 
constructorArguments.ToArray());
                        GenerateParameterlessConstructor(emitter, targetType, 
interceptorsField);

File [modified]: ClassProxyGenerator.cs
Delta lines: +23 -26
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceMethodGenerator.cs
   2009-11-13 16:46:13 UTC (rev 6321)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceMethodGenerator.cs
   2009-11-13 17:13:55 UTC (rev 6322)
@@ -44,20 +44,21 @@
 
                protected override MethodEmitter 
ImplementProxiedMethod(MethodEmitter emitter, ClassEmitter @class, 
ProxyGenerationOptions options,INamingScope namingScope)
                {
-                       emitter.CopyParametersAndReturnTypeFrom(Method.Method, 
@class);
+                       var methodInfo = Method.Method;
+                       emitter.CopyParametersAndReturnTypeFrom(methodInfo, 
@class);
 
                        Type invocationType = invocation.TypeBuilder;
 
                        //TODO: can this ever happen? Should we throw instead?
-                       Trace.Assert(Method.Method.IsGenericMethod == 
invocationType.IsGenericTypeDefinition);
+                       Trace.Assert(methodInfo.IsGenericMethod == 
invocationType.IsGenericTypeDefinition);
 
                        Expression interfaceMethod;
 
                        ConstructorInfo constructor = 
invocation.Constructors[0].ConstructorBuilder;
                        Type[] genericMethodArgs = Type.EmptyTypes;
-                       string tokenFieldName = 
namingScope.GetUniqueName("token_" + Method.Method.Name);
+                       
 
-                       if (Method.Method.IsGenericMethod)
+                       if (methodInfo.IsGenericMethod)
                        {
                                // bind generic method arguments to 
invocation's type arguments
                                genericMethodArgs = 
emitter.MethodBuilder.GetGenericArguments();
@@ -66,17 +67,17 @@
                                constructor = 
TypeBuilder.GetConstructor(invocationType, constructor);
 
                                // Not in the cache: generic method
-                               interfaceMethod = new 
MethodTokenExpression(Method.Method.MakeGenericMethod(genericMethodArgs));
+                               interfaceMethod = new 
MethodTokenExpression(methodInfo.MakeGenericMethod(genericMethodArgs));
                                new 
MethodTokenExpression(Method.MethodOnTarget.MakeGenericMethod(genericMethodArgs));
 
                        }
                        else
                        {
-                               var proxiedMethodToken = 
@class.CreateStaticField(tokenFieldName, typeof(MethodInfo));
+                               var proxiedMethodToken = 
@class.CreateStaticField(namingScope.GetUniqueName("token_" + methodInfo.Name), 
typeof(MethodInfo));
                                interfaceMethod = 
proxiedMethodToken.ToExpression();
 
                                var cctor = @class.ClassConstructor;
-                               cctor.CodeBuilder.AddStatement(new 
AssignStatement(proxiedMethodToken, new MethodTokenExpression(Method.Method)));
+                               cctor.CodeBuilder.AddStatement(new 
AssignStatement(proxiedMethodToken, new MethodTokenExpression(methodInfo)));
                        }
 
 
@@ -84,11 +85,12 @@
 
                        Expression[] ctorArguments;
 
-                       if (options.Selector == null)
+                       var selector = @class.GetField("__selector");
+                       if (selector == null)
                        {
                                ctorArguments = new[]
                                {
-                                       getTargetExpression(@class, 
Method.Method),
+                                       getTargetExpression(@class, methodInfo),
                                        new 
TypeTokenExpression(Method.MethodOnTarget.DeclaringType),
                                        SelfReference.Self.ToExpression(),
                                        interceptors.ToExpression(),
@@ -100,14 +102,14 @@
                        {
                                ctorArguments = new[]
                                {
-                                       getTargetExpression(@class, 
Method.Method),
+                                       getTargetExpression(@class, methodInfo),
                                        new 
TypeTokenExpression(Method.MethodOnTarget.DeclaringType),
                                        SelfReference.Self.ToExpression(),
                                        interceptors.ToExpression(),
                                        interfaceMethod,
                                        new 
ReferencesToObjectArrayExpression(dereferencedArguments),
-                                       BuildGetSelectorInvocation(@class),
-                                       new 
AddressOfReferenceExpression(BuildMethodInterceptorsFiled(@class, 
tokenFieldName))
+                                       selector.ToExpression(),
+                                       new 
AddressOfReferenceExpression(BuildMethodInterceptorsFiled(@class, 
methodInfo,namingScope))
                                };
                        }
 
@@ -115,17 +117,17 @@
                        emitter.CodeBuilder.AddStatement(new 
AssignStatement(invocationLocal,
                                                                             
new NewInstanceExpression(constructor, ctorArguments)));
 
-                       if (Method.Method.ContainsGenericParameters)
+                       if (methodInfo.ContainsGenericParameters)
                        {
-                               EmitLoadGenricMethodArguments(emitter, 
Method.Method.MakeGenericMethod(genericMethodArgs), invocationLocal);
+                               EmitLoadGenricMethodArguments(emitter, 
methodInfo.MakeGenericMethod(genericMethodArgs), invocationLocal);
                        }
 
                        emitter.CodeBuilder.AddStatement(
                                new ExpressionStatement(new 
MethodInvocationExpression(invocationLocal, InvocationMethods.Proceed)));
 
-                       
GeneratorUtil.CopyOutAndRefParameters(dereferencedArguments, invocationLocal, 
Method.Method, emitter);
+                       
GeneratorUtil.CopyOutAndRefParameters(dereferencedArguments, invocationLocal, 
methodInfo, emitter);
 
-                       if (Method.Method.ReturnType != typeof(void))
+                       if (methodInfo.ReturnType != typeof(void))
                        {
                                // Emit code to return with cast from 
ReturnValue
                                var getRetVal = new 
MethodInvocationExpression(invocationLocal, InvocationMethods.GetReturnValue);
@@ -139,17 +141,12 @@
                        return emitter;
                }
 
-               private MethodInvocationExpression 
BuildGetSelectorInvocation(ClassEmitter @class)
+               private FieldReference 
BuildMethodInterceptorsFiled(ClassEmitter @class, MethodInfo method, 
INamingScope namingScope)
                {
-                       return new MethodInvocationExpression(
-                               @class.GetField("proxyGenerationOptions"),
-                               ProxyGenerationOptionsMethods.GetSelector) { 
VirtualCall = true };
-               }
-
-               private FieldReference 
BuildMethodInterceptorsFiled(ClassEmitter @class, string tokenFieldName)
-               {
-                       FieldReference methodInterceptors = 
@class.CreateField(string.Format("{0}_interceptors", tokenFieldName),
-                                                                              
typeof(IInterceptor[]), false);
+                       var methodInterceptors = @class.CreateField(
+                               
namingScope.GetUniqueName(string.Format("interceptors_{0}", method.Name)),
+                               typeof(IInterceptor[]),
+                               false);
 #if !SILVERLIGHT
                        
@class.DefineCustomAttributeFor<XmlIgnoreAttribute>(methodInterceptors);

File [modified]: InterfaceMethodGenerator.cs
Delta lines: +6 -2
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs
  2009-11-13 16:46:13 UTC (rev 6321)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithTargetGenerator.cs
  2009-11-13 17:13:55 UTC (rev 6322)
@@ -157,9 +157,12 @@
 
                        ctorArguments.Add(interceptorsField);
                        ctorArguments.Add(targetField);
+                       var selector = emitter.GetField("__selector");
+                       if (selector != null)
+                       {
+                               ctorArguments.Add(selector);
+                       }
 
-                       CreateInitializeCacheMethodBody(proxyTargetType, 
emitter, cctor);
-
                        GenerateConstructors(emitter, baseType, 
ctorArguments.ToArray());
 
                        // Complete type initializer code body
@@ -190,6 +193,7 @@
                        interceptorsField = CreateInterceptorsField(emitter);
 
                        CreateTargetField(emitter, proxyTargetType);
+                       CreateSelectorField(emitter);
 
                        return baseType;

File [modified]: InterfaceProxyWithTargetGenerator.cs
Delta lines: +0 -2
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs
       2009-11-13 16:46:13 UTC (rev 6321)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/InterfaceProxyWithoutTargetGenerator.cs
       2009-11-13 17:13:55 UTC (rev 6322)
@@ -78,8 +78,6 @@
 
                        var mixinFields = mixinFieldsList.ToArray();
 
-                       CreateInitializeCacheMethodBody(proxyTargetType, 
emitter, cctor);
-
                        GenerateConstructors(emitter, baseType, new 
List<FieldReference>(mixinFields) {interceptorsField, targetField}.ToArray());
 

File [modified]: InterfaceProxyWithoutTargetGenerator.cs
Delta lines: +14 -16
===================================================================

--- 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/MethodWithCallbackGenerator.cs
        2009-11-13 16:46:13 UTC (rev 6321)
+++ 
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/MethodWithCallbackGenerator.cs
        2009-11-13 17:13:55 UTC (rev 6322)
@@ -80,23 +80,19 @@
                        }
 
                        Expression proxiedMethodTokenExpression;
-                       string tokenFieldName;
                        if (methodInfo.IsGenericMethod)
                        {
                                // Not in the cache: generic method
                                MethodInfo genericMethod = 
methodInfo.MakeGenericMethod(genericMethodArgs);
 
-                               tokenFieldName = 
namingScope.GetUniqueName("token_" + methodInfo.Name);
                                proxiedMethodTokenExpression = new 
MethodTokenExpression(genericMethod);
                        }
                        else
                        {
-
-                               var methodTokenField = 
@class.CreateStaticField(namingScope.GetUniqueName("token_" + methodInfo.Name),
-                                                                               
typeof (MethodInfo));
-                               
@class.ClassConstructor.CodeBuilder.AddStatement(new 
AssignStatement(methodTokenField, new MethodTokenExpression(methodInfo)));
-                               tokenFieldName = 
methodTokenField.Reference.Name;
-                               proxiedMethodTokenExpression = 
methodTokenField.ToExpression();
+                               var tokenField = 
@class.CreateStaticField(namingScope.GetUniqueName("token_" + methodInfo.Name), 
typeof(MethodInfo));
+                               
@class.ClassConstructor.CodeBuilder.AddStatement(new 
AssignStatement(tokenField, new MethodTokenExpression(methodInfo)));
+                               
+                               proxiedMethodTokenExpression = 
tokenField.ToExpression();
                        }
 
                        LocalReference invocationImplLocal = 
emitter.CodeBuilder.DeclareLocal(iinvocation);
@@ -113,7 +109,9 @@
                        Type targetType = methodInfo.DeclaringType;
                        Debug.Assert(targetType != null, "targetType != null");
                        Expression[] ctorArgs;
-                       if (options.Selector == null)
+
+                       var selector = @class.GetField("__selector");
+                       if (selector == null)
                        {
                                ctorArgs = new[]
                                {
@@ -135,10 +133,8 @@
                                        interceptors.ToExpression(),
                                        proxiedMethodTokenExpression,
                                        new 
ReferencesToObjectArrayExpression(dereferencedArguments),
-                                       new 
MethodInvocationExpression(@class.GetField("proxyGenerationOptions"),
-                                                                      
ProxyGenerationOptionsMethods.GetSelector)
-                                       { VirtualCall = true },
-                                       new 
AddressOfReferenceExpression(BuildMethodInterceptorsFiled(@class, 
tokenFieldName))
+                                       selector.ToExpression(),
+                                       new 
AddressOfReferenceExpression(BuildMethodInterceptorsField(@class, 
methodInfo,namingScope))
                                };
                        }
 
@@ -172,10 +168,12 @@
                        return emitter;
                }
 
-               private FieldReference 
BuildMethodInterceptorsFiled(ClassEmitter @class, string tokenFieldName)
+               private FieldReference 
BuildMethodInterceptorsField(ClassEmitter @class, MethodInfo method, 
INamingScope namingScope)
                {
-                       var methodInterceptors = 
@class.CreateField(string.Format("{0}_interceptors", tokenFieldName),
-                                                                   typeof 
(IInterceptor[]), false);
+                       var methodInterceptors = @class.CreateField(
+                               
namingScope.GetUniqueName(string.Format("interceptors_{0}", method.Name)),
+                               typeof(IInterceptor[]),
+                               false);
 
 #if !SILVERLIGHT

File [modified]: MethodWithCallbackGenerator.cs
Delta lines: +2 -4
===================================================================

--- DynamicProxy/trunk/src/Castle.DynamicProxy/ProxyGenerationOptions.cs        
2009-11-13 16:46:13 UTC (rev 6321)
+++ DynamicProxy/trunk/src/Castle.DynamicProxy/ProxyGenerationOptions.cs        
2009-11-13 17:13:55 UTC (rev 6322)
@@ -15,12 +15,10 @@
 namespace Castle.DynamicProxy
 {
        using System;
-       using System.Collections;
        using System.Collections.Generic;
        using System.Reflection.Emit;
        using System.Runtime.Serialization;
        using Castle.Core.Interceptor;
-       using Castle.DynamicProxy.Generators;
 
 #if SILVERLIGHT
        public class ProxyGenerationOptions
@@ -169,7 +167,7 @@
                        proxyGenerationOptions.Initialize();
 
                        if (!Equals(Hook, proxyGenerationOptions.Hook)) return 
false;
-                       if (!Equals(Selector, proxyGenerationOptions.Selector)) 
return false;
+                       if (!Equals(Selector == null, 
proxyGenerationOptions.Selector == null )) return false;
                        if (!Equals(MixinData, 
proxyGenerationOptions.MixinData)) return false;
                        if (!Equals(BaseTypeForInterfaceProxy, 
proxyGenerationOptions.BaseTypeForInterfaceProxy)) return false;
                        return true;
@@ -181,7 +179,7 @@
                        Initialize();
 
                        int result = Hook != null ? 
Hook.GetType().GetHashCode() : 0;
-                       result = 29 * result + (Selector != null ? 
Selector.GetHashCode() : 0);
+                       result = 29 * result + (Selector != null ? 1 : 0);
                        result = 29 * result + MixinData.GetHashCode();
                        result = 29 * result + (BaseTypeForInterfaceProxy != 
null ? BaseTypeForInterfaceProxy.GetHashCode() : 0);

--

You received this message because you are subscribed to the Google Groups 
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-commits?hl=.


Reply via email to