| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Roslyn.Shared; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Shared helper utilities for discovering injectable types from Roslyn symbols. |
| | | 7 | | /// Used by both Generators and Analyzers to ensure consistent type discovery logic. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class TypeDiscoveryHelper |
| | | 10 | | { |
| | | 11 | | private const string DoNotAutoRegisterAttributeName = "DoNotAutoRegisterAttribute"; |
| | | 12 | | private const string DoNotAutoRegisterAttributeFullName = "NexusLabs.Needlr.DoNotAutoRegisterAttribute"; |
| | | 13 | | private const string DoNotInjectAttributeName = "DoNotInjectAttribute"; |
| | | 14 | | private const string DoNotInjectAttributeFullName = "NexusLabs.Needlr.DoNotInjectAttribute"; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Determines whether a type symbol represents a concrete injectable type. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 20 | | /// <param name="isCurrentAssembly">True if the type is from the current compilation's assembly (allows internal typ |
| | | 21 | | /// <returns>True if the type is a valid injectable type; otherwise, false.</returns> |
| | | 22 | | public static bool IsInjectableType(INamedTypeSymbol typeSymbol, bool isCurrentAssembly = false) |
| | | 23 | | { |
| | | 24 | | // Must be a class (not interface, struct, enum, delegate) |
| | 1439398 | 25 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 539399 | 26 | | return false; |
| | | 27 | | |
| | | 28 | | // Must be accessible from generated code |
| | | 29 | | // - Current assembly: internal and public types are accessible |
| | | 30 | | // - Referenced assemblies: only public types are accessible |
| | 899999 | 31 | | if (!IsAccessibleFromGeneratedCode(typeSymbol, isCurrentAssembly)) |
| | 0 | 32 | | return false; |
| | | 33 | | |
| | 899999 | 34 | | if (typeSymbol.IsAbstract) |
| | 137910 | 35 | | return false; |
| | | 36 | | |
| | 762089 | 37 | | if (typeSymbol.IsStatic) |
| | 87744 | 38 | | return false; |
| | | 39 | | |
| | 674345 | 40 | | if (typeSymbol.IsUnboundGenericType) |
| | 0 | 41 | | return false; |
| | | 42 | | |
| | | 43 | | // Exclude open generic types (type definitions with type parameters like MyClass<T>) |
| | | 44 | | // These cannot be instantiated directly and would produce invalid typeof() expressions |
| | 674345 | 45 | | if (typeSymbol.TypeParameters.Length > 0) |
| | 39955 | 46 | | return false; |
| | | 47 | | |
| | 634390 | 48 | | if (typeSymbol.ContainingType != null) |
| | 1 | 49 | | return false; |
| | | 50 | | |
| | 634389 | 51 | | if (IsCompilerGenerated(typeSymbol)) |
| | 0 | 52 | | return false; |
| | | 53 | | |
| | 634389 | 54 | | if (InheritsFrom(typeSymbol, "System.Exception")) |
| | 68581 | 55 | | return false; |
| | | 56 | | |
| | 565808 | 57 | | if (InheritsFrom(typeSymbol, "System.Attribute")) |
| | 157262 | 58 | | return false; |
| | | 59 | | |
| | 408546 | 60 | | if (typeSymbol.IsRecord) |
| | 3029 | 61 | | return false; |
| | | 62 | | |
| | 405517 | 63 | | if (HasDoNotAutoRegisterAttribute(typeSymbol)) |
| | 13 | 64 | | return false; |
| | | 65 | | |
| | | 66 | | // Exclude types with required members that can't be set via constructor |
| | | 67 | | // These would cause compilation errors: "Required member 'X' must be set" |
| | 405504 | 68 | | if (HasUnsatisfiedRequiredMembers(typeSymbol)) |
| | 3 | 69 | | return false; |
| | | 70 | | |
| | 405501 | 71 | | return true; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Checks if a type has required members that aren't satisfied by any constructor |
| | | 76 | | /// with [SetsRequiredMembers] attribute. |
| | | 77 | | /// </summary> |
| | | 78 | | public static bool HasUnsatisfiedRequiredMembers(INamedTypeSymbol typeSymbol) |
| | | 79 | | { |
| | | 80 | | // Check if any constructor has [SetsRequiredMembers] attribute |
| | 4466839 | 81 | | foreach (var ctor in typeSymbol.InstanceConstructors) |
| | | 82 | | { |
| | 1427374 | 83 | | if (ctor.IsStatic) |
| | | 84 | | continue; |
| | | 85 | | |
| | | 86 | | // If a constructor has [SetsRequiredMembers], it handles all required members |
| | 4014471 | 87 | | foreach (var attr in ctor.GetAttributes()) |
| | | 88 | | { |
| | 579862 | 89 | | if (attr.AttributeClass?.Name == "SetsRequiredMembersAttribute" || |
| | 579862 | 90 | | attr.AttributeClass?.ToDisplayString() == "System.Diagnostics.CodeAnalysis.SetsRequiredMembersAttrib |
| | | 91 | | { |
| | 1 | 92 | | return false; // This constructor handles required members |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // Check for required properties (including inherited) |
| | 806045 | 98 | | var currentType = typeSymbol; |
| | 3219417 | 99 | | while (currentType != null) |
| | | 100 | | { |
| | 92733891 | 101 | | foreach (var member in currentType.GetMembers()) |
| | | 102 | | { |
| | 43953571 | 103 | | if (member is IPropertySymbol property && property.IsRequired) |
| | 4 | 104 | | return true; |
| | 43953567 | 105 | | if (member is IFieldSymbol field && field.IsRequired) |
| | 1 | 106 | | return true; |
| | | 107 | | } |
| | 2413372 | 108 | | currentType = currentType.BaseType; |
| | | 109 | | } |
| | | 110 | | |
| | 806040 | 111 | | return false; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Checks if a type has the [DoNotAutoRegister] attribute (directly or on interfaces). |
| | | 116 | | /// </summary> |
| | | 117 | | public static bool HasDoNotAutoRegisterAttribute(INamedTypeSymbol typeSymbol) |
| | | 118 | | { |
| | | 119 | | // Check the type itself |
| | 409353 | 120 | | if (HasDoNotAutoRegisterAttributeDirect(typeSymbol)) |
| | 3 | 121 | | return true; |
| | | 122 | | |
| | | 123 | | // Check all implemented interfaces |
| | 1528712 | 124 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 125 | | { |
| | 355012 | 126 | | if (HasDoNotAutoRegisterAttributeDirect(iface)) |
| | 12 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | 409338 | 130 | | return false; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Checks if a type has the [DoNotAutoRegister] or [DoNotInject] attribute directly applied. |
| | | 135 | | /// </summary> |
| | | 136 | | public static bool HasDoNotAutoRegisterAttributeDirect(INamedTypeSymbol typeSymbol) |
| | | 137 | | { |
| | 7959041 | 138 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 139 | | { |
| | 2452776 | 140 | | var attributeClass = attribute.AttributeClass; |
| | 2452776 | 141 | | if (attributeClass == null) |
| | | 142 | | continue; |
| | | 143 | | |
| | 2452776 | 144 | | var name = attributeClass.Name; |
| | 2452776 | 145 | | if (name == DoNotAutoRegisterAttributeName || name == DoNotInjectAttributeName) |
| | 19 | 146 | | return true; |
| | | 147 | | |
| | 2452757 | 148 | | var fullName = attributeClass.ToDisplayString(); |
| | 2452757 | 149 | | if (fullName == DoNotAutoRegisterAttributeFullName || fullName == DoNotInjectAttributeFullName) |
| | 0 | 150 | | return true; |
| | | 151 | | } |
| | | 152 | | |
| | 1526735 | 153 | | return false; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Checks if a type is compiler-generated. |
| | | 158 | | /// </summary> |
| | | 159 | | public static bool IsCompilerGenerated(INamedTypeSymbol typeSymbol) |
| | | 160 | | { |
| | 8265892 | 161 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 162 | | { |
| | 2666116 | 163 | | var name = attribute.AttributeClass?.ToDisplayString(); |
| | 2666116 | 164 | | if (name == "System.Runtime.CompilerServices.CompilerGeneratedAttribute") |
| | 2088 | 165 | | return true; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | // Also check if the name indicates compiler generation |
| | 1465786 | 169 | | var typeName = typeSymbol.Name; |
| | 1465786 | 170 | | if (typeName.StartsWith("<", StringComparison.Ordinal) || |
| | 1465786 | 171 | | typeName.Contains("__") || |
| | 1465786 | 172 | | typeName.Contains("<>")) |
| | | 173 | | { |
| | 64725 | 174 | | return true; |
| | | 175 | | } |
| | | 176 | | |
| | 1401061 | 177 | | return false; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Checks if a type inherits from a base type by name. |
| | | 182 | | /// </summary> |
| | | 183 | | public static bool InheritsFrom(INamedTypeSymbol typeSymbol, string baseTypeName) |
| | | 184 | | { |
| | 1209397 | 185 | | var currentType = typeSymbol.BaseType; |
| | 3173059 | 186 | | while (currentType != null) |
| | | 187 | | { |
| | 2190269 | 188 | | if (currentType.ToDisplayString() == baseTypeName) |
| | 226607 | 189 | | return true; |
| | 1963662 | 190 | | currentType = currentType.BaseType; |
| | | 191 | | } |
| | 982790 | 192 | | return false; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Checks if a type is accessible from generated code. |
| | | 197 | | /// For types in the current assembly, internal and public types are accessible. |
| | | 198 | | /// For types in referenced assemblies, only public types are accessible. |
| | | 199 | | /// </summary> |
| | | 200 | | public static bool IsAccessibleFromGeneratedCode(INamedTypeSymbol typeSymbol, bool isCurrentAssembly) |
| | | 201 | | { |
| | | 202 | | // Check the type itself |
| | 1799988 | 203 | | var accessibility = typeSymbol.DeclaredAccessibility; |
| | | 204 | | |
| | 1799988 | 205 | | if (isCurrentAssembly) |
| | | 206 | | { |
| | | 207 | | // For current assembly, allow public or internal |
| | 1238 | 208 | | if (accessibility != Accessibility.Public && accessibility != Accessibility.Internal) |
| | 0 | 209 | | return false; |
| | | 210 | | } |
| | | 211 | | else |
| | | 212 | | { |
| | | 213 | | // For referenced assemblies, only public is accessible |
| | 1798750 | 214 | | if (accessibility != Accessibility.Public) |
| | 0 | 215 | | return false; |
| | | 216 | | } |
| | | 217 | | |
| | | 218 | | // Check all containing types (for nested types) |
| | 1799988 | 219 | | var containingType = typeSymbol.ContainingType; |
| | 1799989 | 220 | | while (containingType != null) |
| | | 221 | | { |
| | 1 | 222 | | var containingAccessibility = containingType.DeclaredAccessibility; |
| | 1 | 223 | | if (isCurrentAssembly) |
| | | 224 | | { |
| | 0 | 225 | | if (containingAccessibility != Accessibility.Public && containingAccessibility != Accessibility.Internal |
| | 0 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | else |
| | | 229 | | { |
| | 1 | 230 | | if (containingAccessibility != Accessibility.Public) |
| | 0 | 231 | | return false; |
| | | 232 | | } |
| | 1 | 233 | | containingType = containingType.ContainingType; |
| | | 234 | | } |
| | | 235 | | |
| | 1799988 | 236 | | return true; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Checks if a type is from the System namespace or system assemblies. |
| | | 241 | | /// </summary> |
| | | 242 | | public static bool IsSystemType(INamedTypeSymbol typeSymbol) |
| | | 243 | | { |
| | 908656 | 244 | | var ns = typeSymbol.ContainingNamespace?.ToDisplayString() ?? string.Empty; |
| | | 245 | | |
| | | 246 | | // Skip types from system assemblies |
| | 908656 | 247 | | if (ns.StartsWith("System", StringComparison.Ordinal)) |
| | 902736 | 248 | | return true; |
| | | 249 | | |
| | | 250 | | // Check if from mscorlib or similar |
| | 5920 | 251 | | var assembly = typeSymbol.ContainingAssembly; |
| | 5920 | 252 | | if (assembly != null) |
| | | 253 | | { |
| | 5920 | 254 | | var assemblyName = assembly.Name; |
| | 5920 | 255 | | if (assemblyName == "mscorlib" || |
| | 5920 | 256 | | assemblyName == "System.Runtime" || |
| | 5920 | 257 | | assemblyName == "System.Private.CoreLib" || |
| | 5920 | 258 | | assemblyName == "netstandard") |
| | | 259 | | { |
| | 4202 | 260 | | return true; |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | |
| | 1718 | 264 | | return false; |
| | | 265 | | } |
| | | 266 | | } |