Skip to content

Commit

Permalink
greenify tests I
Browse files Browse the repository at this point in the history
  • Loading branch information
Sholtee committed Jun 14, 2020
1 parent eb16843 commit 65c57e4
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 188 deletions.
202 changes: 73 additions & 129 deletions SRC/Private/SyntaxFactories/ProxySyntaxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ private static readonly MemberAccessExpressionSyntax
//
// this.Target
//
TARGET = MemberAccess(null, MemberInfoExtensions.ExtractFrom<InterfaceInterceptor<TInterface>>(ii => ii.Target!)),
//
// this.CALL_TARGET
//
CALL_TARGET = MemberAccess(null, MemberInfoExtensions.ExtractFrom<InterfaceInterceptor<TInterface>>(ii => ii.CALL_TARGET));
TARGET = MemberAccess(null, MemberInfoExtensions.ExtractFrom<InterfaceInterceptor<TInterface>>(ii => ii.Target!));

private static readonly MethodInfo
INVOKE = (MethodInfo) MemberInfoExtensions.ExtractFrom<InterfaceInterceptor<TInterface>>(ii => ii.Invoke(default!, default!, default!)),
Expand Down Expand Up @@ -117,7 +113,69 @@ private static string EnsureUnused(string name, MethodInfo method)
#endregion

#region Internal
public sealed class CallbackLambdaExpressionFactory
/// <summary>
/// object result = Invoke(...);
/// </summary>
internal static LocalDeclarationStatementSyntax CallInvoke(string variableName, params ExpressionSyntax[] arguments) =>
DeclareLocal<object>(variableName, InvokeMethod
(
INVOKE,
target: null,
castTargetTo: null,
arguments: arguments.Select(Argument).ToArray()
));

/// <summary>
/// object result = Invoke(var1, var2, ..., varN);
/// </summary>
internal static LocalDeclarationStatementSyntax CallInvoke(string variableName, params LocalDeclarationStatementSyntax[] arguments) =>
CallInvoke(variableName, arguments.Select(arg => (ExpressionSyntax)ToIdentifierName(arg)).ToArray());

/// <summary>
/// return; <br/>
/// <br/>
/// OR <br/>
/// <br/>
/// return (T) ...;
/// </summary>
internal static ReturnStatementSyntax ReturnResult(Type? returnType, ExpressionSyntax result) => ReturnStatement
(
expression: returnType == typeof(void)
? null
: returnType != null
? CastExpression
(
type: CreateType(returnType),
expression: result
)
: result
);

/// <summary>
/// return; <br/>
/// <br/>
/// OR <br/>
/// <br/>
/// return (T) result;
/// </summary>
internal static ReturnStatementSyntax ReturnResult(Type? returnType, LocalDeclarationStatementSyntax result) =>
ReturnResult(returnType, ToIdentifierName(result));

/// <summary>
/// () => <br/>
/// { <br/>
/// System.Int32 cb_a = (System.Int32)args[0]; <br/>
/// System.String cb_b; <br/>
/// TT cb_c = (TT)args[2]; <br/>
/// System.Object result; <br/>
/// result = this.Target.Foo[TT](cb_a, out cb_b, ref cb_c); <br/>
/// <br/>
/// args[1] = (System.Object)cb_b; <br/>
/// args[2] = (System.Object)cb_c; <br/>
/// return result; <br/>
/// };
/// </summary>
internal sealed class CallbackLambdaExpressionFactory
{
public MethodInfo Method { get; }

Expand Down Expand Up @@ -302,7 +360,7 @@ internal IEnumerable<StatementSyntax> ReassignArgsArray()
/// return (TResult) result; <br/>
/// }
/// </summary>
public sealed class MethodInterceptorFactory
internal sealed class MethodInterceptorFactory
{
public MethodInfo Method { get; }

Expand Down Expand Up @@ -492,121 +550,6 @@ public MethodDeclarationSyntax Build()
}
}

/// <summary>
/// object result = Invoke(...);
/// </summary>
internal static LocalDeclarationStatementSyntax CallInvoke(string variableName, params ExpressionSyntax[] arguments) =>
DeclareLocal<object>(variableName, InvokeMethod
(
INVOKE,
target: null,
castTargetTo: null,
arguments: arguments.Select(Argument).ToArray()
));

/// <summary>
/// object result = Invoke(var1, var2, ..., varN);
/// </summary>
internal static LocalDeclarationStatementSyntax CallInvoke(string variableName, params LocalDeclarationStatementSyntax[] arguments) =>
CallInvoke(variableName, arguments.Select(arg => (ExpressionSyntax) ToIdentifierName(arg)).ToArray());

/// <summary>
/// return Target.Bar(...); <br/>
/// <br/>
/// OR <br/>
/// <br/>
/// { <br/>
/// Target.Bar(...); <br/>
/// return; <br/>
/// }
/// </summary>
internal static StatementSyntax CallTargetAndReturn(MethodInfo method)
{
InvocationExpressionSyntax invocation = InvokeMethod(
method,
TARGET,
castTargetTo: null,
arguments: method
.GetParameters()
.Select(p => p.Name)
.ToArray());

return method.ReturnType != typeof(void)
? (StatementSyntax) ReturnStatement(invocation)
: Block
(
statements: List(new StatementSyntax[]{ExpressionStatement(invocation), ReturnStatement()})
);
}

/// <summary>
/// return Target.Prop;
/// </summary>
internal static StatementSyntax ReadTargetAndReturn(PropertyInfo property) =>
ReturnStatement(PropertyAccess(property, TARGET));

/// <summary>
/// Target.Prop = value;
/// </summary>
internal static StatementSyntax WriteTarget(PropertyInfo property) =>
ExpressionStatement
(
expression: AssignmentExpression
(
kind: SyntaxKind.SimpleAssignmentExpression,
left: PropertyAccess(property, TARGET),
right: IdentifierName(Value)
)
);

/// <summary>
/// if (result == CALL_TARGET) <br/>
/// { <br/>
/// ... <br/>
/// }
/// </summary>
internal static IfStatementSyntax ShouldCallTarget(LocalDeclarationStatementSyntax result, StatementSyntax ifTrue) =>
IfStatement
(
condition: BinaryExpression
(
kind: SyntaxKind.EqualsExpression,
left: ToIdentifierName(result),
right: CALL_TARGET
),
statement: ifTrue
);

/// <summary>
/// return; <br/>
/// <br/>
/// OR <br/>
/// <br/>
/// return (T) ...;
/// </summary>
internal static ReturnStatementSyntax ReturnResult(Type? returnType, ExpressionSyntax result) => ReturnStatement
(
expression: returnType == typeof(void)
? null
: returnType != null
? CastExpression
(
type: CreateType(returnType),
expression: result
)
: result
);

/// <summary>
/// return; <br/>
/// <br/>
/// OR <br/>
/// <br/>
/// return (T) result;
/// </summary>
internal static ReturnStatementSyntax ReturnResult(Type? returnType, LocalDeclarationStatementSyntax result) =>
ReturnResult(returnType, ToIdentifierName(result));

/// <summary>
/// private static readonly PropertyInfo FProp = Properties["IInterface.Prop"]; <br/>
/// <br/>
Expand All @@ -628,7 +571,7 @@ internal static StatementSyntax CallTargetAndReturn(MethodInfo method)
/// } <br/>
/// }
/// </summary>
public class PropertyInterceptorFactory
internal class PropertyInterceptorFactory
{
public PropertyInfo Property { get; }

Expand Down Expand Up @@ -786,7 +729,7 @@ public IEnumerable<MemberDeclarationSyntax> Build()
/// } <br/>
/// }
/// </summary>
public sealed class IndexedPropertyInterceptorFactory : PropertyInterceptorFactory
internal sealed class IndexedPropertyInterceptorFactory : PropertyInterceptorFactory
{
public IndexedPropertyInterceptorFactory(PropertyInfo property, ProxySyntaxFactory<TInterface, TInterceptor> owner) : base(property, owner)
{
Expand Down Expand Up @@ -826,7 +769,7 @@ public IndexedPropertyInterceptorFactory(PropertyInfo property, ProxySyntaxFacto
/// } <br/>
/// }
/// </summary>
public sealed class EventInterceptorFactory
internal sealed class EventInterceptorFactory
{
public EventInfo Event { get; }

Expand Down Expand Up @@ -950,22 +893,23 @@ protected internal override ClassDeclarationSyntax GenerateProxyClass()
.Select(m => new MethodInterceptorFactory(m).Build())
);

/* members.AddRange
members.AddRange
(
interfaceType
.ListMembers<PropertyInfo>()
.Where(p => !implementedInterfaces.Contains(p.DeclaringType))
.SelectMany(GenerateProxyProperty)
.SelectMany(p => p.IsIndexer()
? new IndexedPropertyInterceptorFactory(p, this).Build()
: new PropertyInterceptorFactory(p, this).Build())
);

members.AddRange
(
interfaceType
.ListMembers<EventInfo>()
.Where(e => !implementedInterfaces.Contains(e.DeclaringType))
.SelectMany(GenerateProxyEvent)
.SelectMany(e => new EventInterceptorFactory(e, this).Build())
);
*/

return cls.WithMembers(List(members));
}
Expand Down
8 changes: 0 additions & 8 deletions SRC/Public/InterfaceInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand All @@ -22,13 +21,6 @@ namespace Solti.Utils.Proxy
/// <remarks>This class is not thread safe even if the <see cref="InterfaceInterceptor{TInterface}.Target"/> is it.</remarks>
public class InterfaceInterceptor<TInterface>: IHasTarget<TInterface?> where TInterface: class
{
/// <summary>
/// Signals that the original method should be called.
/// </summary>
/// <remarks>Internal, don't use it!</remarks>
[SuppressMessage("Design", "CA1051:Do not declare visible instance fields", Justification = "Descendants need direct access to the field")]
protected internal readonly object CALL_TARGET = new object();

/// <summary>
/// Extracts the <see cref="MethodInfo"/> from the given expression.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion TEST/CallbackSrc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
result = this.Target.Foo<TT>(cb_a, out cb_b, ref cb_c);
args[1] = (System.Object)cb_b;
args[2] = (System.Object)cb_c;
return (System.Int32)result;
return result;
}
Loading

0 comments on commit 65c57e4

Please sign in to comment.