Skip to content

Commit

Permalink
Skip interceptors for non-intercepted methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jan 30, 2021
1 parent 6302283 commit ad6b06b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Expand Up @@ -60,14 +60,23 @@ protected override IEnumerable<MembersCollector> CollectElementsToProxyInternal(
return null;
}

if (IsDirectlyAccessible(method) == false)
var methodIsDirectlyAccessible = IsDirectlyAccessible(method);

if (!method.Proxyable)
{
return IndirectlyCalledMethodGenerator(method, @class, overrideMethod);
if (methodIsDirectlyAccessible)
{
return new ForwardingMethodGenerator(method, overrideMethod, (c, m) => c.GetField("__target"));
}
else
{
return IndirectlyCalledMethodGenerator(method, @class, overrideMethod, skipInterceptors: true);
}
}

if (!method.Proxyable)
if (!methodIsDirectlyAccessible)
{
return new ForwardingMethodGenerator(method, overrideMethod, (c, m) => c.GetField("__target"));
return IndirectlyCalledMethodGenerator(method, @class, overrideMethod);
}

var invocation = GetInvocationType(method, @class);
Expand Down Expand Up @@ -140,17 +149,19 @@ private Type GetInvocationType(MetaMethod method, ClassEmitter @class)
}

private MethodGenerator IndirectlyCalledMethodGenerator(MetaMethod method, ClassEmitter proxy,
OverrideMethodDelegate overrideMethod)
OverrideMethodDelegate overrideMethod,
bool skipInterceptors = false)
{
var @delegate = GetDelegateType(method, proxy);
var contributor = GetContributor(@delegate, method);
var invocation = new CompositionInvocationTypeGenerator(targetType, method, null, false, contributor)
.Generate(proxy, namingScope)
.BuildType();
return new MethodWithInvocationGenerator(method,
proxy.GetField("__interceptors"),
skipInterceptors ? NullExpression.Instance : proxy.GetField("__interceptors").ToExpression(),
invocation,
(c, m) => c.GetField("__target").ToExpression(),
null,
overrideMethod,
contributor);
}
Expand Down
Expand Up @@ -34,20 +34,28 @@ internal class MethodWithInvocationGenerator : MethodGenerator
private readonly IInvocationCreationContributor contributor;
private readonly GetTargetExpressionDelegate getTargetExpression;
private readonly GetTargetExpressionDelegate getTargetTypeExpression;
private readonly Reference interceptors;
private readonly Expression interceptors;
private readonly Type invocation;

public MethodWithInvocationGenerator(MetaMethod method, Reference interceptors, Type invocation,
GetTargetExpressionDelegate getTargetExpression,
OverrideMethodDelegate createMethod, IInvocationCreationContributor contributor)
: this(method, interceptors, invocation, getTargetExpression, null, createMethod, contributor)
: this(method, interceptors.ToExpression(), invocation, getTargetExpression, null, createMethod, contributor)
{
}

public MethodWithInvocationGenerator(MetaMethod method, Reference interceptors, Type invocation,
GetTargetExpressionDelegate getTargetExpression,
GetTargetExpressionDelegate getTargetTypeExpression,
OverrideMethodDelegate createMethod, IInvocationCreationContributor contributor)
: this(method, interceptors.ToExpression(), invocation, getTargetExpression, getTargetTypeExpression, createMethod, contributor)
{
}

public MethodWithInvocationGenerator(MetaMethod method, Expression interceptors, Type invocation,
GetTargetExpressionDelegate getTargetExpression,
GetTargetExpressionDelegate getTargetTypeExpression,
OverrideMethodDelegate createMethod, IInvocationCreationContributor contributor)
: base(method, createMethod)
{
this.invocation = invocation;
Expand Down Expand Up @@ -182,7 +190,7 @@ private Expression SetMethodInterceptors(ClassEmitter @class, INamingScope namin
var emptyInterceptors = new NewArrayExpression(0, typeof(IInterceptor));
var selectInterceptors = new MethodInvocationExpression(selector, InterceptorSelectorMethods.SelectInterceptors,
targetTypeExpression,
proxiedMethodTokenExpression, interceptors.ToExpression())
proxiedMethodTokenExpression, interceptors)
{ VirtualCall = true };

emitter.CodeBuilder.AddExpression(
Expand Down Expand Up @@ -218,7 +226,7 @@ private Expression[] GetCtorArguments(ClassEmitter @class, Expression proxiedMet
{
getTargetExpression(@class, MethodToOverride),
SelfReference.Self.ToExpression(),
methodInterceptors ?? interceptors.ToExpression(),
methodInterceptors ?? interceptors,
proxiedMethodTokenExpression,
new ReferencesToObjectArrayExpression(dereferencedArguments)
};
Expand Down

0 comments on commit ad6b06b

Please sign in to comment.