-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathHandlerTypeDescriptorProvider.cs
177 lines (155 loc) · 6.07 KB
/
HandlerTypeDescriptorProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using MediatR;
namespace OmniSharp.Extensions.JsonRpc
{
public interface IHandlerTypeDescriptorProvider<out T> where T : IHandlerTypeDescriptor
{
T GetHandlerTypeDescriptor<A>();
T GetHandlerTypeDescriptor(Type type);
string GetMethodName<T>() where T : IJsonRpcHandler;
bool IsMethodName(string name, params Type[] types);
string GetMethodName(Type type);
}
public class HandlerTypeDescriptorHelper
{
internal static Type GetMethodType(Type type)
{
// Custom method
if (MethodAttribute.AllFrom(type).Any())
{
return type;
}
return type.GetTypeInfo()
.ImplementedInterfaces
.FirstOrDefault(t => MethodAttribute.AllFrom(t).Any());
}
private static readonly Type[] HandlerTypes = {
typeof(IJsonRpcNotificationHandler<>),
typeof(IJsonRpcRequestHandler<>),
typeof(IJsonRpcRequestHandler<,>),
typeof(IRequestHandler<>),
typeof(IRequestHandler<,>),
};
private static bool IsValidInterface(Type type)
{
if (type.GetTypeInfo().IsGenericType)
{
return HandlerTypes.Contains(type.GetGenericTypeDefinition());
}
return HandlerTypes.Contains(type);
}
public static Type GetHandlerInterface(Type type)
{
try
{
if (IsValidInterface(type)) return type;
return type?.GetTypeInfo()
.ImplementedInterfaces
.First(IsValidInterface);
}
catch (Exception e)
{
throw new AggregateException("Errored with type: " + type.FullName, e);
}
}
internal static Type UnwrapGenericType(Type genericType, Type type) =>
type?.GetTypeInfo()
.ImplementedInterfaces
.FirstOrDefault(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == genericType)
?.GetTypeInfo()
?.GetGenericArguments()[0];
}
class HandlerTypeDescriptorProvider : IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor>
{
private readonly ConcurrentDictionary<Type, string> MethodNames =
new ConcurrentDictionary<Type, string>();
internal readonly ILookup<string, IHandlerTypeDescriptor> KnownHandlers;
internal HandlerTypeDescriptorProvider(IEnumerable<Assembly> assemblies)
{
try
{
KnownHandlers = GetDescriptors(assemblies)
.ToLookup(x => x.Method, StringComparer.Ordinal);
}
catch (Exception e)
{
throw new AggregateException("Failed", e);
}
}
internal static IEnumerable<IHandlerTypeDescriptor> GetDescriptors(IEnumerable<Assembly> assemblies) => assemblies.SelectMany(
x => {
try
{
return x.GetTypes();
}
catch
{
return Enumerable.Empty<Type>();
}
}
)
.Where(z => ( z.IsInterface || ( z.IsClass && !z.IsAbstract ) ))
// running on mono this call can cause issues when scanning of the entire assembly.
.Where(
z => {
try
{
return typeof(IJsonRpcHandler).IsAssignableFrom(z);
}
catch
{
return false;
}
}
)
.Where(z => MethodAttribute.From(z) != null)
.Where(z => !z.Name.EndsWith("Manager")) // Manager interfaces are generally specializations around the handlers
.Select(HandlerTypeDescriptorHelper.GetMethodType)
.Distinct()
.ToLookup(x => MethodAttribute.From(x).Method)
.SelectMany(
x => x
.Distinct()
.Select(z => new HandlerTypeDescriptor(z) as IHandlerTypeDescriptor)
);
public IHandlerTypeDescriptor GetHandlerTypeDescriptor<A>() => GetHandlerTypeDescriptor(typeof(A));
public IHandlerTypeDescriptor GetHandlerTypeDescriptor(Type type)
{
var @default = KnownHandlers
.SelectMany(g => g)
.FirstOrDefault(x => x.InterfaceType == type || x.HandlerType == type || x.ParamsType == type);
if (@default != null)
{
return @default;
}
var methodName = GetMethodName(type);
return string.IsNullOrWhiteSpace(methodName) ? null : KnownHandlers[methodName].FirstOrDefault();
}
public string GetMethodName<T>() where T : IJsonRpcHandler => GetMethodName(typeof(T));
public bool IsMethodName(string name, params Type[] types) => types.Any(z => GetMethodName(z).Equals(name));
public string GetMethodName(Type type)
{
if (MethodNames.TryGetValue(type, out var method)) return method;
// Custom method
var attribute = MethodAttribute.From(type);
var handler = KnownHandlers.SelectMany(z => z)
.FirstOrDefault(z => z.InterfaceType == type || z.HandlerType == type || z.ParamsType == type);
if (handler != null)
{
return handler.Method;
}
// TODO: Log unknown method name
if (attribute is null)
{
return null;
}
MethodNames.TryAdd(type, attribute.Method);
return attribute.Method;
}
}
}