-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathHandlerTypeDescriptorHelper.cs
148 lines (128 loc) · 5.68 KB
/
HandlerTypeDescriptorHelper.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
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 static class HandlerTypeDescriptorHelper
{
private static readonly ConcurrentDictionary<Type, string> MethodNames =
new ConcurrentDictionary<Type, string>();
internal static readonly ILookup<string, IHandlerTypeDescriptor> KnownHandlers;
static HandlerTypeDescriptorHelper()
{
try
{
KnownHandlers = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(
x => {
try
{
return x.GetTypes();
}
catch
{
return Enumerable.Empty<Type>();
}
}
)
.Where(z => (z.IsInterface || (z.IsClass && !z.IsAbstract)) && typeof(IJsonRpcHandler).IsAssignableFrom(z))
.Where(z => MethodAttribute.From(z) != null)
.Where(z => !z.Name.EndsWith("Manager")) // Manager interfaces are generally specializations around the handlers
.Select(GetMethodType)
.Distinct()
.ToLookup(x => MethodAttribute.From(x).Method)
.SelectMany(x => x.Select(z => new HandlerTypeDescriptor(z) as IHandlerTypeDescriptor))
.ToLookup(x => x.Method, StringComparer.Ordinal);
}
catch (Exception e)
{
throw new AggregateException("Failed", e);
}
}
public static IHandlerTypeDescriptor GetHandlerTypeDescriptor<T>() => GetHandlerTypeDescriptor(typeof(T));
public static 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 static string GetMethodName<T>() where T : IJsonRpcHandler => GetMethodName(typeof(T));
public static bool IsMethodName(string name, params Type[] types) => types.Any(z => GetMethodName(z).Equals(name));
public static 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;
}
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(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);
}
}
public static Type UnwrapGenericType(Type genericType, Type type) =>
type?.GetTypeInfo()
.ImplementedInterfaces
.FirstOrDefault(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == genericType)
?.GetTypeInfo()
?.GetGenericArguments()[0];
}
}