Skip to content

Commit

Permalink
Don't pass around interfaces where not required
Browse files Browse the repository at this point in the history
Also, standardize on `allInterfaces` as the name for the return value of
`GetTypeImplementerMapping` to distinguish it better from `interfaces`.

(This is better than the other previous variant--`implementedInterfaces`
when they aren't implemented just yet--but still not completely accurate
as it may also include delegate types. Maybe those names will be "fixed"
someday.)
  • Loading branch information
stakx committed Jan 3, 2021
1 parent 078a267 commit d58479a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 29 deletions.
5 changes: 2 additions & 3 deletions src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ protected ConstructorEmitter GenerateStaticConstructor(ClassEmitter emitter)
return emitter.CreateTypeConstructor();
}

protected void HandleExplicitlyPassedProxyTargetAccessor(ICollection<Type> targetInterfaces,
ICollection<Type> additionalInterfaces)
protected void HandleExplicitlyPassedProxyTargetAccessor(ICollection<Type> targetInterfaces)
{
var interfaceName = typeof(IProxyTargetAccessor).ToString();
//ok, let's determine who tried to sneak the IProxyTargetAccessor in...
Expand All @@ -369,7 +368,7 @@ protected ConstructorEmitter GenerateStaticConstructor(ClassEmitter emitter)
mixinType.Name, interfaceName);
throw new InvalidOperationException("This is a DynamicProxy2 error: " + message);
}
else if (additionalInterfaces.Contains(typeof(IProxyTargetAccessor)))
else if (interfaces.Contains(typeof(IProxyTargetAccessor)))
{
message =
string.Format(
Expand Down
14 changes: 6 additions & 8 deletions src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override CacheKey GetCacheKey()
protected override Type GenerateType(string name, INamingScope namingScope)
{
IEnumerable<ITypeContributor> contributors;
var implementedInterfaces = GetTypeImplementerMapping(interfaces, out contributors, namingScope);
var allInterfaces = GetTypeImplementerMapping(out contributors, namingScope);

var model = new MetaType();
// Collect methods
Expand All @@ -51,7 +51,7 @@ protected override Type GenerateType(string name, INamingScope namingScope)
}
ProxyGenerationOptions.Hook.MethodsInspected();

var emitter = BuildClassEmitter(name, targetType, implementedInterfaces);
var emitter = BuildClassEmitter(name, targetType, allInterfaces);

CreateFields(emitter);
CreateTypeAttributes(emitter);
Expand Down Expand Up @@ -94,8 +94,7 @@ protected override Type GenerateType(string name, INamingScope namingScope)
return proxyType;
}

protected virtual IEnumerable<Type> GetTypeImplementerMapping(Type[] interfaces,
out IEnumerable<ITypeContributor> contributors,
protected virtual IEnumerable<Type> GetTypeImplementerMapping(out IEnumerable<ITypeContributor> contributors,
INamingScope namingScope)
{
var methodsToSkip = new List<MethodInfo>();
Expand All @@ -109,7 +108,6 @@ protected override Type GenerateType(string name, INamingScope namingScope)
// target is not an interface so we do nothing

var targetInterfaces = targetType.GetAllInterfaces();
var additionalInterfaces = TypeUtil.GetAllInterfaces(interfaces);
// 2. then mixins
var mixins = new MixinContributor(namingScope, false) { Logger = Logger };
if (ProxyGenerationOptions.HasMixins)
Expand All @@ -119,7 +117,7 @@ protected override Type GenerateType(string name, INamingScope namingScope)
if (targetInterfaces.Contains(mixinInterface))
{
// OK, so the target implements this interface. We now do one of two things:
if (additionalInterfaces.Contains(mixinInterface) && typeImplementerMapping.ContainsKey(mixinInterface) == false)
if (interfaces.Contains(mixinInterface) && typeImplementerMapping.ContainsKey(mixinInterface) == false)
{
AddMappingNoCheck(mixinInterface, proxyTarget, typeImplementerMapping);
proxyTarget.AddInterfaceToProxy(mixinInterface);
Expand All @@ -141,7 +139,7 @@ protected override Type GenerateType(string name, INamingScope namingScope)
static (c, m) => NullExpression.Instance)
{ Logger = Logger };
// 3. then additional interfaces
foreach (var @interface in additionalInterfaces)
foreach (var @interface in interfaces)
{
if (targetInterfaces.Contains(@interface))
{
Expand Down Expand Up @@ -173,7 +171,7 @@ protected override Type GenerateType(string name, INamingScope namingScope)
}
catch (ArgumentException)
{
HandleExplicitlyPassedProxyTargetAccessor(targetInterfaces, additionalInterfaces);
HandleExplicitlyPassedProxyTargetAccessor(targetInterfaces);
}

contributors = new List<ITypeContributor>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ internal class ClassProxyWithTargetGenerator : BaseProxyGenerator
}
catch (ArgumentException)
{
HandleExplicitlyPassedProxyTargetAccessor(targetInterfaces, interfaces);
HandleExplicitlyPassedProxyTargetAccessor(targetInterfaces);
}

contributors = new List<ITypeContributor>
Expand Down Expand Up @@ -160,7 +160,7 @@ protected override CacheKey GetCacheKey()
protected override Type GenerateType(string name, INamingScope namingScope)
{
IEnumerable<ITypeContributor> contributors;
var implementedInterfaces = GetTypeImplementerMapping(out contributors, namingScope);
var allInterfaces = GetTypeImplementerMapping(out contributors, namingScope);

var model = new MetaType();
// Collect methods
Expand All @@ -170,7 +170,7 @@ protected override Type GenerateType(string name, INamingScope namingScope)
}
ProxyGenerationOptions.Hook.MethodsInspected();

var emitter = BuildClassEmitter(name, targetType, implementedInterfaces);
var emitter = BuildClassEmitter(name, targetType, allInterfaces);

CreateFields(emitter);
CreateTypeAttributes(emitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ protected virtual string GeneratorType

protected virtual ITypeContributor AddMappingForTargetType(IDictionary<Type, ITypeContributor> typeImplementerMapping,
Type proxyTargetType, ICollection<Type> targetInterfaces,
ICollection<Type> additionalInterfaces,
INamingScope namingScope)
{
var contributor = new InterfaceProxyTargetContributor(proxyTargetType, AllowChangeTarget, namingScope)
Expand All @@ -68,7 +67,7 @@ protected virtual string GeneratorType
AddMappingNoCheck(@interface, contributor, typeImplementerMapping);
}

foreach (var @interface in additionalInterfaces)
foreach (var @interface in interfaces)
{
if (!ImplementedByTarget(targetInterfaces, @interface) || proxiedInterfaces.Contains(@interface))
{
Expand Down Expand Up @@ -97,7 +96,7 @@ protected override CacheKey GetCacheKey()
protected override Type GenerateType(string typeName, INamingScope namingScope)
{
IEnumerable<ITypeContributor> contributors;
var allInterfaces = GetTypeImplementerMapping(interfaces, proxyTargetType, out contributors, namingScope);
var allInterfaces = GetTypeImplementerMapping(proxyTargetType, out contributors, namingScope);

ClassEmitter emitter;
FieldReference interceptorsField;
Expand Down Expand Up @@ -154,7 +153,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope)
return new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance) { Logger = Logger };
}

protected virtual IEnumerable<Type> GetTypeImplementerMapping(Type[] interfaces, Type proxyTargetType,
protected virtual IEnumerable<Type> GetTypeImplementerMapping(Type proxyTargetType,
out IEnumerable<ITypeContributor> contributors,
INamingScope namingScope)
{
Expand All @@ -163,9 +162,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope)
// Order of interface precedence:
// 1. first target
var targetInterfaces = proxyTargetType.GetAllInterfaces();
var additionalInterfaces = TypeUtil.GetAllInterfaces(interfaces);
var target = AddMappingForTargetType(typeImplementerMapping, proxyTargetType, targetInterfaces, additionalInterfaces,
namingScope);
var target = AddMappingForTargetType(typeImplementerMapping, proxyTargetType, targetInterfaces, namingScope);

// 2. then mixins
if (ProxyGenerationOptions.HasMixins)
Expand All @@ -175,7 +172,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope)
if (targetInterfaces.Contains(mixinInterface))
{
// OK, so the target implements this interface. We now do one of two things:
if (additionalInterfaces.Contains(mixinInterface))
if (interfaces.Contains(mixinInterface))
{
// we intercept the interface, and forward calls to the target type
AddMapping(mixinInterface, target, typeImplementerMapping);
Expand All @@ -196,7 +193,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope)

var additionalInterfacesContributor = GetContributorForAdditionalInterfaces(namingScope);
// 3. then additional interfaces
foreach (var @interface in additionalInterfaces)
foreach (var @interface in interfaces)
{
if (typeImplementerMapping.ContainsKey(@interface))
{
Expand All @@ -222,7 +219,7 @@ protected override Type GenerateType(string typeName, INamingScope namingScope)
}
catch (ArgumentException)
{
HandleExplicitlyPassedProxyTargetAccessor(targetInterfaces, additionalInterfaces);
HandleExplicitlyPassedProxyTargetAccessor(targetInterfaces);
}

contributors = new List<ITypeContributor>
Expand All @@ -236,11 +233,11 @@ protected override Type GenerateType(string typeName, INamingScope namingScope)
}

protected virtual Type Init(string typeName, out ClassEmitter emitter, Type proxyTargetType,
out FieldReference interceptorsField, IEnumerable<Type> interfaces)
out FieldReference interceptorsField, IEnumerable<Type> allInterfaces)
{
var baseType = ProxyGenerationOptions.BaseTypeForInterfaceProxy;

emitter = BuildClassEmitter(typeName, baseType, interfaces);
emitter = BuildClassEmitter(typeName, baseType, allInterfaces);

CreateFields(emitter, proxyTargetType);
CreateTypeAttributes(emitter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override string GeneratorType

protected override ITypeContributor AddMappingForTargetType(
IDictionary<Type, ITypeContributor> typeImplementerMapping, Type proxyTargetType, ICollection<Type> targetInterfaces,
ICollection<Type> additionalInterfaces, INamingScope namingScope)
INamingScope namingScope)
{
var contributor = new InterfaceProxyWithTargetInterfaceTargetContributor(
proxyTargetType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override string GeneratorType

protected override ITypeContributor AddMappingForTargetType(
IDictionary<Type, ITypeContributor> interfaceTypeImplementerMapping, Type proxyTargetType,
ICollection<Type> targetInterfaces, ICollection<Type> additionalInterfaces, INamingScope namingScope)
ICollection<Type> targetInterfaces, INamingScope namingScope)
{
var contributor = new InterfaceProxyWithoutTargetContributor(namingScope, (c, m) => NullExpression.Instance)
{ Logger = Logger };
Expand All @@ -53,7 +53,7 @@ protected override string GeneratorType
protected override Type GenerateType(string typeName, INamingScope namingScope)
{
IEnumerable<ITypeContributor> contributors;
var allInterfaces = GetTypeImplementerMapping(interfaces, targetType, out contributors, namingScope);
var allInterfaces = GetTypeImplementerMapping(targetType, out contributors, namingScope);
var model = new MetaType();
// collect elements
foreach (var contributor in contributors)
Expand Down

0 comments on commit d58479a

Please sign in to comment.