Skip to content

Commit

Permalink
Collect interface methods with default implementations
Browse files Browse the repository at this point in the history
A class implementing an interface with default method implementations
will not necessarily override those methods; in such cases, the methods
are currently "invisible" to DynamicProxy and thus won't get proxied.

This can be solved by an extra pass over all interfaces to collect such
methods, as is done in this commit.

We should next check whether the same work can be performed in the pre-
existing infrastructure, so that we don't need to iterate over all
interfaces more than once.
  • Loading branch information
stakx committed May 21, 2022
1 parent a270882 commit 2b39580
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ protected override IEnumerable<MembersCollector> GetCollectors()
var targetItem = new ClassMembersCollector(targetType) { Logger = Logger };
yield return targetItem;

foreach (var @interface in targetType.GetAllInterfaces())
{
yield return new InterfaceMembersWithDefaultImplementationCollector(@interface, targetType);
}

foreach (var @interface in interfaces)
{
var item = new InterfaceMembersOnClassCollector(@interface, true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2004-2022 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.Diagnostics;
using System.Reflection;

using Castle.DynamicProxy.Generators;

internal sealed class InterfaceMembersWithDefaultImplementationCollector : MembersCollector
{
private readonly InterfaceMapping map;

public InterfaceMembersWithDefaultImplementationCollector(Type interfaceType, Type classToProxy)
: base(interfaceType)
{
Debug.Assert(interfaceType != null);
Debug.Assert(interfaceType.IsInterface);

Debug.Assert(classToProxy != null);
Debug.Assert(classToProxy.IsClass);

map = classToProxy.GetInterfaceMap(interfaceType);
}

protected override MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
{
var index = Array.IndexOf(map.InterfaceMethods, method);
Debug.Assert(index >= 0);

var methodOnTarget = map.TargetMethods[index];
if (methodOnTarget.DeclaringType.IsInterface == false)
{
return null;
}

var proxyable = AcceptMethod(method, true, hook);
if (!proxyable)
{
return null;
}

return new MetaMethod(method, methodOnTarget, true, proxyable, !method.IsAbstract);
}
}
}

0 comments on commit 2b39580

Please sign in to comment.