User: xtoff
Date: 2009/11/15 03:02 PM
Added:
/DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/
CompositeTypeContributor.cs
Modified:
/DynamicProxy/trunk/src/Castle.DynamicProxy/
Castle.DynamicProxy-vs2008.csproj
/DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/
ClassProxyTargetContributor.cs, InterfaceProxyTargetContributor.cs
/DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/
ClassProxyGenerator.cs
Log:
- some restructuring... getting rid of code duplication
File Changes:
Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/
=======================================================
File [modified]: Castle.DynamicProxy-vs2008.csproj
Delta lines: +28 -94
===================================================================
---
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/ClassProxyTargetContributor.cs
2009-11-15 15:48:03 UTC (rev 6332)
+++
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/ClassProxyTargetContributor.cs
2009-11-15 22:02:23 UTC (rev 6333)
@@ -23,34 +23,20 @@
using Generators.Emitters;
using Generators.Emitters.SimpleAST;
- public /* internal? */ class ClassProxyTargetContributor :
ITypeContributor
+ public class ClassProxyTargetContributor : CompositeTypeContributor
{
private readonly Type targetType;
private readonly IList<MethodInfo> methodsToSkip;
- private readonly IDictionary<Type, InterfaceMapping> interfaces
= new Dictionary<Type, InterfaceMapping>();
- private readonly ICollection<MembersCollector> targets = new
List<MembersCollector>();
- private readonly INamingScope namingScope;
- public ClassProxyTargetContributor(Type targetType,
IList<MethodInfo> methodsToSkip, INamingScope namingScope)
+ public ClassProxyTargetContributor(Type targetType,
IList<MethodInfo> methodsToSkip, INamingScope namingScope):base(namingScope)
{
this.targetType = targetType;
- this.namingScope = namingScope;
this.methodsToSkip = methodsToSkip;
}
- public void AddInterfaceMapping(Type @interface)
- {
- // TODO: this method is likely to be moved to the
interface
- Debug.Assert(@interface != null, "@interface == null",
"Shouldn't be adding empty interfaces...");
- Debug.Assert(@interface.IsInterface,
"@interface.IsInterface", "Should be adding interfaces only...");
- Debug.Assert(!interfaces.ContainsKey(@interface),
"!interfaces.ContainsKey(@interface)", "Shouldn't be adding same interface
twice...");
- Debug.Assert(@interface.IsAssignableFrom(targetType),
"@interface.IsAssignableFrom(targetType)",
- "Shouldn't be adding mapping
to interface that target does not implement...");
- interfaces.Add(@interface,
targetType.GetInterfaceMap(@interface));
- }
- public void CollectElementsToProxy(IProxyGenerationHook hook)
+ public override void
CollectElementsToProxy(IProxyGenerationHook hook)
{
Debug.Assert(hook != null, "hook != null");
@@ -58,98 +44,46 @@
targetItem.CollectMembersToProxy(hook);
targets.Add(targetItem);
- foreach (var mapping in interfaces)
+ foreach (var @interface in interfaces)
{
- var item = new
InterfaceMembersOnClassCollector(mapping.Key, this, true, mapping.Value);
+ var item = new
InterfaceMembersOnClassCollector(@interface,
+
this,
+
true,
+
targetType.GetInterfaceMap(@interface));
item.CollectMembersToProxy(hook);
targets.Add(item);
}
}
- public void Generate(ClassEmitter @class,
ProxyGenerationOptions options)
+ protected override MethodGenerator
GetMethodGenerator(MethodToGenerate method, ClassEmitter @class,
ProxyGenerationOptions options, CreateMethodDelegate createMethod)
{
- foreach (var target in targets)
- {
- foreach (var method in target.Methods)
- {
- if (!method.Standalone ||
methodsToSkip.Contains(method.Method))
- {
- continue;
- }
+ if (methodsToSkip.Contains(method.Method)) return null;
- ImplementMethod(method,
- @class,
- options,
- @class.CreateMethod);
- }
-
- foreach (var property in target.Properties)
- {
- ImplementProperty(@class, property,
options);
- }
-
- foreach (var @event in target.Events)
- {
- ImplementEvent(@class, @event, options);
- }
- }
- }
-
- private void ImplementEvent(ClassEmitter emitter,
EventToGenerate @event, ProxyGenerationOptions options)
- {
- @event.BuildEventEmitter(emitter);
- ImplementMethod(@event.Adder, emitter, options,
@event.Emitter.CreateAddMethod);
- ImplementMethod(@event.Remover, emitter, options,
@event.Emitter.CreateRemoveMethod);
-
- }
-
- private void ImplementProperty(ClassEmitter emitter,
PropertyToGenerate property, ProxyGenerationOptions options)
- {
- property.BuildPropertyEmitter(emitter);
- if (property.CanRead)
+ if (!method.Proxyable)
{
- ImplementMethod(property.Getter, emitter,
options,
- (name, atts) =>
property.Emitter.CreateGetMethod(name, atts));
+ return new MinimialisticMethodGenerator(method,
+
createMethod,
+
GeneratorUtil.ObtainClassMethodAttributes);
}
- if (property.CanWrite)
+ var methodInfo = method.Method;
+ var callback = default(MethodInfo);
+ var targetForInvocation = targetType;
+ if (!method.MethodOnTarget.IsAbstract &&
!IsExplicitInterfaceImplementation(method.MethodOnTarget))
{
- ImplementMethod(property.Setter, emitter,
options,
- (name, atts) =>
property.Emitter.CreateSetMethod(name, atts));
-
+ callback = CreateCallbackMethod(@class,
methodInfo, method.MethodOnTarget);
+ targetForInvocation = callback.DeclaringType;
}
- }
+ var invocation = new
InvocationTypeGenerator(targetForInvocation,
+ method,
+ callback,
+
false).Generate(@class, options, namingScope);
- private void ImplementMethod(MethodToGenerate method,
ClassEmitter @class, ProxyGenerationOptions options, CreateMethodDelegate
createMethod)
- {
- MethodGenerator generator;
- if (method.Proxyable)
- {
- var methodInfo = method.Method;
- var callback = default(MethodInfo);
- var targetForInvocation = targetType;
- if (!method.MethodOnTarget.IsAbstract &&
!IsExplicitInterfaceImplementation(method.MethodOnTarget))
- {
- // NOTE: factor this out as well.
- callback = CreateCallbackMethod(@class,
methodInfo, method.MethodOnTarget);
- targetForInvocation =
callback.DeclaringType;
- }
- var invocation = new
InvocationTypeGenerator(targetForInvocation, method, callback, false)
- .Generate(@class, options, namingScope);
-
- var interceptors =
@class.GetField("__interceptors");
- generator = new
MethodWithCallbackGenerator(method, invocation, interceptors, createMethod);
- }
- else
- {
- generator = new
MinimialisticMethodGenerator(method, createMethod,
GeneratorUtil.ObtainClassMethodAttributes);
- }
- var proxyMethod = generator.Generate(@class, options,
namingScope);
- foreach (var attribute in
AttributeUtil.GetNonInheritableAttributes(method.Method))
- {
- proxyMethod.DefineCustomAttribute(attribute);
- }
+ return new MethodWithCallbackGenerator(method,
+ invocation,
+
@class.GetField("__interceptors"),
+ createMethod);
}
Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/
====================================================================
File [modified]: ClassProxyTargetContributor.cs
Delta lines: +118 -0
===================================================================
---
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/CompositeTypeContributor.cs
(rev 0)
+++
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/CompositeTypeContributor.cs
2009-11-15 22:02:23 UTC (rev 6333)
@@ -0,0 +1,118 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+//
+// 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.
+
+namespace Castle.DynamicProxy.Contributors
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Diagnostics;
+
+ using Castle.DynamicProxy.Generators;
+ using Castle.DynamicProxy.Generators.Emitters;
+
+ public abstract class CompositeTypeContributor: ITypeContributor
+ {
+ protected readonly INamingScope namingScope;
+ protected readonly IList<Type> interfaces = new List<Type>();
+ protected readonly ICollection<MembersCollector> targets = new
List<MembersCollector>();
+
+ protected CompositeTypeContributor(INamingScope namingScope)
+ {
+ this.namingScope = namingScope;
+ }
+
+ public abstract void
CollectElementsToProxy(IProxyGenerationHook hook);
+
+ public virtual void Generate(ClassEmitter @class,
ProxyGenerationOptions options)
+ {
+ foreach (var target in targets)
+ {
+ foreach (var method in target.Methods)
+ {
+ if (!method.Standalone)
+ {
+ continue;
+ }
+
+ ImplementMethod(method,
+ @class,
+ options,
+ @class.CreateMethod);
+ }
+
+ foreach (var property in target.Properties)
+ {
+ ImplementProperty(@class, property,
options);
+ }
+
+ foreach (var @event in target.Events)
+ {
+ ImplementEvent(@class, @event, options);
+ }
+ }
+ }
+
+ public void AddInterfaceToProxy(Type @interface)
+ {
+ Debug.Assert(@interface != null, "@interface == null",
"Shouldn't be adding empty interfaces...");
+ Debug.Assert(@interface.IsInterface,
"@interface.IsInterface", "Should be adding interfaces only...");
+ Debug.Assert(!interfaces.Contains(@interface),
"!interfaces.ContainsKey(@interface)", "Shouldn't be adding same interface
twice...");
+
+
+ interfaces.Add(@interface);
+ }
+
+ private void ImplementEvent(ClassEmitter emitter,
EventToGenerate @event, ProxyGenerationOptions options)
+ {
+ @event.BuildEventEmitter(emitter);
+ ImplementMethod(@event.Adder, emitter, options,
@event.Emitter.CreateAddMethod);
+ ImplementMethod(@event.Remover, emitter, options,
@event.Emitter.CreateRemoveMethod);
+
+ }
+
+ private void ImplementProperty(ClassEmitter emitter,
PropertyToGenerate property, ProxyGenerationOptions options)
+ {
+ property.BuildPropertyEmitter(emitter);
+ if (property.CanRead)
+ {
+ ImplementMethod(property.Getter, emitter,
options,
+ (name, atts) =>
property.Emitter.CreateGetMethod(name, atts));
+ }
+
+ if (property.CanWrite)
+ {
+ ImplementMethod(property.Setter, emitter,
options,
+ (name, atts) =>
property.Emitter.CreateSetMethod(name, atts));
+
+ }
+ }
+
+ protected abstract MethodGenerator
GetMethodGenerator(MethodToGenerate method, ClassEmitter @class,
+
ProxyGenerationOptions options, CreateMethodDelegate createMethod);
+
+ private void ImplementMethod(MethodToGenerate method,
ClassEmitter @class, ProxyGenerationOptions options,
+ CreateMethodDelegate
createMethod)
+ {
+ {
+ var generator = GetMethodGenerator(method,
@class, options, createMethod);
+ if (generator == null) return;
+ var proxyMethod = generator.Generate(@class,
options, namingScope);
+ foreach (var attribute in
AttributeUtil.GetNonInheritableAttributes(method.Method))
+ {
+
proxyMethod.DefineCustomAttribute(attribute);
+ }
+ }
+ }
+ }
+}
File [added]: CompositeTypeContributor.cs
Delta lines: +24 -106
===================================================================
---
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs
2009-11-15 15:48:03 UTC (rev 6332)
+++
DynamicProxy/trunk/src/Castle.DynamicProxy/Contributors/InterfaceProxyTargetContributor.cs
2009-11-15 22:02:23 UTC (rev 6333)
@@ -15,7 +15,6 @@
namespace Castle.DynamicProxy.Contributors
{
using System;
- using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
@@ -23,145 +22,64 @@
using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Generators.Emitters;
- public class InterfaceProxyTargetContributor: ITypeContributor
+ public class InterfaceProxyTargetContributor : CompositeTypeContributor
{
- private readonly Type targetType;
- private readonly IDictionary<Type, InterfaceMapping> interfaces
= new Dictionary<Type, InterfaceMapping>();
- private readonly ICollection<MembersCollector> targets = new
List<MembersCollector>();
+ private readonly Type proxyTargetType;
private readonly bool canChangeTarget;
- private readonly INamingScope namingScope;
- public InterfaceProxyTargetContributor(Type targetType, bool
canChangeTarget, INamingScope namingScope)
+ public InterfaceProxyTargetContributor(Type proxyTargetType,
bool canChangeTarget, INamingScope namingScope)
+ : base(namingScope)
{
- this.targetType = targetType;
- this.namingScope = namingScope;
+ this.proxyTargetType = proxyTargetType;
this.canChangeTarget = canChangeTarget;
}
- public void AddInterfaceToProxy(Type @interface)
+ public override void
CollectElementsToProxy(IProxyGenerationHook hook)
{
- // TODO: this impl is identcal to
ClassProxyTargetContributor
- Debug.Assert(@interface != null, "@interface == null",
"Shouldn't be adding empty interfaces...");
- Debug.Assert(@interface.IsInterface,
"@interface.IsInterface", "Should be adding interfaces only...");
- Debug.Assert(!interfaces.ContainsKey(@interface),
"!interfaces.ContainsKey(@interface)", "Shouldn't be adding same interface
twice...");
- Debug.Assert(@interface.IsAssignableFrom(targetType),
"@interface.IsAssignableFrom(targetType)",
- "Shouldn't be adding mapping
to interface that target does not implement...");
-
-
- interfaces.Add(@interface, GetMapping(@interface));
-
- }
-
- protected virtual InterfaceMapping GetMapping(Type @interface)
- {
- return targetType.GetInterfaceMap(@interface);
- }
-
- public void CollectElementsToProxy(IProxyGenerationHook hook)
- {
Debug.Assert(hook != null, "hook != null");
- foreach (var mapping in interfaces)
+ foreach (var @interface in interfaces)
{
- var item = new
InterfaceMembersOnClassCollector(mapping.Key, this, false, mapping.Value);
+ var item = new
InterfaceMembersOnClassCollector(@interface, this, false,
GetMapping(@interface));
item.CollectMembersToProxy(hook);
targets.Add(item);
}
}
- public void Generate(ClassEmitter @class,
ProxyGenerationOptions options)
+ protected virtual InterfaceMapping GetMapping(Type @interface)
{
- foreach (var target in targets)
- {
- foreach (var method in target.Methods)
- {
- if (!method.Standalone)
- {
- continue;
- }
- ImplementMethod(method,
- @class,
- options,
-
@class.CreateMethod);
- }
-
- foreach (var property in target.Properties)
- {
- ImplementProperty(@class, property,
options);
- }
-
- foreach (var @event in target.Events)
- {
- ImplementEvent(@class, @event, options);
- }
- }
+ return proxyTargetType.GetInterfaceMap(@interface);
}
- private void ImplementEvent(ClassEmitter @class,
EventToGenerate @event, ProxyGenerationOptions options)
+ protected override MethodGenerator
GetMethodGenerator(MethodToGenerate method, ClassEmitter @class,
ProxyGenerationOptions options, CreateMethodDelegate createMethod)
{
- @event.BuildEventEmitter(@class);
- var adder = @event.Adder;
- ImplementMethod(adder, @class, options,
@event.Emitter.CreateAddMethod);
- var remover = @event.Remover;
- ImplementMethod(remover, @class, options,
@event.Emitter.CreateRemoveMethod);
-
- }
-
- private void ImplementProperty(ClassEmitter @class,
PropertyToGenerate property, ProxyGenerationOptions options)
- {
- property.BuildPropertyEmitter(@class);
- if (property.CanRead)
+ if (!method.Proxyable)
{
- var method = property.Getter;
- ImplementMethod(method, @class, options,
- (name, atts) =>
property.Emitter.CreateGetMethod(name, atts));
+ return new ForwardingMethodGenerator(method,
+
createMethod,
+ (c, m) =>
c.GetField("__target"));
}
- if (property.CanWrite)
- {
- var method = property.Setter;
- ImplementMethod(method, @class, options,
- (name, atts) =>
property.Emitter.CreateSetMethod(name, atts));
- }
- }
+ Type invocation = GetInvocationType(method, @class,
options);
- private void ImplementMethod(MethodToGenerate method,
ClassEmitter @class, ProxyGenerationOptions options, CreateMethodDelegate
createMethod)
- {
- MethodGenerator generator;
- if (method.Proxyable)
- {
- Type invocation = GetInvocationType(method,
@class, options);
-
- generator = new InterfaceMethodGenerator(method,
-
invocation,
-
@class.GetField("__interceptors"),
-
createMethod,
- (c, m)
=> c.GetField("__target").ToExpression());
- }
- else
- {
- generator = new
ForwardingMethodGenerator(method,
-
createMethod,
- (c,
m) => c.GetField("__target"));
- }
- var proxyMethod = generator.Generate(@class, options,
namingScope);
- foreach (var attribute in
AttributeUtil.GetNonInheritableAttributes(method.Method))
- {
- proxyMethod.DefineCustomAttribute(attribute);
- }
+ return new InterfaceMethodGenerator(method,
+ invocation,
+
@class.GetField("__interceptors"),
+ createMethod,
+ (c, m) =>
c.GetField("__target").ToExpression());
}
private Type GetInvocationType(MethodToGenerate method,
ClassEmitter emitter, ProxyGenerationOptions options)
{
var scope = emitter.ModuleScope;
- Type[] interfaces = Type.EmptyTypes;
+ Type[] invocationInterfaces = Type.EmptyTypes;
if(canChangeTarget)
{
- interfaces = new[] { typeof(IChangeProxyTarget)
};
+ invocationInterfaces = new[] {
typeof(IChangeProxyTarget) };
}
- var key = new CacheKey(method.Method, interfaces, null);
+ var key = new CacheKey(method.Method,
invocationInterfaces, null);
// no locking required as we're already within a lock
File [modified]: InterfaceProxyTargetContributor.cs
Delta lines: +2 -2
===================================================================
---
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/ClassProxyGenerator.cs
2009-11-15 15:48:03 UTC (rev 6332)
+++
DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/ClassProxyGenerator.cs
2009-11-15 22:02:23 UTC (rev 6333)
@@ -177,7 +177,7 @@
if
(additionalInterfaces.Contains(mixinInterface) &&
typeImplementerMapping.ContainsKey(mixinInterface) == false)
{
SafeAddMapping(mixinInterface, proxyTarget, typeImplementerMapping);
-
proxyTarget.AddInterfaceMapping(mixinInterface);
+
proxyTarget.AddInterfaceToProxy(mixinInterface);
}
// we do not intercept the
interface
mixins.Add(new
EmptyMixinContributor(mixinInterface));
@@ -205,7 +205,7 @@
// we intercept the interface, and
forward calls to the target type
SafeAddMapping(@interface, proxyTarget,
typeImplementerMapping);
-
proxyTarget.AddInterfaceMapping(@interface);
+
proxyTarget.AddInterfaceToProxy(@interface);
}
else if
(ProxyGenerationOptions.MixinData.ContainsMixin(@interface) == false)
Directory: /DynamicProxy/trunk/src/Castle.DynamicProxy/Generators/
==================================================================
File [modified]: ClassProxyGenerator.cs
Delta lines: +0 -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=.