| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Helper for discovering interceptor attributes from Roslyn symbols. |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class InterceptorDiscoveryHelper |
| | | 9 | | { |
| | | 10 | | private const string InterceptAttributePrefix = "NexusLabs.Needlr.InterceptAttribute"; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Result of interceptor discovery for a type. |
| | | 14 | | /// </summary> |
| | | 15 | | public readonly struct InterceptorInfo |
| | | 16 | | { |
| | | 17 | | public InterceptorInfo( |
| | | 18 | | string interceptorTypeName, |
| | | 19 | | int order, |
| | | 20 | | bool isMethodLevel, |
| | | 21 | | string? methodName) |
| | | 22 | | { |
| | 18 | 23 | | InterceptorTypeName = interceptorTypeName; |
| | 18 | 24 | | Order = order; |
| | 18 | 25 | | IsMethodLevel = isMethodLevel; |
| | 18 | 26 | | MethodName = methodName; |
| | 18 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary>Fully qualified name of the interceptor type.</summary> |
| | 36 | 30 | | public string InterceptorTypeName { get; } |
| | | 31 | | |
| | | 32 | | /// <summary>Order in which interceptor executes (lower = first).</summary> |
| | 18 | 33 | | public int Order { get; } |
| | | 34 | | |
| | | 35 | | /// <summary>True if this interceptor is applied at method level, false for class level.</summary> |
| | 0 | 36 | | public bool IsMethodLevel { get; } |
| | | 37 | | |
| | | 38 | | /// <summary>Method name if IsMethodLevel is true, null otherwise.</summary> |
| | 0 | 39 | | public string? MethodName { get; } |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets all Intercept attributes applied to a type (class-level only). |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 46 | | /// <returns>A list of interceptor info for each Intercept attribute found.</returns> |
| | | 47 | | public static IReadOnlyList<InterceptorInfo> GetInterceptAttributes(INamedTypeSymbol typeSymbol) |
| | | 48 | | { |
| | 15 | 49 | | var result = new List<InterceptorInfo>(); |
| | | 50 | | |
| | 92 | 51 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 52 | | { |
| | 31 | 53 | | var interceptorType = TryGetInterceptorType(attribute); |
| | 31 | 54 | | if (interceptorType is null) |
| | | 55 | | continue; |
| | | 56 | | |
| | 18 | 57 | | var order = GetInterceptorOrder(attribute); |
| | 18 | 58 | | var interceptorTypeName = TypeDiscoveryHelper.GetFullyQualifiedName(interceptorType); |
| | | 59 | | |
| | 18 | 60 | | result.Add(new InterceptorInfo(interceptorTypeName, order, isMethodLevel: false, methodName: null)); |
| | | 61 | | } |
| | | 62 | | |
| | 15 | 63 | | return result; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets all Intercept attributes applied to methods of a type. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 70 | | /// <returns>A list of interceptor info for each method-level Intercept attribute found.</returns> |
| | | 71 | | public static IReadOnlyList<InterceptorInfo> GetMethodLevelInterceptAttributes(INamedTypeSymbol typeSymbol) |
| | | 72 | | { |
| | 15 | 73 | | var result = new List<InterceptorInfo>(); |
| | | 74 | | |
| | 90 | 75 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 76 | | { |
| | 30 | 77 | | if (member is not IMethodSymbol methodSymbol) |
| | | 78 | | continue; |
| | | 79 | | |
| | 30 | 80 | | if (methodSymbol.MethodKind != MethodKind.Ordinary) |
| | | 81 | | continue; |
| | | 82 | | |
| | 30 | 83 | | foreach (var attribute in methodSymbol.GetAttributes()) |
| | | 84 | | { |
| | 0 | 85 | | var interceptorType = TryGetInterceptorType(attribute); |
| | 0 | 86 | | if (interceptorType is null) |
| | | 87 | | continue; |
| | | 88 | | |
| | 0 | 89 | | var order = GetInterceptorOrder(attribute); |
| | 0 | 90 | | var interceptorTypeName = TypeDiscoveryHelper.GetFullyQualifiedName(interceptorType); |
| | | 91 | | |
| | 0 | 92 | | result.Add(new InterceptorInfo(interceptorTypeName, order, isMethodLevel: true, methodName: methodSymbol |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 15 | 96 | | return result; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Checks if a type has any Intercept attributes (class or method level). |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 103 | | /// <returns>True if the type has at least one Intercept attribute.</returns> |
| | | 104 | | public static bool HasInterceptAttributes(INamedTypeSymbol typeSymbol) |
| | | 105 | | { |
| | | 106 | | // Check class-level |
| | 6828335 | 107 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 108 | | { |
| | 1993831 | 109 | | if (TryGetInterceptorType(attribute) is not null) |
| | 19 | 110 | | return true; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | // Check method-level |
| | 54184780 | 114 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 115 | | { |
| | 25672063 | 116 | | if (member is not IMethodSymbol methodSymbol) |
| | | 117 | | continue; |
| | | 118 | | |
| | 47820508 | 119 | | foreach (var attribute in methodSymbol.GetAttributes()) |
| | | 120 | | { |
| | 4340138 | 121 | | if (TryGetInterceptorType(attribute) is not null) |
| | 0 | 122 | | return true; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | 1420327 | 126 | | return false; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the interface methods that need to be proxied for interception. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="typeSymbol">The type symbol implementing the interface(s).</param> |
| | | 133 | | /// <param name="classLevelInterceptors">Interceptors applied at class level.</param> |
| | | 134 | | /// <param name="methodLevelInterceptors">Interceptors applied at method level.</param> |
| | | 135 | | /// <returns>A list of method infos that need to be generated in the proxy.</returns> |
| | | 136 | | public static IReadOnlyList<InterceptedMethodInfo> GetInterceptedMethods( |
| | | 137 | | INamedTypeSymbol typeSymbol, |
| | | 138 | | IReadOnlyList<InterceptorInfo> classLevelInterceptors, |
| | | 139 | | IReadOnlyList<InterceptorInfo> methodLevelInterceptors) |
| | | 140 | | { |
| | 15 | 141 | | var result = new List<InterceptedMethodInfo>(); |
| | 15 | 142 | | var methodNameToInterceptors = methodLevelInterceptors |
| | 0 | 143 | | .GroupBy(i => i.MethodName!) |
| | 15 | 144 | | .ToDictionary(g => g.Key, g => g.ToList()); |
| | | 145 | | |
| | 60 | 146 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 147 | | { |
| | 15 | 148 | | if (IsSystemInterface(iface)) |
| | | 149 | | continue; |
| | | 150 | | |
| | 60 | 151 | | foreach (var member in iface.GetMembers()) |
| | | 152 | | { |
| | 15 | 153 | | if (member is not IMethodSymbol methodSymbol) |
| | | 154 | | continue; |
| | | 155 | | |
| | 15 | 156 | | if (methodSymbol.MethodKind != MethodKind.Ordinary) |
| | | 157 | | continue; |
| | | 158 | | |
| | | 159 | | // Combine class-level and method-level interceptors for this method |
| | 15 | 160 | | var interceptors = new List<InterceptorInfo>(classLevelInterceptors); |
| | 15 | 161 | | if (methodNameToInterceptors.TryGetValue(methodSymbol.Name, out var methodInterceptors)) |
| | | 162 | | { |
| | 0 | 163 | | interceptors.AddRange(methodInterceptors); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // Always include the method - even if no interceptors, the proxy needs to forward the call |
| | 15 | 167 | | var methodInfo = new InterceptedMethodInfo( |
| | 15 | 168 | | methodSymbol.Name, |
| | 15 | 169 | | GetMethodReturnType(methodSymbol), |
| | 15 | 170 | | GetMethodParameters(methodSymbol), |
| | 15 | 171 | | TypeDiscoveryHelper.GetFullyQualifiedName(iface), |
| | 15 | 172 | | methodSymbol.IsAsync || IsTaskOrValueTask(methodSymbol.ReturnType), |
| | 15 | 173 | | methodSymbol.ReturnsVoid, |
| | 51 | 174 | | interceptors.OrderBy(i => i.Order).Select(i => i.InterceptorTypeName).ToArray()); |
| | | 175 | | |
| | 15 | 176 | | result.Add(methodInfo); |
| | | 177 | | } |
| | | 178 | | } |
| | | 179 | | |
| | 15 | 180 | | return result; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | private static INamedTypeSymbol? TryGetInterceptorType(AttributeData attribute) |
| | | 184 | | { |
| | 6334000 | 185 | | var attrClass = attribute.AttributeClass; |
| | 6334000 | 186 | | if (attrClass is null) |
| | 0 | 187 | | return null; |
| | | 188 | | |
| | | 189 | | // Check generic InterceptAttribute<T> |
| | 6334000 | 190 | | if (attrClass.IsGenericType) |
| | | 191 | | { |
| | 57 | 192 | | var unboundTypeName = attrClass.ConstructedFrom?.ToDisplayString(); |
| | 57 | 193 | | if (unboundTypeName is not null && unboundTypeName.StartsWith(InterceptAttributePrefix, StringComparison.Ord |
| | | 194 | | { |
| | 33 | 195 | | if (attrClass.TypeArguments.Length == 1 && attrClass.TypeArguments[0] is INamedTypeSymbol interceptorTyp |
| | | 196 | | { |
| | 33 | 197 | | return interceptorType; |
| | | 198 | | } |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | // Check non-generic InterceptAttribute(typeof(T)) |
| | 6333967 | 203 | | var attrClassName = attrClass.ToDisplayString(); |
| | 6333967 | 204 | | if (attrClassName == InterceptAttributePrefix) |
| | | 205 | | { |
| | | 206 | | // Get from constructor argument |
| | 4 | 207 | | if (attribute.ConstructorArguments.Length > 0 && |
| | 4 | 208 | | attribute.ConstructorArguments[0].Value is INamedTypeSymbol ctorInterceptorType) |
| | | 209 | | { |
| | 4 | 210 | | return ctorInterceptorType; |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | |
| | 6333963 | 214 | | return null; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | private static int GetInterceptorOrder(AttributeData attribute) |
| | | 218 | | { |
| | 41 | 219 | | foreach (var namedArg in attribute.NamedArguments) |
| | | 220 | | { |
| | 5 | 221 | | if (namedArg.Key == "Order" && namedArg.Value.Value is int orderValue) |
| | | 222 | | { |
| | 5 | 223 | | return orderValue; |
| | | 224 | | } |
| | | 225 | | } |
| | 13 | 226 | | return 0; |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | private static string GetMethodReturnType(IMethodSymbol methodSymbol) |
| | | 230 | | { |
| | 15 | 231 | | return methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | private static IReadOnlyList<MethodParameterInfo> GetMethodParameters(IMethodSymbol methodSymbol) |
| | | 235 | | { |
| | 15 | 236 | | var result = new List<MethodParameterInfo>(); |
| | 62 | 237 | | foreach (var param in methodSymbol.Parameters) |
| | | 238 | | { |
| | 16 | 239 | | result.Add(new MethodParameterInfo( |
| | 16 | 240 | | param.Name, |
| | 16 | 241 | | param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), |
| | 16 | 242 | | param.RefKind)); |
| | | 243 | | } |
| | 15 | 244 | | return result; |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | private static bool IsTaskOrValueTask(ITypeSymbol typeSymbol) |
| | | 248 | | { |
| | 15 | 249 | | var name = typeSymbol.ToDisplayString(); |
| | 15 | 250 | | return name.StartsWith("System.Threading.Tasks.Task", StringComparison.Ordinal) || |
| | 15 | 251 | | name.StartsWith("System.Threading.Tasks.ValueTask", StringComparison.Ordinal); |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | private static bool IsSystemInterface(INamedTypeSymbol interfaceSymbol) |
| | | 255 | | { |
| | 15 | 256 | | var ns = interfaceSymbol.ContainingNamespace?.ToDisplayString(); |
| | 15 | 257 | | return ns is not null && (ns.StartsWith("System", StringComparison.Ordinal) || |
| | 15 | 258 | | ns.StartsWith("Microsoft", StringComparison.Ordinal)); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Represents a method parameter for code generation. |
| | | 263 | | /// </summary> |
| | | 264 | | public readonly struct MethodParameterInfo |
| | | 265 | | { |
| | | 266 | | public MethodParameterInfo(string name, string typeName, RefKind refKind) |
| | | 267 | | { |
| | 16 | 268 | | Name = name; |
| | 16 | 269 | | TypeName = typeName; |
| | 16 | 270 | | RefKind = refKind; |
| | 16 | 271 | | } |
| | | 272 | | |
| | 48 | 273 | | public string Name { get; } |
| | 16 | 274 | | public string TypeName { get; } |
| | 32 | 275 | | public RefKind RefKind { get; } |
| | | 276 | | |
| | | 277 | | public string GetDeclaration() |
| | | 278 | | { |
| | 16 | 279 | | var refPrefix = RefKind switch |
| | 16 | 280 | | { |
| | 0 | 281 | | RefKind.Ref => "ref ", |
| | 0 | 282 | | RefKind.Out => "out ", |
| | 0 | 283 | | RefKind.In => "in ", |
| | 0 | 284 | | RefKind.RefReadOnlyParameter => "ref readonly ", |
| | 16 | 285 | | _ => "" |
| | 16 | 286 | | }; |
| | 16 | 287 | | return $"{refPrefix}{TypeName} {Name}"; |
| | | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | /// <summary> |
| | | 292 | | /// Represents a method that needs to be intercepted. |
| | | 293 | | /// </summary> |
| | | 294 | | public readonly struct InterceptedMethodInfo |
| | | 295 | | { |
| | | 296 | | public InterceptedMethodInfo( |
| | | 297 | | string name, |
| | | 298 | | string returnType, |
| | | 299 | | IReadOnlyList<MethodParameterInfo> parameters, |
| | | 300 | | string interfaceTypeName, |
| | | 301 | | bool isAsync, |
| | | 302 | | bool isVoid, |
| | | 303 | | string[] interceptorTypeNames) |
| | | 304 | | { |
| | 15 | 305 | | Name = name; |
| | 15 | 306 | | ReturnType = returnType; |
| | 15 | 307 | | Parameters = parameters; |
| | 15 | 308 | | InterfaceTypeName = interfaceTypeName; |
| | 15 | 309 | | IsAsync = isAsync; |
| | 15 | 310 | | IsVoid = isVoid; |
| | 15 | 311 | | InterceptorTypeNames = interceptorTypeNames; |
| | 15 | 312 | | } |
| | | 313 | | |
| | 63 | 314 | | public string Name { get; } |
| | 33 | 315 | | public string ReturnType { get; } |
| | 59 | 316 | | public IReadOnlyList<MethodParameterInfo> Parameters { get; } |
| | 30 | 317 | | public string InterfaceTypeName { get; } |
| | 26 | 318 | | public bool IsAsync { get; } |
| | 30 | 319 | | public bool IsVoid { get; } |
| | 84 | 320 | | public string[] InterceptorTypeNames { get; } |
| | | 321 | | |
| | | 322 | | public string GetParameterList() |
| | | 323 | | { |
| | 31 | 324 | | return string.Join(", ", Parameters.Select(p => p.GetDeclaration())); |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | public string GetArgumentList() |
| | | 328 | | { |
| | 15 | 329 | | return string.Join(", ", Parameters.Select(p => |
| | 15 | 330 | | { |
| | 16 | 331 | | var prefix = p.RefKind switch |
| | 16 | 332 | | { |
| | 0 | 333 | | RefKind.Ref => "ref ", |
| | 0 | 334 | | RefKind.Out => "out ", |
| | 0 | 335 | | RefKind.In => "in ", |
| | 16 | 336 | | _ => "" |
| | 16 | 337 | | }; |
| | 16 | 338 | | return prefix + p.Name; |
| | 15 | 339 | | })); |
| | | 340 | | } |
| | | 341 | | } |
| | | 342 | | } |