| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | using SharedHelper = NexusLabs.Needlr.Roslyn.Shared.TypeDiscoveryHelper; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.Generators; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Internal lifetime enum used by the generator to avoid runtime dependency on Attributes assembly. |
| | | 8 | | /// Maps 1:1 with InjectableLifetime in the Attributes package. |
| | | 9 | | /// </summary> |
| | | 10 | | internal enum GeneratorLifetime |
| | | 11 | | { |
| | | 12 | | Singleton = 0, |
| | | 13 | | Scoped = 1, |
| | | 14 | | Transient = 2 |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Helper utilities for discovering injectable types from Roslyn symbols. |
| | | 19 | | /// </summary> |
| | | 20 | | internal static class TypeDiscoveryHelper |
| | | 21 | | { |
| | | 22 | | private const string DoNotInjectAttributeName = "DoNotInjectAttribute"; |
| | | 23 | | private const string DoNotInjectAttributeFullName = "NexusLabs.Needlr.DoNotInjectAttribute"; |
| | | 24 | | private const string DeferToContainerAttributeName = "DeferToContainerAttribute"; |
| | | 25 | | private const string DeferToContainerAttributeFullName = "NexusLabs.Needlr.DeferToContainerAttribute"; |
| | | 26 | | private const string DecoratorForAttributePrefix = "NexusLabs.Needlr.DecoratorForAttribute"; |
| | | 27 | | private const string KeyedAttributeName = "KeyedAttribute"; |
| | | 28 | | private const string KeyedAttributeFullName = "NexusLabs.Needlr.KeyedAttribute"; |
| | | 29 | | private const string SingletonAttributeName = "SingletonAttribute"; |
| | | 30 | | private const string SingletonAttributeFullName = "NexusLabs.Needlr.SingletonAttribute"; |
| | | 31 | | private const string ScopedAttributeName = "ScopedAttribute"; |
| | | 32 | | private const string ScopedAttributeFullName = "NexusLabs.Needlr.ScopedAttribute"; |
| | | 33 | | private const string TransientAttributeName = "TransientAttribute"; |
| | | 34 | | private const string TransientAttributeFullName = "NexusLabs.Needlr.TransientAttribute"; |
| | | 35 | | private const string GenerateFactoryAttributeName = "GenerateFactoryAttribute"; |
| | | 36 | | private const string GenerateFactoryAttributeFullName = "NexusLabs.Needlr.Generators.GenerateFactoryAttribute"; |
| | | 37 | | private const string OptionsAttributeName = "OptionsAttribute"; |
| | | 38 | | private const string OptionsAttributeFullName = "NexusLabs.Needlr.Generators.OptionsAttribute"; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Determines whether a type symbol represents a concrete injectable type. |
| | | 42 | | /// Delegates to the shared library for consistency with analyzers. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 45 | | /// <param name="isCurrentAssembly">True if the type is from the current compilation's assembly (allows internal typ |
| | | 46 | | /// <returns>True if the type is a valid injectable type; otherwise, false.</returns> |
| | | 47 | | public static bool IsInjectableType(INamedTypeSymbol typeSymbol, bool isCurrentAssembly = false) |
| | 1457253 | 48 | | => SharedHelper.IsInjectableType(typeSymbol, isCurrentAssembly); |
| | | 49 | | |
| | | 50 | | private const string RegisterAsAttributePrefix = "NexusLabs.Needlr.RegisterAsAttribute"; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the interfaces that should be registered for a type. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="typeSymbol">The type symbol to get interfaces for.</param> |
| | | 56 | | /// <param name="compilationAssembly"> |
| | | 57 | | /// The assembly of the current compilation. Used to determine whether an internal |
| | | 58 | | /// interface is accessible from the generated code. Same-assembly internal interfaces |
| | | 59 | | /// are valid registration targets because the generated TypeRegistry is emitted into |
| | | 60 | | /// the same compilation unit. Cross-assembly internal interfaces (e.g., Avalonia's |
| | | 61 | | /// <c>IContentPresenterHost</c>) are inaccessible and must be skipped to avoid CS0122. |
| | | 62 | | /// </param> |
| | | 63 | | /// <returns>A list of interface symbols suitable for registration.</returns> |
| | | 64 | | public static IReadOnlyList<INamedTypeSymbol> GetRegisterableInterfaces( |
| | | 65 | | INamedTypeSymbol typeSymbol, |
| | | 66 | | IAssemblySymbol? compilationAssembly = null) |
| | | 67 | | { |
| | | 68 | | // Check for [RegisterAs<T>] attributes - if present, only register as those interfaces |
| | 233598 | 69 | | var registerAsInterfaces = GetRegisterAsInterfaces(typeSymbol); |
| | 233598 | 70 | | if (registerAsInterfaces.Count > 0) |
| | | 71 | | { |
| | 0 | 72 | | return registerAsInterfaces; |
| | | 73 | | } |
| | | 74 | | |
| | 233598 | 75 | | var result = new List<INamedTypeSymbol>(); |
| | | 76 | | |
| | | 77 | | // Get all constructor parameter types to detect decorator pattern |
| | 233598 | 78 | | var constructorParamTypes = GetConstructorParameterTypes(typeSymbol); |
| | | 79 | | |
| | 892478 | 80 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 81 | | { |
| | 212641 | 82 | | if (iface.IsUnboundGenericType) |
| | | 83 | | continue; |
| | | 84 | | |
| | 212641 | 85 | | if (IsSystemInterface(iface)) |
| | | 86 | | continue; |
| | | 87 | | |
| | | 88 | | // Skip interfaces that are inaccessible from the generated code. |
| | | 89 | | // |
| | | 90 | | // WHY THIS EXISTS: The generated TypeRegistry emits typeof(IFoo) for each |
| | | 91 | | // registered interface. If IFoo is internal to a DIFFERENT assembly, the |
| | | 92 | | // generated code (which lives in THIS compilation) cannot access it → CS0122. |
| | | 93 | | // |
| | | 94 | | // CRITICAL: Same-assembly internal interfaces MUST be kept. This is the |
| | | 95 | | // standard .NET DI pattern — an internal class implements an internal interface, |
| | | 96 | | // both in the same project. The generated TypeRegistry is emitted into that |
| | | 97 | | // same compilation and CAN legally reference typeof(InternalInterface). |
| | | 98 | | // |
| | | 99 | | // Example that MUST work (same assembly): |
| | | 100 | | // internal interface IAuthConfig { ... } |
| | | 101 | | // internal class AuthConfig : IAuthConfig { ... } |
| | | 102 | | // → typeof(IAuthConfig) in generated code is VALID |
| | | 103 | | // |
| | | 104 | | // Example that MUST be skipped (cross-assembly, e.g., Avalonia): |
| | | 105 | | // // In Avalonia.Controls.dll (internal): |
| | | 106 | | // internal interface IContentPresenterHost { ... } |
| | | 107 | | // // In consumer app: |
| | | 108 | | // public class MainWindow : Window { ... } // inherits IContentPresenterHost via Window |
| | | 109 | | // → typeof(IContentPresenterHost) in generated code produces CS0122 |
| | 331 | 110 | | if (!IsAccessibleFromGeneratedCode(iface, compilationAssembly)) |
| | | 111 | | continue; |
| | | 112 | | |
| | 325 | 113 | | if (HasDoNotAutoRegisterAttributeDirect(iface)) |
| | | 114 | | continue; |
| | | 115 | | |
| | | 116 | | // Skip interfaces that this type also takes as constructor parameters (decorator pattern) |
| | | 117 | | // A type that implements IFoo and takes IFoo in its constructor is likely a decorator |
| | | 118 | | // and should not be auto-registered as IFoo to avoid circular dependencies |
| | 323 | 119 | | if (IsDecoratorInterface(iface, constructorParamTypes)) |
| | | 120 | | continue; |
| | | 121 | | |
| | | 122 | | // Skip IHostedService - hosted services are registered separately via RegisterHostedServices() |
| | | 123 | | // to ensure proper concrete + interface forwarding pattern |
| | 295 | 124 | | if (IsHostedServiceInterface(iface)) |
| | | 125 | | continue; |
| | | 126 | | |
| | 288 | 127 | | result.Add(iface); |
| | | 128 | | } |
| | | 129 | | |
| | 233598 | 130 | | return result; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets interface types specified by [RegisterAs<T>] attributes on the type. |
| | | 135 | | /// </summary> |
| | | 136 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 137 | | /// <returns>A list of interface symbols from RegisterAs attributes.</returns> |
| | | 138 | | public static IReadOnlyList<INamedTypeSymbol> GetRegisterAsInterfaces(INamedTypeSymbol typeSymbol) |
| | | 139 | | { |
| | 233598 | 140 | | var result = new List<INamedTypeSymbol>(); |
| | | 141 | | |
| | 1319424 | 142 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 143 | | { |
| | 426114 | 144 | | var attrClass = attribute.AttributeClass; |
| | 426114 | 145 | | if (attrClass == null) |
| | | 146 | | continue; |
| | | 147 | | |
| | | 148 | | // Check for RegisterAsAttribute<T> |
| | 426114 | 149 | | var attrFullName = attrClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 426114 | 150 | | if (!attrFullName.StartsWith("global::" + RegisterAsAttributePrefix, StringComparison.Ordinal)) |
| | | 151 | | continue; |
| | | 152 | | |
| | | 153 | | // Get the type argument |
| | 0 | 154 | | if (attrClass.IsGenericType && attrClass.TypeArguments.Length == 1) |
| | | 155 | | { |
| | 0 | 156 | | if (attrClass.TypeArguments[0] is INamedTypeSymbol interfaceType) |
| | | 157 | | { |
| | 0 | 158 | | result.Add(interfaceType); |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | |
| | 233598 | 163 | | return result; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets all parameter types from all constructors of a type. |
| | | 168 | | /// </summary> |
| | | 169 | | private static HashSet<string> GetConstructorParameterTypes(INamedTypeSymbol typeSymbol) |
| | | 170 | | { |
| | 233598 | 171 | | var paramTypes = new HashSet<string>(StringComparer.Ordinal); |
| | | 172 | | |
| | 1505690 | 173 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 174 | | { |
| | 519247 | 175 | | if (ctor.IsStatic) |
| | | 176 | | continue; |
| | | 177 | | |
| | 2229938 | 178 | | foreach (var param in ctor.Parameters) |
| | | 179 | | { |
| | 595722 | 180 | | var paramTypeName = param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 595722 | 181 | | paramTypes.Add(paramTypeName); |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | |
| | 233598 | 185 | | return paramTypes; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Checks if an interface is a decorator interface (also taken as a constructor parameter). |
| | | 190 | | /// </summary> |
| | | 191 | | private static bool IsDecoratorInterface(INamedTypeSymbol iface, HashSet<string> constructorParamTypes) |
| | | 192 | | { |
| | 323 | 193 | | var ifaceName = iface.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | 323 | 194 | | return constructorParamTypes.Contains(ifaceName); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Gets the fully qualified name for a type symbol suitable for code generation. |
| | | 199 | | /// For generic type definitions (open generics), outputs open generic syntax (e.g., MyClass<>). |
| | | 200 | | /// For constructed generics with concrete type arguments, outputs the full type (e.g., MyClass<int>). |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="typeSymbol">The type symbol.</param> |
| | | 203 | | /// <returns>The fully qualified type name with global:: prefix.</returns> |
| | | 204 | | public static string GetFullyQualifiedName(INamedTypeSymbol typeSymbol) |
| | | 205 | | { |
| | | 206 | | // Check if this is an open generic type definition (has type parameters, not type arguments) |
| | | 207 | | // e.g., JobScheduler<TJob> where TJob is a TypeParameter, not a concrete type |
| | | 208 | | // We need to convert these to open generic syntax: JobScheduler<> |
| | 4233050 | 209 | | if (typeSymbol.TypeParameters.Length > 0 && !typeSymbol.IsUnboundGenericType) |
| | | 210 | | { |
| | | 211 | | // Check if type arguments are still type parameters (meaning this is a generic definition) |
| | | 212 | | // For a closed generic like ILogger<MyService>, TypeArguments contains MyService (a NamedTypeSymbol) |
| | | 213 | | // For an open generic like JobScheduler<TJob>, TypeArguments contains TJob (a TypeParameterSymbol) |
| | 410907 | 214 | | var hasUnresolvedTypeParameters = typeSymbol.TypeArguments.Any(ta => ta.TypeKind == TypeKind.TypeParameter); |
| | | 215 | | |
| | 202362 | 216 | | if (hasUnresolvedTypeParameters) |
| | | 217 | | { |
| | | 218 | | // Build the open generic name manually |
| | 61004 | 219 | | var containingNamespace = typeSymbol.ContainingNamespace?.ToDisplayString(); |
| | 61004 | 220 | | var typeName = typeSymbol.Name; |
| | 61004 | 221 | | var arity = typeSymbol.TypeParameters.Length; |
| | | 222 | | |
| | | 223 | | // Create the open generic syntax: MyClass<,> for 2 type params, MyClass<> for 1 |
| | 61004 | 224 | | var commas = arity > 1 ? new string(',', arity - 1) : string.Empty; |
| | 61004 | 225 | | var openGenericPart = $"<{commas}>"; |
| | | 226 | | |
| | 61004 | 227 | | if (string.IsNullOrEmpty(containingNamespace) || containingNamespace == "<global namespace>") |
| | | 228 | | { |
| | 0 | 229 | | return $"global::{typeName}{openGenericPart}"; |
| | | 230 | | } |
| | | 231 | | |
| | 61004 | 232 | | return $"global::{containingNamespace}.{typeName}{openGenericPart}"; |
| | | 233 | | } |
| | | 234 | | } |
| | | 235 | | |
| | 4172046 | 236 | | return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Checks if a type matches any of the given namespace prefixes. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 243 | | /// <param name="namespacePrefixes">The namespace prefixes to match.</param> |
| | | 244 | | /// <returns>True if the type's namespace starts with any of the prefixes.</returns> |
| | | 245 | | public static bool MatchesNamespacePrefix(INamedTypeSymbol typeSymbol, IReadOnlyList<string>? namespacePrefixes) |
| | | 246 | | { |
| | 1838404 | 247 | | if (namespacePrefixes == null || namespacePrefixes.Count == 0) |
| | 1530009 | 248 | | return true; |
| | | 249 | | |
| | 308395 | 250 | | var typeNamespace = typeSymbol.ContainingNamespace?.ToDisplayString() ?? string.Empty; |
| | | 251 | | |
| | | 252 | | // Check if type is in the global namespace |
| | 308395 | 253 | | var isGlobalNamespace = typeSymbol.ContainingNamespace?.IsGlobalNamespace == true; |
| | | 254 | | |
| | 1240988 | 255 | | foreach (var prefix in namespacePrefixes) |
| | | 256 | | { |
| | | 257 | | // Empty string prefix matches global namespace types |
| | 312383 | 258 | | if (string.IsNullOrEmpty(prefix)) |
| | | 259 | | { |
| | 7975 | 260 | | if (isGlobalNamespace) |
| | 346 | 261 | | return true; |
| | | 262 | | continue; |
| | | 263 | | } |
| | | 264 | | |
| | 304408 | 265 | | if (IsNamespacePrefixMatch(typeNamespace, prefix)) |
| | 222 | 266 | | return true; |
| | | 267 | | } |
| | | 268 | | |
| | 307827 | 269 | | return false; |
| | 568 | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Checks if a type matches any of the given exclusion namespace prefixes. |
| | | 274 | | /// Returns <c>true</c> if the type should be EXCLUDED (its namespace starts with |
| | | 275 | | /// any of the exclusion prefixes). |
| | | 276 | | /// </summary> |
| | | 277 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 278 | | /// <param name="excludeNamespacePrefixes">The namespace prefixes to exclude. If null or empty, nothing is excluded. |
| | | 279 | | /// <returns>True if the type should be excluded from the registry.</returns> |
| | | 280 | | public static bool MatchesExclusionFilter(INamedTypeSymbol typeSymbol, IReadOnlyList<string>? excludeNamespacePrefix |
| | | 281 | | { |
| | 1530574 | 282 | | if (excludeNamespacePrefixes == null || excludeNamespacePrefixes.Count == 0) |
| | 1530574 | 283 | | return false; |
| | | 284 | | |
| | 0 | 285 | | var typeNamespace = typeSymbol.ContainingNamespace?.ToDisplayString() ?? string.Empty; |
| | | 286 | | |
| | 0 | 287 | | foreach (var prefix in excludeNamespacePrefixes) |
| | | 288 | | { |
| | 0 | 289 | | if (string.IsNullOrEmpty(prefix)) |
| | | 290 | | continue; |
| | | 291 | | |
| | 0 | 292 | | if (IsNamespacePrefixMatch(typeNamespace, prefix)) |
| | 0 | 293 | | return true; |
| | | 294 | | } |
| | | 295 | | |
| | 0 | 296 | | return false; |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Dot-boundary-aware prefix match. <c>"Avalonia"</c> matches <c>"Avalonia"</c> |
| | | 301 | | /// and <c>"Avalonia.Controls"</c> but NOT <c>"AvaloniaDemoApp"</c>. |
| | | 302 | | /// A prefix ending with <c>"."</c> requires an exact sub-namespace match |
| | | 303 | | /// (e.g., <c>"Avalonia."</c> matches only <c>"Avalonia.Controls"</c>, not <c>"Avalonia"</c> itself). |
| | | 304 | | /// </summary> |
| | | 305 | | private static bool IsNamespacePrefixMatch(string typeNamespace, string prefix) |
| | | 306 | | { |
| | 304408 | 307 | | if (!typeNamespace.StartsWith(prefix, StringComparison.Ordinal)) |
| | 304186 | 308 | | return false; |
| | | 309 | | |
| | | 310 | | // Exact match (namespace equals prefix) |
| | 222 | 311 | | if (typeNamespace.Length == prefix.Length) |
| | 216 | 312 | | return true; |
| | | 313 | | |
| | | 314 | | // Prefix already ends with dot — the StartsWith check is sufficient |
| | | 315 | | // (e.g., prefix "Avalonia." matches "Avalonia.Controls") |
| | 6 | 316 | | if (prefix[prefix.Length - 1] == '.') |
| | 0 | 317 | | return true; |
| | | 318 | | |
| | | 319 | | // Sub-namespace: next char after prefix must be a dot |
| | | 320 | | // "Avalonia" matches "Avalonia.Controls" but not "AvaloniaDemoApp" |
| | 6 | 321 | | return typeNamespace[prefix.Length] == '.'; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Recursively iterates all named type symbols in a namespace. |
| | | 326 | | /// </summary> |
| | | 327 | | /// <param name="namespaceSymbol">The namespace to iterate.</param> |
| | | 328 | | /// <returns>All named type symbols in the namespace and nested namespaces.</returns> |
| | | 329 | | public static IEnumerable<INamedTypeSymbol> GetAllTypes(INamespaceSymbol namespaceSymbol) |
| | | 330 | | { |
| | 10958720 | 331 | | foreach (var member in namespaceSymbol.GetMembers()) |
| | | 332 | | { |
| | 4499052 | 333 | | if (member is INamedTypeSymbol typeSymbol) |
| | | 334 | | { |
| | 3675573 | 335 | | yield return typeSymbol; |
| | | 336 | | } |
| | 823479 | 337 | | else if (member is INamespaceSymbol nestedNamespace) |
| | | 338 | | { |
| | 20397174 | 339 | | foreach (var nestedType in GetAllTypes(nestedNamespace)) |
| | | 340 | | { |
| | 9375108 | 341 | | yield return nestedType; |
| | | 342 | | } |
| | | 343 | | } |
| | | 344 | | } |
| | 980308 | 345 | | } |
| | | 346 | | |
| | | 347 | | private static bool HasDoNotAutoRegisterAttribute(INamedTypeSymbol typeSymbol) |
| | 3876 | 348 | | => SharedHelper.HasDoNotAutoRegisterAttribute(typeSymbol); |
| | | 349 | | |
| | | 350 | | private static bool HasDoNotAutoRegisterAttributeDirect(INamedTypeSymbol typeSymbol) |
| | 770850 | 351 | | => SharedHelper.HasDoNotAutoRegisterAttributeDirect(typeSymbol); |
| | | 352 | | |
| | | 353 | | private static bool IsCompilerGenerated(INamedTypeSymbol typeSymbol) |
| | 842477 | 354 | | => SharedHelper.IsCompilerGenerated(typeSymbol); |
| | | 355 | | |
| | | 356 | | private static bool InheritsFrom(INamedTypeSymbol typeSymbol, string baseTypeName) |
| | 4601013 | 357 | | => SharedHelper.InheritsFrom(typeSymbol, baseTypeName); |
| | | 358 | | |
| | | 359 | | /// <summary> |
| | | 360 | | /// Determines whether a type is a .NET MAUI per-platform application entry point — the Windows |
| | | 361 | | /// <c>App : Microsoft.Maui.MauiWinUIApplication</c>, the Android |
| | | 362 | | /// <c>MainApplication : Microsoft.Maui.MauiApplication</c>, or the iOS/Mac |
| | | 363 | | /// <c>AppDelegate : Microsoft.Maui.MauiUIApplicationDelegate</c> that live under <c>Platforms/</c>. |
| | | 364 | | /// </summary> |
| | | 365 | | /// <remarks> |
| | | 366 | | /// These types are framework-owned and constructed by the MAUI platform host; they are never |
| | | 367 | | /// resolved as Needlr services. They are also decorated by the platform's own source generators |
| | | 368 | | /// with interop members (for example WinRT plumbing such as <c>ApplicationRcwFactoryAttribute</c>) |
| | | 369 | | /// that are inaccessible from generated code, so including them in the registry breaks the head |
| | | 370 | | /// build. They are therefore excluded from all discovery. The cross-platform |
| | | 371 | | /// <c>App : Microsoft.Maui.Controls.Application</c>, pages, views, and view models are ordinary |
| | | 372 | | /// types and remain scannable. |
| | | 373 | | /// </remarks> |
| | | 374 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 375 | | /// <returns><see langword="true"/> if the type derives from a MAUI platform application base type.</returns> |
| | | 376 | | public static bool IsMauiPlatformEntryType(INamedTypeSymbol typeSymbol) |
| | 1530574 | 377 | | => InheritsFrom(typeSymbol, "Microsoft.Maui.MauiWinUIApplication") |
| | 1530574 | 378 | | || InheritsFrom(typeSymbol, "Microsoft.Maui.MauiApplication") |
| | 1530574 | 379 | | || InheritsFrom(typeSymbol, "Microsoft.Maui.MauiUIApplicationDelegate"); |
| | | 380 | | |
| | | 381 | | private static bool IsSystemInterface(INamedTypeSymbol interfaceSymbol) |
| | 509782 | 382 | | => SharedHelper.IsSystemType(interfaceSymbol); |
| | | 383 | | |
| | | 384 | | private static bool IsHostedServiceInterface(INamedTypeSymbol interfaceSymbol) |
| | | 385 | | { |
| | 295 | 386 | | var fullName = GetFullyQualifiedName(interfaceSymbol); |
| | 295 | 387 | | return fullName == "global::Microsoft.Extensions.Hosting.IHostedService"; |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | /// <summary> |
| | | 391 | | /// Determines whether a type is a hosted service (implements IHostedService or inherits from BackgroundService). |
| | | 392 | | /// </summary> |
| | | 393 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 394 | | /// <param name="isCurrentAssembly">True if the type is from the current compilation's assembly.</param> |
| | | 395 | | /// <returns>True if the type is a hosted service.</returns> |
| | | 396 | | public static bool IsHostedServiceType(INamedTypeSymbol typeSymbol, bool isCurrentAssembly = false) |
| | | 397 | | { |
| | | 398 | | // Must be a concrete, non-abstract class |
| | 1457236 | 399 | | if (typeSymbol.IsAbstract || typeSymbol.TypeKind != TypeKind.Class) |
| | 686711 | 400 | | return false; |
| | | 401 | | |
| | | 402 | | // Check accessibility |
| | 770525 | 403 | | if (!isCurrentAssembly && IsInternalOrLessAccessible(typeSymbol)) |
| | 0 | 404 | | return false; |
| | | 405 | | |
| | | 406 | | // Skip if marked with [DoNotAutoRegister] |
| | 770525 | 407 | | if (HasDoNotAutoRegisterAttributeDirect(typeSymbol)) |
| | 2 | 408 | | return false; |
| | | 409 | | |
| | | 410 | | // Skip compiler-generated types |
| | 770523 | 411 | | if (IsCompilerGenerated(typeSymbol)) |
| | 0 | 412 | | return false; |
| | | 413 | | |
| | | 414 | | // Skip decorators - types with [DecoratorFor<IHostedService>] should not be |
| | | 415 | | // registered as hosted services (they decorate hosted services, not are hosted services) |
| | 770523 | 416 | | if (IsDecoratorForHostedService(typeSymbol)) |
| | 0 | 417 | | return false; |
| | | 418 | | |
| | | 419 | | // Check if inherits from BackgroundService |
| | 770523 | 420 | | if (InheritsFromBackgroundService(typeSymbol)) |
| | 6 | 421 | | return true; |
| | | 422 | | |
| | | 423 | | // Check if directly implements IHostedService (not via BackgroundService) |
| | 770517 | 424 | | if (ImplementsIHostedService(typeSymbol)) |
| | 1 | 425 | | return true; |
| | | 426 | | |
| | 770516 | 427 | | return false; |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | private static bool IsDecoratorForHostedService(INamedTypeSymbol typeSymbol) |
| | | 431 | | { |
| | 4446674 | 432 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 433 | | { |
| | 1452814 | 434 | | var attrClass = attribute.AttributeClass; |
| | 1452814 | 435 | | if (attrClass == null) |
| | | 436 | | continue; |
| | | 437 | | |
| | 1452814 | 438 | | var attrFullName = attrClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | | 439 | | |
| | | 440 | | // Check for DecoratorForAttribute<IHostedService> |
| | 1452814 | 441 | | if (attrFullName.StartsWith("global::NexusLabs.Needlr.DecoratorForAttribute<", StringComparison.Ordinal)) |
| | | 442 | | { |
| | | 443 | | // Get the type argument |
| | 19 | 444 | | if (attrClass.IsGenericType && attrClass.TypeArguments.Length == 1) |
| | | 445 | | { |
| | 19 | 446 | | var typeArg = attrClass.TypeArguments[0]; |
| | 19 | 447 | | if (typeArg is INamedTypeSymbol namedTypeArg) |
| | | 448 | | { |
| | 19 | 449 | | var typeArgName = GetFullyQualifiedName(namedTypeArg); |
| | 19 | 450 | | if (typeArgName == "global::Microsoft.Extensions.Hosting.IHostedService") |
| | 0 | 451 | | return true; |
| | | 452 | | } |
| | | 453 | | } |
| | | 454 | | } |
| | | 455 | | } |
| | 770523 | 456 | | return false; |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | private static bool InheritsFromBackgroundService(INamedTypeSymbol typeSymbol) |
| | | 460 | | { |
| | 1412003 | 461 | | var baseType = typeSymbol.BaseType; |
| | 4225979 | 462 | | while (baseType != null) |
| | | 463 | | { |
| | 2813989 | 464 | | var fullName = GetFullyQualifiedName(baseType); |
| | 2813989 | 465 | | if (fullName == "global::Microsoft.Extensions.Hosting.BackgroundService") |
| | 13 | 466 | | return true; |
| | 2813976 | 467 | | baseType = baseType.BaseType; |
| | | 468 | | } |
| | 1411990 | 469 | | return false; |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | private static bool ImplementsIHostedService(INamedTypeSymbol typeSymbol) |
| | | 473 | | { |
| | 4807834 | 474 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 475 | | { |
| | 991928 | 476 | | var fullName = GetFullyQualifiedName(iface); |
| | 991928 | 477 | | if (fullName == "global::Microsoft.Extensions.Hosting.IHostedService") |
| | 2 | 478 | | return true; |
| | | 479 | | } |
| | 1411988 | 480 | | return false; |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | private static bool IsSystemType(INamedTypeSymbol typeSymbol) |
| | 408630 | 484 | | => SharedHelper.IsSystemType(typeSymbol); |
| | | 485 | | |
| | | 486 | | private static bool HasUnsatisfiedRequiredMembers(INamedTypeSymbol typeSymbol) |
| | 405152 | 487 | | => SharedHelper.HasUnsatisfiedRequiredMembers(typeSymbol); |
| | | 488 | | |
| | | 489 | | /// <summary> |
| | | 490 | | /// Checks if a type is accessible from generated code. |
| | | 491 | | /// For types in the current assembly, internal and public types are accessible. |
| | | 492 | | /// For types in referenced assemblies, only public types are accessible. |
| | | 493 | | /// </summary> |
| | | 494 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 495 | | /// <param name="isCurrentAssembly">True if the type is from the current compilation's assembly.</param> |
| | | 496 | | /// <returns>True if the type is accessible from generated code.</returns> |
| | | 497 | | private static bool IsAccessibleFromGeneratedCode(INamedTypeSymbol typeSymbol, bool isCurrentAssembly) |
| | 909884 | 498 | | => SharedHelper.IsAccessibleFromGeneratedCode(typeSymbol, isCurrentAssembly); |
| | | 499 | | |
| | | 500 | | /// <summary> |
| | | 501 | | /// Checks if a type would be registerable as injectable, ignoring accessibility constraints. |
| | | 502 | | /// This is used to detect internal types that match namespace filters but cannot be included. |
| | | 503 | | /// </summary> |
| | | 504 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 505 | | /// <returns>True if the type would be injectable if it were accessible.</returns> |
| | | 506 | | public static bool WouldBeInjectableIgnoringAccessibility(INamedTypeSymbol typeSymbol) |
| | | 507 | | { |
| | | 508 | | // Must be a class (not interface, struct, enum, delegate) |
| | 73351 | 509 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 14 | 510 | | return false; |
| | | 511 | | |
| | 73337 | 512 | | if (typeSymbol.IsAbstract) |
| | 0 | 513 | | return false; |
| | | 514 | | |
| | 73337 | 515 | | if (typeSymbol.IsStatic) |
| | 1176 | 516 | | return false; |
| | | 517 | | |
| | 72161 | 518 | | if (typeSymbol.IsUnboundGenericType) |
| | 0 | 519 | | return false; |
| | | 520 | | |
| | | 521 | | // Exclude open generic types (type definitions with type parameters like MyClass<T>) |
| | 72161 | 522 | | if (typeSymbol.TypeParameters.Length > 0) |
| | 207 | 523 | | return false; |
| | | 524 | | |
| | 71954 | 525 | | if (typeSymbol.ContainingType != null) |
| | 0 | 526 | | return false; |
| | | 527 | | |
| | 71954 | 528 | | if (IsCompilerGenerated(typeSymbol)) |
| | 67306 | 529 | | return false; |
| | | 530 | | |
| | 4648 | 531 | | if (InheritsFrom(typeSymbol, "System.Exception")) |
| | 0 | 532 | | return false; |
| | | 533 | | |
| | 4648 | 534 | | if (InheritsFrom(typeSymbol, "System.Attribute")) |
| | 772 | 535 | | return false; |
| | | 536 | | |
| | 3876 | 537 | | if (typeSymbol.IsRecord) |
| | 0 | 538 | | return false; |
| | | 539 | | |
| | 3876 | 540 | | if (HasDoNotAutoRegisterAttribute(typeSymbol)) |
| | 2 | 541 | | return false; |
| | | 542 | | |
| | | 543 | | // Must have a determinable lifetime to be injectable |
| | 3874 | 544 | | var lifetime = DetermineLifetime(typeSymbol); |
| | 3874 | 545 | | if (!lifetime.HasValue) |
| | 1544 | 546 | | return false; |
| | | 547 | | |
| | 2330 | 548 | | return true; |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | /// <summary> |
| | | 552 | | /// Checks if a type would be registerable as a plugin, ignoring accessibility constraints. |
| | | 553 | | /// This is used to detect internal types that match namespace filters but cannot be included. |
| | | 554 | | /// </summary> |
| | | 555 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 556 | | /// <param name="compilationAssembly"> |
| | | 557 | | /// The compilation's assembly for same-assembly accessibility checks. |
| | | 558 | | /// Passed through to <see cref="GetPluginInterfaces"/> so that same-assembly internal |
| | | 559 | | /// interfaces are correctly recognized as plugin interfaces. |
| | | 560 | | /// </param> |
| | | 561 | | /// <returns>True if the type would be a plugin if it were accessible.</returns> |
| | | 562 | | public static bool WouldBePluginIgnoringAccessibility( |
| | | 563 | | INamedTypeSymbol typeSymbol, |
| | | 564 | | IAssemblySymbol? compilationAssembly = null) |
| | | 565 | | { |
| | | 566 | | // Must be a concrete class |
| | 71039 | 567 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 14 | 568 | | return false; |
| | | 569 | | |
| | 71025 | 570 | | if (typeSymbol.IsAbstract) |
| | 0 | 571 | | return false; |
| | | 572 | | |
| | 71025 | 573 | | if (typeSymbol.IsStatic) |
| | 1176 | 574 | | return false; |
| | | 575 | | |
| | 69849 | 576 | | if (typeSymbol.IsUnboundGenericType) |
| | 0 | 577 | | return false; |
| | | 578 | | |
| | | 579 | | // Exclude open generic types (type definitions with type parameters like MyClass<T>) |
| | 69849 | 580 | | if (typeSymbol.TypeParameters.Length > 0) |
| | 207 | 581 | | return false; |
| | | 582 | | |
| | | 583 | | // NOTE: Records ARE allowed as plugins (they are classes with parameterless constructors). |
| | | 584 | | // Records are excluded from IsInjectableType (auto-registration) but not from plugin discovery. |
| | | 585 | | |
| | | 586 | | // Must have a parameterless constructor |
| | 69642 | 587 | | if (!HasParameterlessConstructor(typeSymbol)) |
| | 1534 | 588 | | return false; |
| | | 589 | | |
| | | 590 | | // Must have at least one plugin interface |
| | 68108 | 591 | | var pluginInterfaces = GetPluginInterfaces(typeSymbol, compilationAssembly); |
| | 68108 | 592 | | return pluginInterfaces.Count > 0; |
| | | 593 | | } |
| | | 594 | | |
| | | 595 | | /// <summary> |
| | | 596 | | /// Checks if a type is internal (not public) and would be inaccessible from generated code |
| | | 597 | | /// in a different assembly. |
| | | 598 | | /// </summary> |
| | | 599 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 600 | | /// <returns>True if the type is internal or less accessible.</returns> |
| | | 601 | | public static bool IsInternalOrLessAccessible(INamedTypeSymbol typeSymbol) |
| | | 602 | | { |
| | | 603 | | // Check the type itself |
| | 4136545 | 604 | | if (typeSymbol.DeclaredAccessibility != Accessibility.Public) |
| | 160733 | 605 | | return true; |
| | | 606 | | |
| | | 607 | | // Check all containing types (for nested types) |
| | 3975812 | 608 | | var containingType = typeSymbol.ContainingType; |
| | 3975812 | 609 | | while (containingType != null) |
| | | 610 | | { |
| | 0 | 611 | | if (containingType.DeclaredAccessibility != Accessibility.Public) |
| | 0 | 612 | | return true; |
| | 0 | 613 | | containingType = containingType.ContainingType; |
| | | 614 | | } |
| | | 615 | | |
| | 3975812 | 616 | | return false; |
| | | 617 | | } |
| | | 618 | | |
| | | 619 | | /// <summary> |
| | | 620 | | /// Determines whether a type symbol is accessible from generated code emitted into |
| | | 621 | | /// the given compilation assembly. |
| | | 622 | | /// </summary> |
| | | 623 | | /// <remarks> |
| | | 624 | | /// <para> |
| | | 625 | | /// The generated TypeRegistry is emitted into the compilation's assembly. It can |
| | | 626 | | /// reference any type that is accessible from that assembly: |
| | | 627 | | /// </para> |
| | | 628 | | /// <list type="bullet"> |
| | | 629 | | /// <item><c>public</c> types — always accessible.</item> |
| | | 630 | | /// <item><c>internal</c> / <c>protected internal</c> types — accessible only when |
| | | 631 | | /// they belong to the same assembly as the compilation (same-assembly check).</item> |
| | | 632 | | /// <item><c>private</c> / <c>protected</c> / <c>private protected</c> types — never |
| | | 633 | | /// accessible from generated top-level code (even in the same assembly, generated code |
| | | 634 | | /// is not inside the containing type's hierarchy).</item> |
| | | 635 | | /// </list> |
| | | 636 | | /// <para> |
| | | 637 | | /// Containing types are checked recursively for nested types. |
| | | 638 | | /// </para> |
| | | 639 | | /// </remarks> |
| | | 640 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 641 | | /// <param name="compilationAssembly"> |
| | | 642 | | /// The compilation's output assembly. When <see langword="null"/>, falls back to |
| | | 643 | | /// the conservative behavior of <see cref="IsInternalOrLessAccessible"/> (i.e., |
| | | 644 | | /// any non-public type is considered inaccessible). |
| | | 645 | | /// </param> |
| | | 646 | | /// <returns><see langword="true"/> if the type is accessible from generated code.</returns> |
| | | 647 | | public static bool IsAccessibleFromGeneratedCode( |
| | | 648 | | INamedTypeSymbol typeSymbol, |
| | | 649 | | IAssemblySymbol? compilationAssembly) |
| | | 650 | | { |
| | 1763 | 651 | | return IsAccessibleCore(typeSymbol, compilationAssembly); |
| | | 652 | | } |
| | | 653 | | |
| | | 654 | | private static bool IsAccessibleCore( |
| | | 655 | | INamedTypeSymbol typeSymbol, |
| | | 656 | | IAssemblySymbol? compilationAssembly) |
| | | 657 | | { |
| | 1763 | 658 | | var accessibility = typeSymbol.DeclaredAccessibility; |
| | | 659 | | |
| | 1763 | 660 | | if (accessibility == Accessibility.Public) |
| | | 661 | | { |
| | | 662 | | // Public is universally accessible, but check containing types for nested types |
| | 1733 | 663 | | return typeSymbol.ContainingType == null || |
| | 1733 | 664 | | IsAccessibleCore(typeSymbol.ContainingType, compilationAssembly); |
| | | 665 | | } |
| | | 666 | | |
| | | 667 | | // internal and protected-internal are accessible from the same assembly. |
| | | 668 | | // The generated code lives in the compilation assembly, so if the type is |
| | | 669 | | // also in that assembly, we can emit typeof() for it. |
| | 30 | 670 | | if (accessibility == Accessibility.Internal || |
| | 30 | 671 | | accessibility == Accessibility.ProtectedOrInternal) |
| | | 672 | | { |
| | 24 | 673 | | bool isSameAssembly = compilationAssembly != null && |
| | 24 | 674 | | SymbolEqualityComparer.Default.Equals( |
| | 24 | 675 | | typeSymbol.ContainingAssembly, compilationAssembly); |
| | | 676 | | |
| | 24 | 677 | | if (isSameAssembly) |
| | | 678 | | { |
| | | 679 | | // Same assembly — accessible via internal path. |
| | | 680 | | // Still need to check containing types for nested types. |
| | 18 | 681 | | return typeSymbol.ContainingType == null || |
| | 18 | 682 | | IsAccessibleCore(typeSymbol.ContainingType, compilationAssembly); |
| | | 683 | | } |
| | | 684 | | } |
| | | 685 | | |
| | | 686 | | // private, protected, private-protected, or cross-assembly internal |
| | | 687 | | // → inaccessible from generated code. |
| | 12 | 688 | | return false; |
| | | 689 | | } |
| | | 690 | | |
| | | 691 | | /// <summary> |
| | | 692 | | /// Checks if a type implements IDisposable or IAsyncDisposable. |
| | | 693 | | /// </summary> |
| | | 694 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 695 | | /// <returns>True if the type implements IDisposable or IAsyncDisposable.</returns> |
| | | 696 | | public static bool IsDisposableType(INamedTypeSymbol typeSymbol) |
| | | 697 | | { |
| | 782263 | 698 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 699 | | { |
| | 187124 | 700 | | var fullName = GetFullyQualifiedName(iface); |
| | 187124 | 701 | | if (fullName == "global::System.IDisposable" || fullName == "global::System.IAsyncDisposable") |
| | 59067 | 702 | | return true; |
| | | 703 | | } |
| | 174474 | 704 | | return false; |
| | | 705 | | } |
| | | 706 | | |
| | | 707 | | /// <summary> |
| | | 708 | | /// Known Needlr plugin interface names that indicate a type is a plugin. |
| | | 709 | | /// </summary> |
| | 1 | 710 | | private static readonly string[] NeedlrPluginInterfaceNames = |
| | 1 | 711 | | [ |
| | 1 | 712 | | "NexusLabs.Needlr.IServiceCollectionPlugin", |
| | 1 | 713 | | "NexusLabs.Needlr.IPostBuildServiceCollectionPlugin", |
| | 1 | 714 | | "NexusLabs.Needlr.AspNet.IWebApplicationPlugin", |
| | 1 | 715 | | "NexusLabs.Needlr.AspNet.IWebApplicationBuilderPlugin", |
| | 1 | 716 | | "NexusLabs.Needlr.SignalR.IHubRegistrationPlugin", |
| | 1 | 717 | | "NexusLabs.Needlr.SemanticKernel.IKernelBuilderPlugin", |
| | 1 | 718 | | "NexusLabs.Needlr.Hosting.IHostApplicationBuilderPlugin", |
| | 1 | 719 | | "NexusLabs.Needlr.Hosting.IHostPlugin" |
| | 1 | 720 | | ]; |
| | | 721 | | |
| | | 722 | | /// <summary> |
| | | 723 | | /// Checks if a type implements any known Needlr plugin interface. |
| | | 724 | | /// </summary> |
| | | 725 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 726 | | /// <returns>True if the type implements a Needlr plugin interface.</returns> |
| | | 727 | | public static bool ImplementsNeedlrPluginInterface(INamedTypeSymbol typeSymbol) |
| | | 728 | | { |
| | 178757 | 729 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 730 | | { |
| | 1956 | 731 | | var ifaceName = iface.ToDisplayString(); |
| | 35193 | 732 | | foreach (var pluginInterface in NeedlrPluginInterfaceNames) |
| | | 733 | | { |
| | 15641 | 734 | | if (ifaceName == pluginInterface) |
| | 1 | 735 | | return true; |
| | | 736 | | } |
| | | 737 | | } |
| | 87422 | 738 | | return false; |
| | | 739 | | } |
| | | 740 | | |
| | | 741 | | /// <summary> |
| | | 742 | | /// Gets the name of the first Needlr plugin interface implemented by the type. |
| | | 743 | | /// </summary> |
| | | 744 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 745 | | /// <returns>The interface name, or null if none found.</returns> |
| | | 746 | | public static string? GetNeedlrPluginInterfaceName(INamedTypeSymbol typeSymbol) |
| | | 747 | | { |
| | 0 | 748 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 749 | | { |
| | 0 | 750 | | var ifaceName = iface.ToDisplayString(); |
| | 0 | 751 | | foreach (var pluginInterface in NeedlrPluginInterfaceNames) |
| | | 752 | | { |
| | 0 | 753 | | if (ifaceName == pluginInterface) |
| | 0 | 754 | | return ifaceName; |
| | | 755 | | } |
| | | 756 | | } |
| | 0 | 757 | | return null; |
| | | 758 | | } |
| | | 759 | | |
| | | 760 | | /// <summary> |
| | | 761 | | /// Checks if an assembly has the [GenerateTypeRegistry] attribute. |
| | | 762 | | /// </summary> |
| | | 763 | | /// <param name="assembly">The assembly symbol to check.</param> |
| | | 764 | | /// <returns>True if the assembly has the attribute.</returns> |
| | | 765 | | public static bool HasGenerateTypeRegistryAttribute(IAssemblySymbol assembly) |
| | | 766 | | { |
| | | 767 | | const string attributeName = "NexusLabs.Needlr.Generators.GenerateTypeRegistryAttribute"; |
| | | 768 | | |
| | 9990597 | 769 | | foreach (var attribute in assembly.GetAttributes()) |
| | | 770 | | { |
| | 4744025 | 771 | | var attrClass = attribute.AttributeClass; |
| | 4744025 | 772 | | if (attrClass == null) |
| | | 773 | | continue; |
| | | 774 | | |
| | 4744025 | 775 | | if (attrClass.ToDisplayString() == attributeName) |
| | 71 | 776 | | return true; |
| | | 777 | | } |
| | | 778 | | |
| | 251238 | 779 | | return false; |
| | | 780 | | } |
| | | 781 | | |
| | | 782 | | /// <summary> |
| | | 783 | | /// Checks if a type is publicly accessible (can be referenced from generated code). |
| | | 784 | | /// A type is publicly accessible if it and all its containing types are public. |
| | | 785 | | /// </summary> |
| | | 786 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 787 | | /// <returns>True if the type is publicly accessible.</returns> |
| | | 788 | | private static bool IsPubliclyAccessible(INamedTypeSymbol typeSymbol) |
| | | 789 | | { |
| | | 790 | | // Check the type itself |
| | 0 | 791 | | if (typeSymbol.DeclaredAccessibility != Accessibility.Public) |
| | 0 | 792 | | return false; |
| | | 793 | | |
| | | 794 | | // Check all containing types (for nested types) |
| | 0 | 795 | | var containingType = typeSymbol.ContainingType; |
| | 0 | 796 | | while (containingType != null) |
| | | 797 | | { |
| | 0 | 798 | | if (containingType.DeclaredAccessibility != Accessibility.Public) |
| | 0 | 799 | | return false; |
| | 0 | 800 | | containingType = containingType.ContainingType; |
| | | 801 | | } |
| | | 802 | | |
| | 0 | 803 | | return true; |
| | | 804 | | } |
| | | 805 | | |
| | | 806 | | /// <summary> |
| | | 807 | | /// Determines the injectable lifetime for a type by analyzing its attributes and constructors. |
| | | 808 | | /// Checks for explicit lifetime attributes first, then falls back to Singleton if injectable. |
| | | 809 | | /// </summary> |
| | | 810 | | /// <param name="typeSymbol">The type symbol to analyze.</param> |
| | | 811 | | /// <returns>The determined lifetime, or null if the type is not injectable.</returns> |
| | | 812 | | public static GeneratorLifetime? DetermineLifetime(INamedTypeSymbol typeSymbol) |
| | | 813 | | { |
| | | 814 | | // Check for DoNotInjectAttribute |
| | 413678 | 815 | | if (HasDoNotInjectAttribute(typeSymbol)) |
| | 1 | 816 | | return null; |
| | | 817 | | |
| | | 818 | | // Exclude open generic types (type definitions with type parameters like MyClass<T>) |
| | 413677 | 819 | | if (typeSymbol.TypeParameters.Length > 0) |
| | 1 | 820 | | return null; |
| | | 821 | | |
| | | 822 | | // Types with [DeferToContainer] are always injectable as Singleton |
| | | 823 | | // (the attribute declares constructor params that will be added by another generator) |
| | 413676 | 824 | | if (HasDeferToContainerAttribute(typeSymbol)) |
| | 6 | 825 | | return GetExplicitLifetime(typeSymbol) ?? GeneratorLifetime.Singleton; |
| | | 826 | | |
| | | 827 | | // Get all instance constructors |
| | 413670 | 828 | | var constructors = typeSymbol.InstanceConstructors; |
| | | 829 | | |
| | 1470146 | 830 | | foreach (var ctor in constructors) |
| | | 831 | | { |
| | | 832 | | // Skip static constructors |
| | 439352 | 833 | | if (ctor.IsStatic) |
| | | 834 | | continue; |
| | | 835 | | |
| | 439352 | 836 | | var parameters = ctor.Parameters; |
| | | 837 | | |
| | | 838 | | // Parameterless constructor is always valid |
| | 439352 | 839 | | if (parameters.Length == 0) |
| | 190681 | 840 | | return GetExplicitLifetime(typeSymbol) ?? GeneratorLifetime.Singleton; |
| | | 841 | | |
| | | 842 | | // Single parameter of same type (copy constructor) - not injectable |
| | 248671 | 843 | | if (parameters.Length == 1 && SymbolEqualityComparer.Default.Equals(parameters[0].Type, typeSymbol)) |
| | | 844 | | continue; |
| | | 845 | | |
| | | 846 | | // Check if all parameters are injectable types |
| | 241336 | 847 | | if (AllParametersAreInjectable(parameters)) |
| | 45217 | 848 | | return GetExplicitLifetime(typeSymbol) ?? GeneratorLifetime.Singleton; |
| | | 849 | | } |
| | | 850 | | |
| | 177772 | 851 | | return null; |
| | | 852 | | } |
| | | 853 | | |
| | | 854 | | /// <summary> |
| | | 855 | | /// Gets the explicit lifetime from attributes if specified. |
| | | 856 | | /// </summary> |
| | | 857 | | private static GeneratorLifetime? GetExplicitLifetime(INamedTypeSymbol typeSymbol) |
| | | 858 | | { |
| | 1323924 | 859 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 860 | | { |
| | 426090 | 861 | | var attributeClass = attribute.AttributeClass; |
| | 426090 | 862 | | if (attributeClass == null) |
| | | 863 | | continue; |
| | | 864 | | |
| | 426090 | 865 | | var name = attributeClass.Name; |
| | 426090 | 866 | | var fullName = attributeClass.ToDisplayString(); |
| | | 867 | | |
| | 426090 | 868 | | if (name == TransientAttributeName || fullName == TransientAttributeFullName) |
| | 5 | 869 | | return GeneratorLifetime.Transient; |
| | | 870 | | |
| | 426085 | 871 | | if (name == ScopedAttributeName || fullName == ScopedAttributeFullName) |
| | 36 | 872 | | return GeneratorLifetime.Scoped; |
| | | 873 | | |
| | 426049 | 874 | | if (name == SingletonAttributeName || fullName == SingletonAttributeFullName) |
| | 23 | 875 | | return GeneratorLifetime.Singleton; |
| | | 876 | | } |
| | | 877 | | |
| | 235840 | 878 | | return null; |
| | | 879 | | } |
| | | 880 | | |
| | | 881 | | private static bool AllParametersAreInjectable(System.Collections.Immutable.ImmutableArray<IParameterSymbol> paramet |
| | | 882 | | { |
| | 2080070 | 883 | | foreach (var param in parameters) |
| | | 884 | | { |
| | 689992 | 885 | | if (!IsInjectableParameterType(param.Type)) |
| | 407266 | 886 | | return false; |
| | | 887 | | } |
| | 146410 | 888 | | return true; |
| | | 889 | | } |
| | | 890 | | |
| | | 891 | | private static bool IsInjectableParameterType(ITypeSymbol typeSymbol) |
| | | 892 | | { |
| | | 893 | | // Must not be a delegate |
| | 689992 | 894 | | if (typeSymbol.TypeKind == TypeKind.Delegate) |
| | 7336 | 895 | | return false; |
| | | 896 | | |
| | | 897 | | // Must not be a value type |
| | 682656 | 898 | | if (typeSymbol.IsValueType) |
| | 191845 | 899 | | return false; |
| | | 900 | | |
| | | 901 | | // Must not be string |
| | 490811 | 902 | | if (typeSymbol.SpecialType == SpecialType.System_String) |
| | 164829 | 903 | | return false; |
| | | 904 | | |
| | | 905 | | // Must be a class or interface |
| | 325982 | 906 | | if (typeSymbol.TypeKind != TypeKind.Class && typeSymbol.TypeKind != TypeKind.Interface) |
| | 43256 | 907 | | return false; |
| | | 908 | | |
| | 282726 | 909 | | return true; |
| | | 910 | | } |
| | | 911 | | |
| | | 912 | | private static bool HasDoNotInjectAttribute(INamedTypeSymbol typeSymbol) |
| | | 913 | | { |
| | 2344265 | 914 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 915 | | { |
| | 758455 | 916 | | var attributeClass = attribute.AttributeClass; |
| | 758455 | 917 | | if (attributeClass == null) |
| | | 918 | | continue; |
| | | 919 | | |
| | 758455 | 920 | | var name = attributeClass.Name; |
| | 758455 | 921 | | if (name == DoNotInjectAttributeName) |
| | 1 | 922 | | return true; |
| | | 923 | | |
| | 758454 | 924 | | var fullName = attributeClass.ToDisplayString(); |
| | 758454 | 925 | | if (fullName == DoNotInjectAttributeFullName) |
| | 0 | 926 | | return true; |
| | | 927 | | } |
| | | 928 | | |
| | 413677 | 929 | | return false; |
| | | 930 | | } |
| | | 931 | | |
| | | 932 | | /// <summary> |
| | | 933 | | /// Determines if a type is a valid plugin type (concrete class with parameterless constructor). |
| | | 934 | | /// </summary> |
| | | 935 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 936 | | /// <param name="isCurrentAssembly">True if the type is from the current compilation's assembly (allows internal typ |
| | | 937 | | /// <returns>True if the type is a valid plugin type.</returns> |
| | | 938 | | public static bool IsPluginType(INamedTypeSymbol typeSymbol, bool isCurrentAssembly = false) |
| | | 939 | | { |
| | | 940 | | // Must be a concrete class |
| | 1457242 | 941 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 547358 | 942 | | return false; |
| | | 943 | | |
| | | 944 | | // Must be accessible from generated code |
| | | 945 | | // - Current assembly: internal and public types are accessible |
| | | 946 | | // - Referenced assemblies: only public types are accessible |
| | 909884 | 947 | | if (!IsAccessibleFromGeneratedCode(typeSymbol, isCurrentAssembly)) |
| | 0 | 948 | | return false; |
| | | 949 | | |
| | 909884 | 950 | | if (typeSymbol.IsAbstract) |
| | 139353 | 951 | | return false; |
| | | 952 | | |
| | 770531 | 953 | | if (typeSymbol.IsStatic) |
| | 88673 | 954 | | return false; |
| | | 955 | | |
| | 681858 | 956 | | if (typeSymbol.IsUnboundGenericType) |
| | 0 | 957 | | return false; |
| | | 958 | | |
| | | 959 | | // Exclude open generic types (type definitions with type parameters like MyClass<T>) |
| | | 960 | | // These cannot be instantiated directly and would produce invalid typeof() expressions |
| | 681858 | 961 | | if (typeSymbol.TypeParameters.Length > 0) |
| | 40378 | 962 | | return false; |
| | | 963 | | |
| | | 964 | | // NOTE: Records ARE allowed as plugins (they are classes with parameterless constructors). |
| | | 965 | | // Records are excluded from IsInjectableType (auto-registration) but not from plugin discovery. |
| | | 966 | | // Use case: CacheConfiguration records can be discovered via IPluginFactory.CreatePluginsFromAssemblies<T>() |
| | | 967 | | // IMPORTANT: If the plugin type is emitted by a DIFFERENT source generator, TypeRegistryGenerator |
| | | 968 | | // cannot see it (Roslyn generators receive the original compilation in isolation). In that case, |
| | | 969 | | // the other generator should emit a [ModuleInitializer] that calls |
| | | 970 | | // NeedlrSourceGenBootstrap.RegisterPlugins(() => [...]) to contribute those types at runtime. |
| | | 971 | | |
| | | 972 | | // Exclude hosted service types — they have their own dedicated registration path |
| | | 973 | | // (RegisterHostedServices) and must not be included in plugin types. |
| | 641480 | 974 | | if (InheritsFromBackgroundService(typeSymbol) || ImplementsIHostedService(typeSymbol)) |
| | 8 | 975 | | return false; |
| | | 976 | | |
| | | 977 | | // Must have a parameterless constructor |
| | 641472 | 978 | | if (!HasParameterlessConstructor(typeSymbol)) |
| | 236320 | 979 | | return false; |
| | | 980 | | |
| | | 981 | | // Exclude types with required members that can't be set via constructor |
| | | 982 | | // These would cause compilation errors: "Required member 'X' must be set" |
| | 405152 | 983 | | if (HasUnsatisfiedRequiredMembers(typeSymbol)) |
| | 2 | 984 | | return false; |
| | | 985 | | |
| | 405150 | 986 | | return true; |
| | | 987 | | } |
| | | 988 | | |
| | | 989 | | /// <summary> |
| | | 990 | | /// Gets the plugin base types (interfaces and base classes) for a type. |
| | | 991 | | /// Plugin base types are non-System interfaces and non-System/non-object base classes. |
| | | 992 | | /// </summary> |
| | | 993 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 994 | | /// <param name="compilationAssembly"> |
| | | 995 | | /// The compilation's assembly, used for same-assembly accessibility checks. |
| | | 996 | | /// See <see cref="IsAccessibleFromGeneratedCode(INamedTypeSymbol, IAssemblySymbol?)"/> for details. |
| | | 997 | | /// </param> |
| | | 998 | | /// <returns>A list of plugin base type symbols (interfaces and base classes).</returns> |
| | | 999 | | public static IReadOnlyList<INamedTypeSymbol> GetPluginInterfaces( |
| | | 1000 | | INamedTypeSymbol typeSymbol, |
| | | 1001 | | IAssemblySymbol? compilationAssembly = null) |
| | | 1002 | | { |
| | 473255 | 1003 | | var result = new List<INamedTypeSymbol>(); |
| | | 1004 | | |
| | | 1005 | | // Add non-system interfaces that are accessible from generated code. |
| | | 1006 | | // |
| | | 1007 | | // WHY THIS EXISTS: Same rules as GetRegisterableInterfaces — the generated |
| | | 1008 | | // TypeRegistry emits typeof() for each plugin interface. Same-assembly internal |
| | | 1009 | | // interfaces are valid (generated code is in the same compilation). Cross-assembly |
| | | 1010 | | // internal interfaces MUST be skipped to avoid CS0122. |
| | | 1011 | | // |
| | | 1012 | | // CRITICAL: Same-assembly internal interfaces MUST be kept. This is the standard |
| | | 1013 | | // pattern for internal plugin contracts within a single project. |
| | | 1014 | | // |
| | | 1015 | | // Example that MUST work (same assembly): |
| | | 1016 | | // internal interface IMyPlugin { } |
| | | 1017 | | // internal class MyPlugin : IMyPlugin { } |
| | | 1018 | | // → typeof(IMyPlugin) in generated code is VALID |
| | | 1019 | | // |
| | | 1020 | | // Example that MUST be skipped (cross-assembly, e.g., Avalonia): |
| | | 1021 | | // // In Framework.dll (internal): |
| | | 1022 | | // internal interface IInternalHook { } |
| | | 1023 | | // // In consumer app: |
| | | 1024 | | // public class MyControl : Framework.BaseControl { } // inherits IInternalHook |
| | | 1025 | | // → typeof(IInternalHook) in generated code produces CS0122 |
| | 1540792 | 1026 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 1027 | | { |
| | 297141 | 1028 | | if (iface.IsUnboundGenericType) |
| | | 1029 | | continue; |
| | | 1030 | | |
| | 297141 | 1031 | | if (IsSystemInterface(iface)) |
| | | 1032 | | continue; |
| | | 1033 | | |
| | 270 | 1034 | | if (!IsAccessibleFromGeneratedCode(iface, compilationAssembly)) |
| | | 1035 | | continue; |
| | | 1036 | | |
| | 264 | 1037 | | result.Add(iface); |
| | | 1038 | | } |
| | | 1039 | | |
| | | 1040 | | // Add non-system base classes (walking up the hierarchy) |
| | 473255 | 1041 | | var baseType = typeSymbol.BaseType; |
| | 474417 | 1042 | | while (baseType != null) |
| | | 1043 | | { |
| | | 1044 | | // Stop at System.Object or System types |
| | 408630 | 1045 | | if (IsSystemType(baseType)) |
| | | 1046 | | break; |
| | | 1047 | | |
| | | 1048 | | // Skip inaccessible base types (same rules as interfaces) |
| | 1162 | 1049 | | if (!IsAccessibleFromGeneratedCode(baseType, compilationAssembly)) |
| | | 1050 | | { |
| | 0 | 1051 | | baseType = baseType.BaseType; |
| | 0 | 1052 | | continue; |
| | | 1053 | | } |
| | | 1054 | | |
| | 1162 | 1055 | | result.Add(baseType); |
| | 1162 | 1056 | | baseType = baseType.BaseType; |
| | | 1057 | | } |
| | | 1058 | | |
| | 473255 | 1059 | | return result; |
| | | 1060 | | } |
| | | 1061 | | |
| | | 1062 | | /// <summary> |
| | | 1063 | | /// Checks if a type implements a specific interface by name. |
| | | 1064 | | /// </summary> |
| | | 1065 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1066 | | /// <param name="interfaceFullName">The full name of the interface.</param> |
| | | 1067 | | /// <returns>True if the type implements the interface.</returns> |
| | | 1068 | | public static bool ImplementsInterface(INamedTypeSymbol typeSymbol, string interfaceFullName) |
| | | 1069 | | { |
| | 0 | 1070 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 1071 | | { |
| | 0 | 1072 | | if (iface.ToDisplayString() == interfaceFullName) |
| | 0 | 1073 | | return true; |
| | | 1074 | | } |
| | 0 | 1075 | | return false; |
| | | 1076 | | } |
| | | 1077 | | |
| | | 1078 | | /// <summary> |
| | | 1079 | | /// Checks if a type has a public parameterless constructor. |
| | | 1080 | | /// </summary> |
| | | 1081 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1082 | | /// <returns>True if the type has a parameterless constructor.</returns> |
| | | 1083 | | public static bool HasParameterlessConstructor(INamedTypeSymbol typeSymbol) |
| | | 1084 | | { |
| | 2735461 | 1085 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 1086 | | { |
| | 814612 | 1087 | | if (ctor.DeclaredAccessibility == Accessibility.Public && |
| | 814612 | 1088 | | ctor.Parameters.Length == 0) |
| | | 1089 | | { |
| | 315991 | 1090 | | return true; |
| | | 1091 | | } |
| | | 1092 | | } |
| | | 1093 | | |
| | | 1094 | | // If no explicit constructors, the default constructor is available |
| | | 1095 | | // (unless there are other constructors with parameters) |
| | 395123 | 1096 | | if (typeSymbol.InstanceConstructors.Length == 0) |
| | 157269 | 1097 | | return true; |
| | | 1098 | | |
| | 237854 | 1099 | | return false; |
| | | 1100 | | } |
| | | 1101 | | |
| | | 1102 | | /// <summary> |
| | | 1103 | | /// Gets the attribute types applied to a plugin type. |
| | | 1104 | | /// </summary> |
| | | 1105 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1106 | | /// <returns>A list of attribute type names (fully qualified).</returns> |
| | | 1107 | | public static IReadOnlyList<string> GetPluginAttributes(INamedTypeSymbol typeSymbol) |
| | | 1108 | | { |
| | 1406 | 1109 | | var result = new List<string>(); |
| | | 1110 | | |
| | 2934 | 1111 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 1112 | | { |
| | 61 | 1113 | | var attributeClass = attribute.AttributeClass; |
| | 61 | 1114 | | if (attributeClass == null) |
| | | 1115 | | continue; |
| | | 1116 | | |
| | | 1117 | | // Skip system attributes and compiler-generated attributes |
| | 61 | 1118 | | var ns = attributeClass.ContainingNamespace?.ToDisplayString() ?? string.Empty; |
| | 61 | 1119 | | if (ns.StartsWith("System.Runtime.CompilerServices", StringComparison.Ordinal)) |
| | | 1120 | | continue; |
| | | 1121 | | |
| | | 1122 | | // Include this attribute |
| | 61 | 1123 | | var attributeName = GetFullyQualifiedName(attributeClass); |
| | 61 | 1124 | | if (!result.Contains(attributeName)) |
| | | 1125 | | { |
| | 61 | 1126 | | result.Add(attributeName); |
| | | 1127 | | } |
| | | 1128 | | } |
| | | 1129 | | |
| | | 1130 | | // Also check for inherited attributes from base types |
| | 1406 | 1131 | | var baseType = typeSymbol.BaseType; |
| | 6042 | 1132 | | while (baseType != null && baseType.SpecialType != SpecialType.System_Object) |
| | | 1133 | | { |
| | 9272 | 1134 | | foreach (var attribute in baseType.GetAttributes()) |
| | | 1135 | | { |
| | 0 | 1136 | | var attributeClass = attribute.AttributeClass; |
| | 0 | 1137 | | if (attributeClass == null) |
| | | 1138 | | continue; |
| | | 1139 | | |
| | | 1140 | | // Check if attribute is inherited |
| | 0 | 1141 | | if (!IsInheritedAttribute(attributeClass)) |
| | | 1142 | | continue; |
| | | 1143 | | |
| | 0 | 1144 | | var ns = attributeClass.ContainingNamespace?.ToDisplayString() ?? string.Empty; |
| | 0 | 1145 | | if (ns.StartsWith("System.Runtime.CompilerServices", StringComparison.Ordinal)) |
| | | 1146 | | continue; |
| | | 1147 | | |
| | 0 | 1148 | | var attributeName = GetFullyQualifiedName(attributeClass); |
| | 0 | 1149 | | if (!result.Contains(attributeName)) |
| | | 1150 | | { |
| | 0 | 1151 | | result.Add(attributeName); |
| | | 1152 | | } |
| | | 1153 | | } |
| | | 1154 | | |
| | 4636 | 1155 | | baseType = baseType.BaseType; |
| | | 1156 | | } |
| | | 1157 | | |
| | 1406 | 1158 | | return result; |
| | | 1159 | | } |
| | | 1160 | | |
| | | 1161 | | /// <summary> |
| | | 1162 | | /// Checks if an attribute type has [AttributeUsage(Inherited = true)]. |
| | | 1163 | | /// </summary> |
| | | 1164 | | private static bool IsInheritedAttribute(INamedTypeSymbol attributeClass) |
| | | 1165 | | { |
| | 0 | 1166 | | foreach (var attr in attributeClass.GetAttributes()) |
| | | 1167 | | { |
| | 0 | 1168 | | if (attr.AttributeClass?.ToDisplayString() != "System.AttributeUsageAttribute") |
| | | 1169 | | continue; |
| | | 1170 | | |
| | 0 | 1171 | | foreach (var namedArg in attr.NamedArguments) |
| | | 1172 | | { |
| | 0 | 1173 | | if (namedArg.Key == "Inherited" && namedArg.Value.Value is bool inherited) |
| | | 1174 | | { |
| | 0 | 1175 | | return inherited; |
| | | 1176 | | } |
| | | 1177 | | } |
| | | 1178 | | |
| | | 1179 | | // Default for AttributeUsage is Inherited = true |
| | 0 | 1180 | | return true; |
| | | 1181 | | } |
| | | 1182 | | |
| | | 1183 | | // Default is Inherited = true |
| | 0 | 1184 | | return true; |
| | | 1185 | | } |
| | | 1186 | | |
| | | 1187 | | /// <summary> |
| | | 1188 | | /// Gets the parameters of the best injectable constructor for a type. |
| | | 1189 | | /// Returns the first constructor where all parameters are injectable types. |
| | | 1190 | | /// </summary> |
| | | 1191 | | /// <param name="typeSymbol">The type symbol to analyze.</param> |
| | | 1192 | | /// <returns> |
| | | 1193 | | /// A list of fully qualified parameter type names, or null if no injectable constructor was found. |
| | | 1194 | | /// </returns> |
| | | 1195 | | public static IReadOnlyList<string>? GetBestConstructorParameters(INamedTypeSymbol typeSymbol) |
| | | 1196 | | { |
| | | 1197 | | // Collect ALL satisfiable constructors, then pick the richest (most parameters). |
| | | 1198 | | // This matches the standard .NET DI behavior (ActivatorUtilities) where the |
| | | 1199 | | // constructor with the most resolvable parameters wins. |
| | 23 | 1200 | | string[]? best = null; |
| | | 1201 | | |
| | 100 | 1202 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 1203 | | { |
| | 27 | 1204 | | if (ctor.IsStatic) |
| | | 1205 | | continue; |
| | | 1206 | | |
| | 27 | 1207 | | if (ctor.DeclaredAccessibility != Accessibility.Public) |
| | | 1208 | | continue; |
| | | 1209 | | |
| | 27 | 1210 | | var parameters = ctor.Parameters; |
| | | 1211 | | |
| | | 1212 | | // Single parameter of same type (copy constructor) - skip |
| | 27 | 1213 | | if (parameters.Length == 1 && SymbolEqualityComparer.Default.Equals(parameters[0].Type, typeSymbol)) |
| | | 1214 | | continue; |
| | | 1215 | | |
| | | 1216 | | // Parameterless constructor is a valid candidate |
| | 27 | 1217 | | if (parameters.Length == 0) |
| | | 1218 | | { |
| | 17 | 1219 | | if (best == null) |
| | 17 | 1220 | | best = Array.Empty<string>(); |
| | 17 | 1221 | | continue; |
| | | 1222 | | } |
| | | 1223 | | |
| | | 1224 | | // Check if all parameters are injectable |
| | 10 | 1225 | | if (!AllParametersAreInjectable(parameters)) |
| | | 1226 | | continue; |
| | | 1227 | | |
| | | 1228 | | // This constructor is satisfiable — prefer it if it's richer |
| | 6 | 1229 | | if (best == null || parameters.Length > best.Length) |
| | | 1230 | | { |
| | 6 | 1231 | | var parameterTypes = new string[parameters.Length]; |
| | 26 | 1232 | | for (int i = 0; i < parameters.Length; i++) |
| | | 1233 | | { |
| | 7 | 1234 | | parameterTypes[i] = GetFullyQualifiedNameForType(parameters[i].Type); |
| | | 1235 | | } |
| | 6 | 1236 | | best = parameterTypes; |
| | | 1237 | | } |
| | | 1238 | | } |
| | | 1239 | | |
| | 23 | 1240 | | return best; |
| | | 1241 | | } |
| | | 1242 | | |
| | | 1243 | | /// <summary> |
| | | 1244 | | /// Gets the fully qualified name for any type symbol (including generics like Lazy<T>). |
| | | 1245 | | /// </summary> |
| | | 1246 | | private static string GetFullyQualifiedNameForType(ITypeSymbol typeSymbol) |
| | | 1247 | | { |
| | 110077 | 1248 | | return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| | | 1249 | | } |
| | | 1250 | | |
| | | 1251 | | // NOTE: HasKernelFunctions was moved to NexusLabs.Needlr.SemanticKernel.Generators |
| | | 1252 | | // NOTE: HubRegistrationInfo was moved to NexusLabs.Needlr.SignalR.Generators |
| | | 1253 | | |
| | | 1254 | | /// <summary> |
| | | 1255 | | /// Represents a constructor parameter with optional keyed service information. |
| | | 1256 | | /// </summary> |
| | | 1257 | | public readonly struct ConstructorParameterInfo |
| | | 1258 | | { |
| | | 1259 | | public ConstructorParameterInfo(string typeName, string? serviceKey = null, string? parameterName = null, string |
| | | 1260 | | { |
| | 110134 | 1261 | | TypeName = typeName; |
| | 110134 | 1262 | | ServiceKey = serviceKey; |
| | 110134 | 1263 | | ParameterName = parameterName; |
| | 110134 | 1264 | | DocumentationComment = documentationComment; |
| | 110134 | 1265 | | } |
| | | 1266 | | |
| | | 1267 | | /// <summary> |
| | | 1268 | | /// The fully qualified type name of the parameter. |
| | | 1269 | | /// </summary> |
| | 479553 | 1270 | | public string TypeName { get; } |
| | | 1271 | | |
| | | 1272 | | /// <summary> |
| | | 1273 | | /// The service key from [FromKeyedServices] attribute, or null if not a keyed service. |
| | | 1274 | | /// </summary> |
| | 289390 | 1275 | | public string? ServiceKey { get; } |
| | | 1276 | | |
| | | 1277 | | /// <summary> |
| | | 1278 | | /// The original parameter name from the constructor (used for factory generation). |
| | | 1279 | | /// </summary> |
| | 96955 | 1280 | | public string? ParameterName { get; } |
| | | 1281 | | |
| | | 1282 | | /// <summary> |
| | | 1283 | | /// XML documentation comment for this parameter, extracted from the constructor's XML docs. |
| | | 1284 | | /// </summary> |
| | 39 | 1285 | | public string? DocumentationComment { get; } |
| | | 1286 | | |
| | | 1287 | | /// <summary> |
| | | 1288 | | /// True if this parameter should be resolved as a keyed service. |
| | | 1289 | | /// </summary> |
| | 192602 | 1290 | | public bool IsKeyed => ServiceKey is not null; |
| | | 1291 | | } |
| | | 1292 | | |
| | | 1293 | | /// <summary> |
| | | 1294 | | /// Gets the parameters of the best injectable constructor for a type, including keyed service info. |
| | | 1295 | | /// Picks the constructor with the most satisfiable parameters (richest constructor wins), |
| | | 1296 | | /// matching the standard .NET DI behavior (ActivatorUtilities). |
| | | 1297 | | /// </summary> |
| | | 1298 | | /// <param name="typeSymbol">The type symbol to analyze.</param> |
| | | 1299 | | /// <returns> |
| | | 1300 | | /// A list of constructor parameter info, or null if no injectable constructor was found. |
| | | 1301 | | /// </returns> |
| | | 1302 | | public static IReadOnlyList<ConstructorParameterInfo>? GetBestConstructorParametersWithKeys(INamedTypeSymbol typeSym |
| | | 1303 | | { |
| | | 1304 | | const string FromKeyedServicesAttributeName = "Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribu |
| | | 1305 | | |
| | 233543 | 1306 | | ConstructorParameterInfo[]? best = null; |
| | | 1307 | | |
| | 1505464 | 1308 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 1309 | | { |
| | 519189 | 1310 | | if (ctor.IsStatic) |
| | | 1311 | | continue; |
| | | 1312 | | |
| | 519189 | 1313 | | if (ctor.DeclaredAccessibility != Accessibility.Public) |
| | | 1314 | | continue; |
| | | 1315 | | |
| | 495643 | 1316 | | var parameters = ctor.Parameters; |
| | | 1317 | | |
| | | 1318 | | // Single parameter of same type (copy constructor) - skip |
| | 495643 | 1319 | | if (parameters.Length == 1 && SymbolEqualityComparer.Default.Equals(parameters[0].Type, typeSymbol)) |
| | | 1320 | | continue; |
| | | 1321 | | |
| | | 1322 | | // Parameterless constructor is a valid candidate |
| | 489467 | 1323 | | if (parameters.Length == 0) |
| | | 1324 | | { |
| | 177137 | 1325 | | if (best == null) |
| | 177137 | 1326 | | best = Array.Empty<ConstructorParameterInfo>(); |
| | 177137 | 1327 | | continue; |
| | | 1328 | | } |
| | | 1329 | | |
| | | 1330 | | // Check if all parameters are injectable |
| | 312330 | 1331 | | if (!AllParametersAreInjectable(parameters)) |
| | | 1332 | | continue; |
| | | 1333 | | |
| | | 1334 | | // This constructor is satisfiable — prefer it if it's richer |
| | 101187 | 1335 | | if (best == null || parameters.Length > best.Length) |
| | | 1336 | | { |
| | 86133 | 1337 | | var parameterInfos = new ConstructorParameterInfo[parameters.Length]; |
| | 392406 | 1338 | | for (int i = 0; i < parameters.Length; i++) |
| | | 1339 | | { |
| | 110070 | 1340 | | var param = parameters[i]; |
| | 110070 | 1341 | | var typeName = GetFullyQualifiedNameForType(param.Type); |
| | 110070 | 1342 | | string? serviceKey = null; |
| | | 1343 | | |
| | | 1344 | | // Check for [FromKeyedServices("key")] attribute |
| | 247932 | 1345 | | foreach (var attr in param.GetAttributes()) |
| | | 1346 | | { |
| | 13896 | 1347 | | var attrClass = attr.AttributeClass; |
| | 13896 | 1348 | | if (attrClass is null) |
| | | 1349 | | continue; |
| | | 1350 | | |
| | 13896 | 1351 | | var attrFullName = attrClass.ToDisplayString(); |
| | 13896 | 1352 | | if (attrFullName == FromKeyedServicesAttributeName) |
| | | 1353 | | { |
| | 0 | 1354 | | if (attr.ConstructorArguments.Length > 0) |
| | | 1355 | | { |
| | 0 | 1356 | | var keyArg = attr.ConstructorArguments[0]; |
| | 0 | 1357 | | if (keyArg.Value is string keyValue) |
| | | 1358 | | { |
| | 0 | 1359 | | serviceKey = keyValue; |
| | | 1360 | | } |
| | | 1361 | | } |
| | 0 | 1362 | | break; |
| | | 1363 | | } |
| | | 1364 | | } |
| | | 1365 | | |
| | 110070 | 1366 | | parameterInfos[i] = new ConstructorParameterInfo(typeName, serviceKey); |
| | | 1367 | | } |
| | 86133 | 1368 | | best = parameterInfos; |
| | | 1369 | | } |
| | | 1370 | | } |
| | | 1371 | | |
| | 233543 | 1372 | | return best; |
| | | 1373 | | } |
| | | 1374 | | |
| | | 1375 | | /// <summary> |
| | | 1376 | | /// Gets the service keys from [Keyed] attributes on a type. |
| | | 1377 | | /// </summary> |
| | | 1378 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1379 | | /// <returns>Array of service keys, or empty array if no [Keyed] attributes found.</returns> |
| | | 1380 | | public static string[] GetKeyedServiceKeys(INamedTypeSymbol typeSymbol) |
| | | 1381 | | { |
| | 233559 | 1382 | | var keys = new List<string>(); |
| | | 1383 | | |
| | 1319242 | 1384 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 1385 | | { |
| | 426062 | 1386 | | var attributeClass = attribute.AttributeClass; |
| | 426062 | 1387 | | if (attributeClass == null) |
| | | 1388 | | continue; |
| | | 1389 | | |
| | 426062 | 1390 | | var name = attributeClass.Name; |
| | 426062 | 1391 | | var fullName = attributeClass.ToDisplayString(); |
| | | 1392 | | |
| | 426062 | 1393 | | if (name == KeyedAttributeName || fullName == KeyedAttributeFullName) |
| | | 1394 | | { |
| | | 1395 | | // Extract the key from the constructor argument |
| | 5 | 1396 | | if (attribute.ConstructorArguments.Length > 0) |
| | | 1397 | | { |
| | 5 | 1398 | | var keyArg = attribute.ConstructorArguments[0]; |
| | 5 | 1399 | | if (keyArg.Value is string keyValue) |
| | | 1400 | | { |
| | 5 | 1401 | | keys.Add(keyValue); |
| | | 1402 | | } |
| | | 1403 | | } |
| | | 1404 | | } |
| | | 1405 | | } |
| | | 1406 | | |
| | 233559 | 1407 | | return keys.ToArray(); |
| | | 1408 | | } |
| | | 1409 | | |
| | | 1410 | | // NOTE: TryGetHubRegistrationInfo, TryGetPropertyStringValue, TryGetPropertyTypeValue |
| | | 1411 | | // were moved to NexusLabs.Needlr.SignalR.Generators |
| | | 1412 | | |
| | | 1413 | | /// <summary> |
| | | 1414 | | /// Checks if a type has the <c>[DeferToContainer]</c> attribute. |
| | | 1415 | | /// </summary> |
| | | 1416 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1417 | | /// <returns>True if the type has the DeferToContainer attribute.</returns> |
| | | 1418 | | public static bool HasDeferToContainerAttribute(INamedTypeSymbol typeSymbol) |
| | | 1419 | | { |
| | 2344259 | 1420 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 1421 | | { |
| | 758455 | 1422 | | var attrClass = attribute.AttributeClass; |
| | 758455 | 1423 | | if (attrClass is null) |
| | | 1424 | | continue; |
| | | 1425 | | |
| | 758455 | 1426 | | var name = attrClass.Name; |
| | 758455 | 1427 | | var fullName = attrClass.ToDisplayString(); |
| | | 1428 | | |
| | 758455 | 1429 | | if (name == DeferToContainerAttributeName || fullName == DeferToContainerAttributeFullName) |
| | 7 | 1430 | | return true; |
| | | 1431 | | } |
| | | 1432 | | |
| | 413671 | 1433 | | return false; |
| | | 1434 | | } |
| | | 1435 | | |
| | | 1436 | | /// <summary> |
| | | 1437 | | /// Gets the constructor parameter types declared in the <c>[DeferToContainer]</c> attribute. |
| | | 1438 | | /// </summary> |
| | | 1439 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1440 | | /// <returns> |
| | | 1441 | | /// A list of fully qualified parameter type names from the attribute, |
| | | 1442 | | /// or null if the attribute is not present. |
| | | 1443 | | /// </returns> |
| | | 1444 | | public static IReadOnlyList<string>? GetDeferToContainerParameterTypes(INamedTypeSymbol typeSymbol) |
| | | 1445 | | { |
| | 1319197 | 1446 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 1447 | | { |
| | 426057 | 1448 | | var attrClass = attribute.AttributeClass; |
| | 426057 | 1449 | | if (attrClass is null) |
| | | 1450 | | continue; |
| | | 1451 | | |
| | 426057 | 1452 | | var name = attrClass.Name; |
| | 426057 | 1453 | | var fullName = attrClass.ToDisplayString(); |
| | | 1454 | | |
| | 426057 | 1455 | | if (name != DeferToContainerAttributeName && fullName != DeferToContainerAttributeFullName) |
| | | 1456 | | continue; |
| | | 1457 | | |
| | | 1458 | | // The attribute has a params Type[] constructor parameter |
| | | 1459 | | // Check constructor arguments |
| | 9 | 1460 | | if (attribute.ConstructorArguments.Length == 0) |
| | 0 | 1461 | | return Array.Empty<string>(); |
| | | 1462 | | |
| | 9 | 1463 | | var arg = attribute.ConstructorArguments[0]; |
| | | 1464 | | |
| | | 1465 | | // params array is passed as a single array argument |
| | 9 | 1466 | | if (arg.Kind == TypedConstantKind.Array) |
| | | 1467 | | { |
| | 9 | 1468 | | var types = new List<string>(); |
| | 40 | 1469 | | foreach (var element in arg.Values) |
| | | 1470 | | { |
| | 11 | 1471 | | if (element.Value is INamedTypeSymbol namedType) |
| | | 1472 | | { |
| | 11 | 1473 | | types.Add(GetFullyQualifiedName(namedType)); |
| | | 1474 | | } |
| | | 1475 | | } |
| | 9 | 1476 | | return types; |
| | | 1477 | | } |
| | | 1478 | | } |
| | | 1479 | | |
| | 233537 | 1480 | | return null; |
| | | 1481 | | } |
| | | 1482 | | |
| | | 1483 | | /// <summary> |
| | | 1484 | | /// Result of decorator discovery. |
| | | 1485 | | /// </summary> |
| | | 1486 | | public readonly struct DecoratorInfo |
| | | 1487 | | { |
| | | 1488 | | public DecoratorInfo(string decoratorTypeName, string serviceTypeName, int order) |
| | | 1489 | | { |
| | 19 | 1490 | | DecoratorTypeName = decoratorTypeName; |
| | 19 | 1491 | | ServiceTypeName = serviceTypeName; |
| | 19 | 1492 | | Order = order; |
| | 19 | 1493 | | } |
| | | 1494 | | |
| | 19 | 1495 | | public string DecoratorTypeName { get; } |
| | 19 | 1496 | | public string ServiceTypeName { get; } |
| | 19 | 1497 | | public int Order { get; } |
| | | 1498 | | } |
| | | 1499 | | |
| | | 1500 | | /// <summary> |
| | | 1501 | | /// Gets all DecoratorFor<T> attributes applied to a type. |
| | | 1502 | | /// </summary> |
| | | 1503 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1504 | | /// <returns>A list of decorator info for each DecoratorFor attribute found.</returns> |
| | | 1505 | | public static IReadOnlyList<DecoratorInfo> GetDecoratorForAttributes(INamedTypeSymbol typeSymbol) |
| | | 1506 | | { |
| | 1457236 | 1507 | | var result = new List<DecoratorInfo>(); |
| | | 1508 | | |
| | 7002682 | 1509 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 1510 | | { |
| | 2044105 | 1511 | | var attrClass = attribute.AttributeClass; |
| | 2044105 | 1512 | | if (attrClass is null) |
| | | 1513 | | continue; |
| | | 1514 | | |
| | | 1515 | | // Check if this is a generic DecoratorForAttribute<T> |
| | 2044105 | 1516 | | if (!attrClass.IsGenericType) |
| | | 1517 | | continue; |
| | | 1518 | | |
| | 34 | 1519 | | var unboundTypeName = attrClass.ConstructedFrom?.ToDisplayString(); |
| | 34 | 1520 | | if (unboundTypeName is null || !unboundTypeName.StartsWith(DecoratorForAttributePrefix, StringComparison.Ord |
| | | 1521 | | continue; |
| | | 1522 | | |
| | | 1523 | | // Get the service type from the generic type argument |
| | 19 | 1524 | | if (attrClass.TypeArguments.Length != 1) |
| | | 1525 | | continue; |
| | | 1526 | | |
| | 19 | 1527 | | var serviceType = attrClass.TypeArguments[0] as INamedTypeSymbol; |
| | 19 | 1528 | | if (serviceType is null) |
| | | 1529 | | continue; |
| | | 1530 | | |
| | 19 | 1531 | | var serviceTypeName = GetFullyQualifiedName(serviceType); |
| | 19 | 1532 | | var decoratorTypeName = GetFullyQualifiedName(typeSymbol); |
| | | 1533 | | |
| | | 1534 | | // Get the Order property value |
| | 19 | 1535 | | int order = 0; |
| | 57 | 1536 | | foreach (var namedArg in attribute.NamedArguments) |
| | | 1537 | | { |
| | 19 | 1538 | | if (namedArg.Key == "Order" && namedArg.Value.Value is int orderValue) |
| | | 1539 | | { |
| | 19 | 1540 | | order = orderValue; |
| | 19 | 1541 | | break; |
| | | 1542 | | } |
| | | 1543 | | } |
| | | 1544 | | |
| | 19 | 1545 | | result.Add(new DecoratorInfo(decoratorTypeName, serviceTypeName, order)); |
| | | 1546 | | } |
| | | 1547 | | |
| | 1457236 | 1548 | | return result; |
| | | 1549 | | } |
| | | 1550 | | |
| | | 1551 | | /// <summary> |
| | | 1552 | | /// Checks if a type has any DecoratorFor<T> attributes. |
| | | 1553 | | /// </summary> |
| | | 1554 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 1555 | | /// <returns>True if the type has at least one DecoratorFor attribute.</returns> |
| | | 1556 | | public static bool HasDecoratorForAttribute(INamedTypeSymbol typeSymbol) |
| | | 1557 | | { |
| | 52 | 1558 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 1559 | | { |
| | 9 | 1560 | | var attrClass = attribute.AttributeClass; |
| | 9 | 1561 | | if (attrClass is null) |
| | | 1562 | | continue; |
| | | 1563 | | |
| | 9 | 1564 | | if (!attrClass.IsGenericType) |
| | | 1565 | | continue; |
| | | 1566 | | |
| | 4 | 1567 | | var unboundTypeName = attrClass.ConstructedFrom?.ToDisplayString(); |
| | 4 | 1568 | | if (unboundTypeName is not null && unboundTypeName.StartsWith(DecoratorForAttributePrefix, StringComparison. |
| | 2 | 1569 | | return true; |
| | | 1570 | | } |
| | | 1571 | | |
| | 16 | 1572 | | return false; |
| | | 1573 | | } |
| | | 1574 | | } |